From 81ef1fd7c05db8866ff5aa0856743b42afbb2674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 27 Jun 2021 16:08:52 +0200 Subject: [PATCH] feat: fix text viewer zoom location --- text-viewer/src/text_viewer.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/text-viewer/src/text_viewer.c b/text-viewer/src/text_viewer.c index abcfa98..d932c64 100644 --- a/text-viewer/src/text_viewer.c +++ b/text-viewer/src/text_viewer.c @@ -203,14 +203,32 @@ static void command_handler_full_scroll(void *state, int amount) { } static void command_handler_zoom_in(void *state, int amount) { - multiline_text_t* text = (multiline_text_t*) ((component_t*)state)->state; - text->font->size += amount > 0 ? 1 : -1; + component_t *component = (component_t *)state; + multiline_text_t* text = (multiline_text_t*) (component)->state; + uint16_t old_size = text->font->size; + + amount = amount > 1 ? 1 : -1; + text->font->size += amount; + + component->y += amount * (component->y / (old_size + text->font->line_spacing)); } static void command_handler_zoom_out(void *state, int amount) { command_handler_zoom_in(state, -amount); } +static void command_handler_zoom_reset(void *state, int amount) { + component_t *component = (component_t *)state; + multiline_text_t *text = (multiline_text_t *)(component)->state; + uint16_t old_size = text->font->size; + text->font->size = text->font->font.height; + + + amount = text->font->size - old_size; + component->y += + amount * (component->y / (old_size + text->font->line_spacing)); +} + component_t gui_text_view_create(gui_t *gui, multiline_text_t *text, int16_t x, int16_t y) { component_t text_view = gui_component_create(x, y, 1, 1, gui_text_view_render, @@ -234,6 +252,8 @@ void gui_text_view_register_commands(gui_t *gui, component_t *text_view) { text_view); commands_register(gui->commands, IN_KEYBOARD, 't', command_handler_full_scroll, text_view); + commands_register(gui->commands, IN_KEYBOARD, 'f', + command_handler_zoom_reset, text_view); commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_HORIZONTAL, command_handler_move_right, -- 2.48.1