2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 20:50:04 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-02-09 13:19:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2022-02-26 20:50:04 +03:00
|
|
|
#include <LibCore/Timer.h>
|
2020-12-30 17:20:02 +03:00
|
|
|
#include <LibGUI/AbstractSlider.h>
|
2019-02-09 13:19:38 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
class Scrollbar : public AbstractSlider {
|
|
|
|
C_OBJECT(Scrollbar);
|
2020-12-30 17:20:02 +03:00
|
|
|
|
2019-02-09 13:19:38 +03:00
|
|
|
public:
|
2022-02-26 20:50:04 +03:00
|
|
|
virtual ~Scrollbar() override = default;
|
2019-02-09 13:19:38 +03:00
|
|
|
|
2019-08-20 21:10:02 +03:00
|
|
|
bool is_scrollable() const { return max() != min(); }
|
|
|
|
|
2019-02-10 09:11:01 +03:00
|
|
|
bool has_scrubber() const;
|
2019-02-09 13:19:38 +03:00
|
|
|
|
2022-03-09 00:50:42 +03: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 22:19:35 +03: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 20:31:46 +03:00
|
|
|
virtual Optional<UISize> calculated_min_size() const override;
|
|
|
|
virtual Optional<UISize> calculated_preferred_size() const override;
|
|
|
|
|
2019-06-07 18:13:23 +03:00
|
|
|
enum Component {
|
2020-08-25 19:54:40 +03:00
|
|
|
None,
|
2019-04-06 14:55:56 +03:00
|
|
|
DecrementButton,
|
|
|
|
IncrementButton,
|
|
|
|
Gutter,
|
|
|
|
Scrubber,
|
|
|
|
};
|
|
|
|
|
2021-02-21 02:19:52 +03:00
|
|
|
protected:
|
2021-04-13 17:18:20 +03:00
|
|
|
explicit Scrollbar(Gfx::Orientation = Gfx::Orientation::Vertical);
|
2020-02-02 17:07:41 +03: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 14:34:39 +03:00
|
|
|
virtual void leave_event(Core::Event&) override;
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual void change_event(Event&) override;
|
2019-02-09 13:19:38 +03:00
|
|
|
|
2021-02-21 02:19:52 +03:00
|
|
|
private:
|
2021-11-02 14:01:18 +03:00
|
|
|
enum class GutterClickState {
|
|
|
|
NotPressed,
|
|
|
|
BeforeScrubber,
|
|
|
|
AfterScrubber,
|
2022-03-09 00:48:19 +03:00
|
|
|
} m_gutter_click_state { GutterClickState::NotPressed };
|
2021-11-02 14:01:18 +03:00
|
|
|
|
2019-07-01 10:46:36 +03: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 14:16:43 +03: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 11:57:59 +03:00
|
|
|
Gfx::IntRect decrement_button_rect() const;
|
|
|
|
Gfx::IntRect increment_button_rect() const;
|
|
|
|
Gfx::IntRect scrubber_rect() const;
|
2021-09-22 00:51:56 +03:00
|
|
|
float unclamped_scrubber_size() const;
|
2020-08-12 04:32:40 +03:00
|
|
|
int visible_scrubber_size() const;
|
2019-02-10 08:51:01 +03:00
|
|
|
int scrubbable_range_in_pixels() const;
|
2019-06-07 11:43:10 +03:00
|
|
|
void on_automatic_scrolling_timer_fired();
|
2020-08-25 19:54:40 +03:00
|
|
|
void set_automatic_scrolling_active(bool, Component);
|
2019-02-09 13:19:38 +03:00
|
|
|
|
2022-12-06 23:27:44 +03:00
|
|
|
void scroll_to_position(Gfx::IntPoint);
|
|
|
|
void scroll_by_page(Gfx::IntPoint);
|
2020-08-12 03:56:52 +03:00
|
|
|
|
2022-12-06 23:27:44 +03:00
|
|
|
Component component_at_position(Gfx::IntPoint);
|
2020-08-25 17:31:20 +03:00
|
|
|
|
2022-03-07 22:19:35 +03:00
|
|
|
void update_animated_scroll();
|
|
|
|
|
2022-03-09 00:50:42 +03:00
|
|
|
Animation m_scroll_animation { Animation::SmoothScroll };
|
|
|
|
|
2022-03-07 22:19:35 +03:00
|
|
|
int m_target_value { 0 };
|
|
|
|
int m_start_value { 0 };
|
|
|
|
double m_animation_time_elapsed { 0 };
|
|
|
|
|
2019-02-10 08:51:01 +03:00
|
|
|
int m_scrub_start_value { 0 };
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntPoint m_scrub_origin;
|
2019-02-10 13:57:19 +03:00
|
|
|
|
2020-08-25 19:54:40 +03:00
|
|
|
Component m_hovered_component { Component::None };
|
|
|
|
Component m_pressed_component { Component::None };
|
2020-08-25 18:04:53 +03:00
|
|
|
Gfx::IntPoint m_last_mouse_position;
|
2019-06-07 11:43:10 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
RefPtr<Core::Timer> m_automatic_scrolling_timer;
|
2022-03-07 22:19:35 +03:00
|
|
|
RefPtr<Core::Timer> m_animated_scrolling_timer;
|
2019-02-10 08:51:01 +03:00
|
|
|
};
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|