diff options
author | Nico Weber <thakis@chromium.org> | 2020-08-25 12:54:40 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-25 20:20:45 +0200 |
commit | 23e2944aa4315a937b0404bccf996d02e095022d (patch) | |
tree | cafbefdd2f877eb8528fa50f9df2c93d26c25f86 | |
parent | 9ea6ef4ed1450590ad02f6d501c7345eab779439 (diff) | |
download | serenity-23e2944aa4315a937b0404bccf996d02e095022d.zip |
LibGUI: Make ScrollBar track the currently pressed component
And remove the now-redundant members m_scrubbing, m_scrubber_in_use,
and m_automatic_scrolling_kind.
This also made it clear that we weren't canceling the autoscroll
timer if the scrollbar got disabled while it was scrolling, so
this fixes that too.
-rw-r--r-- | Libraries/LibGUI/ScrollBar.cpp | 54 | ||||
-rw-r--r-- | Libraries/LibGUI/ScrollBar.h | 17 |
2 files changed, 29 insertions, 42 deletions
diff --git a/Libraries/LibGUI/ScrollBar.cpp b/Libraries/LibGUI/ScrollBar.cpp index 7f95b199eb..c727d9cfc9 100644 --- a/Libraries/LibGUI/ScrollBar.cpp +++ b/Libraries/LibGUI/ScrollBar.cpp @@ -237,8 +237,8 @@ void ScrollBar::paint_event(PaintEvent& event) painter.fill_rect_with_dither_pattern(rect(), palette().button().lightened(1.3f), palette().button()); - bool decrement_pressed = m_automatic_scrolling_kind == AutomaticScrollingKind::DecrementButton; - bool increment_pressed = m_automatic_scrolling_kind == AutomaticScrollingKind::IncrementButton; + bool decrement_pressed = m_pressed_component == Component::DecrementButton; + bool increment_pressed = m_pressed_component == Component::IncrementButton; Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, decrement_pressed, m_hovered_component == Component::DecrementButton); Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::Normal, increment_pressed, m_hovered_component == Component::IncrementButton); @@ -256,20 +256,20 @@ void ScrollBar::paint_event(PaintEvent& event) } if (has_scrubber()) - Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber || m_scrubber_in_use); + Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber || m_pressed_component == Component::Scrubber); } void ScrollBar::on_automatic_scrolling_timer_fired() { - if (m_automatic_scrolling_kind == AutomaticScrollingKind::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) { + if (m_pressed_component == Component::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) { set_value(value() - m_step); return; } - if (m_automatic_scrolling_kind == AutomaticScrollingKind::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) { + if (m_pressed_component == Component::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) { set_value(value() + m_step); return; } - if (m_automatic_scrolling_kind == AutomaticScrollingKind::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) { + if (m_pressed_component == Component::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) { scroll_by_page(m_last_mouse_position); return; } @@ -283,27 +283,25 @@ void ScrollBar::mousedown_event(MouseEvent& event) return; m_last_mouse_position = event.position(); - Component clicked_component = component_at_position(m_last_mouse_position); + m_pressed_component = component_at_position(m_last_mouse_position); - if (clicked_component == Component::DecrementButton) { - set_automatic_scrolling_active(true, AutomaticScrollingKind::DecrementButton); + if (m_pressed_component == Component::DecrementButton) { + set_automatic_scrolling_active(true, Component::DecrementButton); update(); return; } - if (clicked_component == Component::IncrementButton) { - set_automatic_scrolling_active(true, AutomaticScrollingKind::IncrementButton); + if (m_pressed_component == Component::IncrementButton) { + set_automatic_scrolling_active(true, Component::IncrementButton); update(); return; } if (event.shift()) { scroll_to_position(event.position()); - clicked_component = component_at_position(event.position()); - ASSERT(clicked_component == Component::Scrubber); + m_pressed_component = component_at_position(event.position()); + ASSERT(m_pressed_component == Component::Scrubber); } - if (clicked_component == Component::Scrubber) { - m_scrubber_in_use = true; - m_scrubbing = true; + if (m_pressed_component == Component::Scrubber) { m_scrub_start_value = value(); m_scrub_origin = event.position(); update(); @@ -311,8 +309,8 @@ void ScrollBar::mousedown_event(MouseEvent& event) } ASSERT(!event.shift()); - ASSERT(clicked_component == Component::Gutter); - set_automatic_scrolling_active(true, AutomaticScrollingKind::Gutter); + ASSERT(m_pressed_component == Component::Gutter); + set_automatic_scrolling_active(true, Component::Gutter); update(); } @@ -320,9 +318,7 @@ void ScrollBar::mouseup_event(MouseEvent& event) { if (event.button() != MouseButton::Left) return; - m_scrubber_in_use = false; - set_automatic_scrolling_active(false, AutomaticScrollingKind::None); - m_scrubbing = false; + set_automatic_scrolling_active(false, Component::None); update(); } @@ -334,10 +330,10 @@ void ScrollBar::mousewheel_event(MouseEvent& event) Widget::mousewheel_event(event); } -void ScrollBar::set_automatic_scrolling_active(bool active, AutomaticScrollingKind kind) +void ScrollBar::set_automatic_scrolling_active(bool active, Component pressed_component) { - m_automatic_scrolling_kind = kind; - if (m_automatic_scrolling_kind == AutomaticScrollingKind::Gutter) + m_pressed_component = pressed_component; + if (m_pressed_component == Component::Gutter) m_automatic_scrolling_timer->set_interval(200); else m_automatic_scrolling_timer->set_interval(100); @@ -383,7 +379,7 @@ ScrollBar::Component ScrollBar::component_at_position(const Gfx::IntPoint& posit return Component::IncrementButton; if (rect().contains(position)) return Component::Gutter; - return Component::Invalid; + return Component::None; } void ScrollBar::mousemove_event(MouseEvent& event) @@ -395,7 +391,7 @@ void ScrollBar::mousemove_event(MouseEvent& event) if (old_hovered_component != m_hovered_component) { update(); } - if (!m_scrubbing) + if (m_pressed_component != Component::Scrubber) return; float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x()); float scrubbable_range = scrubbable_range_in_pixels(); @@ -406,8 +402,8 @@ void ScrollBar::mousemove_event(MouseEvent& event) void ScrollBar::leave_event(Core::Event&) { - if (m_hovered_component != Component::Invalid) { - m_hovered_component = Component::Invalid; + if (m_hovered_component != Component::None) { + m_hovered_component = Component::None; update(); } } @@ -416,7 +412,7 @@ void ScrollBar::change_event(Event& event) { if (event.type() == Event::Type::EnabledChange) { if (!is_enabled()) - m_scrubbing = false; + set_automatic_scrolling_active(false, Component::None); } return Widget::change_event(event); } diff --git a/Libraries/LibGUI/ScrollBar.h b/Libraries/LibGUI/ScrollBar.h index cace204c0f..bbde76aab6 100644 --- a/Libraries/LibGUI/ScrollBar.h +++ b/Libraries/LibGUI/ScrollBar.h @@ -60,7 +60,7 @@ public: Function<void(int)> on_change; enum Component { - Invalid, + None, DecrementButton, IncrementButton, Gutter, @@ -78,13 +78,6 @@ private: virtual void leave_event(Core::Event&) override; virtual void change_event(Event&) override; - enum class AutomaticScrollingKind { - None = 0, - DecrementButton, - IncrementButton, - Gutter, - }; - int default_button_size() const { return 16; } int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); } int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); } @@ -98,7 +91,7 @@ private: int visible_scrubber_size() const; int scrubbable_range_in_pixels() const; void on_automatic_scrolling_timer_fired(); - void set_automatic_scrolling_active(bool, AutomaticScrollingKind); + void set_automatic_scrolling_active(bool, Component); void scroll_to_position(const Gfx::IntPoint&); void scroll_by_page(const Gfx::IntPoint&); @@ -112,16 +105,14 @@ private: int m_step { 1 }; int m_big_step { 5 }; - bool m_scrubbing { false }; int m_scrub_start_value { 0 }; Gfx::IntPoint m_scrub_origin; Gfx::Orientation m_orientation { Gfx::Orientation::Vertical }; - Component m_hovered_component { Component::Invalid }; + Component m_hovered_component { Component::None }; + Component m_pressed_component { Component::None }; Gfx::IntPoint m_last_mouse_position; - bool m_scrubber_in_use { false }; - AutomaticScrollingKind m_automatic_scrolling_kind { AutomaticScrollingKind::None }; RefPtr<Core::Timer> m_automatic_scrolling_timer; }; |