HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
menu_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
11#include "../macros.hpp"
13
14namespace hi { inline namespace v1 {
15
32public:
34 using delegate_type = typename super::delegate_type;
35
54
67 template<
71 menu_button_widget(widget *parent, Value&& value, OnValue&& on_value, Attributes&&...attributes) noexcept
72 requires requires
73 {
74 make_default_radio_button_delegate(hi_forward(value), hi_forward(on_value));
75 } :
77 parent,
78 make_default_radio_button_delegate(hi_forward(value), hi_forward(on_value)),
79 hi_forward(attributes)...)
80 {
81 }
82
84 [[nodiscard]] box_constraints update_constraints() noexcept override
85 {
86 _label_constraints = super::update_constraints();
87
88 // Make room for button and margin.
89 _check_size = {theme().size(), theme().size()};
90 _short_cut_size = {theme().size(), theme().size()};
91
92 // On left side a check mark, on right side short-cut. Around the label extra margin.
93 hilet extra_size = extent2{
94 theme().margin<float>() * 4.0f + _check_size.width() + _short_cut_size.width(), theme().margin<float>() * 2.0f};
95
96 auto constraints = _label_constraints + extra_size;
97 constraints.margins = 0;
98 return constraints;
99 }
100
101 void set_layout(widget_layout const& context) noexcept override
102 {
103 if (compare_store(_layout, context)) {
104 hilet inside_rectangle = context.rectangle() - theme().margin<float>();
105
106 if (os_settings::left_to_right()) {
107 _check_rectangle = align(inside_rectangle, _check_size, alignment::middle_left());
108 _short_cut_rectangle = align(inside_rectangle, _short_cut_size, alignment::middle_right());
109 hilet label_rectangle = aarectangle{
110 point2{_check_rectangle.right() + theme().margin<float>(), 0.0f},
111 point2{_short_cut_rectangle.left() - theme().margin<float>(), context.height()}};
112 _on_label_shape = _off_label_shape = _other_label_shape =
113 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
114
115 } else {
116 _short_cut_rectangle = align(inside_rectangle, _short_cut_size, alignment::middle_left());
117 _check_rectangle = align(inside_rectangle, _check_size, alignment::middle_right());
118 hilet label_rectangle = aarectangle{
119 point2{_short_cut_rectangle.right() + theme().margin<float>(), 0.0f},
120 point2{_check_rectangle.left() - theme().margin<float>(), context.height()}};
121 _on_label_shape = _off_label_shape = _other_label_shape =
122 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
123 }
124
125 _check_glyph = find_glyph(elusive_icon::Ok);
126 hilet check_glyph_bb = _check_glyph.get_metrics().bounding_rectangle * theme().icon_size();
127 _check_glyph_rectangle = align(_check_rectangle, check_glyph_bb, alignment::middle_center());
128 }
129
130 super::set_layout(context);
131 }
132
133 void draw(draw_context const& context) noexcept override
134 {
135 if (*mode > widget_mode::invisible and overlaps(context, layout())) {
136 draw_menu_button(context);
137 draw_check_mark(context);
138 draw_button(context);
139 }
140 }
141
142 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
143 {
144 return *mode >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::menu);
145 }
146
147 bool handle_event(gui_event const& event) noexcept override
148 {
149 using enum gui_event_type;
150
151 switch (event.type()) {
152 case gui_menu_next:
153 if (*mode >= widget_mode::partial and not is_last(keyboard_focus_group::menu)) {
154 process_event(gui_event::window_set_keyboard_target(
155 nullptr, keyboard_focus_group::menu, keyboard_focus_direction::forward));
156 return true;
157 }
158 break;
159
160 case gui_menu_prev:
161 if (*mode >= widget_mode::partial and not is_first(keyboard_focus_group::menu)) {
162 process_event(gui_event::window_set_keyboard_target(
163 nullptr, keyboard_focus_group::menu, keyboard_focus_direction::backward));
164 return true;
165 }
166 break;
167
168 case gui_activate:
169 if (*mode >= widget_mode::partial) {
170 activate();
171 process_event(gui_event::window_set_keyboard_target(
172 nullptr, keyboard_focus_group::normal, keyboard_focus_direction::forward));
173 process_event(gui_event::window_set_keyboard_target(
174 nullptr, keyboard_focus_group::normal, keyboard_focus_direction::backward));
175 return true;
176 }
177 break;
178
179 default:;
180 }
181
182 return super::handle_event(event);
183 }
185private:
186 box_constraints _label_constraints;
187
188 font_book::font_glyph_type _check_glyph;
189 extent2 _check_size;
190 aarectangle _check_rectangle;
191 aarectangle _check_glyph_rectangle;
192 extent2 _short_cut_size;
193 aarectangle _short_cut_rectangle;
194
195 void draw_menu_button(draw_context const& context) noexcept
196 {
197 hilet border_color = *focus ? focus_color() : color::transparent();
198 context.draw_box(
199 layout(), layout().rectangle(), background_color(), border_color, theme().border_width(), border_side::inside);
200 }
201 void draw_check_mark(draw_context const& context) noexcept
202 {
203 auto state_ = state();
204
205 // Checkmark or tristate.
206 if (state_ == hi::button_state::on) {
207 context.draw_glyph(layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
208 }
209 }
210};
211
212}} // namespace hi::v1
Defines abstract_button_widget.
gui_event_type
GUI event type.
Definition gui_event_type.hpp:24
@ rectangle
The gui_event has rectangle data.
std::shared_ptr< button_delegate > make_default_radio_button_delegate(auto &&value, auto &&on_value) noexcept
Make a shared pointer to a radio-button delegate.
Definition button_delegate.hpp:227
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:16
hi_export auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:437
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
@ 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 misc.hpp:56
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Horizontal/Vertical alignment combination.
Definition alignment.hpp:242
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:104
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
2D constraints.
Definition box_constraints.hpp:25
Base class for implementing button widgets.
Definition abstract_button_widget.hpp:33
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:40
button_state state() const noexcept
Get the current state of the button.
Definition abstract_button_widget.hpp:88
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:45
A button that is part of a menu.
Definition menu_button_widget.hpp:31
menu_button_widget(widget *parent, std::shared_ptr< delegate_type > delegate, button_widget_attribute auto &&...attributes) noexcept
Construct a menu button widget.
Definition menu_button_widget.hpp:45
menu_button_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a menu button widget with a default button delegate.
Definition menu_button_widget.hpp:71
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:169
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
Definition widget.hpp:376
bool is_last(keyboard_focus_group group) const noexcept
Is this widget the last widget in the parent container.
Definition widget.hpp:384
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:178
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42
observer< bool > focus
The widget has keyboard focus.
Definition widget.hpp:50
Definition concepts.hpp:54
True if T is a forwarded type of Forward.
Definition concepts.hpp:131
Definition abstract_button_widget.hpp:27
T align(T... args)
T move(T... args)