Browse Source

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.
Sam Atkins 3 years ago
parent
commit
5fd0140772

+ 7 - 7
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)

+ 2 - 2
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" };