12#include "../GUI/GUI.hpp"
13#include "../geometry/geometry.hpp"
14#include "../observer/observer.hpp"
15#include "../utility/utility.hpp"
16#include "../macros.hpp"
23hi_export_module(hikogui.widgets.scroll_bar_widget);
25hi_export
namespace hi {
48 template<forward_of<observer<
float>> Content, forward_of<observer<
float>> Aperture, forward_of<observer<
float>> Offset>
55 content(std::forward<Content>(content)),
56 aperture(std::forward<Aperture>(aperture)),
57 offset(std::forward<Offset>(offset))
59 _content_cbt = this->content.
subscribe([&](
auto...) {
60 ++global_counter<
"scroll_bar_widget:content:relayout">;
63 _aperture_cbt = this->aperture.
subscribe([&](
auto...) {
64 ++global_counter<
"scroll_bar_widget:aperture:relayout">;
67 _offset_cbt = this->offset.
subscribe([&](
auto...) {
68 ++global_counter<
"scroll_bar_widget:offset:relayout">;
84 if constexpr (
axis == axis::vertical) {
86 extent2{theme().icon_size(), theme().size() * 4},
87 extent2{theme().icon_size(), theme().size() * 4},
91 extent2{theme().size() * 4, theme().icon_size()},
92 extent2{theme().size() * 4, theme().icon_size()},
102 _slider_rectangle = {};
108 if constexpr (
axis == axis::vertical) {
117 return *aperture < *content;
130 hi_axiom(loop::main().on_thread());
133 _slider_rectangle.
contains(position)) {
134 return {
id, _layout.elevation, hitbox_type::scroll_bar};
142 switch (
event.type()) {
143 case gui_event_type::mouse_down:
144 if (
event.mouse().cause.left_button) {
146 _offset_before_drag = *offset;
151 case gui_event_type::mouse_drag:
152 if (
event.mouse().cause.left_button) {
155 auto const slider_movement =
axis == axis::vertical ?
event.drag_delta().y() :
event.drag_delta().x();
176 return theme().color(semantic_color::fill, _layout.layer);
181 if (phase() == widget_phase::hover) {
182 return theme().color(semantic_color::fill, _layout.layer + 2);
184 return theme().color(semantic_color::fill, _layout.layer + 1);
189 aarectangle _slider_rectangle;
191 float _offset_before_drag;
193 callback<
void(
float)> _content_cbt;
194 callback<
void(
float)> _aperture_cbt;
195 callback<
void(
float)> _offset_cbt;
209 hi_axiom(loop::main().on_thread());
215 hi_axiom(loop::main().on_thread());
218 if (*content == 0.0f) {
219 return rail_length();
221 return std::round(*aperture * rail_length() / *content);
232 hi_axiom(loop::main().on_thread());
233 return rail_length() - slider_length();
240 hi_axiom(loop::main().on_thread());
241 return *content - *aperture;
250 hi_axiom(loop::main().on_thread());
262 hi_axiom(loop::main().on_thread());
268 void draw_rails(draw_context
const&
context)
noexcept
270 auto const corner_radii =
275 void draw_slider(draw_context
const&
context)
noexcept
277 auto const corner_radii =
axis == axis::vertical ?
hi::corner_radii{_slider_rectangle.width() / 2.0f} :
280 context.draw_box(
layout(), translate_z(0.1f) * _slider_rectangle, foreground_color(), corner_radii);
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:24
@ window_relayout
Request that widgets get laid out on the next frame.
@ rectangle
The gui_event has rectangle data.
@ partial
A widget is partially enabled.
@ collapse
The widget is collapsed.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
This is a RGBA floating point color.
Definition color_intf.hpp:49
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
constexpr bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarectangle.hpp:268
The 4 radii of the corners of a quad or rectangle.
Definition corner_radii.hpp:26
A high-level geometric extent.
Definition extent2.hpp:32
Draw context for drawing using the HikoGUI shaders.
Definition draw_context_intf.hpp:209
A user interface event.
Definition gui_event.hpp:82
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:30
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:206
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:35
The layout of a widget.
Definition widget_layout.hpp:56
2D constraints.
Definition box_constraints.hpp:25
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
callback< void(value_type)> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback to this observer.
Definition observer_intf.hpp:456
Definition not_null.hpp:22
Scroll bar widget This widget is used in a pair of a vertical and horizontal scrollbar as a child of ...
Definition scroll_bar_widget.hpp:38
hitbox hitbox_test(point2 position) const noexcept override
Find the widget that is under the mouse cursor.
Definition scroll_bar_widget.hpp:128
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition scroll_bar_widget.hpp:120
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition scroll_bar_widget.hpp:140
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition scroll_bar_widget.hpp:97
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition scroll_bar_widget.hpp:75
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition scroll_bar_widget.hpp:169
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
widget(widget_intf const *parent) noexcept
Definition widget.hpp:49
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:124
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:144