HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
radio_button_delegate.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "../notifier.hpp"
12
13namespace hi { inline namespace v1 {
14
19public:
20 using notifier_type = notifier<>;
21 using callback_token = notifier_type::callback_token;
22 using callback_proto = notifier_type::callback_proto;
23
24 virtual ~button_delegate() = default;
25
26 virtual void init(widget& sender) noexcept {}
27
28 virtual void deinit(widget& sender) noexcept {}
29
32 virtual void activate(widget& sender) noexcept = 0;
33
36 [[nodiscard]] virtual widget_state state(widget const& sender) const noexcept
37 {
38 return widget_state::off;
39 }
40
43 [[nodiscard]] callback_token
44 subscribe(forward_of<callback_proto> auto&& callback, callback_flags flags = callback_flags::synchronous) noexcept
45 {
46 return _notifier.subscribe(hi_forward(callback), flags);
47 }
48
49protected:
50 notifier_type _notifier;
51};
52
61template<typename T>
63public:
64 using value_type = T;
65
66 observer<value_type> value;
67 observer<value_type> on_value;
68
75 forward_of<observer<value_type>> auto&& value,
76 forward_of<observer<value_type>> auto&& on_value) noexcept :
77 value(hi_forward(value)), on_value(hi_forward(on_value))
78 {
79 // clang-format off
80 _value_cbt = this->value.subscribe([&](auto...){ this->_notifier(); });
81 _on_value_cbt = this->on_value.subscribe([&](auto...){ this->_notifier(); });
82 // clang-format on
83 }
84
86 [[nodiscard]] widget_state state(widget const& sender) const noexcept override
87 {
88 if (*value == *on_value) {
89 return widget_state::on;
90 } else {
91 return widget_state::off;
92 }
93 }
94
95 void activate(widget& sender) noexcept override
96 {
97 value = *on_value;
98 }
100private:
101 typename decltype(value)::callback_token _value_cbt;
102 typename decltype(on_value)::callback_token _on_value_cbt;
103};
104
105}} // namespace hi::v1
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
widget_state
Definition widget_state.hpp:14
@ off
The widget in the off-state.
@ on
The widget is in the on-state.
Definition widget.hpp:26
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:23
A default radio button delegate.
Definition button_delegate.hpp:67
default_radio_button_delegate(forward_of< observer< value_type > > auto &&value, forward_of< observer< value_type > > auto &&on_value) noexcept
Construct a delegate.
Definition radio_button_delegate.hpp:74
A radio button delegate controls the state of a radio button widget.
Definition radio_button_delegate.hpp:18
callback_token subscribe(forward_of< callback_proto > auto &&callback, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition radio_button_delegate.hpp:44
virtual void activate(widget &sender) noexcept=0
Called when the button is pressed by the user.
virtual widget_state state(widget const &sender) const noexcept
Used by the widget to check the state of the button.
Definition radio_button_delegate.hpp:36