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 "../macros.hpp"
12#include <memory>
13#include <functional>
14
15namespace hi { inline namespace v1 {
16class tab_widget;
17
23public:
24 using notifier_type = notifier<>;
25 using callback_token = notifier_type::callback_token;
26 using callback_proto = notifier_type::callback_proto;
27
28 virtual ~tab_delegate() = default;
29 virtual void init(tab_widget& sender) noexcept {}
30 virtual void deinit(tab_widget& sender) noexcept {}
31
32 virtual void add_tab(tab_widget& sender, std::size_t key, std::size_t index) noexcept {}
33
34 virtual ssize_t index(tab_widget& sender) noexcept
35 {
36 return -1;
37 }
38
41 callback_token
42 subscribe(forward_of<callback_proto> auto&& callback, callback_flags flags = callback_flags::synchronous) noexcept
43 {
44 return _notifier.subscribe(hi_forward(callback), flags);
45 }
46
47protected:
48 notifier_type _notifier;
49};
50
56template<typename T>
58public:
59 using value_type = T;
60
63
68 default_tab_delegate(forward_of<observer<value_type>> auto&& value) noexcept : value(hi_forward(value))
69 {
70 _value_cbt = this->value.subscribe([&](auto...) {
71 this->_notifier();
72 });
73 }
74
75 // XXX key should really be of value_type, not sure how to handle that with the tab_widget not knowing the type of key.
76 void add_tab(tab_widget& sender, std::size_t key, std::size_t index) noexcept override
77 {
78 hi_assert(not tab_indices.contains(key));
79 tab_indices[key] = index;
80 }
81
82 [[nodiscard]] ssize_t index(tab_widget& sender) noexcept override
83 {
84 auto it = tab_indices.find(*value);
85 if (it == tab_indices.end()) {
86 return -1;
87 } else {
88 return static_cast<ssize_t>(it->second);
89 }
90 }
91
92private:
93 typename decltype(value)::callback_token _value_cbt;
94};
95
103std::shared_ptr<tab_delegate> make_default_tab_delegate(auto&& value) noexcept requires requires
104{
105 default_tab_delegate<observer_decay_t<decltype(value)>>{hi_forward(value)};
106}
107{
108 using value_type = observer_decay_t<decltype(value)>;
109 return std::make_shared<default_tab_delegate<value_type>>(hi_forward(value));
110}
111
112}} // namespace hi::v1
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:103
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:33
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A delegate that controls the state of a tab_widget.
Definition tab_delegate.hpp:22
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:42
A delegate that control the state of a tab_widget.
Definition tab_delegate.hpp:57
default_tab_delegate(forward_of< observer< value_type > > auto &&value) noexcept
Construct a default tab delegate.
Definition tab_delegate.hpp:68
A graphical element that shows only one of a predefined set of mutually exclusive child widgets.
Definition tab_widget.hpp:37
True if T is a forwarded type of Forward.
Definition concepts.hpp:131
T end(T... args)
T find(T... args)