1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 23:07:34 +00:00

GScrollBar: Keep scrolling while pushing down increment/decrement button.

This commit is contained in:
Andreas Kling 2019-06-07 10:43:10 +02:00
parent 0ece5fee14
commit 164742f316
2 changed files with 53 additions and 3 deletions

View file

@ -78,6 +78,11 @@ GScrollBar::GScrollBar(Orientation orientation, GWidget* parent)
} else { } else {
set_preferred_size({ 0, 15 }); set_preferred_size({ 0, 15 });
} }
m_automatic_scrolling_timer.set_interval(100);
m_automatic_scrolling_timer.on_timeout = [this] {
on_automatic_scrolling_timer_fired();
};
} }
GScrollBar::~GScrollBar() GScrollBar::~GScrollBar()
@ -207,16 +212,30 @@ void GScrollBar::paint_event(GPaintEvent& event)
StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber); StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber);
} }
void GScrollBar::on_automatic_scrolling_timer_fired()
{
if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement) {
set_value(value() - m_step);
return;
}
if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment) {
set_value(value() + m_step);
return;
}
}
void GScrollBar::mousedown_event(GMouseEvent& event) void GScrollBar::mousedown_event(GMouseEvent& event)
{ {
if (event.button() != GMouseButton::Left) if (event.button() != GMouseButton::Left)
return; return;
if (up_button_rect().contains(event.position())) { if (up_button_rect().contains(event.position())) {
set_value(value() - m_step); m_automatic_scrolling_direction = AutomaticScrollingDirection::Decrement;
set_automatic_scrolling_active(true);
return; return;
} }
if (down_button_rect().contains(event.position())) { if (down_button_rect().contains(event.position())) {
set_value(value() + m_step); m_automatic_scrolling_direction = AutomaticScrollingDirection::Increment;
set_automatic_scrolling_active(true);
return; return;
} }
#ifdef GUTTER_DOES_PAGEUP_PAGEDOWN #ifdef GUTTER_DOES_PAGEUP_PAGEDOWN
@ -263,12 +282,24 @@ void GScrollBar::mouseup_event(GMouseEvent& event)
{ {
if (event.button() != GMouseButton::Left) if (event.button() != GMouseButton::Left)
return; return;
m_automatic_scrolling_direction = AutomaticScrollingDirection::None;
set_automatic_scrolling_active(false);
if (!m_scrubbing) if (!m_scrubbing)
return; return;
m_scrubbing = false; m_scrubbing = false;
update(); update();
} }
void GScrollBar::set_automatic_scrolling_active(bool active)
{
if (active) {
on_automatic_scrolling_timer_fired();
m_automatic_scrolling_timer.start();
} else {
m_automatic_scrolling_timer.stop();
}
}
void GScrollBar::mousemove_event(GMouseEvent& event) void GScrollBar::mousemove_event(GMouseEvent& event)
{ {
auto old_hovered_component = m_hovered_component; auto old_hovered_component = m_hovered_component;
@ -282,8 +313,14 @@ void GScrollBar::mousemove_event(GMouseEvent& event)
m_hovered_component = Component::Gutter; m_hovered_component = Component::Gutter;
else else
m_hovered_component = Component::Invalid; m_hovered_component = Component::Invalid;
if (old_hovered_component != m_hovered_component) if (old_hovered_component != m_hovered_component) {
update(); update();
if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Decrement)
set_automatic_scrolling_active(m_hovered_component == Component::DecrementButton);
else if (m_automatic_scrolling_direction == AutomaticScrollingDirection::Increment)
set_automatic_scrolling_active(m_hovered_component == Component::IncrementButton);
}
if (!m_scrubbing) if (!m_scrubbing)
return; return;
float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x()); float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x());

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <AK/Function.h> #include <AK/Function.h>
#include <LibCore/CTimer.h>
#include <LibGUI/GWidget.h> #include <LibGUI/GWidget.h>
class GScrollBar final : public GWidget { class GScrollBar final : public GWidget {
@ -55,6 +56,8 @@ private:
Rect scrubber_rect() const; Rect scrubber_rect() const;
int scrubber_size() const; int scrubber_size() const;
int scrubbable_range_in_pixels() const; int scrubbable_range_in_pixels() const;
void on_automatic_scrolling_timer_fired();
void set_automatic_scrolling_active(bool);
int m_min { 0 }; int m_min { 0 };
int m_max { 0 }; int m_max { 0 };
@ -68,4 +71,14 @@ private:
Orientation m_orientation { Orientation::Vertical }; Orientation m_orientation { Orientation::Vertical };
Component m_hovered_component { Component::Invalid }; Component m_hovered_component { Component::Invalid };
enum class AutomaticScrollingDirection
{
None = 0,
Decrement,
Increment,
};
AutomaticScrollingDirection m_automatic_scrolling_direction { AutomaticScrollingDirection::None };
CTimer m_automatic_scrolling_timer;
}; };