Serenity Operating System
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
8#pragma once
9
10#include <LibGUI/AbstractSlider.h>
11
12namespace GUI {
13
14class Slider : public AbstractSlider {
15 C_OBJECT(Slider);
16
17public:
18 enum class KnobSizeMode {
19 Fixed,
20 Proportional,
21 };
22
23 virtual ~Slider() override = default;
24
25 void set_knob_size_mode(KnobSizeMode mode) { m_knob_size_mode = mode; }
26 KnobSizeMode knob_size_mode() const { return m_knob_size_mode; }
27
28 int track_size() const { return 2; }
29 int track_margin() const { return 10; }
30 int knob_fixed_primary_size() const { return 8; }
31 int knob_secondary_size() const { return 20; }
32
33 bool knob_dragging() const { return m_dragging; }
34 Gfx::IntRect knob_rect() const;
35
36 Gfx::IntRect inner_rect() const
37 {
38 if (orientation() == Orientation::Horizontal)
39 return rect().shrunken(track_margin() * 2, 0);
40 return rect().shrunken(0, track_margin() * 2);
41 }
42
43 Function<void()> on_drag_start;
44 Function<void()> on_drag_end;
45
46protected:
47 explicit Slider(Orientation = Orientation::Vertical);
48
49 virtual Optional<UISize> calculated_min_size() const override;
50 virtual Optional<UISize> calculated_preferred_size() const override;
51
52 virtual void paint_event(PaintEvent&) override;
53 void start_drag(Gfx::IntPoint);
54 virtual void mousedown_event(MouseEvent&) override;
55 virtual void mousemove_event(MouseEvent&) override;
56 void end_drag();
57 virtual void mouseup_event(MouseEvent&) override;
58 virtual void mousewheel_event(MouseEvent&) override;
59 virtual void leave_event(Core::Event&) override;
60 virtual void change_event(Event&) override;
61
62private:
63 void set_knob_hovered(bool);
64
65 bool m_knob_hovered { false };
66 bool m_dragging { false };
67 int m_drag_origin_value { 0 };
68 Gfx::IntPoint m_drag_origin;
69 KnobSizeMode m_knob_size_mode { KnobSizeMode::Fixed };
70};
71
72class VerticalSlider final : public Slider {
73 C_OBJECT(VerticalSlider);
74
75public:
76 virtual ~VerticalSlider() override = default;
77
78private:
79 VerticalSlider()
80 : Slider(Orientation::Vertical)
81 {
82 }
83};
84
85class HorizontalSlider final : public Slider {
86 C_OBJECT(HorizontalSlider);
87
88public:
89 virtual ~HorizontalSlider() override = default;
90
91private:
92 HorizontalSlider()
93 : Slider(Orientation::Horizontal)
94 {
95 }
96};
97
98}