ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
VirtualList.hpp
Go to the documentation of this file.
1#pragma once
2
14
15#include "Common.hpp"
16#include "Layout.hpp"
17#include "ScrollView.hpp"
18
19#include <deque>
20#include <functional>
21#include <memory>
22#include <string>
23#include <vector>
24
25namespace ZenitUI::ZMarkup
26{
27 struct BuildContext;
28 class Registry;
29 class UINode;
30}
31
32namespace ZenitUI::UI
33{
37
45 class VirtualList : public TLayout<VirtualList, ScrollView>
46 {
47 public:
48 VirtualList(std::string prefabName, int itemCount, float rowHeight = 32.0f);
49 ~VirtualList() override;
50
57 static std::shared_ptr<VirtualList> create(
58 std::string prefabName,
59 int itemCount,
60 float rowHeight = 32.0f);
61
63 void setItemCount(int count);
65 int getItemCount() const noexcept { return itemCount_; }
66
68 void setRowHeight(float px);
70 float getRowHeight() const noexcept { return rowHeight_; }
71
73 void setPrefab(std::string prefabName);
75 const std::string &getPrefab() const noexcept { return prefabName_; }
76
78 void setBufferRows(int rows);
80 int getBufferRows() const noexcept { return bufferRows_; }
81
88 void setRegistry(const ZMarkup::Registry *reg) noexcept { explicitRegistry_ = reg; }
89
91 void refresh();
92
94 void scrollToRow(int index);
95
97 int getFirstVisibleRow() const;
98
100 int getLastVisibleRow() const;
101
103 size_t activeRowCount() const noexcept { return active_.size(); }
104
106 size_t pooledRowCount() const noexcept { return freePool_.size(); }
107
109 void removeAllChildren() override;
110
117 std::function<void(int index, Layout *row)> onBindRow;
118
122 std::function<void(Layout *row)> onRowCreated;
123
127 std::function<void(int index, ZMarkup::UINode &row)> onBindRowNode;
128
132 std::function<void(ZMarkup::UINode &row)> onRowNodeCreated;
133
134 protected:
135 void onBuild() override;
136 void arrange(Rect space) override;
137 Vec2 computeIntrinsicSize(float availW, float availH) override;
138
139 private:
140 struct RowSlot
141 {
142 std::shared_ptr<Layout> node;
143 std::shared_ptr<ZMarkup::BuildContext> buildCtx;
144 int index{-1};
145 };
146
147 const ZMarkup::Registry *resolveRegistry() const;
148 bool updatePool();
149 void bindSlot(RowSlot &slot, int index);
150 void clearAllSlots();
151
152 std::string prefabName_;
153 int itemCount_{0};
154 float rowHeight_{32.0f};
155 int bufferRows_{2};
156 bool needsRebindAll_{false};
157 const ZMarkup::Registry *explicitRegistry_{nullptr};
158
159 std::vector<RowSlot> active_;
160 std::deque<RowSlot> freePool_;
161 };
162
163} // namespace ZenitUI::UI
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
A generic scrolling container.
The base node type: tree, style, layout, input, scroll, render.
Definition Layout.hpp:154
TLayout(Args &&...args)
Definition Layout.hpp:1401
void onBuild() override
Hook: called once after create() finishes constructing the object.
std::function< void(int index, Layout *row)> onBindRow
Called whenever a row is (re)bound to index.
Definition VirtualList.hpp:117
size_t activeRowCount() const noexcept
Number of currently active (mounted) row nodes.
Definition VirtualList.hpp:103
void setBufferRows(int rows)
Set number of overscan rows buffered above and below the viewport (default: 2).
void setRegistry(const ZMarkup::Registry *reg) noexcept
Optionally pin an explicit ZMarkup::Registry for prefab instantiation.
Definition VirtualList.hpp:88
int getLastVisibleRow() const
Last visible row index (inclusive), or -1 if empty or not yet arranged.
void scrollToRow(int index)
Scroll so that index sits at the top of the viewport (clamped to valid range).
std::function< void(int index, ZMarkup::UINode &row)> onBindRowNode
Optional ZMarkup::UINode counterpart to onBindRow for @ref-based helpers.
Definition VirtualList.hpp:127
void refresh()
Force re-binding of all currently active rows on the next arrange pass.
int getItemCount() const noexcept
Current item count.
Definition VirtualList.hpp:65
void setPrefab(std::string prefabName)
Replace the prefab tag name, clearing any pooled/active rows.
float getRowHeight() const noexcept
Current fixed row height in pixels.
Definition VirtualList.hpp:70
std::function< void(ZMarkup::UINode &row)> onRowNodeCreated
Optional ZMarkup::UINode counterpart to onRowCreated.
Definition VirtualList.hpp:132
void removeAllChildren() override
Clear active rows, the recycle pool, and base children.
static std::shared_ptr< VirtualList > create(std::string prefabName, int itemCount, float rowHeight=32.0f)
Create a VirtualList backed by a registered ZMarkup prefab.
std::function< void(Layout *row)> onRowCreated
Called once per pooled row immediately after prefab instantiation.
Definition VirtualList.hpp:122
void setItemCount(int count)
Set total item count (clamped to >= 0).
void setRowHeight(float px)
Set fixed row height in pixels (clamped to >= 1.0f).
const std::string & getPrefab() const noexcept
Current prefab tag name.
Definition VirtualList.hpp:75
int getFirstVisibleRow() const
First visible row index, or -1 if empty or not yet arranged.
size_t pooledRowCount() const noexcept
Number of parked row nodes waiting in the FIFO recycle pool.
Definition VirtualList.hpp:106
int getBufferRows() const noexcept
Current overscan buffer size in rows.
Definition VirtualList.hpp:80
Tag → factory map plus the prefab table (either per-UIContext or standalone/global).
Definition ZMarkup.hpp:215
Root handle returned by build / instantiate: root node + build context + wiring helpers.
Definition ZMarkup.hpp:331
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25
Per-build accumulator for #id, @ref, and runtime structural bindings.
Definition ZMarkup.hpp:166