diff --git a/Libraries/LibGUI/ColorInput.cpp b/Libraries/LibGUI/ColorInput.cpp index 46dc803ee5..fc2d69cda1 100644 --- a/Libraries/LibGUI/ColorInput.cpp +++ b/Libraries/LibGUI/ColorInput.cpp @@ -25,9 +25,9 @@ */ #include +#include #include #include -#include #include namespace GUI { @@ -35,83 +35,78 @@ namespace GUI { ColorInput::ColorInput() : TextEditor(TextEditor::SingleLine) { - set_readonly(true); + TextEditor::on_change = [this] { + auto parsed_color = Color::from_string(text()); + if (parsed_color.has_value()) + set_color_without_changing_text(parsed_color.value()); + }; } ColorInput::~ColorInput() { } -void ColorInput::set_color(Color color) +Gfx::Rect ColorInput::color_rect() const { + auto color_box_padding = 3; + auto color_box_size = height() - color_box_padding - color_box_padding; + 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) +{ + if (m_color == color) + return; m_color = color; - set_text(color.to_string()); - update(); - if (on_change) on_change(); +} + +void ColorInput::set_color(Color color) +{ + if (m_color == color) + return; + set_text(color.to_string()); }; void ColorInput::mousedown_event(MouseEvent& event) { if (event.button() == MouseButton::Left) { - if (is_enabled()) { - m_being_pressed = true; - update(); + if (is_enabled() && color_rect().contains(event.position())) { + auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title); + if (dialog->exec() == GUI::Dialog::ExecOK) + set_color(dialog->color()); + event.accept(); + return; } } - Widget::mousedown_event(event); + TextEditor::mousedown_event(event); } -void ColorInput::mouseup_event(MouseEvent& event) +void ColorInput::mousemove_event(MouseEvent& event) { - if (event.button() == MouseButton::Left) { - if (is_enabled()) { - bool was_being_pressed = m_being_pressed; - m_being_pressed = false; - update(); - if (was_being_pressed) - click(); - } + if (color_rect().contains(event.position())) { + window()->set_override_cursor(StandardCursor::Hand); + event.accept(); + return; + } else { + window()->set_override_cursor(StandardCursor::IBeam); } - Widget::mouseup_event(event); -} -void ColorInput::enter_event(Core::Event&) -{ - ASSERT(window()); - window()->set_override_cursor(StandardCursor::Arrow); + TextEditor::mousemove_event(event); } void ColorInput::paint_event(PaintEvent& event) { - // Set cursor color to base color and stop timer. FIXME: Find better way to hide cursor. - auto pal = palette(); - pal.set_color(ColorRole::TextCursor, palette().base()); - set_palette(pal); - stop_timer(); - TextEditor::paint_event(event); - auto color_box_padding = 3; - auto color_box_size = event.rect().height() - color_box_padding - color_box_padding; - Painter painter(*this); - painter.fill_rect({ event.rect().width() - color_box_size - color_box_padding , color_box_padding, color_box_size, color_box_size}, m_color); -} + painter.add_clip_rect(event.rect()); -void ColorInput::click() -{ - if (!is_enabled()) - return; - - auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title); - if (dialog->exec() == GUI::Dialog::ExecOK) { - auto tmp = dialog->color(); - set_color(tmp); - } + painter.fill_rect(color_rect(), m_color); + painter.draw_rect(color_rect(), Color::Black); } } diff --git a/Libraries/LibGUI/ColorInput.h b/Libraries/LibGUI/ColorInput.h index 8e541d6e3d..5efbfa98af 100644 --- a/Libraries/LibGUI/ColorInput.h +++ b/Libraries/LibGUI/ColorInput.h @@ -41,24 +41,22 @@ public: void set_color(Color); Color color() { return m_color; } - void set_color_picker_title(String title) { m_color_picker_title = title; } + void set_color_picker_title(String title) { m_color_picker_title = move(title); } String color_picker_title() { return m_color_picker_title; } Function on_change; protected: virtual void mousedown_event(MouseEvent&) override; - virtual void mouseup_event(MouseEvent&) override; - virtual void enter_event(Core::Event&) override; + virtual void mousemove_event(MouseEvent&) override; virtual void paint_event(PaintEvent&) override; private: - void click(); + Gfx::Rect color_rect() const; + void set_color_without_changing_text(Color); Color m_color; - String m_color_picker_title { "Select Color" }; - - bool m_being_pressed { false }; + String m_color_picker_title { "Select color" }; }; }