ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
MenuBar.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include "Common.hpp"
10#include "Layout.hpp"
11#include "Layouts.hpp"
12#include "Button.hpp"
13#include "Text.hpp"
14#include "Popup.hpp"
15
16#include <functional>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace ZenitUI::UI
22{
26
27 class Menu;
28 class MenuBar;
29
34 struct MenuEntry
35 {
36 enum class Kind
37 {
38 Action,
40 Separator,
41 Submenu
42 };
43
44 Kind kind{Kind::Action};
45 std::string label;
46 std::string shortcut;
47 std::string value;
48 bool enabled{true};
49 bool checked{false};
50 std::function<void()> onTrigger;
51 std::function<void(bool)> onToggle;
52 std::vector<MenuEntry> children;
53
54 static MenuEntry action(std::string label,
55 std::function<void()> onTrigger = nullptr,
56 std::string shortcut = "",
57 bool enabled = true,
58 std::string value = "")
59 {
60 MenuEntry e;
61 e.kind = Kind::Action;
62 e.label = std::move(label);
63 e.shortcut = std::move(shortcut);
64 e.value = std::move(value);
65 e.enabled = enabled;
66 e.onTrigger = std::move(onTrigger);
67 return e;
68 }
69
70 static MenuEntry checkbox(std::string label,
71 bool checked,
72 std::function<void(bool)> onToggle = nullptr,
73 std::string shortcut = "",
74 bool enabled = true,
75 std::string value = "")
76 {
77 MenuEntry e;
78 e.kind = Kind::Checkbox;
79 e.label = std::move(label);
80 e.shortcut = std::move(shortcut);
81 e.value = std::move(value);
82 e.enabled = enabled;
83 e.checked = checked;
84 e.onToggle = std::move(onToggle);
85 return e;
86 }
87
88 static MenuEntry separator()
89 {
90 MenuEntry e;
91 e.kind = Kind::Separator;
92 return e;
93 }
94
95 static MenuEntry submenu(std::string label,
96 std::vector<MenuEntry> items,
97 bool enabled = true,
98 std::string value = "")
99 {
100 MenuEntry e;
101 e.kind = Kind::Submenu;
102 e.label = std::move(label);
103 e.value = std::move(value);
104 e.enabled = enabled;
105 e.children = std::move(items);
106 return e;
107 }
108 };
109
116 class MenuSeparator : public TLayout<MenuSeparator>
117 {
118 public:
119 MenuSeparator();
120 };
121
129 class MenuItem : public TLayout<MenuItem>
130 {
131 public:
132 explicit MenuItem(std::string label = "",
133 std::string shortcut = "",
134 std::string value = "");
135
136 const std::string &getLabel() const noexcept { return label_; }
137 void setLabel(std::string label);
138
139 const std::string &getShortcut() const noexcept { return shortcut_; }
140 void setShortcut(std::string shortcut);
141
142 const std::string &getValue() const noexcept { return value_; }
143 void setValue(std::string value) { value_ = std::move(value); }
144
145 void setCheckable(bool checkable);
146 bool isCheckable() const noexcept { return checkable_; }
147
149 void setItemChecked(bool checked, bool notify = false);
150 bool isItemChecked() const { return getChecked(); }
151
153 bool hasSubmenu() const;
154
156 std::shared_ptr<Menu> ensureSubmenu();
157
159 std::shared_ptr<Menu> submenu() const { return submenu_; }
160
162 std::shared_ptr<MenuItem> addSubItem(std::string label,
163 std::function<void()> onTrigger = nullptr,
164 std::string shortcut = "");
165
167 std::shared_ptr<MenuSeparator> addSubSeparator();
168
170 void trigger();
171
172 std::shared_ptr<Text> labelNode() const { return labelNode_; }
173 std::shared_ptr<Text> shortcutNode() const { return shortcutNode_; }
174
176 std::function<void()> onTriggered;
177
179 std::function<void(bool)> onToggle;
180
181 Layout *contentTarget() override { return submenuStaging_ ? submenuStaging_.get() : this; }
182
183 protected:
184 void onBuild() override;
185
186 private:
187 friend class Menu;
188
189 void setHighlighted(bool active);
190 void syncRowVisuals();
191
192 std::string label_;
193 std::string shortcut_;
194 std::string value_;
195 bool checkable_{false};
196 bool highlighted_{false};
197
198 std::shared_ptr<Text> checkNode_;
199 std::shared_ptr<Text> labelNode_;
200 std::shared_ptr<Text> shortcutNode_;
201 std::shared_ptr<Text> arrowNode_;
202 std::shared_ptr<Layout> submenuStaging_;
203 std::shared_ptr<Menu> submenu_;
204 std::weak_ptr<Menu> ownerMenu_;
205 };
206
215 class Menu : public TLayout<Menu, Popup>
216 {
217 public:
218 explicit Menu(std::string title = "");
219
221 static std::shared_ptr<Menu> fromEntries(std::vector<MenuEntry> entries,
222 std::string title = "");
223
224 const std::string &getTitle() const noexcept { return title_; }
225 void setTitle(std::string title);
226
228 void populateEntries(std::vector<MenuEntry> entries);
229
231 std::shared_ptr<MenuItem> addItem(std::string label,
232 std::function<void()> onTrigger = nullptr,
233 std::string shortcut = "",
234 std::string value = "");
235
237 std::shared_ptr<MenuItem> addCheckItem(std::string label,
238 bool checked,
239 std::function<void(bool)> onToggle = nullptr,
240 std::string shortcut = "",
241 std::string value = "");
242
244 std::shared_ptr<MenuSeparator> addSeparator();
245
247 std::shared_ptr<MenuItem> addSubmenu(std::string label,
248 std::vector<MenuEntry> entries = {});
249
251 void addMenuChild(std::shared_ptr<Layout> child);
252
255
257 std::vector<std::shared_ptr<MenuItem>> items() const;
258
260 int getHighlightedIndex() const noexcept { return highlightedIndex_; }
261
263 std::shared_ptr<Menu> activeSubmenu() const { return activeSubmenu_.lock(); }
264
265 void openBelow(std::shared_ptr<Layout> anchor);
266 void openAbove(std::shared_ptr<Layout> anchor);
267 void openAt(Vec2 screenPos);
268 void close();
269
272
274 std::function<void(std::shared_ptr<MenuItem>)> onItemTriggered;
275
276 Layout *contentTarget() override { return itemsHost_ ? itemsHost_.get() : this; }
277 Vec2 measure(float parent_w, float parent_h) override;
278
279 protected:
280 void onBuild() override;
281 void onUpdate(float dt) override;
282
283 private:
284 friend class MenuItem;
285 friend class MenuBar;
286
287 void syncItems();
288 void setHighlightedIndex(int idx);
289 void openSubmenuFor(const std::shared_ptr<MenuItem> &item, bool highlightFirst);
290 void closeSubmenu();
291 bool hasOpenSubmenu() const;
292 bool isPointInsideOpenSubmenu(Vec2 p) const;
293 void handleItemActivated(const std::shared_ptr<MenuItem> &item, bool invokeOnClick);
294 Menu *rootMenu();
295 int findNextEnabled(const std::vector<std::shared_ptr<MenuItem>> &list, int from, int dir) const;
296 int findEdgeEnabled(const std::vector<std::shared_ptr<MenuItem>> &list, bool first) const;
297
298 std::string title_;
299 std::shared_ptr<Layout> itemsHost_;
300 std::weak_ptr<Menu> parentMenu_;
301 std::weak_ptr<MenuBar> ownerBar_;
302 std::weak_ptr<Menu> activeSubmenu_;
303 std::weak_ptr<MenuItem> activeSubmenuOwner_;
304
305 int highlightedIndex_{-1};
306 bool wasOpen_{false};
307 void markKeyConsumed();
308 bool childConsumedKeyThisFrame_{false};
309 };
310
318 class MenuBar : public TLayout<MenuBar>
319 {
320 public:
321 MenuBar();
322
324 std::shared_ptr<Menu> addMenu(std::string title);
325
327 std::shared_ptr<Menu> addMenu(std::string title, std::vector<MenuEntry> entries);
328
330 void addMenu(std::shared_ptr<Menu> menu);
331
333 bool removeMenu(int index);
334
337
338 int menuCount() const;
339 std::shared_ptr<Menu> menuAt(int index) const;
340 std::shared_ptr<Button> menuButton(int index);
341
343 void openMenu(int index, bool highlightFirst = false);
344
347
348 int getOpenMenuIndex() const noexcept { return openIndex_; }
349 bool isMenuOpen() const noexcept { return openIndex_ >= 0; }
350
351 std::function<void(int)> onMenuOpened;
352 std::function<void(int)> onMenuClosed;
353
354 Vec2 measure(float parent_w, float parent_h) override;
355
356 protected:
357 void onUpdate(float dt) override;
358
359 private:
360 friend class Menu;
361
362 struct BarEntry
363 {
364 std::weak_ptr<Menu> menu;
365 std::shared_ptr<Button> button;
366 };
367
368 std::vector<std::shared_ptr<Menu>> collectMenus() const;
369 void syncMenus();
370 void applyButtonStates();
371 void switchAdjacentMenu(int dir);
372
373 std::vector<BarEntry> entries_;
374 int openIndex_{-1};
375 int suppressReopenIdx_{-1};
376 bool keyConsumedThisFrame_{false};
377 };
378
384 std::shared_ptr<Menu> ContextMenu(std::vector<MenuEntry> entries);
385
387} // 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.
An anchored floating panel with close-on-outside and close-on-Escape.
A read-only text node backed by UIContext::textEngine.
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
A checkbox with a custom-painted check mark.
Definition Checkbox.hpp:29
Horizontal application menu bar managing top-level Menu dropdowns.
Definition MenuBar.hpp:319
void closeMenus()
Close whichever menu is currently open.
void addMenu(std::shared_ptr< Menu > menu)
Attach an existing Menu to this bar.
void onUpdate(float dt) override
Called every frame from update, after input dispatch.
Vec2 measure(float parent_w, float parent_h) override
Compute the intrinsic size given available space.
void openMenu(int index, bool highlightFirst=false)
Open the menu at index (closing any other open menu).
std::shared_ptr< Menu > addMenu(std::string title)
Create, attach, and return a top-level Menu.
std::shared_ptr< Menu > addMenu(std::string title, std::vector< MenuEntry > entries)
Create, populate, attach, and return a top-level Menu.
bool removeMenu(int index)
Remove the menu at index.
void clearMenus()
Remove all menus and trigger buttons.
void trigger()
Programmatically trigger this item (fires callbacks and closes menu hierarchy).
Layout * contentTarget() override
Return the node that receives children when built from markup.
Definition MenuBar.hpp:181
void setItemChecked(bool checked, bool notify=false)
Set checked state (auto-enables checkable mode) and optionally notify onToggle.
void onBuild() override
Hook: called once after create() finishes constructing the object.
std::function< void()> onTriggered
Fired when triggered by click, keyboard, or trigger().
Definition MenuBar.hpp:176
std::shared_ptr< Menu > ensureSubmenu()
Access or lazily create the child submenu Menu.
std::function< void(bool)> onToggle
Fired when a checkable item changes its checked state.
Definition MenuBar.hpp:179
std::shared_ptr< MenuItem > addSubItem(std::string label, std::function< void()> onTrigger=nullptr, std::string shortcut="")
Convenience: add a nested action item under this item's submenu.
std::shared_ptr< Menu > submenu() const
Return the child submenu Menu, or nullptr if none exists.
Definition MenuBar.hpp:159
std::shared_ptr< MenuSeparator > addSubSeparator()
Convenience: add a separator under this item's submenu.
bool hasSubmenu() const
true if this item owns nested submenu entries.
Structured floating menu built on Popup, supporting separators, shortcuts, checkable items,...
Definition MenuBar.hpp:216
std::shared_ptr< MenuItem > addItem(std::string label, std::function< void()> onTrigger=nullptr, std::string shortcut="", std::string value="")
Append an action MenuItem and return it.
std::vector< std::shared_ptr< MenuItem > > items() const
Direct MenuItem children (excluding separators).
void populateEntries(std::vector< MenuEntry > entries)
Populate/append entries from MenuEntry descriptors.
std::shared_ptr< Menu > activeSubmenu() const
Currently open child submenu, or nullptr.
Definition MenuBar.hpp:263
int getHighlightedIndex() const noexcept
Currently highlighted item index among items(), or -1.
Definition MenuBar.hpp:260
std::shared_ptr< MenuSeparator > addSeparator()
Append a MenuSeparator and return it.
std::shared_ptr< MenuItem > addSubmenu(std::string label, std::vector< MenuEntry > entries={})
Append a submenu MenuItem and return it.
void onBuild() override
Hook: called once after create() finishes constructing the object.
void clearItems()
Remove all items and close any active submenu.
void closeHierarchy()
Close this menu and walk up to close the root menu and MenuBar state.
std::function< void(std::shared_ptr< MenuItem >)> onItemTriggered
Fired when any leaf item in this menu (or its submenus) is triggered.
Definition MenuBar.hpp:274
std::shared_ptr< MenuItem > addCheckItem(std::string label, bool checked, std::function< void(bool)> onToggle=nullptr, std::string shortcut="", std::string value="")
Append a checkable MenuItem and return it.
static std::shared_ptr< Menu > fromEntries(std::vector< MenuEntry > entries, std::string title="")
Build a Menu populated from declarative MenuEntry descriptors.
void addMenuChild(std::shared_ptr< Layout > child)
Append an existing MenuItem or MenuSeparator.
std::shared_ptr< Menu > ContextMenu(std::vector< MenuEntry > entries)
Convenience factory: create a structured context Menu from MenuEntry items.
Declarative C++ descriptor for populating a Menu or structured ContextMenu.
Definition MenuBar.hpp:35
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25