From d7d57884694ad5fda53e865f1b92ef48fddc9ca8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 29 Apr 2020 19:15:34 +0200 Subject: LibGUI: ColorPicker should pick custom color from bitmap, not window It's not always safe to access pixel data of a window's backing store since the kernel may decide to purge it at his leisure. Fix this by instead picking colors from the color spectrum bitmap directly instead. Also fix up mouse event logic while we're here so it only cares about the left mouse button Fixes #1657. --- Libraries/LibGUI/ColorPicker.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'Libraries/LibGUI') diff --git a/Libraries/LibGUI/ColorPicker.cpp b/Libraries/LibGUI/ColorPicker.cpp index e82027e99a..b090fabcf7 100644 --- a/Libraries/LibGUI/ColorPicker.cpp +++ b/Libraries/LibGUI/ColorPicker.cpp @@ -72,10 +72,10 @@ private: CustomColorWidget(); RefPtr m_custom_colors; - bool m_status { false }; + bool m_being_pressed { false }; Gfx::Point m_last_position; - void fire_event(GUI::MouseEvent& event); + void pick_color_at_position(GUI::MouseEvent& event); virtual void mousedown_event(GUI::MouseEvent&) override; virtual void mouseup_event(GUI::MouseEvent&) override; @@ -409,22 +409,20 @@ void CustomColorWidget::clear_last_position() update(); } -void CustomColorWidget::fire_event(GUI::MouseEvent& event) +void CustomColorWidget::pick_color_at_position(GUI::MouseEvent& event) { - if (!m_status) - return; - - if (!on_pick) + if (!m_being_pressed) return; auto position = event.position(); - if (!this->rect().contains(position)) { + if (!rect().contains(position)) return; - } + + auto color = m_custom_colors->get_pixel(position); m_last_position = position; - auto color = this->window()->back_bitmap()->get_pixel(position); - on_pick(color); + if (on_pick) + on_pick(color); update(); } @@ -432,21 +430,23 @@ void CustomColorWidget::fire_event(GUI::MouseEvent& event) void CustomColorWidget::mousedown_event(GUI::MouseEvent& event) { if (event.button() == GUI::MouseButton::Left) { - m_status = true; - } else { - m_status = false; + m_being_pressed = true; + pick_color_at_position(event); } } void CustomColorWidget::mouseup_event(GUI::MouseEvent& event) { - fire_event(event); - m_status = false; + if (event.button() == GUI::MouseButton::Left) { + m_being_pressed = false; + pick_color_at_position(event); + } } void CustomColorWidget::mousemove_event(GUI::MouseEvent& event) { - fire_event(event); + if (event.buttons() & GUI::MouseButton::Left) + pick_color_at_position(event); } void CustomColorWidget::paint_event(GUI::PaintEvent& event) -- cgit v1.2.3