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