From 825c9eaeb15f369aab0368766342a52ec1909422 Mon Sep 17 00:00:00 2001 From: Valtteri Koskivuori Date: Fri, 21 Jul 2023 00:51:05 +0300 Subject: [PATCH] LibGUI: Make `GUI::ColorPicker` interactive There is now a `on_color_changed` callback that clients can optionally hook into to receive real-time updates while the user is picking a color. If the user hits Cancel, the callback gets called once more with the color passed in while constructing `ColorPicker`. If the user hits OK, the same happens with the currently selected color instead. Programs therefore can perform all their updates with this callback, and only care about `ExecResult` if they want to make a decision, like if we should write the result to `ConfigServer`, for example. --- Userland/Libraries/LibGUI/ColorInput.cpp | 6 ++++-- Userland/Libraries/LibGUI/ColorPicker.cpp | 7 +++++++ Userland/Libraries/LibGUI/ColorPicker.h | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGUI/ColorInput.cpp b/Userland/Libraries/LibGUI/ColorInput.cpp index e15344f6d8..d65abf4de4 100644 --- a/Userland/Libraries/LibGUI/ColorInput.cpp +++ b/Userland/Libraries/LibGUI/ColorInput.cpp @@ -71,9 +71,11 @@ void ColorInput::mouseup_event(MouseEvent& event) m_may_be_color_rect_click = false; if (is_color_rect_click) { auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title); + dialog->on_color_changed = [this](Gfx::Color color) { + set_color(color); + }; dialog->set_color_has_alpha_channel(m_color_has_alpha_channel); - if (dialog->exec() == GUI::Dialog::ExecResult::OK) - set_color(dialog->color()); + dialog->exec(); event.accept(); return; } diff --git a/Userland/Libraries/LibGUI/ColorPicker.cpp b/Userland/Libraries/LibGUI/ColorPicker.cpp index 7563da4c7c..d1cf66e1df 100644 --- a/Userland/Libraries/LibGUI/ColorPicker.cpp +++ b/Userland/Libraries/LibGUI/ColorPicker.cpp @@ -185,6 +185,7 @@ private: ColorPicker::ColorPicker(Color color, Window* parent_window, DeprecatedString title) : Dialog(parent_window) + , m_original_color(color) , m_color(color) { set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"sv).release_value_but_fixme_should_propagate_errors()); @@ -230,6 +231,8 @@ void ColorPicker::build_ui() auto& ok_button = button_container.add(); ok_button.set_text("OK"_short_string); ok_button.on_click = [this](auto) { + if (on_color_changed) + on_color_changed(m_color); done(ExecResult::OK); }; ok_button.set_default(true); @@ -237,6 +240,8 @@ void ColorPicker::build_ui() auto& cancel_button = button_container.add(); cancel_button.set_text("Cancel"_short_string); cancel_button.on_click = [this](auto) { + if (on_color_changed) + on_color_changed(m_original_color); done(ExecResult::Cancel); }; } @@ -439,6 +444,8 @@ void ColorPicker::update_color_widgets() m_alpha_spinbox->set_enabled(m_color_has_alpha_channel); m_alpha->set_value(m_color.alpha()); m_alpha->set_visible(m_color_has_alpha_channel); + if (on_color_changed) + on_color_changed(m_color); } void ColorPicker::create_color_button(Widget& container, unsigned rgb) diff --git a/Userland/Libraries/LibGUI/ColorPicker.h b/Userland/Libraries/LibGUI/ColorPicker.h index f69f3a17e8..79db518d88 100644 --- a/Userland/Libraries/LibGUI/ColorPicker.h +++ b/Userland/Libraries/LibGUI/ColorPicker.h @@ -26,6 +26,7 @@ public: bool color_has_alpha_channel() const { return m_color_has_alpha_channel; } void set_color_has_alpha_channel(bool); Color color() const { return m_color; } + Function on_color_changed; private: explicit ColorPicker(Color, Window* parent_window = nullptr, DeprecatedString title = "Color Picker"); @@ -36,6 +37,7 @@ private: void update_color_widgets(); void create_color_button(Widget& container, unsigned rgb); + Color m_original_color; Color m_color; bool m_color_has_alpha_channel { true };