From b28b96fb4a99e5af87fe774757f744fcb3e8981c Mon Sep 17 00:00:00 2001 From: Mark Thompson <129641948+NotherNgineer@users.noreply.github.com> Date: Thu, 29 Jun 2023 12:55:25 -0500 Subject: [PATCH] Notepad text zoom support 5x8 to 8x16 font (#1211) * Zoom button 5/8 to 8/16 font * Zoom button 5/8 to 8/16 font * Clang & revert unintended change * Suggested improvements * Suggested code changes * Added char_width function * Added char_width function * Oops copy-paste error * Delete added blank line --- firmware/application/apps/ui_text_editor.cpp | 21 ++++++------ firmware/application/apps/ui_text_editor.hpp | 36 ++++++++++++++------ firmware/common/ui_text.cpp | 4 +++ firmware/common/ui_text.hpp | 2 ++ 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/firmware/application/apps/ui_text_editor.cpp b/firmware/application/apps/ui_text_editor.cpp index 461e7e3d..c3009743 100644 --- a/firmware/application/apps/ui_text_editor.cpp +++ b/firmware/application/apps/ui_text_editor.cpp @@ -42,10 +42,9 @@ namespace ui { /* TextViewer *******************************************************/ TextViewer::TextViewer(Rect parent_rect) - : Widget(parent_rect), - max_line{static_cast(parent_rect.height() / char_height)}, - max_col{static_cast(parent_rect.width() / char_width)} { + : Widget(parent_rect) { set_focusable(true); + set_font_zoom(false); } void TextViewer::paint(Painter& painter) { @@ -207,7 +206,7 @@ void TextViewer::paint_text(Painter& painter, uint32_t line, uint16_t col) { if (result && *result > 0) painter.draw_string( {0, r.top() + (int)i * char_height}, - Styles::white_small, {buffer, *result}); + style(), {buffer, *result}); // Clear empty line sections. This is less visually jarring than full clear. int32_t clear_width = max_col - (result ? *result : 0); @@ -216,7 +215,7 @@ void TextViewer::paint_text(Painter& painter, uint32_t line, uint16_t col) { {(max_col - clear_width) * char_width, r.top() + (int)i * char_height, clear_width * char_width, char_height}, - Styles::white_small.background); + style().background); } } @@ -237,8 +236,8 @@ void TextViewer::paint_cursor(Painter& painter) { }; // Clear old cursor. CONSIDER: XOR cursor? - draw_cursor(paint_state_.line, paint_state_.col, Styles::white_small.background); - draw_cursor(cursor_.line, cursor_.col, Styles::white_small.foreground); + draw_cursor(paint_state_.line, paint_state_.col, style().background); + draw_cursor(cursor_.line, cursor_.col, style().foreground); paint_state_.line = cursor_.line; paint_state_.col = cursor_.col; } @@ -261,7 +260,7 @@ TextEditorMenu::TextEditorMenu() &rect_frame, &button_cut, &button_paste, - &button_copy, + &button_zoom, &button_delline, &button_edit, &button_addline, @@ -316,8 +315,10 @@ TextEditorView::TextEditorView(NavigationView& nav) menu.on_paste() = [this]() { show_nyi(); }; - menu.on_copy() = [this]() { - show_nyi(); + menu.on_zoom() = [this]() { + viewer.toggle_font_zoom(); + refresh_ui(); + hide_menu(true); }; menu.on_delete_line() = [this]() { diff --git a/firmware/application/apps/ui_text_editor.hpp b/firmware/application/apps/ui_text_editor.hpp index e7436a20..99053a83 100644 --- a/firmware/application/apps/ui_text_editor.hpp +++ b/firmware/application/apps/ui_text_editor.hpp @@ -75,12 +75,26 @@ class TextViewer : public Widget { // Gets the length of the current line. uint16_t line_length(); - private: - static constexpr int8_t char_width = 5; - static constexpr int8_t char_height = 8; + const Style& style() { return *font_style; } - const uint8_t max_line = 32; - const uint8_t max_col = 48; + void set_font_zoom(bool zoom) { + font_zoom = zoom; + font_style = font_zoom ? &Styles::white : &Styles::white_small; + char_height = style().font.line_height(); + char_width = style().font.char_width(); + max_line = (uint8_t)(parent_rect().height() / char_height); + max_col = (uint8_t)(parent_rect().width() / char_width); + } + + void toggle_font_zoom() { set_font_zoom(!font_zoom); }; + + private: + bool font_zoom{}; + const Style* font_style{}; + int8_t char_width{}; + int8_t char_height{}; + uint8_t max_line{}; + uint8_t max_col{}; /* Returns true if the cursor was updated. */ bool apply_scrolling_constraints( @@ -122,7 +136,7 @@ class TextEditorMenu : public View { std::function& on_cut() { return button_cut.on_select; } std::function& on_paste() { return button_paste.on_select; } - std::function& on_copy() { return button_copy.on_select; } + std::function& on_zoom() { return button_zoom.on_select; } std::function& on_delete_line() { return button_delline.on_select; } std::function& on_edit_line() { return button_edit.on_select; } @@ -151,11 +165,11 @@ class TextEditorMenu : public View { &bitmap_icon_paste, Color::dark_grey()}; - NewButton button_copy{ + NewButton button_zoom{ {15 * 8, 1 * 8, 7 * 8, 7 * 8}, - "Copy", - &bitmap_icon_copy, - Color::dark_grey()}; + "Zoom", + &bitmap_icon_search, + Color::dark_green()}; NewButton button_delline{ {1 * 8, 8 * 8, 7 * 8, 7 * 8}, @@ -258,4 +272,4 @@ class TextEditorView : public View { } // namespace ui -#endif // __UI_TEXT_EDITOR_H__ \ No newline at end of file +#endif // __UI_TEXT_EDITOR_H__ diff --git a/firmware/common/ui_text.cpp b/firmware/common/ui_text.cpp index 7f7b1b4e..0bac1641 100644 --- a/firmware/common/ui_text.cpp +++ b/firmware/common/ui_text.cpp @@ -39,6 +39,10 @@ Dim Font::line_height() const { return h; } +Dim Font::char_width() const { + return w; +} + Size Font::size_of(const std::string s) const { Size size; diff --git a/firmware/common/ui_text.hpp b/firmware/common/ui_text.hpp index 45a9f6cc..cdf31b09 100644 --- a/firmware/common/ui_text.hpp +++ b/firmware/common/ui_text.hpp @@ -86,6 +86,8 @@ class Font { Glyph glyph(const char c) const; Dim line_height() const; + Dim char_width() const; + Size size_of(const std::string s) const; private: