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 {
15
21public:
22 using notifier_type = notifier<>;
23 using callback_token = notifier_type::callback_token;
24 using callback_proto = notifier_type::callback_proto;
25
26 virtual ~tab_delegate() = default;
27 virtual void init(widget& sender) noexcept {}
28 virtual void deinit(widget& sender) noexcept {}
29
30 virtual void add_tab(widget& sender, std::size_t key, std::size_t index) noexcept {}
31
32 virtual ssize_t index(widget& sender) noexcept
33 {
34 return -1;
35 }
36
39 callback_token
40 subscribe(forward_of<callback_proto> auto&& callback, callback_flags flags = callback_flags::synchronous) noexcept
41 {
42 return _notifier.subscribe(hi_forward(callback), flags);
43 }
44
45protected:
46 notifier_type _notifier;
47};
48
54template<typename T>
56public:
57 using value_type = T;
58
59 observer<value_type> value;
61
66 default_tab_delegate(forward_of<observer<value_type>> auto&& value) noexcept : value(hi_forward(value))
67 {
68 _value_cbt = this->value.subscribe([&](auto...) {
69 this->_notifier();
70 });
71 }
72
73 // XXX key should really be of value_type, not sure how to handle that with the tab_widget not knowing the type of key.
74 void add_tab(widget& sender, std::size_t key, std::size_t index) noexcept override
75 {
76 hi_assert(not tab_indices.contains(key));
77 tab_indices[key] = index;
78 }
79
80 [[nodiscard]] ssize_t index(widget& sender) noexcept override
81 {
82 auto it = tab_indices.find(*value);
83 if (it == tab_indices.end()) {
84 return -1;
85 } else {
86 return static_cast<ssize_t>(it->second);
87 }
88 }
89
90private:
91 typename decltype(value)::callback_token _value_cbt;
92};
93
101std::shared_ptr<tab_delegate> make_default_tab_delegate(auto&& value) noexcept requires requires
102{
103 default_tab_delegate<observer_decay_t<decltype(value)>>{hi_forward(value)};
104}
105{
106 using value_type = observer_decay_t<decltype(value)>;
107 return std::make_shared<default_tab_delegate<value_type>>(hi_forward(value));
108}
109
110}} // namespace hi::v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#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:101
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition utility.hpp:189
Definition widget.hpp:26
A delegate that controls the state of a tab_widget.
Definition tab_delegate.hpp:20
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:40
A delegate that control the state of a tab_widget.
Definition tab_delegate.hpp:55
default_tab_delegate(forward_of< observer< value_type > > auto &&value) noexcept
Construct a default tab delegate.
Definition tab_delegate.hpp:66
T end(T... args)
T find(T... args)