From b70772372ef51a64dece064aaa0e25e0a83354fd Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Fri, 24 Jun 2022 16:48:15 +0100 Subject: Horizontal mode fixes. Implement horizontal mode for Pango, and add min-input-width option. --- src/config.c | 2 ++ src/entry.h | 1 + src/entry_backend/harfbuzz.c | 4 ++++ src/entry_backend/pango.c | 14 ++++++++++++-- src/main.c | 9 ++++++--- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/config.c b/src/config.c index 2239b31..400c73f 100644 --- a/src/config.c +++ b/src/config.c @@ -253,6 +253,8 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const tofi->window.entry.border.outline_color = parse_color(filename, lineno, value, &err); } else if (strcasecmp(option, "prompt-text") == 0) { snprintf(tofi->window.entry.prompt_text, N_ELEM(tofi->window.entry.prompt_text), "%s", value); + } else if (strcasecmp(option, "min-input-width") == 0) { + tofi->window.entry.input_width = parse_uint32(filename, lineno, value, &err); } else if (strcasecmp(option, "result-padding") == 0) { tofi->window.entry.result_padding = parse_int32(filename, lineno, value, &err); } else if (strcasecmp(option, "border-width") == 0) { diff --git a/src/entry.h b/src/entry.h index 01cf1b9..f59411f 100644 --- a/src/entry.h +++ b/src/entry.h @@ -46,6 +46,7 @@ struct entry { char prompt_text[MAX_PROMPT_LENGTH]; uint32_t corner_radius; uint32_t padding; + uint32_t input_width; int32_t result_padding; struct color foreground_color; struct color background_color; diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c index 61a691f..fff4950 100644 --- a/src/entry_backend/harfbuzz.c +++ b/src/entry_backend/harfbuzz.c @@ -23,6 +23,9 @@ const struct { #include +#undef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + static const char *get_ft_error_string(int err_code) { for (size_t i = 0; i < N_ELEM(ft_errors); i++) { @@ -199,6 +202,7 @@ void entry_backend_harfbuzz_update(struct entry *entry) hb_buffer_add_utf8(buffer, entry->input_mb, -1, 0, -1); hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0); width = render_hb_buffer(cr, buffer); + width = MAX(width, entry->input_width); cairo_font_extents_t font_extents; cairo_font_extents(cr, &font_extents); diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c index 49365fb..084d34f 100644 --- a/src/entry_backend/pango.c +++ b/src/entry_backend/pango.c @@ -5,6 +5,9 @@ #include "../log.h" #include "../nelem.h" +#undef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + void entry_backend_pango_init(struct entry *entry, uint32_t *width, uint32_t *height) { cairo_t *cr = entry->cairo[0].cr; @@ -60,11 +63,17 @@ void entry_backend_pango_update(struct entry *entry) pango_cairo_update_layout(cr, layout); pango_cairo_show_layout(cr, layout); + int width; int height; - pango_layout_get_size(layout, NULL, &height); + pango_layout_get_size(layout, &width, &height); + width = MAX(width, (int)entry->input_width * PANGO_SCALE); for (size_t i = 0; i < entry->num_results && i < entry->results.count; i++) { - cairo_translate(cr, 0, (int)(height / PANGO_SCALE)); + if (entry->horizontal) { + cairo_translate(cr, (int)(width / PANGO_SCALE) + entry->result_padding, 0); + } else { + cairo_translate(cr, 0, (int)(height / PANGO_SCALE) + entry->result_padding); + } const char *str; if (i < entry->results.count) { str = entry->results.buf[i]; @@ -82,5 +91,6 @@ void entry_backend_pango_update(struct entry *entry) if (i == entry->selection) { cairo_restore(cr); } + pango_layout_get_size(layout, &width, &height); } } diff --git a/src/main.c b/src/main.c index 95c7b33..cf0e3a8 100644 --- a/src/main.c +++ b/src/main.c @@ -195,12 +195,12 @@ static void wl_keyboard_key( return; } - if (sym == XKB_KEY_Up) { + if (sym == XKB_KEY_Up || sym == XKB_KEY_Left) { 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) { + } else if (sym == XKB_KEY_Down || sym == XKB_KEY_Right) { 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; @@ -575,6 +575,7 @@ static void usage() " --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" +" --min-input-width Minimum width of input in horizontal mode.\n" " --width Width of the window.\n" " --height Height of the window.\n" " --anchor Location on screen to anchor window.\n" @@ -583,6 +584,7 @@ static void usage() " --margin-left Offset from left of screen.\n" " --margin-right Offset from right of screen.\n" " --hide-cursor Hide the cursor.\n" +" --horizontal List results horizontally.\n" " --history Sort results by number of usages.\n" ); } @@ -606,6 +608,7 @@ static void parse_args(struct tofi *tofi, int argc, char *argv[]) {"outline-color", required_argument, NULL, 0}, {"prompt-text", required_argument, NULL, 0}, {"result-padding", required_argument, NULL, 0}, + {"min-input-width", required_argument, NULL, 0}, {"border-width", required_argument, NULL, 0}, {"border-color", required_argument, NULL, 0}, {"text-color", required_argument, NULL, 0}, @@ -615,7 +618,7 @@ static void parse_args(struct tofi *tofi, int argc, char *argv[]) {"margin-bottom", required_argument, NULL, 0}, {"margin-left", required_argument, NULL, 0}, {"margin-right", required_argument, NULL, 0}, - {"layout-horizontal", required_argument, NULL, 0}, + {"horizontal", required_argument, NULL, 0}, {"hide-cursor", required_argument, NULL, 0}, {"history", required_argument, NULL, 0}, {NULL, 0, NULL, 0} -- cgit v1.2.3