diff options
-rw-r--r-- | completions/tofi | 1 | ||||
-rw-r--r-- | doc/config | 3 | ||||
-rw-r--r-- | doc/tofi.5.md | 11 | ||||
-rw-r--r-- | doc/tofi.5.scd | 10 | ||||
-rw-r--r-- | src/config.c | 124 | ||||
-rw-r--r-- | src/config.h | 2 | ||||
-rw-r--r-- | src/main.c | 12 | ||||
-rw-r--r-- | src/tofi.h | 1 |
8 files changed, 129 insertions, 35 deletions
diff --git a/completions/tofi b/completions/tofi index 33cc222..88988f9 100644 --- a/completions/tofi +++ b/completions/tofi @@ -9,6 +9,7 @@ _tofi() --help --config --output + --scale --anchor --background-color --corner-radius @@ -92,6 +92,9 @@ padding-left = 0 padding-right = 0 + # Whether to scale the window by the output's scale factor. + scale = true + # ### Window positioning # diff --git a/doc/tofi.5.md b/doc/tofi.5.md index 6de4e60..6eed96d 100644 --- a/doc/tofi.5.md +++ b/doc/tofi.5.md @@ -163,6 +163,17 @@ options. > > Default: "" +**scale**=*true\|false* + +> Scale the window by the output's scale factor. +> +> **WARNING**: In the next version of tofi, this will default to true, +> so set this to false now if you don't want that behaviour. Note that +> currently, font scaling will still occur when this is set to *false* - +> that will change when *true* becomes the default. +> +> Default: false + **margin-top**=*px\|%* > Offset from top of screen. See **PERCENTAGE VALUES** for more diff --git a/doc/tofi.5.scd b/doc/tofi.5.scd index df2f4d6..cf605b3 100644 --- a/doc/tofi.5.scd +++ b/doc/tofi.5.scd @@ -143,6 +143,16 @@ options. Default: "" +*scale*=_true|false_ + Scale the window by the output's scale factor. + + *WARNING*: In the next version of tofi, this will default to true, so + set this to false now if you don't want that behaviour. Note that + currently, font scaling will still occur when this is set to _false_ - + that will change when _true_ becomes the default. + + Default: false + *margin-top*=_px|%_ Offset from top of screen. See *PERCENTAGE VALUES* for more information. Only has an effect when anchored to the top of the screen. diff --git a/src/config.c b/src/config.c index 801a5bf..df4a2f2 100644 --- a/src/config.c +++ b/src/config.c @@ -25,6 +25,7 @@ struct uint32_percent { static char *strip(const char *str); static bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const char *option, const char *value); static char *get_config_path(void); +static uint32_t fixup_percentage(uint32_t value, uint32_t base, bool is_percent, uint32_t scale, bool use_scale); static uint32_t parse_anchor(const char *filename, size_t lineno, const char *str, bool *err); static bool parse_bool(const char *filename, size_t lineno, const char *str, bool *err); @@ -334,6 +335,8 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const tofi->late_keyboard_init = parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "output") == 0) { snprintf(tofi->target_output_name, N_ELEM(tofi->target_output_name), "%s", value); + } else if (strcasecmp(option, "scale") == 0) { + tofi->use_scale = parse_bool(filename, lineno, value, &err); } else { PARSE_ERROR(filename, lineno, "Unknown option \"%s\"\n", option); err = true; @@ -348,38 +351,101 @@ void config_apply(struct tofi *tofi, const char *option, const char *value) }; } -void config_fix_percentages(struct tofi *tofi) +uint32_t fixup_percentage(uint32_t value, uint32_t base, bool is_percent, uint32_t scale, bool use_scale) { - if (tofi->window.width_is_percent) { - tofi->window.width = tofi->window.width * tofi->output_width / 100; + if (is_percent) { + return value * base / 100; + } else if (use_scale) { + return value * scale; } - if (tofi->window.height_is_percent) { - tofi->window.height = tofi->window.height * tofi->output_height / 100; - } - if (tofi->window.margin_top_is_percent) { - tofi->window.margin_top = tofi->window.margin_top * tofi->output_height / 100; - } - if (tofi->window.margin_bottom_is_percent) { - tofi->window.margin_bottom = tofi->window.margin_bottom * tofi->output_height / 100; - } - if (tofi->window.margin_left_is_percent) { - tofi->window.margin_left = tofi->window.margin_left * tofi->output_width / 100; - } - if (tofi->window.margin_right_is_percent) { - tofi->window.margin_right = tofi->window.margin_right * tofi->output_width / 100; - } - if (tofi->window.entry.padding_top_is_percent) { - tofi->window.entry.padding_top = tofi->window.entry.padding_top * tofi->output_height / 100; - } - if (tofi->window.entry.padding_bottom_is_percent) { - tofi->window.entry.padding_bottom = tofi->window.entry.padding_bottom * tofi->output_height / 100; - } - if (tofi->window.entry.padding_left_is_percent) { - tofi->window.entry.padding_left = tofi->window.entry.padding_left * tofi->output_width / 100; - } - if (tofi->window.entry.padding_right_is_percent) { - tofi->window.entry.padding_right = tofi->window.entry.padding_right * tofi->output_width / 100; + return value; +} + +void config_fixup_values(struct tofi *tofi) +{ + uint32_t scale = tofi->window.scale; + + /* + * Scale fonts to the correct size. + * + * TODO: In the next release (0.6.0), this should be moved within + * the use_scale conditional. + */ + tofi->window.entry.font_size *= scale; + + if (tofi->use_scale) { + struct entry *entry = &tofi->window.entry; + + entry->corner_radius *= scale; + entry->selection_background_padding *= scale; + entry->result_spacing *= scale; + entry->input_width *= scale; + entry->outline_width *= scale; + entry->border_width *= scale; + entry->input_width *= scale; } + + /* These values should only be scaled if they're not percentages. */ + tofi->window.width = fixup_percentage( + tofi->window.width, + tofi->output_width, + tofi->window.width_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.height = fixup_percentage( + tofi->window.height, + tofi->output_height, + tofi->window.height_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.margin_top = fixup_percentage( + tofi->window.margin_top, + tofi->output_height, + tofi->window.margin_top_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.margin_bottom = fixup_percentage( + tofi->window.margin_bottom, + tofi->output_height, + tofi->window.margin_bottom_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.margin_left = fixup_percentage( + tofi->window.margin_left, + tofi->output_width, + tofi->window.margin_left_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.margin_right = fixup_percentage( + tofi->window.margin_right, + tofi->output_width, + tofi->window.margin_right_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.entry.padding_top = fixup_percentage( + tofi->window.entry.padding_top, + tofi->output_height, + tofi->window.entry.padding_top_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.entry.padding_bottom = fixup_percentage( + tofi->window.entry.padding_bottom, + tofi->output_height, + tofi->window.entry.padding_bottom_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.entry.padding_left = fixup_percentage( + tofi->window.entry.padding_left, + tofi->output_width, + tofi->window.entry.padding_left_is_percent, + tofi->window.scale, + tofi->use_scale); + tofi->window.entry.padding_right = fixup_percentage( + tofi->window.entry.padding_right, + tofi->output_width, + tofi->window.entry.padding_right_is_percent, + tofi->window.scale, + tofi->use_scale); } char *get_config_path() diff --git a/src/config.h b/src/config.h index c8db6b4..8dbd241 100644 --- a/src/config.h +++ b/src/config.h @@ -5,6 +5,6 @@ void config_load(struct tofi *tofi, const char *filename); void config_apply(struct tofi *tofi, const char *option, const char *value); -void config_fix_percentages(struct tofi *tofi); +void config_fixup_values(struct tofi *tofi); #endif /* TOFI_CONFIG_H */ @@ -763,6 +763,7 @@ static void usage() " --height <px|%> Height of the window.\n" " --corner-radius <px> Radius of window corners.\n" " --output <name> Name of output to display window on.\n" +" --scale <true|false> Follow the output's scale factor.\n" " --anchor <position> Location on screen to anchor window.\n" " --margin-top <px|%> Offset from top of screen.\n" " --margin-bottom <px|%> Offset from bottom of screen.\n" @@ -827,6 +828,7 @@ const struct option long_options[] = { {"drun-print-exec", required_argument, NULL, 0}, {"hint-font", required_argument, NULL, 0}, {"output", required_argument, NULL, 0}, + {"scale", required_argument, NULL, 0}, {"late-keyboard-init", optional_argument, NULL, 'k'}, {NULL, 0, NULL, 0} }; @@ -1139,11 +1141,11 @@ int main(int argc, char *argv[]) log_debug("Selected output %s.\n", el->name); } - /* We can now calculate any percentages, as we know the output size. */ - config_fix_percentages(&tofi); - - /* Scale fonts to the correct size. */ - tofi.window.entry.font_size *= tofi.window.scale; + /* + * We can now scale values and calculate any percentages, as we know + * the output size and scale. + */ + config_fixup_values(&tofi); /* * If we were invoked as tofi-run, generate the command list. @@ -79,6 +79,7 @@ struct tofi { uint32_t anchor; bool hide_cursor; bool use_history; + bool use_scale; bool late_keyboard_init; bool drun_launch; bool drun_print_exec; |