TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_menu_manager.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
6
7#include <gui_core/gui_id.h>
9
10#include <functional>
11#include <unordered_map>
12#include <vector>
13
14namespace tachyon
15{
18
19 class ElementStorage;
20 class GUIStateManager;
21 class InputManager;
22 class IDManager;
23 class OSWindowManager;
24 class ScrollManagement;
25
29 {
30 public:
31 static constexpr int kMaxDepth = 8;
32
34 struct Level
35 {
36 int depth = -1;
42 bool canScrollUp = false;
43 bool canScrollDown = false;
44 bool isOpen = false;
45 bool reopened = false;
47 bool reserveIconColumn = false;
48 bool itemsHaveIcons = false;
49 std::string filter;
51 bool filtering = false;
52 };
53
55 int currentDepth = -1;
56 int maxOpenDepth = -1;
57 bool requestCloseAll = false;
61
65
68 bool skipActivityCheck = false;
69
70 bool mouseHiddenForMenu = false;
71 int2 lastMousePos = {0, 0};
72 bool focusSubmenuOnOpen = false;
73 bool navigatedThisFrame = false;
74
77 std::unordered_map<ID, bool> menuItemsHaveIcons;
78
83 static constexpr size_t kMaxPopupWindows = 8;
85 {
88 uint64_t lastUse = 0;
89 };
90 std::vector<PopupWindow> popupWindows;
91 uint64_t popupWindowUses = 0;
93 ID PopupWindowFor(ID triggerID, IDManager& idManager);
95 void ReleaseWindowIDs(IDManager& idManager);
96
97 bool buildingMenuBar = false;
99 std::vector<ID> menuBarTriggers;
100
102 bool IsMenuBarTrigger(ID id) const;
103
104
105
108
109
112 void BeginFrame(GUIStateManager& guiStateManager, OSWindowManager& windowManager);
114 void EndFrame(ElementStorage& elementStorage, GUIStateManager& guiStateManager);
115
117 void CloseFromDepth(int fromDepth);
118
120 bool IsOpen() const { return levels[0].isOpen; }
121
123 bool IsBalanced() const { return currentDepth == -1; }
124
127
130 void CloseOnInputOutside(const InputManager& inputManager, const ElementStorage& elementStorage,
131 GUIStateManager& guiStateManager);
132
135 void PrepareLevelWindow(Level& level, ScrollManagement& scrollManagement);
138 void OpenSubmenu(int parentDepth, ID triggerID);
139
141 void CloseAll(GUIStateManager& guiStateManager);
142
145 void CloseAll(GUIStateManager& guiStateManager, OSWindowManager& windowManager);
146
149 void ToggleOrOpenMenu(GUIStateManager& guiStateManager, ID id, bool focusIntoMenu = true);
152 void OpenMenu(GUIStateManager& guiStateManager, ID id, bool focusIntoMenu = true);
153
156 bool ClickMenu(GUIStateManager& guiStateManager, bool isSubmenu, int depth, ID currentItemID, std::function<void()> onClick);
157
159 bool NavigateMenuSibling(GUIStateManager& guiStateManager, ElementStorage& elementStorage, bool isSubmenu, int depth, ID currentItemID, KeyCode key);
160
162 bool NavigateMenuBar(GUIStateManager& guiStateManager, ID currentTriggerID, KeyCode key);
163
165 void EnterMenuFromTrigger(GUIStateManager& guiStateManager, ID triggerID);
166 };
167
170 GUIStateManager& guiStateManager,
171 ElementStorage& elementStorage,
172 MenuManager& menuManager,
173 int depth,
174 std::function<void()> onClick);
175
178 GUIStateManager& guiStateManager,
179 ElementStorage& elementStorage,
180 MenuManager& menuManager,
181 int depth);
182
184} // namespace tachyon
The frame's element tree.
Definition gui_element_storage.h:96
Owns the per-frame state objects and runs their BeginFrame / EndFrame in order.
Definition gui_state_manager.h:65
Makes element IDs: per-frame dynamic / ranged IDs (hashed from the call site or a hint and the parent...
Definition gui_id_manager.h:31
Receives raw OS events, records them, and runs the frame's input pipeline (tab navigation,...
Definition gui_input_manager.h:41
State of the open menus (menu bar dropdowns, context menus, submenus) and their mouse and keyboard be...
Definition gui_menu_manager.h:29
void EnterMenuFromTrigger(GUIStateManager &guiStateManager, ID triggerID)
Focuses the first item of the trigger's dropdown, opening it if needed.
std::vector< PopupWindow > popupWindows
Definition gui_menu_manager.h:90
bool skipActivityCheck
Set when a menu opens: skips the next BeginFrame auto-close (construction hasn't set the triggerIDs y...
Definition gui_menu_manager.h:68
void BeginFrame(GUIStateManager &guiStateManager, OSWindowManager &windowManager)
Unhides the cursor on real mouse movement and closes all menus once none has an active item or a focu...
uint64_t popupWindowUses
Definition gui_menu_manager.h:91
void ToggleOrOpenMenu(GUIStateManager &guiStateManager, ID id, bool focusIntoMenu=true)
Closes the root menu of trigger id when it is open, else opens it (see OpenMenu).
void OpenMenu(GUIStateManager &guiStateManager, ID id, bool focusIntoMenu=true)
Opens the root menu of trigger id, replacing any open menu.
std::unordered_map< ID, bool > menuItemsHaveIcons
Per trigger ID: whether the menu had icons last time, so a reopen reserves the icon column on its fir...
Definition gui_menu_manager.h:77
bool focusSubmenuOnOpen
select and focus the first item of the next opened level
Definition gui_menu_manager.h:72
ID PopupWindowFor(ID triggerID, IDManager &idManager)
The popup window ID of triggerID's menu.
void OpenSubmenu(int parentDepth, ID triggerID)
Opens the level below parentDepth for submenu trigger triggerID, focusing its first item.
void CloseFromDepth(int fromDepth)
Closes level fromDepth and every level below it; window IDs are kept for reuse.
void RequestCloseAll()
Closes all menus once construction is back outside them (in the outermost EndMenu).
bool IsOpen() const
True while the root menu is open.
Definition gui_menu_manager.h:120
int currentDepth
Definition gui_menu_manager.h:55
void CloseAll(GUIStateManager &guiStateManager, OSWindowManager &windowManager)
As CloseAll(), and also hides the OS windows at once, for when the frame loop isn't running (such as ...
void PrepareLevelWindow(Level &level, ScrollManagement &scrollManagement)
Call before BeginWindow(level.windowID): a reopened level starts at the top (its window ID,...
static constexpr int kMaxDepth
deepest submenu nesting, root included
Definition gui_menu_manager.h:31
std::vector< ID > menuBarTriggers
Menu-bar triggers in order, registered during construction for Left/Right navigation.
Definition gui_menu_manager.h:99
int suppressedSubmenuTriggerDepth
Depth of a submenu the Left key closed; it doesn't auto-reopen until the parent's activeItemID change...
Definition gui_menu_manager.h:64
bool IsBalanced() const
True iff every BeginMenu was matched by EndMenu.
Definition gui_menu_manager.h:123
bool buildingMenuBar
true while a menu bar is being built
Definition gui_menu_manager.h:97
void CloseOnInputOutside(const InputManager &inputManager, const ElementStorage &elementStorage, GUIStateManager &guiStateManager)
Closes all menus on a press or scroll this frame outside them and the open menu's trigger (which togg...
ID priorFocusID
focus to restore when all menus close
Definition gui_menu_manager.h:60
Level & GetOrCreateNextLevel(IDManager &idManager)
The level below currentDepth, with a window ID assigned on first use.
static constexpr size_t kMaxPopupWindows
The popup windows of the menus used last, one per trigger, so switching between them never shows anot...
Definition gui_menu_manager.h:83
bool ClickMenu(GUIStateManager &guiStateManager, bool isSubmenu, int depth, ID currentItemID, std::function< void()> onClick)
Activates item currentItemID at depth: runs onClick and closes all menus, or for a submenu trigger (i...
bool requestCloseAll
Definition gui_menu_manager.h:57
void EndFrame(ElementStorage &elementStorage, GUIStateManager &guiStateManager)
Updates the scroll indicators from this frame's layout.
int maxOpenDepth
Definition gui_menu_manager.h:56
void CloseAll(GUIStateManager &guiStateManager)
Closes all menus and restores prior focus (the only place that does).
bool mouseHiddenForMenu
set by keyboard nav, cleared on mouse move
Definition gui_menu_manager.h:70
bool NavigateMenuBar(GUIStateManager &guiStateManager, ID currentTriggerID, KeyCode key)
Left/Right between menu-bar triggers; an open dropdown follows. True if handled.
int2 lastMousePos
to detect real mouse movement
Definition gui_menu_manager.h:71
Level levels[kMaxDepth]
Definition gui_menu_manager.h:54
void ReleaseWindowIDs(IDManager &idManager)
Gives back the window IDs its levels and popups were given: at exit.
bool IsMenuBarTrigger(ID id) const
True iff id was registered as a menu-bar trigger this frame.
bool navigatedThisFrame
throttle: at most one Up/Down navigation per frame
Definition gui_menu_manager.h:73
ID justClosedTriggerID
Trigger whose menu closed this frame; its own click doesn't reopen it.
Definition gui_menu_manager.h:59
bool NavigateMenuSibling(GUIStateManager &guiStateManager, ElementStorage &elementStorage, bool isSubmenu, int depth, ID currentItemID, KeyCode key)
Handles Escape, Left/Right or Up/Down key on item currentItemID at depth. True if handled.
Keeps the OS windows (through an IWindowBackend) and their window contexts for the GUI's window eleme...
Definition gui_os_window_manager.h:61
Scroll containers' offsets (pixels), kept across frames by ID behind handles.
Definition gui_scroll_management.h:33
EventHandler MakeSubmenuTriggerHandler(IDManager &idManager, GUIStateManager &guiStateManager, ElementStorage &elementStorage, MenuManager &menuManager, int depth)
Like MakeMenuItemHandler, but Right arrow or activation opens a submenu instead of closing.
EventHandler MakeMenuItemHandler(IDManager &idManager, GUIStateManager &guiStateManager, ElementStorage &elementStorage, MenuManager &menuManager, int depth, std::function< void()> onClick)
Menu item event handler - fires the onClick callback on click/activation.
KeyCode
Keys by what the keyboard layout makes them, not by position.
Definition gui_input_enums.h:106
std::function< bool(ElementEvent &&input)> EventHandler
Element-level input handler used by the immediate-mode dispatcher; returns true when it consumed the ...
Definition gui_input_handler_func.h:35
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
One open menu: the root dropdown or a submenu.
Definition gui_menu_manager.h:35
bool canScrollUp
from last frame's layout
Definition gui_menu_manager.h:42
ID scrollUpID
pre-allocated each frame for stable dynamic IDs
Definition gui_menu_manager.h:40
std::string filter
MenuFilter's text; cleared when the menu closes.
Definition gui_menu_manager.h:49
ID scrollDownID
Definition gui_menu_manager.h:41
int depth
root is 0, each submenu level adds 1
Definition gui_menu_manager.h:36
ID windowID
Definition gui_menu_manager.h:37
bool reserveIconColumn
This frame: reserve the icon slot (from last frame's content).
Definition gui_menu_manager.h:47
ID activeItemID
highlighted item (mouse or keyboard)
Definition gui_menu_manager.h:39
bool itemsHaveIcons
this frame: accumulated as items with icons are added
Definition gui_menu_manager.h:48
bool reopened
closed since last shown: start scrolled to the top
Definition gui_menu_manager.h:45
bool canScrollDown
from last frame's layout
Definition gui_menu_manager.h:43
bool filtering
This frame: MenuFilter holds text, so items not matching it hide.
Definition gui_menu_manager.h:51
ID triggerID
Definition gui_menu_manager.h:38
bool isOpen
Definition gui_menu_manager.h:44
Definition gui_menu_manager.h:85
ID triggerID
Definition gui_menu_manager.h:86
ID windowID
Definition gui_menu_manager.h:87
uint64_t lastUse
Definition gui_menu_manager.h:88
Integer 2D vector.
Definition gui_base_types.h:18