From 5ece5078e5bbbdb76d5f88c6e14171bd978656b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 27 Jun 2021 15:35:20 +0200 Subject: [PATCH] feat: add text zoom to text viewer --- lib-gui/include/keyboard_const.h | 2 ++ text-viewer/src/text_viewer.c | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib-gui/include/keyboard_const.h b/lib-gui/include/keyboard_const.h index 5f90490..9dfb2ee 100644 --- a/lib-gui/include/keyboard_const.h +++ b/lib-gui/include/keyboard_const.h @@ -2,3 +2,5 @@ #define KEYBOARD_RIGHT 'l' #define KEYBOARD_DOWN 'j' #define KEYBOARD_UP 'k' +#define KEYBOARD_ZOOM_IN 'z' +#define KEYBOARD_ZOOM_OUT 'x' diff --git a/text-viewer/src/text_viewer.c b/text-viewer/src/text_viewer.c index 435566f..abcfa98 100644 --- a/text-viewer/src/text_viewer.c +++ b/text-viewer/src/text_viewer.c @@ -202,6 +202,15 @@ static void command_handler_full_scroll(void *state, int amount) { gui_text_view_full_scroll((component_t *)state); } +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; +} + +static void command_handler_zoom_out(void *state, int amount) { + command_handler_zoom_in(state, -amount); +} + 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, @@ -230,6 +239,14 @@ void gui_text_view_register_commands(gui_t *gui, component_t *text_view) { ROTATION_ENCODER_HORIZONTAL, command_handler_move_right, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_IN, + command_handler_zoom_in, text_view); + commands_register(gui->commands, IN_KEYBOARD, KEYBOARD_ZOOM_OUT, + command_handler_zoom_out, text_view); + + commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_ZOOM, + command_handler_zoom_in, text_view); + commands_register(gui->commands, IN_ENCODER_ROTATE, ROTATION_ENCODER_VERTICAL, command_handler_move_down, text_view); @@ -253,7 +270,8 @@ void text_viewer_start_loop(text_viewer_t *text_viewer) { // name and line component_t name_and_line_components[2]; - text_t name_text = {.font = &text_viewer->font, + font_t text_viewer_font_copy = text_viewer->font; + text_t name_text = {.font = &text_viewer_font_copy, .line = basename(text_viewer->path), .color = WHITE_PIXEL}; container_t *name_and_line = text_viewer_gui_add_name_and_line( @@ -285,7 +303,7 @@ void text_viewer_start_loop(text_viewer_t *text_viewer) { int32_t ledstrip_index = ((double)lines_scrolled / (text_viewer->multiline_text->lines_count - - text_viewer->gui.size.y / + (double)text_viewer->gui.size.y / (text_viewer->multiline_text->font->size + text_viewer->multiline_text->font->line_spacing))) * LED_STRIP_COUNT; -- 2.48.1