2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-02-09 11:19:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2022-02-26 10:50:04 -07:00
|
|
|
#include <LibCore/Timer.h>
|
2020-12-30 15:20:02 +01:00
|
|
|
#include <LibGUI/AbstractSlider.h>
|
2019-02-09 11:19:38 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
class Scrollbar : public AbstractSlider {
|
|
|
|
|
C_OBJECT(Scrollbar);
|
2020-12-30 15:20:02 +01:00
|
|
|
|
2019-02-09 11:19:38 +01:00
|
|
|
public:
|
2022-02-26 10:50:04 -07:00
|
|
|
virtual ~Scrollbar() override = default;
|
2019-02-09 11:19:38 +01:00
|
|
|
|
2019-08-20 20:10:02 +02:00
|
|
|
bool is_scrollable() const { return max() != min(); }
|
|
|
|
|
|
2019-02-10 07:11:01 +01:00
|
|
|
bool has_scrubber() const;
|
2019-02-09 11:19:38 +01:00
|
|
|
|
2022-03-08 21:50:42 +00:00
|
|
|
enum class Animation {
|
|
|
|
|
SmoothScroll,
|
|
|
|
|
CoarseScroll
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void set_scroll_animation(Animation scroll_animation);
|
|
|
|
|
|
2022-03-29 16:01:59 +03:00
|
|
|
virtual void set_value(int, AllowCallback = AllowCallback::Yes, DoClamp = DoClamp::Yes) override;
|
2022-03-07 14:19:35 -05:00
|
|
|
void set_target_value(int);
|
|
|
|
|
|
|
|
|
|
virtual void increase_slider_by(int delta) override { set_target_value(m_target_value + delta); }
|
|
|
|
|
virtual void decrease_slider_by(int delta) override { set_target_value(m_target_value - delta); }
|
|
|
|
|
virtual void increase_slider_by_page_steps(int page_steps) override { set_target_value(m_target_value + page_step() * page_steps); }
|
|
|
|
|
virtual void decrease_slider_by_page_steps(int page_steps) override { set_target_value(m_target_value - page_step() * page_steps); }
|
|
|
|
|
virtual void increase_slider_by_steps(int steps) override { set_target_value(m_target_value + step() * steps); }
|
|
|
|
|
virtual void decrease_slider_by_steps(int steps) override { set_target_value(m_target_value - step() * steps); }
|
|
|
|
|
|
2022-01-03 18:31:46 +01:00
|
|
|
virtual Optional<UISize> calculated_min_size() const override;
|
|
|
|
|
virtual Optional<UISize> calculated_preferred_size() const override;
|
|
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Component {
|
2020-08-25 12:54:40 -04:00
|
|
|
None,
|
2019-04-06 13:55:56 +02:00
|
|
|
DecrementButton,
|
|
|
|
|
IncrementButton,
|
|
|
|
|
Gutter,
|
|
|
|
|
Scrubber,
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-20 16:19:52 -07:00
|
|
|
protected:
|
2021-04-13 16:18:20 +02:00
|
|
|
explicit Scrollbar(Gfx::Orientation = Gfx::Orientation::Vertical);
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
virtual void paint_event(PaintEvent&) override;
|
|
|
|
|
virtual void mousedown_event(MouseEvent&) override;
|
|
|
|
|
virtual void mouseup_event(MouseEvent&) override;
|
|
|
|
|
virtual void mousemove_event(MouseEvent&) override;
|
|
|
|
|
virtual void mousewheel_event(MouseEvent&) override;
|
2020-02-02 12:34:39 +01:00
|
|
|
virtual void leave_event(Core::Event&) override;
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual void change_event(Event&) override;
|
2019-02-09 11:19:38 +01:00
|
|
|
|
2021-02-20 16:19:52 -07:00
|
|
|
private:
|
2021-11-02 12:01:18 +01:00
|
|
|
enum class GutterClickState {
|
|
|
|
|
NotPressed,
|
|
|
|
|
BeforeScrubber,
|
|
|
|
|
AfterScrubber,
|
2022-03-08 21:48:19 +00:00
|
|
|
} m_gutter_click_state { GutterClickState::NotPressed };
|
2021-11-02 12:01:18 +01:00
|
|
|
|
2019-07-01 02:46:36 -05:00
|
|
|
int default_button_size() const { return 16; }
|
|
|
|
|
int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
|
2019-04-11 13:16:43 +02:00
|
|
|
int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
|
|
|
|
|
int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect decrement_button_rect() const;
|
|
|
|
|
Gfx::IntRect increment_button_rect() const;
|
|
|
|
|
Gfx::IntRect scrubber_rect() const;
|
2021-09-21 17:51:56 -04:00
|
|
|
float unclamped_scrubber_size() const;
|
2020-08-11 21:32:40 -04:00
|
|
|
int visible_scrubber_size() const;
|
2019-02-10 06:51:01 +01:00
|
|
|
int scrubbable_range_in_pixels() const;
|
2022-12-25 15:41:33 -05:00
|
|
|
void automatic_scrolling_timer_did_fire();
|
|
|
|
|
void set_automatic_scrolling_timer_active(bool, Component);
|
2019-02-09 11:19:38 +01:00
|
|
|
|
2022-12-06 20:27:44 +00:00
|
|
|
void scroll_to_position(Gfx::IntPoint);
|
|
|
|
|
void scroll_by_page(Gfx::IntPoint);
|
2020-08-11 20:56:52 -04:00
|
|
|
|
2022-12-06 20:27:44 +00:00
|
|
|
Component component_at_position(Gfx::IntPoint);
|
2020-08-25 10:31:20 -04:00
|
|
|
|
2022-03-07 14:19:35 -05:00
|
|
|
void update_animated_scroll();
|
|
|
|
|
|
2022-03-08 21:50:42 +00:00
|
|
|
Animation m_scroll_animation { Animation::SmoothScroll };
|
|
|
|
|
|
2022-03-07 14:19:35 -05:00
|
|
|
int m_target_value { 0 };
|
|
|
|
|
int m_start_value { 0 };
|
|
|
|
|
double m_animation_time_elapsed { 0 };
|
|
|
|
|
|
2019-02-10 06:51:01 +01:00
|
|
|
int m_scrub_start_value { 0 };
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntPoint m_scrub_origin;
|
2019-02-10 11:57:19 +01:00
|
|
|
|
2020-08-25 12:54:40 -04:00
|
|
|
Component m_hovered_component { Component::None };
|
|
|
|
|
Component m_pressed_component { Component::None };
|
2020-08-25 11:04:53 -04:00
|
|
|
Gfx::IntPoint m_last_mouse_position;
|
2019-06-07 10:43:10 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
RefPtr<Core::Timer> m_automatic_scrolling_timer;
|
2022-03-07 14:19:35 -05:00
|
|
|
RefPtr<Core::Timer> m_animated_scrolling_timer;
|
2019-02-10 06:51:01 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|