summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Jones <philj56@gmail.com>2022-11-16 13:36:25 +0000
committerPhil Jones <philj56@gmail.com>2022-11-16 13:36:25 +0000
commitca75d29bd2208ea7e2f5ccdb2b146953419be9b2 (patch)
treeaf56f7530f756a4d7ed96ca37a1d35bdceb83f7c
parent25c0035ed0614ae334ae034ee90d7898c15e779f (diff)
Add placeholder text options.
-rw-r--r--completions/tofi2
-rw-r--r--doc/config6
-rw-r--r--doc/tofi.5.md12
-rw-r--r--doc/tofi.5.scd10
-rw-r--r--src/config.c4
-rw-r--r--src/entry.h2
-rw-r--r--src/entry_backend/harfbuzz.c9
-rw-r--r--src/entry_backend/pango.c9
-rw-r--r--src/main.c5
9 files changed, 57 insertions, 2 deletions
diff --git a/completions/tofi b/completions/tofi
index d40e306..fc8ab90 100644
--- a/completions/tofi
+++ b/completions/tofi
@@ -25,6 +25,8 @@ _tofi()
--outline-color
--prompt-text
--prompt-padding
+ --placeholder-text
+ --placeholder-color
--result-spacing
--min-input-width
--border-width
diff --git a/doc/config b/doc/config
index 9f15f35..a43b258 100644
--- a/doc/config
+++ b/doc/config
@@ -39,6 +39,9 @@
# Default text
text-color = #FFFFFF
+ # Placeholder input text
+ placeholder-color = #FFFFFFA8
+
# Selection text
selection-color = #F92672
@@ -57,6 +60,9 @@
# Extra horizontal padding between prompt and input.
prompt-padding = 0
+ # Placeholder input text.
+ placeholder-text = ""
+
# Maximum number of results to display.
# If 0, tofi will draw as many results as it can fit in the window.
num-results = 0
diff --git a/doc/tofi.5.md b/doc/tofi.5.md
index 73a5699..ff616ff 100644
--- a/doc/tofi.5.md
+++ b/doc/tofi.5.md
@@ -181,6 +181,18 @@ options.
>
> Default: 0
+**placeholder-text**=*string*
+
+> Placeholder input text.
+>
+> Default: ""
+
+**placeholder-color**=*string*
+
+> Color of placeholder input text. See **COLORS** for more information.
+>
+> Default: \#FFFFFFA8
+
**num-results**=*n*
> Maximum number of results to display. If *n* = 0, tofi will draw as
diff --git a/doc/tofi.5.scd b/doc/tofi.5.scd
index 25fdf0b..f809103 100644
--- a/doc/tofi.5.scd
+++ b/doc/tofi.5.scd
@@ -161,6 +161,16 @@ options.
Default: 0
+*placeholder-text*=_string_
+ Placeholder input text.
+
+ Default: ""
+
+*placeholder-color*=_string_
+ Color of placeholder input text. See *COLORS* for more information.
+
+ Default: #FFFFFFA8
+
*num-results*=_n_
Maximum number of results to display. If _n_ = 0, tofi will draw as
many results as it can fit in the window.
diff --git a/src/config.c b/src/config.c
index 75f70b6..0875d86 100644
--- a/src/config.c
+++ b/src/config.c
@@ -335,6 +335,10 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const
snprintf(tofi->window.entry.prompt_text, N_ELEM(tofi->window.entry.prompt_text), "%s", value);
} else if (strcasecmp(option, "prompt-padding") == 0) {
tofi->window.entry.prompt_padding = parse_uint32(filename, lineno, value, &err);
+ } else if (strcasecmp(option, "placeholder-text") == 0) {
+ snprintf(tofi->window.entry.placeholder_text, N_ELEM(tofi->window.entry.placeholder_text), "%s", value);
+ } else if (strcasecmp(option, "placeholder-color") == 0) {
+ tofi->window.entry.placeholder_color = parse_color(filename, lineno, value, &err);
} else if (strcasecmp(option, "min-input-width") == 0) {
tofi->window.entry.input_width = parse_uint32(filename, lineno, value, &err);
} else if (strcasecmp(option, "result-spacing") == 0) {
diff --git a/src/entry.h b/src/entry.h
index caf8c00..335cba2 100644
--- a/src/entry.h
+++ b/src/entry.h
@@ -58,6 +58,7 @@ struct entry {
uint32_t font_size;
char font_name[MAX_FONT_NAME_LENGTH];
char prompt_text[MAX_PROMPT_LENGTH];
+ char placeholder_text[MAX_PROMPT_LENGTH];
uint32_t prompt_padding;
uint32_t corner_radius;
uint32_t padding_top;
@@ -74,6 +75,7 @@ struct entry {
uint32_t outline_width;
struct color foreground_color;
struct color background_color;
+ struct color placeholder_color;
struct color selection_highlight_color;
struct color selection_foreground_color;
struct color selection_background_color;
diff --git a/src/entry_backend/harfbuzz.c b/src/entry_backend/harfbuzz.c
index 5d9ba99..fd3c3d7 100644
--- a/src/entry_backend/harfbuzz.c
+++ b/src/entry_backend/harfbuzz.c
@@ -231,7 +231,11 @@ void entry_backend_harfbuzz_update(struct entry *entry)
/* Render the entry text */
hb_buffer_clear_contents(buffer);
setup_hb_buffer(buffer);
- if (entry->hide_input) {
+ if (entry->input_utf8_length == 0) {
+ color = entry->placeholder_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+ hb_buffer_add_utf8(buffer, entry->placeholder_text, -1, 0, -1);
+ } else if (entry->hide_input) {
size_t char_len = N_ELEM(entry->hidden_character_utf8);
for (size_t i = 0; i < entry->input_utf32_length; i++) {
hb_buffer_add_utf8(buffer, entry->hidden_character_utf8, char_len, 0, char_len);
@@ -246,6 +250,9 @@ void entry_backend_harfbuzz_update(struct entry *entry)
cairo_font_extents_t font_extents;
cairo_font_extents(cr, &font_extents);
+ color = entry->foreground_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+
uint32_t num_results;
if (entry->num_results == 0) {
num_results = entry->results.count;
diff --git a/src/entry_backend/pango.c b/src/entry_backend/pango.c
index d533742..c3dadef 100644
--- a/src/entry_backend/pango.c
+++ b/src/entry_backend/pango.c
@@ -86,7 +86,11 @@ void entry_backend_pango_update(struct entry *entry)
cairo_translate(cr, entry->prompt_padding, 0);
/* Render the entry text */
- if (entry->hide_input) {
+ if (entry->input_utf8_length == 0) {
+ color = entry->placeholder_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+ pango_layout_set_text(layout, entry->placeholder_text, -1);
+ } else if (entry->hide_input) {
/*
* Pango needs to be passed the whole text at once, so we need
* to manually replicate the replacement character in a buffer.
@@ -107,6 +111,9 @@ void entry_backend_pango_update(struct entry *entry)
pango_layout_get_pixel_extents(entry->pango.layout, &ink_rect, &logical_rect);
logical_rect.width = MAX(logical_rect.width, (int)entry->input_width);
+ color = entry->foreground_color;
+ cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
+
uint32_t num_results;
if (entry->num_results == 0) {
num_results = entry->results.count;
diff --git a/src/main.c b/src/main.c
index c70daea..142991b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -642,6 +642,8 @@ static void usage()
" --text-color <color> Color of text.\n"
" --prompt-text <string> Prompt text.\n"
" --prompt-padding <px> Padding between prompt and input.\n"
+" --placeholder-text <string> Placeholder input text.\n"
+" --placeholder-color <px> Color of placeholder input text.\n"
" --num-results <n> Maximum number of results to display.\n"
" --selection-color <color> Color of selected result.\n"
" --selection-match-color <color> Color of the matching portion of the\n"
@@ -707,6 +709,8 @@ const struct option long_options[] = {
{"outline-color", required_argument, NULL, 0},
{"prompt-text", required_argument, NULL, 0},
{"prompt-padding", required_argument, NULL, 0},
+ {"placeholder-text", required_argument, NULL, 0},
+ {"placeholder-color", required_argument, NULL, 0},
{"result-spacing", required_argument, NULL, 0},
{"min-input-width", required_argument, NULL, 0},
{"border-width", required_argument, NULL, 0},
@@ -884,6 +888,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},
+ .placeholder_color = {1.0f, 1.0f, 1.0f, 0.66f},
.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},