HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
scroll_bar_widget.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "widget.hpp"
8#include "../GUI/draw_context.hpp"
9#include "../observable.hpp"
10#include <memory>
11#include <string>
12#include <array>
13#include <optional>
14#include <future>
15
16namespace tt {
17
18template<bool IsVertical>
19class scroll_bar_widget final : public widget {
20public:
21 using super = widget;
22
23 static constexpr bool is_vertical = IsVertical;
24
25 template<typename Content, typename Aperture, typename Offset>
29 Content &&content,
30 Aperture &&aperture,
31 Offset &&offset) noexcept :
33 content(std::forward<Content>(content)),
34 aperture(std::forward<Aperture>(aperture)),
35 offset(std::forward<Offset>(offset))
36 {
37 _content_callback = this->content.subscribe([this](auto...) {
38 this->window.requestLayout = true;
39 });
40 _aperture_callback = this->aperture.subscribe([this](auto...) {
41 this->window.requestLayout = true;
42 });
43 _offset_callback = this->offset.subscribe([this](auto...) {
44 this->window.requestLayout = true;
45 });
46 }
47
49
50 [[nodiscard]] bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
51 {
52 tt_axiom(gui_system_mutex.recurse_lock_count());
53
54 if (super::update_constraints(display_time_point, need_reconstrain)) {
55 ttlet minimum_length = theme::global->width; // even for vertical bars.
56
57 if constexpr (is_vertical) {
58 _preferred_size = interval_vec2{
59 f32x4{theme::global->scroll_bar_thickness, minimum_length},
60 f32x4{theme::global->scroll_bar_thickness, std::numeric_limits<float>::max()}};
61 } else {
62 _preferred_size = interval_vec2{
63 f32x4{minimum_length, theme::global->scroll_bar_thickness},
64 f32x4{std::numeric_limits<float>::max(), theme::global->scroll_bar_thickness}};
65 }
66
67 _preferred_base_line = {};
68 return true;
69 } else {
70 return false;
71 }
72 }
73
74 [[nodiscard]] void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override
75 {
76 tt_axiom(gui_system_mutex.recurse_lock_count());
77
78 need_layout |= std::exchange(_request_relayout, false);
79 if (need_layout) {
80 tt_axiom(*content != 0.0f);
81
82 // Calculate the position of the slider.
83 ttlet slider_offset = *offset * travel_vs_hidden_content_ratio();
84
85 if constexpr (is_vertical) {
86 slider_rectangle = aarect{rectangle().x(), rectangle().y() + slider_offset, rectangle().width(), slider_length()};
87 } else {
88 slider_rectangle =
89 aarect{rectangle().x() + slider_offset, rectangle().y(), slider_length(), rectangle().height()};
90 }
91 }
92
93 super::update_layout(display_time_point, need_layout);
94 }
95
96 void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
97 {
98 tt_axiom(gui_system_mutex.recurse_lock_count());
99
100 if (overlaps(context, this->window_clipping_rectangle()) && visible()) {
101 draw_rails(context);
102 draw_slider(context);
103 }
104 super::draw(std::move(context), display_time_point);
105 }
106
107 hit_box hitbox_test(f32x4 window_position) const noexcept override
108 {
109 ttlet lock = std::scoped_lock(gui_system_mutex);
110 ttlet position = _from_window_transform * window_position;
111
112 if (window_clipping_rectangle().contains(window_position) && slider_rectangle.contains(position) && visible()) {
113 return hit_box{weak_from_this(), _draw_layer};
114 } else {
115 return hit_box{};
116 }
117 }
118
119 [[nodiscard]] bool handle_event(mouse_event const &event) noexcept
120 {
121 ttlet lock = std::scoped_lock(gui_system_mutex);
122 auto handled = super::handle_event(event);
123
124 if (event.cause.leftButton) {
125 handled = true;
126
127 switch (event.type) {
128 using enum mouse_event::Type;
129 case ButtonDown:
130 // Record the original scroll-position before the drag starts.
131 offset_before_drag = *offset;
132 break;
133
134 case Drag: {
135 // The distance the slider has to move relative to the slider position at the
136 // start of the drag.
137 ttlet slider_movement = is_vertical ? event.delta().y() : event.delta().x();
138 ttlet content_movement = slider_movement * hidden_content_vs_travel_ratio();
139 offset = offset_before_drag + content_movement;
140 } break;
141
142 default:;
143 }
144 }
145 return handled;
146 }
147
148 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
149 {
150 return false;
151 }
152
157 [[nodiscard]] bool visible() const noexcept {
158 tt_axiom(gui_system_mutex.recurse_lock_count());
159 return hidden_content() >= 1.0f;
160 }
161
162private:
163 observable<float> offset;
164 observable<float> aperture;
165 observable<float> content;
166
167 typename decltype(offset)::callback_ptr_type _offset_callback;
168 typename decltype(aperture)::callback_ptr_type _aperture_callback;
169 typename decltype(content)::callback_ptr_type _content_callback;
170
171 aarect slider_rectangle;
172
173 float offset_before_drag;
174
175 [[nodiscard]] float rail_length() const noexcept
176 {
177 tt_axiom(gui_system_mutex.recurse_lock_count());
178 return is_vertical ? rectangle().height() : rectangle().width();
179 }
180
181 [[nodiscard]] float slider_length() const noexcept
182 {
183 tt_axiom(gui_system_mutex.recurse_lock_count());
184
185 ttlet content_aperture_ratio = *aperture / *content;
186 return std::max(rail_length() * content_aperture_ratio, theme::global->smallSize * 2.0f);
187 }
188
191 [[nodiscard]] float slider_travel_range() const noexcept
192 {
193 tt_axiom(gui_system_mutex.recurse_lock_count());
194 return rail_length() - slider_length();
195 }
196
199 [[nodiscard]] float hidden_content() const noexcept
200 {
201 tt_axiom(gui_system_mutex.recurse_lock_count());
202 return *content - *aperture;
203 }
204
209 [[nodiscard]] float hidden_content_vs_travel_ratio() const noexcept
210 {
211 tt_axiom(gui_system_mutex.recurse_lock_count());
212
213 ttlet _slider_travel_range = slider_travel_range();
214 return _slider_travel_range != 0.0f ? hidden_content() / _slider_travel_range : 0.0f;
215 }
216
221 [[nodiscard]] float travel_vs_hidden_content_ratio() const noexcept
222 {
223 tt_axiom(gui_system_mutex.recurse_lock_count());
224
225 ttlet _hidden_content = hidden_content();
226 return _hidden_content != 0.0f ? slider_travel_range() / _hidden_content : 0.0f;
227 }
228
229 void draw_rails(draw_context context) noexcept
230 {
231 tt_axiom(gui_system_mutex.recurse_lock_count());
232
233 context.line_color = theme::global->fillColor(_semantic_layer);
234 context.fill_color = theme::global->fillColor(_semantic_layer);
235 if constexpr (is_vertical) {
236 context.corner_shapes = f32x4::broadcast(rectangle().width() * 0.5f);
237 } else {
238 context.corner_shapes = f32x4::broadcast(rectangle().height() * 0.5f);
239 }
240 context.draw_box_with_border_inside(rectangle());
241 }
242
243 void draw_slider(draw_context context) noexcept
244 {
245 tt_axiom(gui_system_mutex.recurse_lock_count());
246
247 context.line_color = theme::global->fillColor(_semantic_layer + 1);
248 context.fill_color = theme::global->fillColor(_semantic_layer + 1);
249 context.transform = translate3{0.0f, 0.0f, 0.1f} * context.transform;
250 if constexpr (is_vertical) {
251 context.corner_shapes = f32x4::broadcast(slider_rectangle.width() * 0.5f);
252 } else {
253 context.corner_shapes = f32x4::broadcast(slider_rectangle.height() * 0.5f);
254 }
255 context.draw_box_with_border_inside(slider_rectangle);
256 }
257};
258
259} // namespace tt
bool contains(numeric_array< T, 4 > const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarect.hpp:300
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
Definition gui_window.hpp:39
std::atomic< bool > requestLayout
When set to true the widgets will be layed out.
Definition gui_window.hpp:56
Definition hit_box.hpp:15
Definition mouse_event.hpp:13
A 2D vector using interval arithmetic.
Definition interval_vec2.hpp:18
Definition observable.hpp:20
int recurse_lock_count() const noexcept
This function should be used in tt_axiom() to check if the lock is held by current thread.
Definition unfair_recursive_mutex.hpp:60
Definition scroll_bar_widget.hpp:19
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition scroll_bar_widget.hpp:148
hit_box hitbox_test(f32x4 window_position) const noexcept override
Find the widget that is under the mouse cursor.
Definition scroll_bar_widget.hpp:107
void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
Draw the widget.
Definition scroll_bar_widget.hpp:96
bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
Definition scroll_bar_widget.hpp:50
void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override
Update the internal layout of the widget.
Definition scroll_bar_widget.hpp:74
bool visible() const noexcept
Is the scrollbar visible.
Definition scroll_bar_widget.hpp:157
bool handle_event(mouse_event const &event) noexcept
Definition scroll_bar_widget.hpp:119
Definition widget.hpp:96
virtual void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.
widget(gui_window &window, std::shared_ptr< abstract_container_widget > parent) noexcept
virtual bool handle_event(command command) noexcept
Handle command.
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:100
virtual void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept
Draw the widget.
Definition widget.hpp:460
virtual bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept
Update the constraints of the widget.
abstract_container_widget const & parent() const noexcept
Get a reference to the parent.
virtual aarect window_clipping_rectangle() const noexcept
Get the clipping-rectangle in window coordinates.
Definition widget.hpp:320
aarect rectangle() const noexcept
Get the rectangle in local coordinates.
Definition widget.hpp:340
T max(T... args)
T move(T... args)