From c149768f888efbf9ab237730280402bd607894ac Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Tue, 21 Jun 2022 23:42:59 +0100 Subject: Add result selection. --- src/config.c | 8 +++++--- src/entry.h | 2 ++ src/entry_backend/harfbuzz.c | 8 ++++++++ src/entry_backend/pango.c | 8 ++++++++ src/main.c | 25 ++++++++++++++++++++++--- 5 files changed, 45 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index 834bc46..fa1b41a 100644 --- a/src/config.c +++ b/src/config.c @@ -32,7 +32,7 @@ static int32_t parse_int32(size_t lineno, const char *str, bool *err); */ #define PARSE_ERROR_NO_ARGS(lineno, fmt) \ if ((lineno) > 0) {\ - log_error("\tLine %zu: ", (lineno));\ + log_error("Config file: line %zu: ", (lineno));\ log_append_error((fmt)); \ } else {\ log_error((fmt)); \ @@ -40,7 +40,7 @@ static int32_t parse_int32(size_t lineno, const char *str, bool *err); #define PARSE_ERROR(lineno, fmt, ...) \ if ((lineno) > 0) {\ - log_error("\tLine %zu: ", (lineno));\ + log_error("Config file: line %zu: ", (lineno));\ log_append_error((fmt), __VA_ARGS__); \ } else {\ log_error((fmt), __VA_ARGS__); \ @@ -262,6 +262,8 @@ bool parse_option(struct tofi *tofi, size_t lineno, const char *option, const ch tofi->window.entry.border.color = parse_color(lineno, value, &err); } else if (strcasecmp(option, "text-color") == 0) { tofi->window.entry.foreground_color = parse_color(lineno, value, &err); + } else if (strcasecmp(option, "selection-color") == 0) { + tofi->window.entry.selection_color = parse_color(lineno, value, &err); } else if (strcasecmp(option, "width") == 0) { tofi->window.width = parse_uint32(lineno, value, &err); } else if (strcasecmp(option, "height") == 0) { @@ -279,7 +281,7 @@ bool parse_option(struct tofi *tofi, size_t lineno, const char *option, const ch } else if (strcasecmp(option, "hide-cursor") == 0) { tofi->hide_cursor = parse_bool(lineno, value, &err); } else { - PARSE_ERROR(lineno, "Bad config file option \"%s\"\n", option); + PARSE_ERROR(lineno, "Unrecognised option \"%s\"\n", option); err = true; } return !err; diff --git a/src/entry.h b/src/entry.h index 1c919dc..d0295bc 100644 --- a/src/entry.h +++ b/src/entry.h @@ -32,6 +32,7 @@ struct entry { uint32_t input_length; uint32_t input_mb_length; + uint32_t selection; struct string_vec results; struct string_vec commands; struct history history; @@ -47,6 +48,7 @@ struct entry { int32_t result_padding; struct color foreground_color; struct color background_color; + struct color selection_color; struct { struct color color; struct color outline_color; diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c index dc479d3..51f08ea 100644 --- a/src/entry_backend/harfbuzz.c +++ b/src/entry_backend/harfbuzz.c @@ -218,6 +218,14 @@ void entry_backend_update(struct entry *entry) setup_hb_buffer(buffer); hb_buffer_add_utf8(buffer, entry->results.buf[i], -1, 0, -1); hb_shape(entry->backend.hb_font, buffer, NULL, 0); + if (i == entry->selection) { + cairo_save(cr); + struct color color = entry->selection_color; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); + } width = render_hb_buffer(cr, buffer, entry->image.scale); + if (i == entry->selection) { + cairo_restore(cr); + } } } diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c index d5dc8b4..38b8a02 100644 --- a/src/entry_backend/pango.c +++ b/src/entry_backend/pango.c @@ -74,6 +74,14 @@ void entry_backend_update(struct entry *entry) } pango_layout_set_text(layout, str, -1); pango_cairo_update_layout(cr, layout); + if (i == entry->selection) { + cairo_save(cr); + struct color color = entry->selection_color; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); + } pango_cairo_show_layout(cr, layout); + if (i == entry->selection) { + cairo_restore(cr); + } } } diff --git a/src/main.c b/src/main.c index 593da15..15af4c0 100644 --- a/src/main.c +++ b/src/main.c @@ -24,7 +24,9 @@ #include "string_vec.h" #undef MAX +#undef MIN #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) static void zwlr_layer_surface_configure( void *data, @@ -192,6 +194,19 @@ static void wl_keyboard_key( tofi->submit = true; return; } + + if (sym == XKB_KEY_Up) { + uint32_t nsel = MAX(MIN(tofi->window.entry.num_results, tofi->window.entry.results.count), 1); + tofi->window.entry.selection += nsel; + tofi->window.entry.selection--; + tofi->window.entry.selection %= nsel; + } else if (sym == XKB_KEY_Down) { + uint32_t nsel = MAX(MIN(tofi->window.entry.num_results, tofi->window.entry.results.count), 1); + tofi->window.entry.selection++; + tofi->window.entry.selection %= nsel; + } else { + tofi->window.entry.selection = 0; + } entry_update(&tofi->window.entry); tofi->window.surface.redraw = true; @@ -557,6 +572,7 @@ static void usage() " --text-color Color of text.\n" " --prompt-text Prompt text.\n" " --num-results Maximum number of results to display.\n" +" --selection-color Color of selected result.\n" " --result-padding Spacing between results. Can be negative.\n" " --width Width of the window.\n" " --height Height of the window.\n" @@ -583,6 +599,7 @@ static void parse_args(struct tofi *tofi, int argc, char *argv[]) {"font-name", required_argument, NULL, 0}, {"font-size", required_argument, NULL, 0}, {"num-results", required_argument, NULL, 0}, + {"selection-color", required_argument, NULL, 0}, {"outline-width", required_argument, NULL, 0}, {"outline-color", required_argument, NULL, 0}, {"prompt-text", required_argument, NULL, 0}, @@ -676,7 +693,8 @@ int main(int argc, char *argv[]) .num_results = 5, .padding = 8, .background_color = {0.106f, 0.114f, 0.118f, 1.0f}, - .foreground_color = {1.0f, 1.0f, 1.0f, 1.0f} + .foreground_color = {1.0f, 1.0f, 1.0f, 1.0f}, + .selection_color = {0.976f, 0.149f, 0.447f, 1.0f} } } }; @@ -866,10 +884,11 @@ int main(int argc, char *argv[]) if (tofi.submit) { tofi.submit = false; if (tofi.window.entry.results.count > 0) { - printf("%s\n", tofi.window.entry.results.buf[0]); + uint32_t selection = tofi.window.entry.selection; + printf("%s\n", tofi.window.entry.results.buf[selection]); history_add( &tofi.window.entry.history, - tofi.window.entry.results.buf[0]); + tofi.window.entry.results.buf[selection]); history_save(&tofi.window.entry.history); break; } -- cgit v1.2.3