HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
momentary_button_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-2022.
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
9#pragma once
10
12
13namespace hi { inline namespace v1 {
14
18template<fixed_string Name = "">
19class momentary_button_widget final : public abstract_button_widget<Name / "momentary-button"> {
20public:
21 using super = abstract_button_widget<Name / "momentary-button">;
22 using delegate_type = typename super::delegate_type;
23 constexpr static auto prefix = super::prefix;
24
28 button_widget_attribute auto&&...attributes) noexcept :
30 {
31 this->alignment = alignment::middle_center();
32 this->set_attributes<0>(hi_forward(attributes)...);
33 }
34
35 momentary_button_widget(widget *parent, button_widget_attribute auto&&...attributes) noexcept :
36 momentary_button_widget(parent, std::make_shared<delegate_type>(), hi_forward(attributes)...)
37 {
38 }
39
41 [[nodiscard]] box_constraints update_constraints() noexcept override
42 {
43 _label_constraints = super::update_constraints();
44
45 // On left side a check mark, on right side short-cut. Around the label extra margin.
46 hilet extra_size = theme<prefix>{}.spacing_horizontal(this) * 2;
47
48 auto constraints = _label_constraints + extra_size;
49 constraints.margins = theme<prefix>.margin(this);
50 return constraints;
51 }
52
53 void set_layout(widget_layout const& context) noexcept override
54 {
55 if (compare_store(this->layout, context)) {
56 hilet inner_margin = theme<prefix>.horizontal_spacing(this);
57 hilet label_rectangle = aarectanglei{inner_margin, 0, context.width() - inner_margin * 2, context.height()};
58 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
59 box_shape{_label_constraints, label_rectangle, theme<prefix>.cap_height(this)};
60 }
61 super::set_layout(context);
62 }
63
64 void draw(widget_draw_context const& context) noexcept override
65 {
66 if (*this->mode > widget_mode::invisible and overlaps(context, this->layout)) {
67 draw_label_button(context);
68 this->draw_button(context);
69 }
70 }
72private:
73 box_constraints _label_constraints;
74
75 void draw_label_button(widget_draw_context const& context) noexcept
76 {
77 // Move the border of the button in the middle of a pixel.
78 context.draw_box(
79 this->layout,
80 this->layout.rectangle(),
81 theme<prefix>.background_color(this),
82 theme<prefix>.border_color(this),
83 theme<prefix>.border_width(this),
85 theme<prefix>.border_radius(this));
86 }
87};
88
89}} // namespace hi::v1
Defines abstract_button_widget.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
@ inside
The border is drawn inside the edge of a quad.
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
Definition widget.hpp:26
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:53
Draw context for drawing using the HikoGUI shaders.
Definition widget_draw_context.hpp:204
The layout of a widget.
Definition widget_layout.hpp:37
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
Base class for implementing button widgets.
Definition abstract_button_widget.hpp:33
observer< hi::alignment > alignment
The alignment of the button and on/off/other label.
Definition abstract_button_widget.hpp:58
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:42
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:23
A momentary button widget.
Definition momentary_button_widget.hpp:19
T move(T... args)