Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
recycler.hpp
Go to the documentation of this file.
1/*
2 Copyright 2021 XITRIX
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#include <functional>
20#include <map>
21#include <vector>
23#include <aurore/core/bind.hpp>
28
29namespace aurore
30{
31
35{
36 size_t section;
37 int row;
38 size_t item;
39
41 : IndexPath(0, 0, 0)
42 {
43 }
44
45 IndexPath(size_t section, size_t row, size_t item)
46 {
47 this->section = section;
48 this->row = row;
49 this->item = item;
50 }
51
52 IndexPath(size_t section, size_t row)
54 {
55 }
56
57 bool operator==(const IndexPath& other)
58 {
59 return section == other.section && row == other.row && item == other.item;
60 }
61};
62
65class RecyclerCell : public Box
66{
67 public:
71
75 IndexPath getIndexPath() const { return indexPath; }
76
81
85 std::string reuseIdentifier;
86
90 virtual void prepareForReuse() { }
91
94
95 void onFocusGained() override;
96 void onFocusLost() override;
97 void onTextScaleChanged() override;
98
99 protected:
104
105 private:
106 void showSeparator(bool show);
107 bool growsWithText = false;
108 bool grown = false;
109 IndexPath indexPath;
111};
112
115 : public RecyclerCell
116{
117 public:
119
121 void setTitle(std::string text);
123 void setSubtitle(std::string text);
124
127
128 private:
129 Header* header;
130};
131
132class RecyclerFrame;
136{
137 public:
141 virtual int numberOfSections(RecyclerFrame* recycler) { return 1; }
142
146 virtual int numberOfRows(RecyclerFrame* recycler, int section) { return 0; }
147
151 virtual RecyclerCell* cellForRow(RecyclerFrame* recycler, IndexPath index) { return nullptr; }
152
156 virtual RecyclerCell* cellForHeader(RecyclerFrame* recycler, int section);
157
162 virtual float heightForRow(RecyclerFrame* recycler, IndexPath index) { return -1; }
163
168 virtual float heightForHeader(RecyclerFrame* recycler, int section);
169
173 virtual std::string titleForHeader(RecyclerFrame* recycler, int section) { return ""; }
174
178 virtual void didSelectRowAt(RecyclerFrame* recycler, IndexPath index) { }
179
180 virtual ~RecyclerDataSource() = default;
181};
182
185{
186 public:
188 View* getNextFocus(FocusDirection direction, View* currentView) override;
189
190 private:
191 RecyclerFrame* recycler;
192};
193
199{
200 public:
204
206 View* getNextCellFocus(FocusDirection direction, View* currentView);
207 void draw(NVGcontext* vg, float x, float y, float width, float height, Style style, FrameContext* ctx) override;
208 void onLayout() override;
209 void onThemeChanged() override;
210 void setPadding(float padding) override;
211 void setPadding(float top, float right, float bottom, float left) override;
212 void setPaddingTop(float top) override;
213 void setPaddingRight(float right) override;
214 void setPaddingBottom(float bottom) override;
215 void setPaddingLeft(float left) override;
216
223 void setDataSource(RecyclerDataSource* source, bool deleteDataSource = true);
224
229
234
238 void registerCell(std::string identifier, std::function<RecyclerCell*(void)> allocation);
239
243 RecyclerCell* dequeueReusableCell(std::string identifier);
244
248 void selectRowAt(IndexPath indexPath, bool animated);
249
255
258 {
259 return this->defaultCellFocus;
260 }
261
264 {
265 this->defaultCellFocus = indexPath;
266 }
267
269 static View* create();
270
271 private:
272 RecyclerDataSource* dataSource = nullptr;
273 bool deleteDataSource = false;
274 bool layouted = false;
275
276 uint32_t visibleMin, visibleMax;
277
278 IndexPath defaultCellFocus;
279
280 float paddingTop = 0;
281 float paddingRight = 0;
282 float paddingBottom = 0;
283 float paddingLeft = 0;
284
285 Box* contentBox;
286 Rect renderedFrame;
287 std::vector<Size> cacheFramesData;
288 std::vector<IndexPath> cacheIndexPathData;
289 std::map<std::string, std::vector<RecyclerCell*>*> queueMap;
290 std::map<std::string, std::function<RecyclerCell*(void)>> allocationMap;
291
292 bool checkWidth();
293 float oldWidth = 0;
294
295 void cacheCellFrames();
296 void cellsRecyclingLoop();
297 void queueReusableCell(RecyclerCell* cell);
298
299 void addCellAt(size_t index, size_t downSide);
300 void removeCell(View* view);
301};
302
303} // namespace aurore
Generic FlexBox layout.
Definition box.hpp:74
CallbacksList::iterator Subscription
Definition event.hpp:45
Definition frame_context.hpp:30
Definition header.hpp:30
Definition recycler.hpp:66
void onTextScaleChanged() override
void onFocusGained() override
IndexPath getIndexPath() const
Definition recycler.hpp:75
void onFocusLost() override
std::string reuseIdentifier
Definition recycler.hpp:85
void setIndexPath(IndexPath value)
RecyclerCell()
An empty cell.
static RecyclerCell * create()
Makes an empty cell (registerCell's default).
virtual void prepareForReuse()
Definition recycler.hpp:90
The box a RecyclerFrame lays its cells in, which asks it where the focus goes.
Definition recycler.hpp:185
RecyclerContentBox(RecyclerFrame *recycler)
View * getNextFocus(FocusDirection direction, View *currentView) override
Definition recycler.hpp:136
virtual std::string titleForHeader(RecyclerFrame *recycler, int section)
Definition recycler.hpp:173
virtual int numberOfRows(RecyclerFrame *recycler, int section)
Definition recycler.hpp:146
virtual RecyclerCell * cellForRow(RecyclerFrame *recycler, IndexPath index)
Definition recycler.hpp:151
virtual ~RecyclerDataSource()=default
virtual int numberOfSections(RecyclerFrame *recycler)
Definition recycler.hpp:141
virtual float heightForRow(RecyclerFrame *recycler, IndexPath index)
Definition recycler.hpp:162
virtual void didSelectRowAt(RecyclerFrame *recycler, IndexPath index)
Definition recycler.hpp:178
virtual float heightForHeader(RecyclerFrame *recycler, int section)
virtual RecyclerCell * cellForHeader(RecyclerFrame *recycler, int section)
Definition recycler.hpp:199
void setPaddingTop(float top) override
void onThemeChanged() override
IndexPath getDefaultCellFocus()
The cell that takes the focus first.
Definition recycler.hpp:257
View * getNextCellFocus(FocusDirection direction, View *currentView)
The cell the focus goes to from currentView (cells off screen included).
RecyclerFrame()
An empty list; give it a data source and register its cells.
void setDataSource(RecyclerDataSource *source, bool deleteDataSource=true)
void setPadding(float top, float right, float bottom, float left) override
float estimatedRowHeight
Definition recycler.hpp:254
void setPaddingRight(float right) override
void onLayout() override
void setPaddingLeft(float left) override
RecyclerDataSource * getDataSource() const
void setDefaultCellFocus(IndexPath indexPath)
See getDefaultCellFocus().
Definition recycler.hpp:263
void draw(NVGcontext *vg, float x, float y, float width, float height, Style style, FrameContext *ctx) override
RecyclerCell * dequeueReusableCell(std::string identifier)
void selectRowAt(IndexPath indexPath, bool animated)
void registerCell(std::string identifier, std::function< RecyclerCell *(void)> allocation)
void setPaddingBottom(float bottom) override
static View * create()
Makes a list for XML (<aurore:RecyclerFrame>).
void setPadding(float padding) override
A section's header in a RecyclerFrame: a title and a subtitle.
Definition recycler.hpp:116
static RecyclerHeader * create()
Makes an empty header.
void setTitle(std::string text)
The header's title.
void setSubtitle(std::string text)
The header's subtitle, on the right.
Definition scrolling_frame.hpp:45
Simple wrapper around StyleValues for the array operator.
Definition style.hpp:115
Definition view.hpp:232
virtual void show(std::function< void(void)> cb)
Definition actions.hpp:25
FocusDirection
Focus direction when navigating.
Definition view.hpp:96
Definition text.hpp:13
Definition recycler.hpp:35
IndexPath()
Definition recycler.hpp:40
IndexPath(size_t section, size_t row, size_t item)
Definition recycler.hpp:45
bool operator==(const IndexPath &other)
Definition recycler.hpp:57
size_t section
Definition recycler.hpp:36
size_t item
Definition recycler.hpp:38
IndexPath(size_t section, size_t row)
Definition recycler.hpp:52
int row
Definition recycler.hpp:37
Definition geometry.hpp:70