guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
list.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/list.hpp"
24
25#include <algorithm>
26#include <utility>
27
29{
30 namespace
31 {
38 bool sameItem(const List::Item &left, const List::Item &right)
39 {
40 return left.headline == right.headline
41 && left.supporting == right.supporting
42 && left.leadingIcon == right.leadingIcon
43 && left.trailingIcon == right.trailingIcon
44 && left.leadingImage == right.leadingImage
45 && left.disabled == right.disabled && left.kind == right.kind
46 && left.expanded == right.expanded
47 && std::equal(left.children.begin(), left.children.end(),
48 right.children.begin(), right.children.end(),
49 sameItem);
50 }
51 } // namespace
52
53 bool List::Row::operator==(const Row &other) const noexcept
54 {
55 return entity == other.entity && view == other.view
56 && lead == other.lead && spacer == other.spacer
57 && leadingImage == other.leadingImage
58 && leadingIcon == other.leadingIcon && content == other.content
59 && headline == other.headline && supporting == other.supporting
60 && trailingIcon == other.trailingIcon && children == other.children;
61 }
62
63 List::Variant List::getVariant(void) const noexcept
64 {
65 return _variant;
66 }
67
69 {
70 if (_variant != variant) {
71 _variant = variant;
72 setHasChanged(true);
73 }
74 return *this;
75 }
76
77 const std::vector<List::Item> &List::getItems(void) const noexcept
78 {
79 return _items;
80 }
81
82 List &List::setItems(std::vector<Item> items)
83 {
84 const bool same = std::equal(_items.begin(), _items.end(),
85 items.begin(), items.end(), sameItem);
86
87 _items = std::move(items);
88 if (!same) {
89 setHasChanged(true);
90 }
91 return *this;
92 }
93
94 const List::Item *List::findItem(const Path &path) const noexcept
95 {
96 if (path.empty() || path[0] >= _items.size()) {
97 return nullptr;
98 }
99
100 const Item *item = &_items[path[0]];
101 for (std::size_t depth = 1; depth < path.size(); ++depth) {
102 if (path[depth] >= item->children.size()) {
103 return nullptr;
104 }
105 item = &item->children[path[depth]];
106 }
107 return item;
108 }
109
110 const List::Row *List::findRow(const Path &path) const noexcept
111 {
112 if (path.empty() || path[0] >= _rows.size()) {
113 return nullptr;
114 }
115
116 const Row *row = &_rows[path[0]];
117 for (std::size_t depth = 1; depth < path.size(); ++depth) {
118 if (path[depth] >= row->children.size()) {
119 return nullptr;
120 }
121 row = &row->children[path[depth]];
122 }
123 return row;
124 }
125
126 List &List::setItemDisabled(std::size_t index, bool disabled)
127 {
128 if (index < _items.size() && _items[index].disabled != disabled) {
129 _items[index].disabled = disabled;
130 setHasChanged(true);
131 }
132 return *this;
133 }
134
135 List &List::setItemExpanded(std::size_t index, bool expanded)
136 {
137 if (index < _items.size() && !_items[index].children.empty()
138 && _items[index].expanded != expanded) {
139 _items[index].expanded = expanded;
140 setHasChanged(true);
141 }
142 return *this;
143 }
144
145 const std::vector<List::Row> &List::getRows(void) const noexcept
146 {
147 return _rows;
148 }
149
150 List &List::setRows(const std::vector<Row> &rows)
151 {
152 if (_rows != rows) {
153 _rows = rows;
154 setHasChanged(true);
155 }
156 return *this;
157 }
158
159 bool List::isRowsDirty(void) const noexcept
160 {
161 return _rowsDirty;
162 }
163
165 {
166 if (_rowsDirty != dirty) {
167 _rowsDirty = dirty;
168 setHasChanged(true);
169 }
170 return *this;
171 }
172} // namespace guillaume::components
const Item * findItem(const Path &path) const noexcept
Find an entry from its position in the tree.
Definition list.cpp:94
List & setRows(const std::vector< Row > &rows)
Record the entities built for the entries.
Definition list.cpp:150
Variant getVariant(void) const noexcept
Get the number of text lines a row is meant for.
Definition list.cpp:63
List & setRowsDirty(bool dirty)
Mark the rows as out of date or as up to date.
Definition list.cpp:164
List & setVariant(Variant variant)
Set the number of text lines a row is meant for.
Definition list.cpp:68
List & setItems(std::vector< Item > items)
Replace the entries.
Definition list.cpp:82
Variant
Number of text lines a row is meant for.
Definition list.hpp:66
List & setItemDisabled(std::size_t index, bool disabled)
Change the disabled flag recorded for one top-level entry.
Definition list.cpp:126
bool isRowsDirty(void) const noexcept
Tell whether the rows must be rebuilt from the entries.
Definition list.cpp:159
const std::vector< Row > & getRows(void) const noexcept
Get the entities built for the entries.
Definition list.cpp:145
std::vector< std::size_t > Path
Definition list.hpp:159
const std::vector< Item > & getItems(void) const noexcept
Get the description of every top-level entry.
Definition list.cpp:77
List(void)=default
Create the configuration of an empty single-line list.
const Row * findRow(const Path &path) const noexcept
Find the entities built for an entry from its position in the tree.
Definition list.cpp:110
List & setItemExpanded(std::size_t index, bool expanded)
Record whether one top-level accordion shows its children.
Definition list.cpp:135
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
State. Every class here is plain data attached to an entity.
Description of one list entry, as given by the user.
Definition list.hpp:84
std::vector< Item > children
Definition list.hpp:98
Identifiers of the entities built for one entry.
Definition list.hpp:113
ecs::Entity::Identifier headline
Headline text child.
Definition list.hpp:135
std::vector< Row > children
Definition list.hpp:144
ecs::Entity::Identifier leadingImage
Leading image child.
Definition list.hpp:126
ecs::Entity::Identifier content
Column holding the headline and supporting text.
Definition list.hpp:132
ecs::Entity::Identifier entity
Row root (container, subheader text or divider).
Definition list.hpp:114
ecs::Entity::Identifier view
Pressable row container.
Definition list.hpp:117
ecs::Entity::Identifier supporting
Supporting text child.
Definition list.hpp:138
ecs::Entity::Identifier trailingIcon
Trailing icon child (the chevron of an accordion).
Definition list.hpp:141
bool operator==(const Row &other) const noexcept
Compare two rows.
Definition list.cpp:53
ecs::Entity::Identifier lead
Leading group.
Definition list.hpp:120
ecs::Entity::Identifier leadingIcon
Leading icon child.
Definition list.hpp:129
ecs::Entity::Identifier spacer
Nesting inset of an accordion child.
Definition list.hpp:123