Skip to content

Commit 23e2944

Browse files
nicoawesomekling
authored andcommitted
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.
1 parent 9ea6ef4 commit 23e2944

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

Libraries/LibGUI/ScrollBar.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ void ScrollBar::paint_event(PaintEvent& event)
237237

238238
painter.fill_rect_with_dither_pattern(rect(), palette().button().lightened(1.3f), palette().button());
239239

240-
bool decrement_pressed = m_automatic_scrolling_kind == AutomaticScrollingKind::DecrementButton;
241-
bool increment_pressed = m_automatic_scrolling_kind == AutomaticScrollingKind::IncrementButton;
240+
bool decrement_pressed = m_pressed_component == Component::DecrementButton;
241+
bool increment_pressed = m_pressed_component == Component::IncrementButton;
242242

243243
Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, decrement_pressed, m_hovered_component == Component::DecrementButton);
244244
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)
256256
}
257257

258258
if (has_scrubber())
259-
Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber || m_scrubber_in_use);
259+
Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_hovered_component == Component::Scrubber || m_pressed_component == Component::Scrubber);
260260
}
261261

262262
void ScrollBar::on_automatic_scrolling_timer_fired()
263263
{
264-
if (m_automatic_scrolling_kind == AutomaticScrollingKind::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) {
264+
if (m_pressed_component == Component::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) {
265265
set_value(value() - m_step);
266266
return;
267267
}
268-
if (m_automatic_scrolling_kind == AutomaticScrollingKind::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) {
268+
if (m_pressed_component == Component::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) {
269269
set_value(value() + m_step);
270270
return;
271271
}
272-
if (m_automatic_scrolling_kind == AutomaticScrollingKind::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) {
272+
if (m_pressed_component == Component::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) {
273273
scroll_by_page(m_last_mouse_position);
274274
return;
275275
}
@@ -283,46 +283,42 @@ void ScrollBar::mousedown_event(MouseEvent& event)
283283
return;
284284

285285
m_last_mouse_position = event.position();
286-
Component clicked_component = component_at_position(m_last_mouse_position);
286+
m_pressed_component = component_at_position(m_last_mouse_position);
287287

288-
if (clicked_component == Component::DecrementButton) {
289-
set_automatic_scrolling_active(true, AutomaticScrollingKind::DecrementButton);
288+
if (m_pressed_component == Component::DecrementButton) {
289+
set_automatic_scrolling_active(true, Component::DecrementButton);
290290
update();
291291
return;
292292
}
293-
if (clicked_component == Component::IncrementButton) {
294-
set_automatic_scrolling_active(true, AutomaticScrollingKind::IncrementButton);
293+
if (m_pressed_component == Component::IncrementButton) {
294+
set_automatic_scrolling_active(true, Component::IncrementButton);
295295
update();
296296
return;
297297
}
298298

299299
if (event.shift()) {
300300
scroll_to_position(event.position());
301-
clicked_component = component_at_position(event.position());
302-
ASSERT(clicked_component == Component::Scrubber);
301+
m_pressed_component = component_at_position(event.position());
302+
ASSERT(m_pressed_component == Component::Scrubber);
303303
}
304-
if (clicked_component == Component::Scrubber) {
305-
m_scrubber_in_use = true;
306-
m_scrubbing = true;
304+
if (m_pressed_component == Component::Scrubber) {
307305
m_scrub_start_value = value();
308306
m_scrub_origin = event.position();
309307
update();
310308
return;
311309
}
312310
ASSERT(!event.shift());
313311

314-
ASSERT(clicked_component == Component::Gutter);
315-
set_automatic_scrolling_active(true, AutomaticScrollingKind::Gutter);
312+
ASSERT(m_pressed_component == Component::Gutter);
313+
set_automatic_scrolling_active(true, Component::Gutter);
316314
update();
317315
}
318316

319317
void ScrollBar::mouseup_event(MouseEvent& event)
320318
{
321319
if (event.button() != MouseButton::Left)
322320
return;
323-
m_scrubber_in_use = false;
324-
set_automatic_scrolling_active(false, AutomaticScrollingKind::None);
325-
m_scrubbing = false;
321+
set_automatic_scrolling_active(false, Component::None);
326322
update();
327323
}
328324

@@ -334,10 +330,10 @@ void ScrollBar::mousewheel_event(MouseEvent& event)
334330
Widget::mousewheel_event(event);
335331
}
336332

337-
void ScrollBar::set_automatic_scrolling_active(bool active, AutomaticScrollingKind kind)
333+
void ScrollBar::set_automatic_scrolling_active(bool active, Component pressed_component)
338334
{
339-
m_automatic_scrolling_kind = kind;
340-
if (m_automatic_scrolling_kind == AutomaticScrollingKind::Gutter)
335+
m_pressed_component = pressed_component;
336+
if (m_pressed_component == Component::Gutter)
341337
m_automatic_scrolling_timer->set_interval(200);
342338
else
343339
m_automatic_scrolling_timer->set_interval(100);
@@ -383,7 +379,7 @@ ScrollBar::Component ScrollBar::component_at_position(const Gfx::IntPoint& posit
383379
return Component::IncrementButton;
384380
if (rect().contains(position))
385381
return Component::Gutter;
386-
return Component::Invalid;
382+
return Component::None;
387383
}
388384

389385
void ScrollBar::mousemove_event(MouseEvent& event)
@@ -395,7 +391,7 @@ void ScrollBar::mousemove_event(MouseEvent& event)
395391
if (old_hovered_component != m_hovered_component) {
396392
update();
397393
}
398-
if (!m_scrubbing)
394+
if (m_pressed_component != Component::Scrubber)
399395
return;
400396
float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x());
401397
float scrubbable_range = scrubbable_range_in_pixels();
@@ -406,8 +402,8 @@ void ScrollBar::mousemove_event(MouseEvent& event)
406402

407403
void ScrollBar::leave_event(Core::Event&)
408404
{
409-
if (m_hovered_component != Component::Invalid) {
410-
m_hovered_component = Component::Invalid;
405+
if (m_hovered_component != Component::None) {
406+
m_hovered_component = Component::None;
411407
update();
412408
}
413409
}
@@ -416,7 +412,7 @@ void ScrollBar::change_event(Event& event)
416412
{
417413
if (event.type() == Event::Type::EnabledChange) {
418414
if (!is_enabled())
419-
m_scrubbing = false;
415+
set_automatic_scrolling_active(false, Component::None);
420416
}
421417
return Widget::change_event(event);
422418
}

Libraries/LibGUI/ScrollBar.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ScrollBar final : public Widget {
6060
Function<void(int)> on_change;
6161

6262
enum Component {
63-
Invalid,
63+
None,
6464
DecrementButton,
6565
IncrementButton,
6666
Gutter,
@@ -78,13 +78,6 @@ class ScrollBar final : public Widget {
7878
virtual void leave_event(Core::Event&) override;
7979
virtual void change_event(Event&) override;
8080

81-
enum class AutomaticScrollingKind {
82-
None = 0,
83-
DecrementButton,
84-
IncrementButton,
85-
Gutter,
86-
};
87-
8881
int default_button_size() const { return 16; }
8982
int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
9083
int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
@@ -98,7 +91,7 @@ class ScrollBar final : public Widget {
9891
int visible_scrubber_size() const;
9992
int scrubbable_range_in_pixels() const;
10093
void on_automatic_scrolling_timer_fired();
101-
void set_automatic_scrolling_active(bool, AutomaticScrollingKind);
94+
void set_automatic_scrolling_active(bool, Component);
10295

10396
void scroll_to_position(const Gfx::IntPoint&);
10497
void scroll_by_page(const Gfx::IntPoint&);
@@ -112,16 +105,14 @@ class ScrollBar final : public Widget {
112105
int m_step { 1 };
113106
int m_big_step { 5 };
114107

115-
bool m_scrubbing { false };
116108
int m_scrub_start_value { 0 };
117109
Gfx::IntPoint m_scrub_origin;
118110

119111
Gfx::Orientation m_orientation { Gfx::Orientation::Vertical };
120-
Component m_hovered_component { Component::Invalid };
112+
Component m_hovered_component { Component::None };
113+
Component m_pressed_component { Component::None };
121114
Gfx::IntPoint m_last_mouse_position;
122-
bool m_scrubber_in_use { false };
123115

124-
AutomaticScrollingKind m_automatic_scrolling_kind { AutomaticScrollingKind::None };
125116
RefPtr<Core::Timer> m_automatic_scrolling_timer;
126117
};
127118

0 commit comments

Comments
 (0)