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;
83 template<forward_of<
void()> Func>
91 template<forward_of<
void()> Func>
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>
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
190 auto const it =
std::lower_bound(_senders.begin(), _senders.end(), sender.id);
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;
210 [[nodiscard]] std::unique_ptr<widget>
211 make_option_widget(widget_intf
const& sender, widget_intf
const &parent, value_type
const& value, label
const& label, std::shared_ptr<option_delegate_type> shared_this)
noexcept
213 using button_widget = radio_menu_button_widget;
214 using button_attributes = radio_menu_button_widget::attributes_type;
218 return make_unique<button_widget>(make_not_null(parent), button_attributes{label},
std::move(shared_this));
221 [[nodiscard]] widget_value state(widget_intf
const& sender)
const noexcept override
223 auto const it =
std::lower_bound(_senders.begin(), _senders.end(), sender.id);
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
236 auto const it =
std::lower_bound(_senders.begin(), _senders.end(), sender.id);
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;
259 not_null<default_selection_delegate *> _parent;
260 std::vector<sender_info_type> _senders;
261 std::optional<value_type> _next_value;
262 callback<void(value_type)> _value_cbt;
265 std::shared_ptr<option_delegate_type> _option_delegate;
267 callback<void(value_type)> _value_cbt;
268 callback<void(options_type)> _options_cbt;
271template<
typename Value,
typename Options>
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
The HikoGUI API version 1.
Definition recursive_iterator.hpp:16
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
Definition not_null.hpp:22
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
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition selection_widget.hpp:48