From d3012f2df16aff60d38e3008ce4a13768c577c6e Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 5 Feb 2024 11:57:40 +0000 Subject: [PATCH] HexEditor: Share the code for updating the content size And make setting the bytes_per_row a no-op if it's the same value. --- Userland/Applications/HexEditor/HexEditor.cpp | 15 ++++++++++----- Userland/Applications/HexEditor/HexEditor.h | 3 ++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index a695aa5b5d..0121cb1894 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -232,23 +232,28 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code() return true; } -void HexEditor::set_bytes_per_row(size_t bytes_per_row) +void HexEditor::update_content_size() { - m_bytes_per_row = bytes_per_row; auto new_width = offset_area_width() + hex_area_width() + text_area_width(); auto new_height = total_rows() * line_height() + 2 * m_padding; set_content_size({ static_cast(new_width), static_cast(new_height) }); update(); } +void HexEditor::set_bytes_per_row(size_t bytes_per_row) +{ + if (bytes_per_row == m_bytes_per_row) + return; + m_bytes_per_row = bytes_per_row; + update_content_size(); +} + void HexEditor::set_content_length(size_t length) { if (length == m_content_length) return; m_content_length = length; - auto new_width = offset_area_width() + hex_area_width() + text_area_width(); - auto new_height = total_rows() * line_height() + 2 * m_padding; - set_content_size({ static_cast(new_width), static_cast(new_height) }); + update_content_size(); } Optional HexEditor::get_byte(size_t position) diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 44d245c7df..396157a97f 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -125,7 +125,8 @@ private: ErrorOr hex_mode_keydown_event(GUI::KeyEvent&); ErrorOr text_mode_keydown_event(GUI::KeyEvent&); - void set_content_length(size_t); // I might make this public if I add fetching data on demand. + void set_content_length(size_t); + void update_content_size(); void update_status(); void did_change(); ErrorOr did_complete_action(size_t position, u8 old_value, u8 new_value);