ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
TabView.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "Common.hpp"
9#include "Layout.hpp"
10#include "Layouts.hpp"
11#include "Button.hpp"
12
13#include <functional>
14#include <string>
15#include <string_view>
16#include <vector>
17
18namespace ZenitUI::UI
19{
23
25 enum class TabBarPosition
26 {
29 };
30
38 class Tab : public TLayout<Tab>
39 {
40 public:
41 explicit Tab(std::string title = "");
42
43 const std::string &getTitle() const noexcept { return title_; }
44 void setTitle(std::string title);
45
46 private:
47 std::string title_;
48 };
49
62 class TabView : public TLayout<TabView>
63 {
64 public:
70 explicit TabView(int initialSelected = 0,
72
79 std::shared_ptr<Tab> addTab(std::string title, std::shared_ptr<Layout> content = nullptr);
80
82 bool removeTab(int index);
83
85 void clearTabs();
86
88 int tabCount() const;
89
91 int getActiveTab() const;
92
98 void setActiveTab(int index, bool notify = false);
99
101 void setTabTitle(int index, std::string title);
102
104 std::string getTabTitle(int index) const;
105
107 void setTabEnabled(int index, bool enabled);
108
110 std::shared_ptr<Layout> getTabPage(int index) const;
111
113 std::shared_ptr<Button> tabButton(int index);
114
115 void setTabBarPosition(TabBarPosition pos);
116 TabBarPosition getTabBarPosition() const noexcept { return barPosition_; }
117
119 std::shared_ptr<Layout> tabBar() const { return tabBar_; }
120
122 std::shared_ptr<Layout> contentHost() const { return contentHost_; }
123
125 static bool parseTabBarPosition(std::string_view s, TabBarPosition &out) noexcept;
126
128 std::function<void(int)> onTabChanged;
129
130 Layout *contentTarget() override { return contentHost_ ? contentHost_.get() : this; }
131 Vec2 measure(float parent_w, float parent_h) override;
132
133 protected:
134 void onBuild() override;
135 void onUpdate(float dt) override;
136
137 private:
138 struct TabEntry
139 {
140 std::weak_ptr<Layout> pane;
141 std::shared_ptr<Button> button;
142 };
143
144 std::vector<std::shared_ptr<Layout>> collectPanes() const;
145 std::string resolvePaneTitle(const Layout &pane, int index) const;
146 void syncTabs();
147 void applyActiveVisuals();
148 int findNextEnabled(int from, int dir) const;
149 int findEdgeEnabled(bool first) const;
150
151 std::shared_ptr<Layout> tabBar_;
152 std::shared_ptr<Layout> contentHost_;
153 std::vector<TabEntry> entries_;
154
156 int requestedInitialIndex_{0};
157 int activeIndex_{-1};
158 };
159
161} // namespace ZenitUI::UI
A clickable button with an optional text label.
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
Basic layout containers: VStack, HStack, Panel.
The base node type: tree, style, layout, input, scroll, render.
Definition Layout.hpp:154
Layout(LayoutType type=LayoutType::Stack)
Construct a node with the given layout strategy.
Definition Layout.hpp:163
TLayout(Args &&...args)
Definition Layout.hpp:1401
void setTabTitle(int index, std::string title)
Update the title of the tab at index.
int tabCount() const
Number of tab pages currently inside this TabView.
static bool parseTabBarPosition(std::string_view s, TabBarPosition &out) noexcept
Parse "top" or "bottom".
int getActiveTab() const
Index of the active tab, or -1 if empty.
std::string getTabTitle(int index) const
Return the title of the tab at index, or "" if out of range.
std::shared_ptr< Layout > getTabPage(int index) const
Return the page node at index, or nullptr if out of range.
std::function< void(int)> onTabChanged
Fired whenever the active tab changes via user interaction or setActiveTab(i, true).
Definition TabView.hpp:128
std::shared_ptr< Layout > tabBar() const
Access the internal TabBar container.
Definition TabView.hpp:119
Vec2 measure(float parent_w, float parent_h) override
Compute the intrinsic size given available space.
bool removeTab(int index)
Remove the tab at index.
std::shared_ptr< Layout > contentHost() const
Access the internal TabContent container.
Definition TabView.hpp:122
std::shared_ptr< Button > tabButton(int index)
Return the Button corresponding to tab index, or nullptr if out of range.
void setActiveTab(int index, bool notify=false)
Activate the tab at index.
std::shared_ptr< Tab > addTab(std::string title, std::shared_ptr< Layout > content=nullptr)
Append a tab page and return its Tab wrapper node.
void setTabEnabled(int index, bool enabled)
Enable or disable the tab at index.
TabView(int initialSelected=0, TabBarPosition position=TabBarPosition::Top)
Construct a TabView.
void clearTabs()
Remove every tab page and tab button.
void onUpdate(float dt) override
Called every frame from update, after input dispatch.
Layout * contentTarget() override
Return the node that receives children when built from markup.
Definition TabView.hpp:130
void onBuild() override
Hook: called once after create() finishes constructing the object.
TabBarPosition
Placement of the tab strip relative to the content area.
Definition TabView.hpp:26
@ Bottom
Tab bar below the active page.
Definition TabView.hpp:28
@ Top
Tab bar above the active page (default).
Definition TabView.hpp:27
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25