Scrollbar.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <LibCore/Timer.h>
  10. #include <LibGUI/AbstractSlider.h>
  11. namespace GUI {
  12. class Scrollbar : public AbstractSlider {
  13. C_OBJECT(Scrollbar);
  14. public:
  15. virtual ~Scrollbar() override = default;
  16. bool is_scrollable() const { return max() != min(); }
  17. bool has_scrubber() const;
  18. enum class Animation {
  19. SmoothScroll,
  20. CoarseScroll
  21. };
  22. void set_scroll_animation(Animation scroll_animation);
  23. virtual void set_value(int, AllowCallback = AllowCallback::Yes, DoClamp = DoClamp::Yes) override;
  24. void set_target_value(int);
  25. virtual void increase_slider_by(int delta) override { set_target_value(m_target_value + delta); }
  26. virtual void decrease_slider_by(int delta) override { set_target_value(m_target_value - delta); }
  27. virtual void increase_slider_by_page_steps(int page_steps) override { set_target_value(m_target_value + page_step() * page_steps); }
  28. virtual void decrease_slider_by_page_steps(int page_steps) override { set_target_value(m_target_value - page_step() * page_steps); }
  29. virtual void increase_slider_by_steps(int steps) override { set_target_value(m_target_value + step() * steps); }
  30. virtual void decrease_slider_by_steps(int steps) override { set_target_value(m_target_value - step() * steps); }
  31. enum Component {
  32. None,
  33. DecrementButton,
  34. IncrementButton,
  35. Gutter,
  36. Scrubber,
  37. };
  38. protected:
  39. explicit Scrollbar(Gfx::Orientation = Gfx::Orientation::Vertical);
  40. virtual void paint_event(PaintEvent&) override;
  41. virtual void mousedown_event(MouseEvent&) override;
  42. virtual void mouseup_event(MouseEvent&) override;
  43. virtual void mousemove_event(MouseEvent&) override;
  44. virtual void mousewheel_event(MouseEvent&) override;
  45. virtual void leave_event(Core::Event&) override;
  46. virtual void change_event(Event&) override;
  47. private:
  48. enum class GutterClickState {
  49. NotPressed,
  50. BeforeScrubber,
  51. AfterScrubber,
  52. } m_gutter_click_state { GutterClickState::NotPressed };
  53. int default_button_size() const { return 16; }
  54. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  55. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  56. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  57. Gfx::IntRect decrement_button_rect() const;
  58. Gfx::IntRect increment_button_rect() const;
  59. Gfx::IntRect scrubber_rect() const;
  60. float unclamped_scrubber_size() const;
  61. int visible_scrubber_size() const;
  62. int scrubbable_range_in_pixels() const;
  63. void on_automatic_scrolling_timer_fired();
  64. void set_automatic_scrolling_active(bool, Component);
  65. void scroll_to_position(Gfx::IntPoint const&);
  66. void scroll_by_page(Gfx::IntPoint const&);
  67. Component component_at_position(Gfx::IntPoint const&);
  68. void update_animated_scroll();
  69. Animation m_scroll_animation { Animation::SmoothScroll };
  70. int m_target_value { 0 };
  71. int m_start_value { 0 };
  72. double m_animation_time_elapsed { 0 };
  73. int m_scrub_start_value { 0 };
  74. Gfx::IntPoint m_scrub_origin;
  75. Component m_hovered_component { Component::None };
  76. Component m_pressed_component { Component::None };
  77. Gfx::IntPoint m_last_mouse_position;
  78. RefPtr<Core::Timer> m_automatic_scrolling_timer;
  79. RefPtr<Core::Timer> m_animated_scrolling_timer;
  80. };
  81. }