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 "../observer/observer.hpp"
12#include "../utility/utility.hpp"
13#include "../concurrency/concurrency.hpp"
14#include "../dispatch/dispatch.hpp"
15#include "../GUI/GUI.hpp"
16#include "../macros.hpp"
17#include "../macros.hpp"
18#include <memory>
19#include <functional>
20
21hi_export_module(hikogui.widgets.tab_delegate);
22
23hi_export namespace hi { inline namespace v1 {
24
30public:
31 virtual ~tab_delegate() = default;
32 virtual void init(widget_intf const& sender) noexcept {}
33 virtual void deinit(widget_intf const& sender) noexcept {}
34
35 virtual void add_tab(widget_intf const& sender, std::size_t key, std::size_t index) noexcept {}
36
37 virtual std::ptrdiff_t index(widget_intf const& sender) noexcept
38 {
39 return -1;
40 }
41
44 template<forward_of<void()> Func>
45 [[nodiscard]] callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
46 {
47 return _notifier.subscribe(std::forward<Func>(func), flags);
48 }
49
50protected:
51 notifier<void()> _notifier;
52};
53
59template<typename T>
61public:
62 using value_type = T;
63
66
71 template<forward_of<observer<value_type>> Value>
72 default_tab_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
73 {
74 _value_cbt = this->value.subscribe([&](auto...) {
75 this->_notifier();
76 });
77 }
78
79 // XXX key should really be of value_type, not sure how to handle that with the tab_widget not knowing the type of key.
80 void add_tab(widget_intf const& sender, std::size_t key, std::size_t index) noexcept override
81 {
82 hi_assert(not tab_indices.contains(key));
83 tab_indices[key] = index;
84 }
85
86 [[nodiscard]] std::ptrdiff_t index(widget_intf const& sender) noexcept override
87 {
88 auto it = tab_indices.find(*value);
89 if (it == tab_indices.end()) {
90 return -1;
91 } else {
92 return static_cast<std::ptrdiff_t>(it->second);
93 }
94 }
95
96private:
97 callback<void(value_type)> _value_cbt;
98};
99
107template<typename Value>
109 requires requires { default_tab_delegate<observer_decay_t<Value>>{std::forward<Value>(value)}; }
110{
111 using value_type = observer_decay_t<Value>;
112 return std::make_shared<default_tab_delegate<value_type>>(std::forward<Value>(value));
113}
114
115}} // namespace hi::v1
std::shared_ptr< tab_delegate > make_default_tab_delegate(Value &&value) noexcept
Create a shared pointer to a default tab delegate.
Definition tab_delegate.hpp:108
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 delegate that controls the state of a tab_widget.
Definition tab_delegate.hpp:29
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition tab_delegate.hpp:45
A delegate that control the state of a tab_widget.
Definition tab_delegate.hpp:60
default_tab_delegate(Value &&value) noexcept
Construct a default tab delegate.
Definition tab_delegate.hpp:72
T end(T... args)
T find(T... args)