From 61fce2edab50b837533effea490689d037f231c8 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 23 Mar 2023 17:30:23 +0100 Subject: fix scroll --- dmenu-wl.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/dmenu-wl.c b/dmenu-wl.c index 1fb7ef4..ee2b0a7 100644 --- a/dmenu-wl.c +++ b/dmenu-wl.c @@ -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,12 +144,27 @@ 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) { @@ -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); -- cgit v1.2.3-70-g09d2