From bcff7094627e423dd9ca52a246345512dc3d791c Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sun, 26 Jun 2022 23:44:28 +0100 Subject: Add optional background around selected result. --- completions/tofi | 2 ++ doc/tofi.5.md | 7 +++++ doc/tofi.5.scd | 5 +++ src/config.c | 4 ++- src/entry.h | 3 +- src/entry_backend/harfbuzz.c | 13 ++++++-- src/entry_backend/pango.c | 15 +++++++-- src/main.c | 73 +++++++++++++++++++++++--------------------- 8 files changed, 80 insertions(+), 42 deletions(-) diff --git a/completions/tofi b/completions/tofi index 7bf35ed..ea9b6de 100644 --- a/completions/tofi +++ b/completions/tofi @@ -8,6 +8,7 @@ _tofi() words=( --help --config + --output --anchor --background-color --corner-radius @@ -15,6 +16,7 @@ _tofi() --font-size --num-results --selection-color + --selection-background --outline-width --outline-color --prompt-text diff --git a/doc/tofi.5.md b/doc/tofi.5.md index 24b14e9..f5df5cf 100644 --- a/doc/tofi.5.md +++ b/doc/tofi.5.md @@ -93,6 +93,13 @@ options. > > Default: \#F92672 +**selection-background**=*color* + +> Background color of selected result. See **COLORS** for more +> information. +> +> Default: \#00000000 + **result-spacing**=*px* > Spacing between results. Can be negative. diff --git a/doc/tofi.5.scd b/doc/tofi.5.scd index 4b39093..7946cf1 100644 --- a/doc/tofi.5.scd +++ b/doc/tofi.5.scd @@ -84,6 +84,11 @@ options. Default: #F92672 +*selection-background*=_color_ + Background color of selected result. See *COLORS* for more information. + + Default: #00000000 + *result-spacing*=_px_ Spacing between results. Can be negative. diff --git a/src/config.c b/src/config.c index 8ec6de4..713cc7e 100644 --- a/src/config.c +++ b/src/config.c @@ -262,7 +262,9 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const } else if (strcasecmp(option, "text-color") == 0) { tofi->window.entry.foreground_color = parse_color(filename, lineno, value, &err); } else if (strcasecmp(option, "selection-color") == 0) { - tofi->window.entry.selection_color = parse_color(filename, lineno, value, &err); + tofi->window.entry.selection_foreground_color = parse_color(filename, lineno, value, &err); + } else if (strcasecmp(option, "selection-background") == 0) { + tofi->window.entry.selection_background_color = parse_color(filename, lineno, value, &err); } else if (strcasecmp(option, "width") == 0) { tofi->window.width = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); } else if (strcasecmp(option, "height") == 0) { diff --git a/src/entry.h b/src/entry.h index 5fa29c4..4f01252 100644 --- a/src/entry.h +++ b/src/entry.h @@ -55,7 +55,8 @@ struct entry { uint32_t outline_width; struct color foreground_color; struct color background_color; - struct color selection_color; + struct color selection_foreground_color; + struct color selection_background_color; struct color border_color; struct color outline_color; }; diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c index 58f4ca6..fe4d7dc 100644 --- a/src/entry_backend/harfbuzz.c +++ b/src/entry_backend/harfbuzz.c @@ -231,13 +231,22 @@ void entry_backend_harfbuzz_update(struct entry *entry) hb_buffer_add_utf8(buffer, entry->results.buf[i], -1, 0, -1); hb_shape(entry->harfbuzz.hb_font, buffer, NULL, 0); if (i == entry->selection) { - cairo_save(cr); - color = entry->selection_color; + cairo_push_group(cr); + color = entry->selection_foreground_color; cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); } width = render_hb_buffer(cr, buffer); if (i == entry->selection) { + cairo_pop_group_to_source(cr); + cairo_save(cr); + color = entry->selection_background_color; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); + cairo_rectangle(cr, 0, 0, width, font_extents.height); + cairo_fill(cr); cairo_restore(cr); + cairo_paint(cr); + color = entry->foreground_color; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); } } diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c index 019933e..8a0add7 100644 --- a/src/entry_backend/pango.c +++ b/src/entry_backend/pango.c @@ -87,15 +87,24 @@ void entry_backend_pango_update(struct entry *entry) pango_layout_set_text(layout, str, -1); pango_cairo_update_layout(cr, layout); if (i == entry->selection) { - cairo_save(cr); - color = entry->selection_color; + cairo_push_group(cr); + color = entry->selection_foreground_color; cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); } pango_cairo_show_layout(cr, layout); + pango_layout_get_size(layout, &width, &height); if (i == entry->selection) { + cairo_pop_group_to_source(cr); + cairo_save(cr); + color = entry->selection_background_color; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); + cairo_rectangle(cr, 0, 0, (int)(width / PANGO_SCALE), (int)(height / PANGO_SCALE)); + cairo_fill(cr); cairo_restore(cr); + cairo_paint(cr); + color = entry->foreground_color; + cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); } - pango_layout_get_size(layout, &width, &height); } cairo_restore(cr); diff --git a/src/main.c b/src/main.c index d6f6d88..362fef6 100644 --- a/src/main.c +++ b/src/main.c @@ -592,40 +592,42 @@ static void usage() fprintf(stderr, "%s", "Usage: tofi [options]\n" "\n" -" -h, --help Print this message and exit.\n" -" -c, --config Specify a config file.\n" -" --font Font to use.\n" -" --font-size Point size of text.\n" -" --background-color Color of the background.\n" -" --outline-width Width of the border outlines.\n" -" --outline-color Color of the border outlines.\n" -" --border-width Width of the border.\n" -" --border-color Color of the border.\n" -" --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-spacing 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" -" --corner-radius Radius of window corners.\n" -" --output Name of output to display window on.\n" -" --anchor Location on screen to anchor window.\n" -" --margin-top Offset from top of screen.\n" -" --margin-bottom Offset from bottom of screen.\n" -" --margin-left Offset from left of screen.\n" -" --margin-right Offset from right of screen.\n" -" --padding-top Padding between top border and text.\n" -" --padding-bottom Padding between bottom border and text.\n" -" --padding-left Padding between left border and text.\n" -" --padding-right Padding between right border and text.\n" -" --hide-cursor Hide the cursor.\n" -" --horizontal List results horizontally.\n" -" --history Sort results by number of usages.\n" -" --hint-font Perform font hinting.\n" -" --late-keyboard-init (EXPERIMENTAL) Delay keyboard initialisation\n" -" until after the first draw to screen.\n" +" -h, --help Print this message and exit.\n" +" -c, --config Specify a config file.\n" +" --font Font to use.\n" +" --font-size Point size of text.\n" +" --background-color Color of the background.\n" +" --outline-width Width of the border outlines.\n" +" --outline-color Color of the border outlines.\n" +" --border-width Width of the border.\n" +" --border-color Color of the border.\n" +" --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" +" --selection-background Color of selected result background.\n" +" --result-spacing Spacing between results.\n" +" --min-input-width Minimum input width in horizontal mode.\n" +" --width Width of the window.\n" +" --height Height of the window.\n" +" --corner-radius Radius of window corners.\n" +" --output Name of output to display window on.\n" +" --anchor Location on screen to anchor window.\n" +" --margin-top Offset from top of screen.\n" +" --margin-bottom Offset from bottom of screen.\n" +" --margin-left Offset from left of screen.\n" +" --margin-right Offset from right of screen.\n" +" --padding-top Padding between top border and text.\n" +" --padding-bottom Padding between bottom border and text.\n" +" --padding-left Padding between left border and text.\n" +" --padding-right Padding between right border and text.\n" +" --hide-cursor Hide the cursor.\n" +" --horizontal List results horizontally.\n" +" --history Sort results by number of usages.\n" +" --hint-font Perform font hinting.\n" +" --late-keyboard-init (EXPERIMENTAL) Delay keyboard\n" +" initialisation until after the first\n" +" draw to screen.\n" ); } @@ -640,6 +642,7 @@ const struct option long_options[] = { {"font-size", required_argument, NULL, 0}, {"num-results", required_argument, NULL, 0}, {"selection-color", required_argument, NULL, 0}, + {"selection-background", required_argument, NULL, 0}, {"outline-width", required_argument, NULL, 0}, {"outline-color", required_argument, NULL, 0}, {"prompt-text", required_argument, NULL, 0}, @@ -771,7 +774,7 @@ int main(int argc, char *argv[]) .outline_width = 4, .background_color = {0.106f, 0.114f, 0.118f, 1.0f}, .foreground_color = {1.0f, 1.0f, 1.0f, 1.0f}, - .selection_color = {0.976f, 0.149f, 0.447f, 1.0f}, + .selection_foreground_color = {0.976f, 0.149f, 0.447f, 1.0f}, .border_color = {0.976f, 0.149f, 0.447f, 1.0f}, .outline_color = {0.031f, 0.031f, 0.0f, 1.0f}, } -- cgit v1.2.3