HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
toggle_delegate.hpp
1
2
3#pragma once
4
5#include "../dispatch/dispatch.hpp"
6#include "../observer/observer.hpp"
7#include "../GUI/GUI.hpp"
8#include "../utility/utility.hpp"
9
10hi_export_module(hikogui.widgets.toggle_delegate);
11
12hi_export namespace hi {
13inline namespace v1 {
14
19public:
20 virtual ~toggle_delegate() = default;
21
22 virtual void init(widget_intf const& sender) noexcept {}
23
24 virtual void deinit(widget_intf const& sender) noexcept {}
25
28 virtual void activate(widget_intf const& sender) noexcept {}
29
32 [[nodiscard]] virtual widget_value state(widget_intf const& sender) const noexcept
33 {
34 return widget_value::off;
35 }
36
39 template<forward_of<void()> Func>
40 [[nodiscard]] callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
41 {
42 return _notifier.subscribe(std::forward<Func>(func), flags);
43 }
44
45protected:
46 notifier<void()> _notifier;
47};
48
57template<std::equality_comparable T>
59public:
60 using value_type = T;
61 constexpr static bool can_make_defaults =
62 std::is_same_v<value_type, bool> or std::is_integral_v<value_type> or std::is_enum_v<value_type>;
63
65 observer<value_type> on_value;
66 observer<value_type> off_value;
67
74 template<
75 forward_of<observer<value_type>> Value,
76 forward_of<observer<value_type>> OnValue,
77 forward_of<observer<value_type>> OffValue>
78 default_toggle_delegate(Value&& value, OnValue&& on_value, OffValue&& off_value) noexcept :
79 value(std::forward<Value>(value)), on_value(std::forward<OnValue>(on_value)), off_value(std::forward<OffValue>(off_value))
80 {
81 // clang-format off
82 _value_cbt = this->value.subscribe([&](auto...){ this->_notifier(); });
83 _on_value_cbt = this->on_value.subscribe([&](auto...){ this->_notifier(); });
84 _off_value_cbt = this->off_value.subscribe([&](auto...){ this->_notifier(); });
85 // clang-format on
86 }
87
93 template<forward_of<observer<value_type>> Value, forward_of<observer<value_type>> OnValue>
95 Value&& value,
96 OnValue&& on_value) noexcept requires can_make_defaults
97 : default_toggle_delegate(std::forward<Value>(value), std::forward<OnValue>(on_value), value_type{})
98 {
99 }
100
105 template<forward_of<observer<value_type>> Value>
106 default_toggle_delegate(Value&& value) noexcept requires can_make_defaults
107 : default_toggle_delegate(std::forward<Value>(value), value_type{1}, value_type{})
108 {
109 }
110
112 [[nodiscard]] widget_value state(widget_intf const& sender) const noexcept override
113 {
114 if (*value == *on_value) {
115 return widget_value::on;
116 } else if (*value == *off_value) {
117 return widget_value::off;
118 } else {
119 return widget_value::other;
120 }
121 }
122
123 void activate(widget_intf const& sender) noexcept override
124 {
125 if (*value == *off_value) {
126 value = *on_value;
127 } else {
128 value = *off_value;
129 }
130 }
132private:
133 callback<void(value_type)> _value_cbt;
134 callback<void(value_type)> _on_value_cbt;
135 callback<void(value_type)> _off_value_cbt;
136};
137
138template<typename Value>
139default_toggle_delegate(Value&&) -> default_toggle_delegate<observer_decay_t<Value>>;
140
141template<typename Value, typename OnValue>
142default_toggle_delegate(Value&&, OnValue&&) -> default_toggle_delegate<observer_decay_t<Value>>;
143
144template<typename Value, typename OnValue, typename OffValue>
145default_toggle_delegate(Value&&, OnValue&&, OffValue&&) -> default_toggle_delegate<observer_decay_t<Value>>;
146
147} // namespace v1
148}
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 button delegate controls the state of a button widget.
Definition toggle_delegate.hpp:18
virtual widget_value state(widget_intf const &sender) const noexcept
Used by the widget to check the state of the button.
Definition toggle_delegate.hpp:32
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition toggle_delegate.hpp:40
virtual void activate(widget_intf const &sender) noexcept
Called when the button is pressed by the user.
Definition toggle_delegate.hpp:28
A default toggle button delegate.
Definition toggle_delegate.hpp:58
default_toggle_delegate(Value &&value) noexcept
Construct a delegate.
Definition toggle_delegate.hpp:106
default_toggle_delegate(Value &&value, OnValue &&on_value) noexcept
Construct a delegate.
Definition toggle_delegate.hpp:94
default_toggle_delegate(Value &&value, OnValue &&on_value, OffValue &&off_value) noexcept
Construct a delegate.
Definition toggle_delegate.hpp:78