From 6306cf5c27c8f24067845be842719f55b5152f5d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 6 Apr 2019 13:55:56 +0200 Subject: [PATCH] GScrollBar: Add the same hover highlight effect as GButton. --- LibGUI/GScrollBar.cpp | 24 +++++++++++++++++++++--- LibGUI/GScrollBar.h | 10 ++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/LibGUI/GScrollBar.cpp b/LibGUI/GScrollBar.cpp index d41dd99bdd0..71ed2f8012e 100644 --- a/LibGUI/GScrollBar.cpp +++ b/LibGUI/GScrollBar.cpp @@ -197,14 +197,14 @@ void GScrollBar::paint_event(GPaintEvent& event) painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce)); - StylePainter::paint_button(painter, up_button_rect(), ButtonStyle::Normal, false); + StylePainter::paint_button(painter, up_button_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::DecrementButton); painter.draw_bitmap(up_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); - StylePainter::paint_button(painter, down_button_rect(), ButtonStyle::Normal, false); + StylePainter::paint_button(painter, down_button_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::IncrementButton); painter.draw_bitmap(down_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); if (has_scrubber()) - StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false); + StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber); } void GScrollBar::mousedown_event(GMouseEvent& event) @@ -271,6 +271,19 @@ void GScrollBar::mouseup_event(GMouseEvent& event) void GScrollBar::mousemove_event(GMouseEvent& event) { + auto old_hovered_component = m_hovered_component; + if (scrubber_rect().contains(event.position())) + m_hovered_component = Component::Scrubber; + else if (up_button_rect().contains(event.position())) + m_hovered_component = Component::DecrementButton; + else if (down_button_rect().contains(event.position())) + m_hovered_component = Component::IncrementButton; + else if (rect().contains(event.position())) + m_hovered_component = Component::Gutter; + else + m_hovered_component = Component::Invalid; + if (old_hovered_component != m_hovered_component) + update(); if (!m_scrubbing) return; float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x()); @@ -279,3 +292,8 @@ void GScrollBar::mousemove_event(GMouseEvent& event) float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta); set_value(new_value); } + +void GScrollBar::leave_event(GEvent&) +{ + m_hovered_component = Component::Invalid; +} diff --git a/LibGUI/GScrollBar.h b/LibGUI/GScrollBar.h index b29e9f38117..fb34b168eb7 100644 --- a/LibGUI/GScrollBar.h +++ b/LibGUI/GScrollBar.h @@ -26,11 +26,20 @@ public: virtual const char* class_name() const override { return "GScrollBar"; } + enum Component { + Invalid, + DecrementButton, + IncrementButton, + Gutter, + Scrubber, + }; + private: virtual void paint_event(GPaintEvent&) override; virtual void mousedown_event(GMouseEvent&) override; virtual void mouseup_event(GMouseEvent&) override; virtual void mousemove_event(GMouseEvent&) override; + virtual void leave_event(GEvent&) override; int button_size() const { return orientation() == Orientation::Vertical ? width() : height(); } Rect up_button_rect() const; @@ -52,4 +61,5 @@ private: Point m_scrub_origin; Orientation m_orientation { Orientation::Vertical }; + Component m_hovered_component { Component::Invalid }; };