HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
selection_delegate.hpp
Go to the documentation of this file.
1// Copyright Take Vos 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
9#pragma once
10
11#include "../l10n/l10n.hpp"
12#include "../macros.hpp"
13#include <memory>
14#include <functional>
15#include <vector>
16
17namespace hi { inline namespace v1 {
18class selection_widget;
19
25public:
26 using notifier_type = notifier<>;
27 using callback_token = notifier_type::callback_token;
28 using callback_proto = notifier_type::callback_proto;
29
30 virtual ~selection_delegate() = default;
31
32 virtual void init(selection_widget& sender) noexcept {}
33
34 virtual void deinit(selection_widget& sender) noexcept {}
35
41 virtual void set_selected(selection_widget& sender, ssize_t index) noexcept {};
42
48 {
49 return {{}, -1};
50 }
51
54 callback_token
55 subscribe(forward_of<callback_proto> auto&& callback, callback_flags flags = callback_flags::synchronous) noexcept
56 {
57 return _notifier.subscribe(hi_forward(callback), flags);
58 }
59
60protected:
61 notifier_type _notifier;
62};
63
69template<typename T>
71public:
72 using value_type = T;
73 using option_type = std::pair<value_type, label>;
74 using options_type = std::vector<option_type>;
75
78 observer<value_type> off_value;
79
87 forward_of<observer<value_type>> auto&& value,
88 forward_of<observer<options_type>> auto&& options,
89 forward_of<observer<value_type>> auto&& off_value) noexcept :
90 value(hi_forward(value)), options(hi_forward(options)), off_value(hi_forward(off_value))
91 {
92 // clang-format off
93 _value_cbt = this->value.subscribe([&](auto...){ this->_notifier(); });
94 _options_cbt = this->options.subscribe([&](auto...){ this->_notifier(); });
95 _off_value_cbt = this->off_value.subscribe([&](auto...){ this->_notifier(); });
96 // clang-format on
97 }
98
105 forward_of<observer<value_type>> auto&& value,
107 default_selection_delegate(hi_forward(value), hi_forward(options), value_type{})
108 {
109 }
110
111 void set_selected(selection_widget& sender, ptrdiff_t index) noexcept override
112 {
113 if (index == -1 || index >= std::ssize(*options)) {
114 value = *off_value;
115 } else {
116 value = (*options)[index].first;
117 }
118 }
119
121 {
122 auto labels = std::vector<label>{};
123 labels.reserve(options->size());
124
125 auto index = 0_z;
126 auto selected_index = -1_z;
127 for (auto&& option : *options) {
128 if (*value == option.first) {
129 selected_index = index;
130 }
131 labels.push_back(option.second);
132 ++index;
133 }
134
136 }
137
138private:
139 typename decltype(value)::callback_token _value_cbt;
140 typename decltype(options)::callback_token _options_cbt;
141 typename decltype(off_value)::callback_token _off_value_cbt;
142};
143
154 make_default_selection_delegate(auto&& value, auto&& options, auto&&...off_value) noexcept
155 requires(sizeof...(off_value) <= 1) and
156 requires
157{
158 default_selection_delegate<observer_decay_t<decltype(value)>>{
159 hi_forward(value), hi_forward(options), hi_forward(off_value)...};
160}
161{
162 return std::make_shared<default_selection_delegate<observer_decay_t<decltype(value)>>>(
163 hi_forward(value), hi_forward(options), hi_forward(off_value)...);
164}
165
166}} // namespace hi::v1
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:154
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A delegate that controls the state of a selection_widget.
Definition selection_delegate.hpp:24
virtual std::pair< std::vector< label >, ssize_t > options_and_selected(selection_widget const &sender) const noexcept
Retrieve the label of an option.
Definition selection_delegate.hpp:47
virtual void set_selected(selection_widget &sender, ssize_t index) noexcept
Called when an option is selected by the user.
Definition selection_delegate.hpp:41
callback_token subscribe(forward_of< callback_proto > auto &&callback, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition selection_delegate.hpp:55
A delegate that control the state of a selection_widget.
Definition selection_delegate.hpp:70
std::pair< std::vector< label >, ptrdiff_t > options_and_selected(selection_widget const &sender) const noexcept override
Retrieve the label of an option.
Definition selection_delegate.hpp:120
default_selection_delegate(forward_of< observer< value_type > > auto &&value, forward_of< observer< options_type > > auto &&options, forward_of< observer< value_type > > auto &&off_value) noexcept
Construct a default selection delegate.
Definition selection_delegate.hpp:86
default_selection_delegate(forward_of< observer< value_type > > auto &&value, forward_of< observer< options_type > > auto &&options) noexcept
Construct a default selection delegate.
Definition selection_delegate.hpp:104
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition selection_widget.hpp:45
True if T is a forwarded type of Forward.
Definition concepts.hpp:131
T make_shared(T... args)
T move(T... args)
T reserve(T... args)