HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
selection_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-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 "widget.hpp"
12#include "label_widget.hpp"
13#include "overlay_widget.hpp"
14#include "scroll_widget.hpp"
15#include "row_column_widget.hpp"
18#include "../observer.hpp"
19#include <memory>
20#include <string>
21#include <array>
22#include <optional>
23#include <future>
24
25namespace hi { inline namespace v1 {
26
27template<typename Context>
29
44class selection_widget final : public widget {
45public:
46 using super = widget;
47 using delegate_type = selection_delegate;
48
50
53 observer<label> off_label;
54
73 observer<alignment> alignment = hi::alignment::middle_flush();
74
77 observer<semantic_text_style> text_style = semantic_text_style::label;
78
80
87
100 selection_widget_attribute auto&& first_attribute,
101 selection_widget_attribute auto&&...attributes) noexcept :
103 {
104 set_attributes(hi_forward(first_attribute), hi_forward(attributes)...);
105 }
106
119 template<
120 different_from<std::shared_ptr<delegate_type>> Value,
121 forward_of<observer<std::vector<std::pair<observer_decay_t<Value>, label>>>> OptionList,
122 selection_widget_attribute... Attributes>
124 widget *parent,
125 Value&& value,
126 OptionList&& option_list,
127 Attributes&&...attributes) noexcept requires requires
128 {
130 } :
132 parent,
134 hi_forward(attributes)...)
135 {
136 }
137
152 template<
153 different_from<std::shared_ptr<delegate_type>> Value,
154 forward_of<observer<std::vector<std::pair<observer_decay_t<Value>, label>>>> OptionList,
155 forward_of<observer<observer_decay_t<Value>>> OffValue,
156 selection_widget_attribute... Attributes>
158 widget *parent,
159 Value&& value,
160 OptionList&& option_list,
161 OffValue&& off_value,
162 Attributes&&...attributes) noexcept requires requires
163 {
164 make_default_selection_delegate(hi_forward(value), hi_forward(option_list), hi_forward(off_value));
165 } :
167 parent,
168 make_default_selection_delegate(hi_forward(value), hi_forward(option_list), hi_forward(off_value)),
169 hi_forward(attributes)...)
170 {
171 }
172
174 [[nodiscard]] generator<widget const &> children(bool include_invisible) const noexcept override;
175 [[nodiscard]] box_constraints update_constraints() noexcept override;
176 void set_layout(widget_layout const& context) noexcept override;
177 void draw(draw_context const& context) noexcept override;
178 bool handle_event(gui_event const& event) noexcept override;
179 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override;
180 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
181 [[nodiscard]] color focus_color() const noexcept override;
183private:
184 notifier<>::callback_token _delegate_cbt;
185 std::atomic<bool> _notification_from_delegate = true;
186
187 std::unique_ptr<label_widget> _current_label_widget;
188 box_constraints _current_label_constraints;
189 box_shape _current_label_shape;
190
191 std::unique_ptr<label_widget> _off_label_widget;
192 box_constraints _off_label_constraints;
193 box_shape _off_label_shape;
194
195 aarectanglei _left_box_rectangle;
196
197 glyph_ids _chevrons_glyph;
198 aarectanglei _chevrons_rectangle;
199
200 bool _selecting = false;
201 bool _has_options = false;
202
203 std::unique_ptr<overlay_widget> _overlay_widget;
204 box_constraints _overlay_constraints;
205 box_shape _overlay_shape;
206
207 vertical_scroll_widget *_scroll_widget = nullptr;
208 column_widget *_column_widget = nullptr;
209
210 decltype(off_label)::callback_token _off_label_cbt;
211 std::vector<menu_button_widget *> _menu_button_widgets;
212 std::vector<notifier<>::callback_token> _menu_button_tokens;
213
214 void set_attributes() noexcept {}
215 void set_attributes(label_widget_attribute auto&& first, label_widget_attribute auto&&...rest) noexcept
216 {
217 if constexpr (forward_of<decltype(first), observer<hi::label>>) {
218 off_label = hi_forward(first);
219 } else if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
220 alignment = hi_forward(first);
221 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
222 text_style = hi_forward(first);
223 } else {
225 }
226
227 set_attributes(hi_forward(rest)...);
228 }
229
230 [[nodiscard]] menu_button_widget const *get_first_menu_button() const noexcept;
231 [[nodiscard]] menu_button_widget const *get_selected_menu_button() const noexcept;
232 void start_selecting() noexcept;
233 void stop_selecting() noexcept;
234 void repopulate_options() noexcept;
235 void draw_outline(draw_context const& context) noexcept;
236 void draw_left_box(draw_context const& context) noexcept;
237 void draw_chevrons(draw_context const& context) noexcept;
238};
239
240}} // namespace hi::v1
Defines delegate_delegate and some default selection delegates.
Defines scroll_widget.
Defines row_column_widget.
Defines widget.
Defines label_widget.
Defines overlay_widget.
Defines menu_button_widget.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:308
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
std::shared_ptr< selection_delegate > make_default_selection_delegate(auto &&value, auto &&options, auto &&...off_value) noexcept
Create a shared pointer to a default selection delegate.
Definition selection_delegate.hpp:153
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
This is a RGBA floating point color.
Definition color.hpp:42
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:211
A user interface event.
Definition gui_event.hpp:74
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:42
A button that is part of a menu.
Definition menu_button_widget.hpp:30
A GUI widget which may exist anywhere on a window overlaid above any other widget.
Definition overlay_widget.hpp:38
A row/column widget lays out child widgets along a row or column.
Definition row_column_widget.hpp:40
The scroll widget allows a content widget to be shown in less space than is required.
Definition scroll_widget.hpp:46
A delegate that controls the state of a selection_widget.
Definition selection_delegate.hpp:23
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition selection_widget.hpp:44
selection_widget(widget *parent, Value &&value, OptionList &&option_list, OffValue &&off_value, Attributes &&...attributes) noexcept
Construct a selection widget which will monitor an option list and a value.
Definition selection_widget.hpp:157
selection_widget(widget *parent, std::shared_ptr< delegate_type > delegate, selection_widget_attribute auto &&first_attribute, selection_widget_attribute auto &&...attributes) noexcept
Construct a selection widget with a delegate.
Definition selection_widget.hpp:97
selection_widget(widget *parent, std::shared_ptr< delegate_type > delegate) noexcept
Construct a selection widget with a delegate.
observer< label > off_label
The label to show when nothing is selected.
Definition selection_widget.hpp:53
observer< semantic_text_style > text_style
The text style to display the label's text in and color of the label's (non-color) icon.
Definition selection_widget.hpp:77
observer< alignment > alignment
How the label and icon are aligned.
Definition selection_widget.hpp:73
selection_widget(widget *parent, Value &&value, OptionList &&option_list, Attributes &&...attributes) noexcept
Construct a selection widget which will monitor an option list and a value.
Definition selection_widget.hpp:123
An interactive graphical object as part of the user-interface.
Definition widget.hpp:46
widget(widget *parent) noexcept
widget * parent
Pointer to the parent widget.
Definition widget.hpp:51
The layout of a widget.
Definition widget_layout.hpp:38
Definition label_widget.hpp:26
Definition selection_widget.hpp:28
T move(T... args)