summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-29 19:15:34 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-29 19:17:40 +0200
commitd7d57884694ad5fda53e865f1b92ef48fddc9ca8 (patch)
tree87782ac80fdfe3544c7b89f8e8d8d6e0b1326888
parent6a018270461646536d8516be722ab45f0f475c93 (diff)
downloadserenity-d7d57884694ad5fda53e865f1b92ef48fddc9ca8.zip
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.
-rw-r--r--Libraries/LibGUI/ColorPicker.cpp34
1 files changed, 17 insertions, 17 deletions
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<Gfx::Bitmap> 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)