Scrollbar.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <LibGUI/AbstractSlider.h>
  9. namespace GUI {
  10. class Scrollbar : public AbstractSlider {
  11. C_OBJECT(Scrollbar);
  12. public:
  13. virtual ~Scrollbar() override;
  14. bool is_scrollable() const { return max() != min(); }
  15. bool has_scrubber() const;
  16. enum Component {
  17. None,
  18. DecrementButton,
  19. IncrementButton,
  20. Gutter,
  21. Scrubber,
  22. };
  23. protected:
  24. explicit Scrollbar(Gfx::Orientation = Gfx::Orientation::Vertical);
  25. virtual void paint_event(PaintEvent&) override;
  26. virtual void mousedown_event(MouseEvent&) override;
  27. virtual void mouseup_event(MouseEvent&) override;
  28. virtual void mousemove_event(MouseEvent&) override;
  29. virtual void mousewheel_event(MouseEvent&) override;
  30. virtual void leave_event(Core::Event&) override;
  31. virtual void change_event(Event&) override;
  32. private:
  33. enum class GutterClickState {
  34. NotPressed,
  35. BeforeScrubber,
  36. AfterScrubber,
  37. } gutter_click_state
  38. = GutterClickState::NotPressed;
  39. int default_button_size() const { return 16; }
  40. int button_size() const { return length(orientation()) <= (default_button_size() * 2) ? length(orientation()) / 2 : default_button_size(); }
  41. int button_width() const { return orientation() == Orientation::Vertical ? width() : button_size(); }
  42. int button_height() const { return orientation() == Orientation::Horizontal ? height() : button_size(); }
  43. Gfx::IntRect decrement_button_rect() const;
  44. Gfx::IntRect increment_button_rect() const;
  45. Gfx::IntRect scrubber_rect() const;
  46. float unclamped_scrubber_size() const;
  47. int visible_scrubber_size() const;
  48. int scrubbable_range_in_pixels() const;
  49. void on_automatic_scrolling_timer_fired();
  50. void set_automatic_scrolling_active(bool, Component);
  51. void scroll_to_position(const Gfx::IntPoint&);
  52. void scroll_by_page(const Gfx::IntPoint&);
  53. Component component_at_position(const Gfx::IntPoint&);
  54. int m_scrub_start_value { 0 };
  55. Gfx::IntPoint m_scrub_origin;
  56. Component m_hovered_component { Component::None };
  57. Component m_pressed_component { Component::None };
  58. Gfx::IntPoint m_last_mouse_position;
  59. RefPtr<Core::Timer> m_automatic_scrolling_timer;
  60. };
  61. }