guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
menu.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <cstddef>
26#include <functional>
27#include <string>
28#include <vector>
29
30#include "guillaume/ecs/component.hpp"
31#include "guillaume/ecs/entity.hpp"
32
34{
35
58 class Menu: public ecs::Component
59 {
60 public:
65 enum class Variant { Dropdown, Context, Cascading };
66
70 enum class Mode {
73 };
74
78 enum class AnchorPosition {
81 };
82
86 struct Item {
87 std::string label;
88 std::string leadingIcon;
90 std::string trailingIcon {};
92 std::function<void(void)> onPress {};
93 bool disabled { false };
95 bool dense { false };
97 };
98
102 struct Row {
106 std::vector<ecs::Entity::Identifier>
111 std::vector<ecs::Entity::Identifier>
112 icons {};
113
119 bool operator==(const Row &other) const noexcept;
120 };
121
123 static constexpr float MinWidth = 112.0f;
124
126 static constexpr float MaxWidth = 280.0f;
127
128 private:
129 Variant _variant { Variant::Dropdown };
130 Mode _mode { Mode::Elevated };
131 std::vector<Item> _items {};
132 std::vector<Row> _rows {};
133 bool _rowsDirty { true };
136 };
137
138 AnchorPosition _anchorPosition {
140 };
141
142 public:
147 Menu(void) = default;
148
152 ~Menu(void) = default;
153
158 Variant getVariant(void) const noexcept;
159
167 Menu &setVariant(Variant variant);
168
173 Mode getMode(void) const noexcept;
174
182 Menu &setMode(Mode mode);
183
188 const std::vector<Item> &getItems(void) const noexcept;
189
200 Menu &setItems(std::vector<Item> items);
201
213 Menu &setItemDisabled(std::size_t index, bool disabled);
214
220 const std::vector<Row> &getRows(void) const noexcept;
221
229 Menu &setRows(const std::vector<Row> &rows);
230
236 bool isRowsDirty(void) const noexcept;
237
247 Menu &setRowsDirty(bool dirty);
248
254 ecs::Entity::Identifier getAnchor(void) const noexcept;
255
268
273 AnchorPosition getAnchorPosition(void) const noexcept;
274
283 };
284
285} // namespace guillaume::components
static constexpr float MaxWidth
Largest width of a menu, in dp.
Definition menu.hpp:126
Mode
How the menu surface stands out from what is behind it.
Definition menu.hpp:70
@ Elevated
Level 2 shadow and tonal tint.
Definition menu.hpp:71
ecs::Entity::Identifier getAnchor(void) const noexcept
Get the entity the menu opens next to.
Definition menu.cpp:125
Menu & setItems(std::vector< Item > items)
Replace the items.
Definition menu.cpp:69
Menu & setAnchor(ecs::Entity::Identifier anchor)
Set the entity the menu opens next to.
Definition menu.cpp:130
Menu & setRows(const std::vector< Row > &rows)
Record the entities built for the items.
Definition menu.cpp:102
AnchorPosition
Side of the anchor the menu opens on.
Definition menu.hpp:78
Menu & setAnchorPosition(AnchorPosition position)
Choose the side of the anchor the menu opens on.
Definition menu.cpp:144
bool isRowsDirty(void) const noexcept
Tell whether the rows must be rebuilt from the items.
Definition menu.cpp:111
const std::vector< Row > & getRows(void) const noexcept
Get the entities built for the items.
Definition menu.cpp:97
Menu & setRowsDirty(bool dirty)
Mark the rows as out of date or as up to date.
Definition menu.cpp:116
Menu(void)=default
Create the configuration of an empty, elevated dropdown menu with no anchor.
Variant getVariant(void) const noexcept
Get the kind of menu.
Definition menu.cpp:36
Variant
Kind of menu. It is stored for the application; the menu system styles and places every kind alike.
Definition menu.hpp:65
Menu & setVariant(Variant variant)
Set the kind of menu.
Definition menu.cpp:41
Menu & setMode(Mode mode)
Choose between an elevated and a flat menu surface.
Definition menu.cpp:55
Menu & setItemDisabled(std::size_t index, bool disabled)
Change the disabled flag recorded for one item.
Definition menu.cpp:88
static constexpr float MinWidth
Smallest width of a menu, in dp.
Definition menu.hpp:123
const std::vector< Item > & getItems(void) const noexcept
Get the description of every item.
Definition menu.cpp:64
~Menu(void)=default
Default destructor.
AnchorPosition getAnchorPosition(void) const noexcept
Get the side of the anchor the menu opens on.
Definition menu.cpp:139
Mode getMode(void) const noexcept
Get how the menu surface stands out.
Definition menu.cpp:50
Base class of every component: a piece of state attached to an entity, plus a "changed" flag.
Definition component.hpp:72
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
State. Every class here is plain data attached to an entity.
Description of one menu item, as given by the user.
Definition menu.hpp:86
std::function< void(void)> onPress
Called on press.
Definition menu.hpp:92
std::string label
Text of the item.
Definition menu.hpp:87
Identifiers of the entities built for one item.
Definition menu.hpp:102
ecs::Entity::Identifier view
Pressable container of the row.
Definition menu.hpp:103
std::vector< ecs::Entity::Identifier > icons
Leading and trailing icon children.
Definition menu.hpp:112
std::vector< ecs::Entity::Identifier > content
Children of the row, in layout order.
Definition menu.hpp:107
bool operator==(const Row &other) const noexcept
Compare two rows.
Definition menu.cpp:30
ecs::Entity::Identifier label
Text child that draws the label.
Definition menu.hpp:108