ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
VirtualView.hpp File Reference

A virtualized scrolling list with lazy materialization. More...

#include "Common.hpp"
#include "Layout.hpp"
#include "ScrollView.hpp"
#include <functional>

Go to the source code of this file.

Classes

class  ZenitUI::UI::VirtualView
 A virtualized list. More...

Detailed Description

A virtualized scrolling list with lazy materialization.

VirtualView renders only the items currently in (or near) the viewport, so it can present lists with tens of thousands of items at 60 fps. It is a ScrollView subclass (Vertical flow), so it inherits scrolling, clipping and scrollbar rendering from the base.

Three data structures drive the virtualization:

  • Cumulative heights (cumulativeHeights): prefix-sum of item heights, so that "which item is at y?" is an upper_bound lookup. Length is itemCount + 1; the last entry is the total content height. Invalidated by setItemCount, setUniformHeight, and by width changes.
  • Materialized (materialized): the actual child Layout nodes, one per visible (plus a small overscan) item. Each entry stores both the node and its index, so insertion / removal is O(visible), not O(n).
  • Uniform height (uniformHeight): optional optimization. When set to a positive value, rebuildCumulative skips measuring — the full prefix sum is i * uniformHeight. This makes 100k-item lists O(1) per frame for height computation, at the cost of not supporting variable-height items.

The public API (getFirstVisibleItem, getLastVisibleItem, scrollToItem, setItemCount, setUniformHeight) uses the cumulative heights, which must have been built at least once. Before the first measure, they are empty and those methods return -1 / no-op.