guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
menu.cpp
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#include "guillaume/components/menu.hpp"
24
25#include <algorithm>
26#include <utility>
27
29{
30 bool Menu::Row::operator==(const Row &other) const noexcept
31 {
32 return view == other.view && content == other.content
33 && label == other.label && icons == other.icons;
34 }
35
36 Menu::Variant Menu::getVariant(void) const noexcept
37 {
38 return _variant;
39 }
40
42 {
43 if (_variant != variant) {
44 _variant = variant;
45 setHasChanged(true);
46 }
47 return *this;
48 }
49
50 Menu::Mode Menu::getMode(void) const noexcept
51 {
52 return _mode;
53 }
54
56 {
57 if (_mode != mode) {
58 _mode = mode;
59 setHasChanged(true);
60 }
61 return *this;
62 }
63
64 const std::vector<Menu::Item> &Menu::getItems(void) const noexcept
65 {
66 return _items;
67 }
68
69 Menu &Menu::setItems(std::vector<Item> items)
70 {
71 const bool same =
72 std::equal(_items.begin(), _items.end(), items.begin(), items.end(),
73 [](const Item &left, const Item &right) {
74 return left.label == right.label
75 && left.leadingIcon == right.leadingIcon
76 && left.trailingIcon == right.trailingIcon
77 && left.disabled == right.disabled
78 && left.dense == right.dense;
79 });
80
81 _items = std::move(items);
82 if (!same) {
83 setHasChanged(true);
84 }
85 return *this;
86 }
87
88 Menu &Menu::setItemDisabled(std::size_t index, bool disabled)
89 {
90 if (index < _items.size() && _items[index].disabled != disabled) {
91 _items[index].disabled = disabled;
92 setHasChanged(true);
93 }
94 return *this;
95 }
96
97 const std::vector<Menu::Row> &Menu::getRows(void) const noexcept
98 {
99 return _rows;
100 }
101
102 Menu &Menu::setRows(const std::vector<Row> &rows)
103 {
104 if (_rows != rows) {
105 _rows = rows;
106 setHasChanged(true);
107 }
108 return *this;
109 }
110
111 bool Menu::isRowsDirty(void) const noexcept
112 {
113 return _rowsDirty;
114 }
115
117 {
118 if (_rowsDirty != dirty) {
119 _rowsDirty = dirty;
120 setHasChanged(true);
121 }
122 return *this;
123 }
124
126 {
127 return _anchor;
128 }
129
131 {
132 if (_anchor != anchor) {
133 _anchor = anchor;
134 setHasChanged(true);
135 }
136 return *this;
137 }
138
140 {
141 return _anchorPosition;
142 }
143
145 {
146 if (_anchorPosition != position) {
147 _anchorPosition = position;
148 setHasChanged(true);
149 }
150 return *this;
151 }
152} // namespace guillaume::components
Mode
How the menu surface stands out from what is behind it.
Definition menu.hpp:70
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
const std::vector< Item > & getItems(void) const noexcept
Get the description of every item.
Definition menu.cpp:64
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
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
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
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
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