11#include "../l10n/l10n.hpp"
12#include "../observer/observer.hpp"
13#include "../utility/utility.hpp"
14#include "../concurrency/concurrency.hpp"
15#include "../dispatch/dispatch.hpp"
16#include "../GUI/GUI.hpp"
17#include "../macros.hpp"
18#include "radio_delegate.hpp"
24hi_export_module(hikogui.widgets.selection_delegate);
26hi_export
namespace hi {
inline namespace v1 {
57 [[nodiscard]]
bool empty(
widget_intf const& sender)
const noexcept
59 return size(sender) == 0;
82 template<forward_of<
void()> Func>
90 template<forward_of<
void()> Func>
97 notifier<void()> _value_notifier;
98 notifier<void()> _options_notifier;
109 using value_type = T;
121 template<forward_of<observer<value_type>> Value, forward_of<observer<options_type>> Options>
128 _value_cbt = this->value.
subscribe([&](
auto...){ this->_value_notifier(); });
129 _options_cbt = this->options.
subscribe([&](
auto...){ this->_options_notifier(); });
135 return options->size();
140 for (
auto&& option : *options) {
141 if (option.first == *value) {
142 return option.second;
150 return _option_delegate->keyboard_focus_id();
161 auto& [option_value, option_label] = options->at(index);
162 return _option_delegate->make_option_widget(sender, option_value, option_label, _option_delegate);
170 _value_cbt = _parent->value.
subscribe([&](
auto...) {
175 void init(widget_intf
const& sender)
noexcept override
177 hi_assert(_next_value,
"The value was not set of this option widget.");
180 hi_assert(it == _senders.
end() or it->id != sender.id,
"button was already registered with selection-delegate.");
182 _senders.
emplace(it, sender.id, *_next_value);
183 _next_value = std::nullopt;
186 void deinit(widget_intf
const& sender)
noexcept override
188 auto const it =
std::lower_bound(_senders.begin(), _senders.end(), sender.id);
189 if (it != _senders.end() and it->id == sender.id) {
194 [[nodiscard]] std::optional<widget_id> keyboard_focus_id() const noexcept
196 if (_senders.empty()) {
200 for (
auto const& sender : _senders) {
201 if (sender.value == *_parent->value) {
205 return _senders.front().id;
208 [[nodiscard]] std::unique_ptr<widget>
209 make_option_widget(widget_intf
const& sender, value_type
const& value, label
const& label, std::shared_ptr<option_delegate_type> shared_this)
noexcept
211 using button_widget = radio_menu_button_widget;
212 using button_attributes = radio_menu_button_widget::attributes_type;
216 return std::make_unique<button_widget>(button_attributes{label},
std::move(shared_this));
219 [[nodiscard]] widget_value state(widget_intf
const& sender)
const noexcept override
221 auto const it =
std::lower_bound(_senders.begin(), _senders.end(), sender.id);
223 if (it != _senders.end() and it->id == sender.id) {
224 return *_parent->value == it->value ? widget_value::on : widget_value::off;
228 return widget_value::off;
232 void activate(widget_intf
const& sender)
noexcept override
234 auto const it =
std::lower_bound(_senders.begin(), _senders.end(), sender.id);
236 if (it != _senders.end() and it->id == sender.id) {
237 _parent->value = it->value;
242 struct sender_info_type {
246 [[nodiscard]]
friend bool operator==(sender_info_type
const& lhs, widget_id
const& rhs)
noexcept
248 return lhs.id == rhs;
251 [[nodiscard]]
friend auto operator<=>(sender_info_type
const& lhs, widget_id
const& rhs)
noexcept
253 return lhs.id <=> rhs;
258 std::vector<sender_info_type> _senders;
259 std::optional<value_type> _next_value;
260 callback<void(value_type)> _value_cbt;
263 std::shared_ptr<option_delegate_type> _option_delegate;
265 callback<void(value_type)> _value_cbt;
266 callback<void(options_type)> _options_cbt;
269template<
typename Value,
typename Options>
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
Definition callback.hpp:77
Definition widget_intf.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:458
A radio delegate controls the state of a radio button widget.
Definition radio_delegate.hpp:16
A delegate that controls the state of a selection_widget.
Definition selection_delegate.hpp:33
callback< void()> subscribe_on_value(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a change in the value.
Definition selection_delegate.hpp:83
virtual size_t size(widget_intf const &sender) const noexcept
The number of options in the pull-down menu.
Definition selection_delegate.hpp:52
virtual std::optional< widget_id > keyboard_focus_id(widget_intf const &sender) const noexcept
The id of the widget that will need to get keyboard focus when the pull-down menu is opened.
Definition selection_delegate.hpp:45
callback< void()> subscribe_on_options(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a change in the options.
Definition selection_delegate.hpp:91
virtual std::optional< label > selected_label(widget_intf const &sender) const noexcept
Get the label of the selected option.
Definition selection_delegate.hpp:75
virtual std::unique_ptr< widget > make_option_widget(widget_intf const &sender, size_t index) noexcept=0
Create a new widget that represents the button in the selection menu.
A delegate that control the state of a selection_widget.
Definition selection_delegate.hpp:107
size_t size(widget_intf const &sender) const noexcept override
The number of options in the pull-down menu.
Definition selection_delegate.hpp:133
std::optional< widget_id > keyboard_focus_id(widget_intf const &sender) const noexcept override
The id of the widget that will need to get keyboard focus when the pull-down menu is opened.
Definition selection_delegate.hpp:148
std::unique_ptr< widget > make_option_widget(widget_intf const &sender, size_t index) noexcept override
Create a new widget that represents the button in the selection menu.
Definition selection_delegate.hpp:159
default_selection_delegate(Value &&value, Options &&options) noexcept
Construct a default selection delegate.
Definition selection_delegate.hpp:122
std::optional< label > selected_label(widget_intf const &sender) const noexcept override
Get the label of the selected option.
Definition selection_delegate.hpp:138
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition selection_widget.hpp:48