ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
VirtualView.hpp
Go to the documentation of this file.
1#pragma once
2
32
33#include "Common.hpp"
34#include "Layout.hpp"
35#include "ScrollView.hpp"
36
37#include <functional>
38
39namespace ZenitUI::UI
40{
44
54 class VirtualView : public TLayout<VirtualView, ScrollView>
55 {
56 public:
58 using ItemFactory = std::function<std::shared_ptr<Layout>(int index)>;
59
67 VirtualView(ItemFactory factory, int itemCount);
68
75 static std::shared_ptr<VirtualView> create(ItemFactory factory, int count);
76
83 static std::shared_ptr<VirtualView> create(
84 std::function<std::shared_ptr<Layout>()> factory,
85 int count);
86
89 {
90 itemFactory = std::move(factory);
92 cumulativeHeights.clear();
93 lastMeasuredWidth = -1.0f;
94 }
95
97 int getItemCount() const noexcept { return itemCount; }
98
105 void setItemCount(int count);
106
111 float getUniformHeight() const noexcept { return uniformHeight; }
112
120 void setUniformHeight(float px);
121
127
130
138 void scrollToItem(int index);
139
142 void removeAllChildren() override;
143
144 Vec2 measure(float parent_w, float parent_h) override;
145
153 std::function<void(Layout *, int)> onItemCreated;
154
155 protected:
156 void onBuild() override;
157 void arrange(Rect space) override;
158 Vec2 computeIntrinsicSize(float availW, float availH) override;
159
160 private:
161 ItemFactory itemFactory;
162 int itemCount{0};
163 float uniformHeight{0.0f};
164 float lastParentW_{0.0f};
165 float lastParentH_{0.0f};
166
167 std::vector<float> cumulativeHeights; // size = itemCount + 1
168 float lastMeasuredWidth{-1.0f};
169
171 struct Entry
172 {
173 std::shared_ptr<Layout> node;
174 int index{-1};
175 float naturalWidth{0.0f};
176 float appliedWidth{-1.0f};
177 };
178 std::vector<Entry> materialized;
179
180 void rebuildCumulative(float availW);
181 int itemAtY(float y) const;
182 bool updateMaterialization();
183 bool hasMaterialized(int index) const;
184 bool addMaterialized(int index);
185 };
186
187} // 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
static std::shared_ptr< VirtualView > create(ItemFactory factory, int count)
Construct with an indexed factory.
void setItemFactory(ItemFactory factory)
Replace the item factory (useful when instantiating VirtualView from ZMarkup).
Definition VirtualView.hpp:88
VirtualView(ItemFactory factory, int itemCount)
Construct with a factory and an item count.
std::function< std::shared_ptr< Layout >(int index)> ItemFactory
Signature for an item factory: index → node.
Definition VirtualView.hpp:58
void onBuild() override
Hook: called once after create() finishes constructing the object.
int getFirstVisibleItem() const
First item whose range overlaps [scrollY, scrollY+height).
void setItemCount(int count)
Replace the item count.
void removeAllChildren() override
Clear the materialized children and the materialization bookkeeping.
float getUniformHeight() const noexcept
Return the uniform height, or 0 if variable heights are being measured.
Definition VirtualView.hpp:111
void setUniformHeight(float px)
Set a uniform height for all items.
int getItemCount() const noexcept
Number of items in the list.
Definition VirtualView.hpp:97
static std::shared_ptr< VirtualView > create(std::function< std::shared_ptr< Layout >()> factory, int count)
Construct with an index-agnostic factory.
int getLastVisibleItem() const
Last visible item, inclusive.
void scrollToItem(int index)
Scroll so that the given item is the first visible.
std::function< void(Layout *, int)> onItemCreated
Fired each time an item node is materialized.
Definition VirtualView.hpp:153
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25