HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
radio_delegate.hpp
1
2#pragma once
3
4#include "../dispatch/dispatch.hpp"
5#include "../observer/observer.hpp"
6#include "../GUI/GUI.hpp"
7#include "../utility/utility.hpp"
8
9hi_export_module(hikogui.widgets.radio_delegate);
10
11hi_export namespace hi { inline namespace v1 {
12
17public:
18 virtual ~radio_delegate() = default;
19
20 virtual void init(widget_intf const& sender) noexcept {}
21
22 virtual void deinit(widget_intf const& sender) noexcept {}
23
26 virtual void activate(widget_intf const& sender) noexcept {}
27
30 [[nodiscard]] virtual widget_value state(widget_intf const& sender) const noexcept
31 {
32 return widget_value::off;
33 }
34
37 template<forward_of<void()> Func>
38 [[nodiscard]] callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
39 {
40 return _notifier.subscribe(std::forward<Func>(func), flags);
41 }
42
43protected:
44 notifier<void()> _notifier;
45};
46
55template<std::equality_comparable T>
57public:
58 using value_type = T;
59
61 observer<value_type> on_value;
62
68 template<forward_of<observer<value_type>> Value, forward_of<observer<value_type>> OnValue>
70 Value&& value,
71 OnValue&& on_value) noexcept :
72 value(std::forward<Value>(value)), on_value(std::forward<OnValue>(on_value))
73 {
74 // clang-format off
75 _value_cbt = this->value.subscribe([&](auto...){ this->_notifier(); });
76 _on_value_cbt = this->on_value.subscribe([&](auto...){ this->_notifier(); });
77 // clang-format on
78 }
79
81 [[nodiscard]] widget_value state(widget_intf const& sender) const noexcept override
82 {
83 if (*value == *on_value) {
84 return widget_value::on;
85 } else {
86 return widget_value::off;
87 }
88 }
89
90 void activate(widget_intf const& sender) noexcept override
91 {
92 value = *on_value;
93 }
95private:
96 callback<void(value_type)> _value_cbt;
97 callback<void(value_type)> _on_value_cbt;
98};
99
100template<typename Value, typename OnValue>
101default_radio_delegate(Value&&, OnValue&&) -> default_radio_delegate<observer_decay_t<Value>>;
102
103}}
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
virtual widget_value state(widget_intf const &sender) const noexcept
Used by the widget to check the state of the button.
Definition radio_delegate.hpp:30
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition radio_delegate.hpp:38
virtual void activate(widget_intf const &sender) noexcept
Called when the button is pressed by the user.
Definition radio_delegate.hpp:26
A default radio button delegate.
Definition radio_delegate.hpp:56
default_radio_delegate(Value &&value, OnValue &&on_value) noexcept
Construct a delegate.
Definition radio_delegate.hpp:69