HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
tab_delegate.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-2022.
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 <memory>
12#include <functional>
13
14namespace hi { inline namespace v1 {
15class tab_widget;
16
22public:
23 using notifier_type = notifier<>;
24 using callback_token = notifier_type::callback_token;
25 using callback_proto = notifier_type::callback_proto;
26
27 virtual ~tab_delegate() = default;
28 virtual void init(tab_widget& sender) noexcept {}
29 virtual void deinit(tab_widget& sender) noexcept {}
30
31 virtual void add_tab(tab_widget& sender, std::size_t key, std::size_t index) noexcept {}
32
33 virtual ssize_t index(tab_widget& sender) noexcept
34 {
35 return -1;
36 }
37
40 callback_token
41 subscribe(forward_of<callback_proto> auto&& callback, callback_flags flags = callback_flags::synchronous) noexcept
42 {
43 return _notifier.subscribe(hi_forward(callback), flags);
44 }
45
46protected:
47 notifier_type _notifier;
48};
49
55template<typename T>
57public:
58 using value_type = T;
59
60 observer<value_type> value;
62
67 default_tab_delegate(forward_of<observer<value_type>> auto&& value) noexcept : value(hi_forward(value))
68 {
69 _value_cbt = this->value.subscribe([&](auto...) {
70 this->_notifier();
71 });
72 }
73
74 // XXX key should really be of value_type, not sure how to handle that with the tab_widget not knowing the type of key.
75 void add_tab(tab_widget& sender, std::size_t key, std::size_t index) noexcept override
76 {
77 hi_assert(not tab_indices.contains(key));
78 tab_indices[key] = index;
79 }
80
81 [[nodiscard]] ssize_t index(tab_widget& sender) noexcept override
82 {
83 auto it = tab_indices.find(*value);
84 if (it == tab_indices.end()) {
85 return -1;
86 } else {
87 return static_cast<ssize_t>(it->second);
88 }
89 }
90
91private:
92 typename decltype(value)::callback_token _value_cbt;
93};
94
102std::shared_ptr<tab_delegate> make_default_tab_delegate(auto&& value) noexcept requires requires
103{
104 default_tab_delegate<observer_decay_t<decltype(value)>>{hi_forward(value)};
105}
106{
107 using value_type = observer_decay_t<decltype(value)>;
108 return std::make_shared<default_tab_delegate<value_type>>(hi_forward(value));
109}
110
111}} // namespace hi::v1
#define hi_assert(expression)
Assert if expression is true.
Definition assert.hpp:86
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
std::shared_ptr< tab_delegate > make_default_tab_delegate(auto &&value) noexcept
Create a shared pointer to a default tab delegate.
Definition tab_delegate.hpp:102
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition utility.hpp:173
A delegate that controls the state of a tab_widget.
Definition tab_delegate.hpp:21
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 tab_delegate.hpp:41
A delegate that control the state of a tab_widget.
Definition tab_delegate.hpp:56
default_tab_delegate(forward_of< observer< value_type > > auto &&value) noexcept
Construct a default tab delegate.
Definition tab_delegate.hpp:67
A graphical element that shows only one of a predefined set of mutually exclusive child widgets.
Definition tab_widget.hpp:36
T end(T... args)
T find(T... args)