Use common handling for scrolling

This commit is contained in:
Benny Baumann 2020-11-21 17:03:17 +01:00 committed by BenBE
parent a7955c4966
commit 5fe2a88c08
1 changed files with 10 additions and 8 deletions

18
Panel.c
View File

@ -327,6 +327,10 @@ bool Panel_onKey(Panel* this, int key) {
assert (this != NULL); assert (this != NULL);
int size = Vector_size(this->items); int size = Vector_size(this->items);
#define CLAMP_INDEX(var, delta, min, max) \
CLAMP((var) + (delta), (min), MAXIMUM(0, (max)))
switch (key) { switch (key) {
case KEY_DOWN: case KEY_DOWN:
case KEY_CTRL('N'): case KEY_CTRL('N'):
@ -358,27 +362,23 @@ bool Panel_onKey(Panel* this, int key) {
break; break;
case KEY_PPAGE: case KEY_PPAGE:
this->selected -= (this->h - 1); this->selected -= (this->h - 1);
this->scrollV = MAXIMUM(0, this->scrollV - this->h + 1); this->scrollV = CLAMP_INDEX(this->scrollV, -(this->h - 1), 0, size - this->h);
this->needsRedraw = true; this->needsRedraw = true;
break; break;
case KEY_NPAGE: case KEY_NPAGE:
this->selected += (this->h - 1); this->selected += (this->h - 1);
this->scrollV = MAXIMUM(0, MINIMUM(Vector_size(this->items) - this->h, this->scrollV = CLAMP_INDEX(this->scrollV, +(this->h - 1), 0, size - this->h);
this->scrollV + this->h - 1));
this->needsRedraw = true; this->needsRedraw = true;
break; break;
case KEY_WHEELUP: case KEY_WHEELUP:
this->selected -= CRT_scrollWheelVAmount; this->selected -= CRT_scrollWheelVAmount;
this->scrollV -= CRT_scrollWheelVAmount; this->scrollV = CLAMP_INDEX(this->scrollV, -CRT_scrollWheelVAmount, 0, size - this->h);
this->needsRedraw = true; this->needsRedraw = true;
break; break;
case KEY_WHEELDOWN: case KEY_WHEELDOWN:
{ {
this->selected += CRT_scrollWheelVAmount; this->selected += CRT_scrollWheelVAmount;
this->scrollV += CRT_scrollWheelVAmount; this->scrollV = CLAMP_INDEX(this->scrollV, +CRT_scrollWheelVAmount, 0, size - this->h);
if (this->scrollV > Vector_size(this->items) - this->h) {
this->scrollV = Vector_size(this->items) - this->h;
}
this->needsRedraw = true; this->needsRedraw = true;
break; break;
} }
@ -402,6 +402,8 @@ bool Panel_onKey(Panel* this, int key) {
return false; return false;
} }
#undef CLAMP_INDEX
// ensure selection within bounds // ensure selection within bounds
if (this->selected < 0 || size == 0) { if (this->selected < 0 || size == 0) {
this->selected = 0; this->selected = 0;