From ea64fc43a6a321fcc7417f35d5ce50a2c7ee9d7e Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sat, 20 Aug 2022 09:37:40 +0100 Subject: Add scale option. Previously, tofi wouldn't scale pixel values by the output's scale factor. This allows pixel-perfect sizes on displays with scale factors >1, but means that configs need to be changed on a per-monitor basis, and is at odds with how other applications (notable Sway) behave. This commit adds a new option, --scale, to scale pixel values by the output's scale factor. For backwards compatibility, this currently defaults to false, and font scaling is performed regardless (the existing behaviour). In the next release, this will default to true, and font scaling will follow the same behaviour as everything else. --- src/config.c | 124 +++++++++++++++++++++++++++++++++++++++++++++-------------- src/config.h | 2 +- src/main.c | 12 +++--- src/tofi.h | 1 + 4 files changed, 104 insertions(+), 35 deletions(-) (limited to 'src') 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 */ diff --git a/src/main.c b/src/main.c index 8cc913d..7d861b4 100644 --- a/src/main.c +++ b/src/main.c @@ -763,6 +763,7 @@ static void usage() " --height Height of the window.\n" " --corner-radius Radius of window corners.\n" " --output Name of output to display window on.\n" +" --scale Follow the output's scale factor.\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" @@ -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. diff --git a/src/tofi.h b/src/tofi.h index 1fcdf58..eb3f0ce 100644 --- a/src/tofi.h +++ b/src/tofi.h @@ -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; -- cgit v1.2.3