1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:37:35 +00:00

HexEditor: Add comments to annotations

The comment appears as a tooltip when hovering over the annotation.

A couple of properties of the TextEditor would ideally be set in GML,
but either don't have a setter exposed, or the GML compiler doesn't
recognise the enum. I'll fix those up after the current big GML
compiler PR gets merged.
This commit is contained in:
Sam Atkins 2024-01-29 16:03:37 +00:00 committed by Sam Atkins
parent 4b1c7533f5
commit a54952795a
5 changed files with 32 additions and 0 deletions

View file

@ -50,11 +50,16 @@ EditAnnotationDialog::EditAnnotationDialog(GUI::Window* parent_window, NonnullRe
m_start_offset = find_descendant_of_type_named<GUI::NumericInput>("start_offset"); m_start_offset = find_descendant_of_type_named<GUI::NumericInput>("start_offset");
m_end_offset = find_descendant_of_type_named<GUI::NumericInput>("end_offset"); m_end_offset = find_descendant_of_type_named<GUI::NumericInput>("end_offset");
m_background_color = find_descendant_of_type_named<GUI::ColorInput>("background_color"); m_background_color = find_descendant_of_type_named<GUI::ColorInput>("background_color");
m_comments = find_descendant_of_type_named<GUI::TextEditor>("comments");
m_save_button = find_descendant_of_type_named<GUI::DialogButton>("save_button"); m_save_button = find_descendant_of_type_named<GUI::DialogButton>("save_button");
m_cancel_button = find_descendant_of_type_named<GUI::DialogButton>("cancel_button"); m_cancel_button = find_descendant_of_type_named<GUI::DialogButton>("cancel_button");
// FIXME: This could be specified in GML, but the GML doesn't like property setters that aren't `set_FOO()`. // FIXME: This could be specified in GML, but the GML doesn't like property setters that aren't `set_FOO()`.
m_background_color->set_color_has_alpha_channel(false); m_background_color->set_color_has_alpha_channel(false);
// FIXME: Move this to GML too.
m_comments->set_wrapping_mode(GUI::TextEditor::WrapAtWords);
// FIXME: `font_type: "Normal"` in GML once the compiler supports that.
m_comments->set_font(widget->font());
// NOTE: The NumericInput stores an i64, so not all size_t values can fit. But I don't think we'll be // NOTE: The NumericInput stores an i64, so not all size_t values can fit. But I don't think we'll be
// hex-editing files larger than 9000 petabytes for the foreseeable future! // hex-editing files larger than 9000 petabytes for the foreseeable future!
@ -74,6 +79,7 @@ EditAnnotationDialog::EditAnnotationDialog(GUI::Window* parent_window, NonnullRe
m_start_offset->set_value(m_annotation->start_offset); m_start_offset->set_value(m_annotation->start_offset);
m_end_offset->set_value(m_annotation->end_offset); m_end_offset->set_value(m_annotation->end_offset);
m_background_color->set_color(m_annotation->background_color); m_background_color->set_color(m_annotation->background_color);
m_comments->set_text(m_annotation->comments);
}, },
[this](Selection& selection) { [this](Selection& selection) {
set_title("Add Annotation"sv); set_title("Add Annotation"sv);
@ -84,6 +90,7 @@ EditAnnotationDialog::EditAnnotationDialog(GUI::Window* parent_window, NonnullRe
m_end_offset->set_value(selection.is_empty() ? selection.end : selection.end - 1); m_end_offset->set_value(selection.is_empty() ? selection.end : selection.end - 1);
// Default to the most recently used annotation color. // Default to the most recently used annotation color.
m_background_color->set_color(s_most_recent_color); m_background_color->set_color(s_most_recent_color);
m_comments->clear();
}); });
m_save_button->on_click = [this](auto) { m_save_button->on_click = [this](auto) {
@ -93,6 +100,7 @@ EditAnnotationDialog::EditAnnotationDialog(GUI::Window* parent_window, NonnullRe
.start_offset = min(start_offset, end_offset), .start_offset = min(start_offset, end_offset),
.end_offset = max(start_offset, end_offset), .end_offset = max(start_offset, end_offset),
.background_color = m_background_color->color(), .background_color = m_background_color->color(),
.comments = MUST(String::from_byte_string(m_comments->text())),
}; };
if (m_annotation.has_value()) { if (m_annotation.has_value()) {
*m_annotation = move(result); *m_annotation = move(result);

View file

@ -13,6 +13,7 @@
#include <LibGUI/ColorInput.h> #include <LibGUI/ColorInput.h>
#include <LibGUI/Dialog.h> #include <LibGUI/Dialog.h>
#include <LibGUI/NumericInput.h> #include <LibGUI/NumericInput.h>
#include <LibGUI/TextEditor.h>
class EditAnnotationDialog : public GUI::Dialog { class EditAnnotationDialog : public GUI::Dialog {
C_OBJECT_ABSTRACT(EditAnnotationDialog) C_OBJECT_ABSTRACT(EditAnnotationDialog)
@ -32,6 +33,7 @@ private:
RefPtr<GUI::NumericInput> m_start_offset; RefPtr<GUI::NumericInput> m_start_offset;
RefPtr<GUI::NumericInput> m_end_offset; RefPtr<GUI::NumericInput> m_end_offset;
RefPtr<GUI::ColorInput> m_background_color; RefPtr<GUI::ColorInput> m_background_color;
RefPtr<GUI::TextEditor> m_comments;
RefPtr<GUI::Button> m_save_button; RefPtr<GUI::Button> m_save_button;
RefPtr<GUI::Button> m_cancel_button; RefPtr<GUI::Button> m_cancel_button;
}; };

View file

@ -54,6 +54,22 @@
} }
} }
@GUI::Widget {
layout: @GUI::VerticalBoxLayout {
margins: [4]
}
preferred_height: "fit"
@GUI::Label {
text: "Comments:"
text_alignment: "CenterLeft"
}
@GUI::TextEditor {
name: "comments"
}
}
@GUI::Widget { @GUI::Widget {
layout: @GUI::HorizontalBoxLayout { layout: @GUI::HorizontalBoxLayout {
margins: [4] margins: [4]

View file

@ -9,6 +9,7 @@
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h> #include <AK/NonnullOwnPtr.h>
#include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Time.h> #include <AK/Time.h>
#include <AK/Types.h> #include <AK/Types.h>
@ -23,6 +24,7 @@ struct Annotation {
size_t start_offset { 0 }; size_t start_offset { 0 };
size_t end_offset { 0 }; size_t end_offset { 0 };
Gfx::Color background_color { Color::from_argb(0xfffce94f) }; Gfx::Color background_color { Color::from_argb(0xfffce94f) };
String comments {};
bool operator==(Annotation const& other) const = default; bool operator==(Annotation const& other) const = default;
}; };

View file

@ -363,7 +363,11 @@ void HexEditor::mousemove_event(GUI::MouseEvent& event)
update(); update();
update_status(); update_status();
set_tooltip(""_string);
} else {
set_tooltip(m_hovered_annotation.has_value() ? m_hovered_annotation->comments : ""_string);
} }
show_or_hide_tooltip();
} }
void HexEditor::mouseup_event(GUI::MouseEvent& event) void HexEditor::mouseup_event(GUI::MouseEvent& event)