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
This commit is contained in:
Mark Thompson 2023-06-29 12:55:25 -05:00 committed by GitHub
parent e15a8ed2d8
commit b28b96fb4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 21 deletions

View File

@ -42,10 +42,9 @@ namespace ui {
/* TextViewer *******************************************************/
TextViewer::TextViewer(Rect parent_rect)
: Widget(parent_rect),
max_line{static_cast<uint8_t>(parent_rect.height() / char_height)},
max_col{static_cast<uint8_t>(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]() {

View File

@ -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<void()>& on_cut() { return button_cut.on_select; }
std::function<void()>& on_paste() { return button_paste.on_select; }
std::function<void()>& on_copy() { return button_copy.on_select; }
std::function<void()>& on_zoom() { return button_zoom.on_select; }
std::function<void()>& on_delete_line() { return button_delline.on_select; }
std::function<void()>& 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},

View File

@ -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;

View File

@ -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: