diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-23 17:30:23 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-23 17:30:23 +0100 |
| commit | 61fce2edab50b837533effea490689d037f231c8 (patch) | |
| tree | c97899425ba3b9259a9315c57072516d44e7faa4 | |
| parent | 4f6ee2b6abed1de775dea65412fdc30fcffaea98 (diff) | |
fix scroll
| -rw-r--r-- | dmenu-wl.c | 28 |
1 files changed, 24 insertions, 4 deletions
@@ -81,10 +81,12 @@ struct Options { Option *last; Option *first_match; Option *last_match; + Option *current; Option *selection; } options = { 0 }; /* function definition */ +static void calculate_scroll(); static void append_match(Option *item, Option **first, Option **last); static void generate_matches(); static void draw_dmenu(Monitor *monitor); @@ -142,13 +144,28 @@ Monitor *monitors = 0; Monitor *active_monitor = 0; uint32_t height = 50; uint32_t numitems = 0; -char input_field[MAX_LINE_LENGTH] = { 0 }; +char input_field[MAX_LINE_LENGTH] = ""; int running = 1; #include "config.h" /* function implementations */ void +calculate_scroll() +{ + unsigned i = 0; + Option *match = options.first_match; + options.current = match; + + for (; match && match != options.selection; match = match->next_match) { + if (i % lines == lines - 1) + options.current = match->next_match; + ++i; + } +} + + +void append_match(Option *item, Option **first, Option **last) { if (*last) @@ -224,7 +241,7 @@ generate_matches() void draw_dmenu(Monitor *monitor) { - Option *match = options.first_match; + Option *match = options.current; unsigned x = 0; unsigned y = 0; @@ -241,7 +258,7 @@ draw_dmenu(Monitor *monitor) y = 2 * borderwidth + fontsize + 2 * padding; - for (; match; match = match->next_match) { + for (; match && y < height; match = match->next_match) { if (match == options.selection) draw_rect(monitor->canvas, 0, y, monitor->width, fontsize + 2 * padding, highlight); y += fontsize + padding; @@ -418,10 +435,12 @@ handle_keyboard_event() case XKB_KEY_Down: if (options.selection->next_match) options.selection = options.selection->next_match; + calculate_scroll(); break; case XKB_KEY_Up: if (options.selection->previous_match) options.selection = options.selection->previous_match; + calculate_scroll(); break; case XKB_KEY_Left: break; @@ -506,8 +525,9 @@ setup() if (numitems < lines) lines = numitems; - generate_matches(); height = (lines + 1) * (fontsize + padding * 2) + 3 * borderwidth; + generate_matches(); + calculate_scroll(); client.repeat.timer = timerfd_create(CLOCK_MONOTONIC, 0); client.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); |