diff options
author | Nico Weber <thakis@chromium.org> | 2020-08-11 20:56:52 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-12 11:02:30 +0200 |
commit | eab7712ab0c939ed2f63e3e42f1af4bc9782e4cb (patch) | |
tree | 4bdc74c0833601d6cd5ea860a367a510227a6f91 | |
parent | d1571ce00a8e2a5da8771d9897e54c4f19fca54b (diff) | |
download | serenity-eab7712ab0c939ed2f63e3e42f1af4bc9782e4cb.zip |
GUI: Extract a ScrollBar::scroll_to_position method
-rw-r--r-- | Libraries/LibGUI/ScrollBar.cpp | 32 | ||||
-rw-r--r-- | Libraries/LibGUI/ScrollBar.h | 2 |
2 files changed, 21 insertions, 13 deletions
diff --git a/Libraries/LibGUI/ScrollBar.cpp b/Libraries/LibGUI/ScrollBar.cpp index a5fbdd367e..313fd1f588 100644 --- a/Libraries/LibGUI/ScrollBar.cpp +++ b/Libraries/LibGUI/ScrollBar.cpp @@ -294,19 +294,7 @@ void ScrollBar::mousedown_event(MouseEvent& event) return; } - float range_size = m_max - m_min; - float available = scrubbable_range_in_pixels(); - - float x = ::max(0, event.position().x() - button_width() - button_width() / 2); - float y = ::max(0, event.position().y() - button_height() - button_height() / 2); - - float rel_x = x / available; - float rel_y = y / available; - - if (orientation() == Orientation::Vertical) - set_value(m_min + rel_y * range_size); - else - set_value(m_min + rel_x * range_size); + scroll_to_position(event.position()); m_scrubbing = true; m_scrub_start_value = value(); @@ -342,6 +330,24 @@ void ScrollBar::set_automatic_scrolling_active(bool active) } } +void ScrollBar::scroll_to_position(const Gfx::IntPoint& position) +{ + float range_size = m_max - m_min; + float available = scrubbable_range_in_pixels(); + + float x = ::max(0, position.x() - button_width() - button_width() / 2); + float y = ::max(0, position.y() - button_height() - button_height() / 2); + + float rel_x = x / available; + float rel_y = y / available; + + if (orientation() == Orientation::Vertical) + set_value(m_min + rel_y * range_size); + else + set_value(m_min + rel_x * range_size); + +} + void ScrollBar::mousemove_event(MouseEvent& event) { auto old_hovered_component = m_hovered_component; diff --git a/Libraries/LibGUI/ScrollBar.h b/Libraries/LibGUI/ScrollBar.h index e490a2c2f4..0604d719cb 100644 --- a/Libraries/LibGUI/ScrollBar.h +++ b/Libraries/LibGUI/ScrollBar.h @@ -92,6 +92,8 @@ private: void on_automatic_scrolling_timer_fired(); void set_automatic_scrolling_active(bool); + void scroll_to_position(const Gfx::IntPoint&); + int m_min { 0 }; int m_max { 0 }; int m_page { 0 }; |