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 {
27class selection_widget;
57 [[nodiscard]]
bool empty(
widget_intf const& sender)
const noexcept
59 return size(sender) == 0;
83 template<forward_of<
void()> Func>
86 return _value_notifier.subscribe(std::forward<Func>(func), flags);
91 template<forward_of<
void()> Func>
94 return _options_notifier.subscribe(std::forward<Func>(func), flags);
98 notifier<void()> _value_notifier;
99 notifier<void()> _options_notifier;
110 using value_type = T;
122 template<forward_of<observer<value_type>> Value, forward_of<observer<options_type>> Options>
124 value(std::forward<Value>(value)), options(std::forward<Options>(options))
126 _option_delegate = std::make_shared<option_delegate_type>(*
this);
129 _value_cbt = this->value.
subscribe([&](
auto...){ this->_value_notifier(); });
130 _options_cbt = this->options.
subscribe([&](
auto...){ this->_options_notifier(); });
136 return options->size();
141 for (
auto&& option : *options) {
142 if (option.first == *value) {
143 return option.second;
151 return _option_delegate->keyboard_focus_id();
163 auto& [option_value, option_label] = options->at(index);
164 return _option_delegate->make_option_widget(sender, parent, option_value, option_label, _option_delegate);
172 _value_cbt = _parent->value.
subscribe([&](
auto...) {
177 void init(widget_intf
const& sender)
noexcept override
179 hi_assert(_next_value,
"The value was not set of this option widget.");
182 hi_assert(it == _senders.
end() or it->id != sender.id,
"button was already registered with selection-delegate.");
184 _senders.
emplace(it, sender.id, *_next_value);
185 _next_value = std::nullopt;
188 void deinit(widget_intf
const& sender)
noexcept override
191 if (it != _senders.
end() and it->id == sender.id) {
196 [[nodiscard]] std::optional<widget_id> keyboard_focus_id() const noexcept
198 if (_senders.
empty()) {
202 for (
auto const& sender : _senders) {
203 if (sender.value == *_parent->value) {
207 return _senders.
front().id;
213 using button_widget = radio_menu_button_widget;
214 using button_attributes = radio_menu_button_widget::attributes_type;
218 return std::make_unique<button_widget>(
std::addressof(parent), button_attributes{label},
std::move(shared_this));
221 [[nodiscard]] widget_value state(widget_intf
const& sender)
const noexcept override
225 if (it != _senders.
end() and it->id == sender.id) {
226 return *_parent->value == it->value ? widget_value::on : widget_value::off;
230 return widget_value::off;
234 void activate(widget_intf
const& sender)
noexcept override
238 if (it != _senders.
end() and it->id == sender.id) {
239 _parent->value = it->value;
244 struct sender_info_type {
248 [[nodiscard]]
friend bool operator==(sender_info_type
const& lhs, widget_id
const& rhs)
noexcept
250 return lhs.id == rhs;
253 [[nodiscard]]
friend auto operator<=>(sender_info_type
const& lhs, widget_id
const& rhs)
noexcept
255 return lhs.id <=> rhs;
261 std::optional<value_type> _next_value;
262 callback<void(value_type)> _value_cbt;
267 callback<void(value_type)> _value_cbt;
268 callback<void(options_type)> _options_cbt;
271template<
typename Value,
typename Options>
272default_selection_delegate(Value&&, Options&&) -> default_selection_delegate<observer_decay_t<Value>>;
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:77
Definition widget_intf.hpp:24
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:456
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:84
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::unique_ptr< widget > make_option_widget(widget_intf const &sender, widget_intf const &parent, size_t index) noexcept=0
Create a new widget that represents the button in the selection menu.
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:92
virtual std::optional< label > selected_label(widget_intf const &sender) const noexcept
Get the label of the selected option.
Definition selection_delegate.hpp:76
A delegate that control the state of a selection_widget.
Definition selection_delegate.hpp:108
size_t size(widget_intf const &sender) const noexcept override
The number of options in the pull-down menu.
Definition selection_delegate.hpp:134
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:149
default_selection_delegate(Value &&value, Options &&options) noexcept
Construct a default selection delegate.
Definition selection_delegate.hpp:123
std::unique_ptr< widget > make_option_widget(widget_intf const &sender, widget_intf const &parent, size_t index) noexcept override
Create a new widget that represents the button in the selection menu.
Definition selection_delegate.hpp:161
std::optional< label > selected_label(widget_intf const &sender) const noexcept override
Get the label of the selected option.
Definition selection_delegate.hpp:139