HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
default_tab_delegate.hpp
1// Copyright Take Vos 2021.
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
5#pragma once
6
7#include "tab_delegate.hpp"
8#include "../observable.hpp"
9#include "../cast.hpp"
10#include <type_traits>
11#include <memory>
12#include <unordered_map>
13
14namespace hi::inline v1 {
15
16template<typename T>
18public:
19 using value_type = T;
20
23
24 default_tab_delegate(auto &&value) noexcept : value(hi_forward(value))
25 {
26 _value_cbt = this->value.subscribe([&](auto...) {
27 this->_notifier();
28 });
29 }
30
31 void add_tab(tab_widget &sender, std::size_t key, std::size_t index) noexcept override
32 {
33 hi_axiom(not tab_indices.contains(key));
34 tab_indices[key] = index;
35 }
36
37 [[nodiscard]] ssize_t index(tab_widget &sender) noexcept override
38 {
39 auto it = tab_indices.find(*value);
40 if (it == tab_indices.end()) {
41 return -1;
42 } else {
43 return static_cast<ssize_t>(it->second);
44 }
45 }
46
47private:
48 typename decltype(value)::token_type _value_cbt;
49};
50
51template<typename Value>
53
54template<typename Value>
55std::unique_ptr<tab_delegate> make_unique_default_tab_delegate(Value &&value) noexcept
56{
57 using value_type = observable_argument_t<std::remove_cvref_t<Value>>;
58 return std::make_unique<default_tab_delegate<value_type>>(std::forward<Value>(value));
59}
60
61} // namespace hi::inline v1
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
An observable value.
Definition observable.hpp:359
Definition default_tab_delegate.hpp:17
Definition tab_delegate.hpp:13
A graphical element that shows only one of a predefined set of mutually exclusive child widgets.
Definition tab_widget.hpp:32
T end(T... args)
T find(T... args)