diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-04-29 10:46:56 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-29 20:06:39 +0200 |
commit | 5fd014077203b38e819a8a195c2c40b62c33c11b (patch) | |
tree | cdd3cf52123801a6dc18b56d8cf9f4407d6239da | |
parent | 91230ff28d88f1134dd9fa0d3766a4081b5b0b6f (diff) | |
download | serenity-5fd014077203b38e819a8a195c2c40b62c33c11b.zip |
LibGUI: Add AllowCallback parameter to ColorInput::set_color()
The `TextEditor::on_change` callback now only fires if the user types in
the box, or `set_text()` is called with `AllowCallback::Yes`.
Previously that callback was what set `m_color`, so I've rearranged
things a little so that the color still updates regardless of what
source the color came from.
-rw-r--r-- | Userland/Libraries/LibGUI/ColorInput.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/ColorInput.h | 4 |
2 files changed, 9 insertions, 9 deletions
diff --git a/Userland/Libraries/LibGUI/ColorInput.cpp b/Userland/Libraries/LibGUI/ColorInput.cpp index ec52400982..0cf960adea 100644 --- a/Userland/Libraries/LibGUI/ColorInput.cpp +++ b/Userland/Libraries/LibGUI/ColorInput.cpp @@ -23,7 +23,7 @@ ColorInput::ColorInput() TextEditor::on_change = [this] { auto parsed_color = Color::from_string(text()); if (parsed_color.has_value()) - set_color_without_changing_text(parsed_color.value()); + set_color_internal(parsed_color.value(), AllowCallback::Yes, false); }; REGISTER_STRING_PROPERTY("color_picker_title", color_picker_title, set_color_picker_title); @@ -37,21 +37,21 @@ Gfx::IntRect ColorInput::color_rect() const return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size }; } -void ColorInput::set_color_without_changing_text(Color color) +void ColorInput::set_color_internal(Color color, AllowCallback allow_callback, bool change_text) { if (m_color == color) return; m_color = color; + if (change_text) + set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha(), AllowCallback::No); update(); - if (on_change) + if (allow_callback == AllowCallback::Yes && on_change) on_change(); } -void ColorInput::set_color(Color color) +void ColorInput::set_color(Color color, AllowCallback allow_callback) { - if (m_color == color) - return; - set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha()); + set_color_internal(color, allow_callback, true); }; void ColorInput::mousedown_event(MouseEvent& event) diff --git a/Userland/Libraries/LibGUI/ColorInput.h b/Userland/Libraries/LibGUI/ColorInput.h index 099478cef5..f0ef9c4122 100644 --- a/Userland/Libraries/LibGUI/ColorInput.h +++ b/Userland/Libraries/LibGUI/ColorInput.h @@ -21,7 +21,7 @@ public: bool has_alpha_channel() const { return m_color_has_alpha_channel; } void set_color_has_alpha_channel(bool has_alpha) { m_color_has_alpha_channel = has_alpha; } - void set_color(Color); + void set_color(Color, AllowCallback = AllowCallback::Yes); Color color() { return m_color; } void set_color_picker_title(String title) { m_color_picker_title = move(title); } @@ -39,7 +39,7 @@ private: ColorInput(); Gfx::IntRect color_rect() const; - void set_color_without_changing_text(Color); + void set_color_internal(Color, AllowCallback, bool change_text); Color m_color; String m_color_picker_title { "Select color" }; |