44 Kind kind{Kind::Action};
50 std::function<void()> onTrigger;
51 std::function<void(
bool)> onToggle;
52 std::vector<MenuEntry> children;
54 static MenuEntry action(std::string label,
55 std::function<
void()> onTrigger =
nullptr,
56 std::string shortcut =
"",
58 std::string value =
"")
61 e.kind = Kind::Action;
62 e.label = std::move(label);
63 e.shortcut = std::move(shortcut);
64 e.value = std::move(value);
66 e.onTrigger = std::move(onTrigger);
70 static MenuEntry checkbox(std::string label,
72 std::function<
void(
bool)> onToggle =
nullptr,
73 std::string shortcut =
"",
75 std::string value =
"")
78 e.kind = Kind::Checkbox;
79 e.label = std::move(label);
80 e.shortcut = std::move(shortcut);
81 e.value = std::move(value);
84 e.onToggle = std::move(onToggle);
91 e.kind = Kind::Separator;
95 static MenuEntry submenu(std::string label,
96 std::vector<MenuEntry> items,
98 std::string value =
"")
101 e.kind = Kind::Submenu;
102 e.label = std::move(label);
103 e.value = std::move(value);
105 e.children = std::move(items);
132 explicit MenuItem(std::string label =
"",
133 std::string shortcut =
"",
134 std::string value =
"");
136 const std::string &getLabel()
const noexcept {
return label_; }
137 void setLabel(std::string label);
139 const std::string &getShortcut()
const noexcept {
return shortcut_; }
140 void setShortcut(std::string shortcut);
142 const std::string &getValue()
const noexcept {
return value_; }
143 void setValue(std::string value) { value_ = std::move(value); }
145 void setCheckable(
bool checkable);
146 bool isCheckable()
const noexcept {
return checkable_; }
150 bool isItemChecked()
const {
return getChecked(); }
159 std::shared_ptr<Menu>
submenu()
const {
return submenu_; }
163 std::function<
void()> onTrigger =
nullptr,
164 std::string shortcut =
"");
172 std::shared_ptr<Text> labelNode()
const {
return labelNode_; }
173 std::shared_ptr<Text> shortcutNode()
const {
return shortcutNode_; }
189 void setHighlighted(
bool active);
190 void syncRowVisuals();
193 std::string shortcut_;
195 bool checkable_{
false};
196 bool highlighted_{
false};
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_;
218 explicit Menu(std::string title =
"");
221 static std::shared_ptr<Menu>
fromEntries(std::vector<MenuEntry> entries,
222 std::string title =
"");
224 const std::string &getTitle() const noexcept {
return title_; }
225 void setTitle(std::string title);
231 std::shared_ptr<MenuItem>
addItem(std::string label,
232 std::function<
void()> onTrigger =
nullptr,
233 std::string shortcut =
"",
234 std::string value =
"");
239 std::function<
void(
bool)> onToggle =
nullptr,
240 std::string shortcut =
"",
241 std::string value =
"");
248 std::vector<MenuEntry> entries = {});
257 std::vector<std::shared_ptr<MenuItem>>
items()
const;
263 std::shared_ptr<Menu>
activeSubmenu()
const {
return activeSubmenu_.lock(); }
265 void openBelow(std::shared_ptr<Layout> anchor);
266 void openAbove(std::shared_ptr<Layout> anchor);
267 void openAt(
Vec2 screenPos);
276 Layout *contentTarget()
override {
return itemsHost_ ? itemsHost_.get() :
this; }
277 Vec2 measure(
float parent_w,
float parent_h)
override;
281 void onUpdate(
float dt)
override;
284 friend class MenuItem;
285 friend class MenuBar;
288 void setHighlightedIndex(
int idx);
289 void openSubmenuFor(
const std::shared_ptr<MenuItem> &item,
bool highlightFirst);
291 bool hasOpenSubmenu()
const;
292 bool isPointInsideOpenSubmenu(
Vec2 p)
const;
293 void handleItemActivated(
const std::shared_ptr<MenuItem> &item,
bool invokeOnClick);
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;
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_;
305 int highlightedIndex_{-1};
306 bool wasOpen_{
false};
307 void markKeyConsumed();
308 bool childConsumedKeyThisFrame_{
false};
324 std::shared_ptr<Menu>
addMenu(std::string title);
327 std::shared_ptr<Menu>
addMenu(std::string title, std::vector<MenuEntry> entries);
338 int menuCount()
const;
339 std::shared_ptr<Menu> menuAt(
int index)
const;
340 std::shared_ptr<Button> menuButton(
int index);
343 void openMenu(
int index,
bool highlightFirst =
false);
348 int getOpenMenuIndex() const noexcept {
return openIndex_; }
349 bool isMenuOpen() const noexcept {
return openIndex_ >= 0; }
351 std::function<void(
int)> onMenuOpened;
352 std::function<void(
int)> onMenuClosed;
364 std::weak_ptr<Menu> menu;
365 std::shared_ptr<Button> button;
368 std::vector<std::shared_ptr<Menu>> collectMenus()
const;
370 void applyButtonStates();
371 void switchAdjacentMenu(
int dir);
373 std::vector<BarEntry> entries_;
375 int suppressReopenIdx_{-1};
376 bool keyConsumedThisFrame_{
false};
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
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