Simplify match scrolling

This commit is contained in:
adnano 2024-02-26 10:47:35 -05:00
parent 906b55019e
commit d23a2c563a

88
main.c
View file

@ -183,79 +183,47 @@ void scroll_matches(struct menu_state *state) {
return; return;
} }
if (state->leftmost == NULL && state->rightmost == NULL) {
state->leftmost = state->matches;
}
if (state->vertical) { if (state->vertical) {
if (state->leftmost == NULL) { if (state->leftmost == NULL) {
state->leftmost = state->matches; struct menu_item *item = state->rightmost;
if (state->rightmost == NULL) { for (int i = 1; item->left && i < state->lines; i++) {
int offs = 0; item = item->left;
struct menu_item *item;
for (item = state->matches; item->left != state->selection; item = item->right) {
offs += state->line_height;
if (offs >= state->height) {
state->leftmost = item->left;
offs = state->height - offs;
}
}
} else {
int offs = 0;
struct menu_item *item;
for (item = state->rightmost; item; item = item->left) {
offs += state->line_height;
if (offs >= state->height) {
state->leftmost = item->right;
break;
}
}
} }
} state->leftmost = item;
if (state->rightmost == NULL) { } else if (state->rightmost == NULL) {
state->rightmost = state->matches; struct menu_item *item = state->leftmost;
int offs = 0; for (int i = 1; item->right && i < state->lines; i++) {
struct menu_item *item; item = item->right;
for (item = state->leftmost; item; item = item->right) {
offs += state->line_height;
if (offs >= state->height) {
break;
}
state->rightmost = item;
} }
state->rightmost = item;
} }
} else { } else {
// Calculate available space // Calculate available space
int padding = state->padding; int max_width = state->width - state->inputw - state->promptw
int width = state->width - state->inputw - state->promptw
- state->left_arrow - state->right_arrow; - state->left_arrow - state->right_arrow;
if (state->leftmost == NULL) { if (state->leftmost == NULL) {
state->leftmost = state->matches; state->leftmost = state->rightmost;
if (state->rightmost == NULL) { int total_width = 0;
int offs = 0; struct menu_item *item;
struct menu_item *item; for (item = state->rightmost; item; item = item->left) {
for (item = state->matches; item->left != state->selection; item = item->right) { total_width += item->width + 2 * state->padding;
offs += item->width + 2 * padding; if (total_width > max_width) {
if (offs >= width) { break;
state->leftmost = item->left;
offs = width - offs;
}
}
} else {
int offs = 0;
struct menu_item *item;
for (item = state->rightmost; item; item = item->left) {
offs += item->width + 2 * padding;
if (offs >= width) {
state->leftmost = item->right;
break;
}
} }
state->leftmost = item;
} }
} } else if (state->rightmost == NULL) {
if (state->rightmost == NULL) { state->rightmost = state->leftmost;
state->rightmost = state->matches; int total_width = 0;
int offs = 0;
struct menu_item *item; struct menu_item *item;
for (item = state->leftmost; item; item = item->right) { for (item = state->leftmost; item; item = item->right) {
offs += item->width + 2 * padding; total_width += item->width + 2 * state->padding;
if (offs >= width) { if (total_width > max_width) {
break; break;
} }
state->rightmost = item; state->rightmost = item;