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

A virtualized list. More...

#include <VirtualView.hpp>

Inheritance diagram for ZenitUI::UI::VirtualView:
[legend]

Public Types

using ItemFactory = std::function<std::shared_ptr<Layout>(int index)>
 Signature for an item factory: index → node.

Public Member Functions

 VirtualView (ItemFactory factory, int itemCount)
 Construct with a factory and an item count.
void setItemFactory (ItemFactory factory)
 Replace the item factory (useful when instantiating VirtualView from ZMarkup).
int getItemCount () const noexcept
 Number of items in the list.
void setItemCount (int count)
 Replace the item count.
float getUniformHeight () const noexcept
 Return the uniform height, or 0 if variable heights are being measured.
void setUniformHeight (float px)
 Set a uniform height for all items.
int getFirstVisibleItem () const
 First item whose range overlaps [scrollY, scrollY+height).
int getLastVisibleItem () const
 Last visible item, inclusive.
void scrollToItem (int index)
 Scroll so that the given item is the first visible.
void removeAllChildren () override
 Clear the materialized children and the materialization bookkeeping.
Vec2 measure (float parent_w, float parent_h) override
Public Member Functions inherited from ZenitUI::TLayout< VirtualView, ScrollView >
 TLayout (Args &&...args)
 Forward constructor arguments to Base.
std::shared_ptr< VirtualView > cls (const std::string &name)
 Fluent addClass; returns typed shared_ptr<Derived>.
std::shared_ptr< VirtualView > id (const std::string &node_id)
 Fluent setId; returns typed shared_ptr<Derived>.
std::shared_ptr< VirtualView > withRef (const std::string &r)
 Fluent setRef; returns typed shared_ptr<Derived>.
std::shared_ptr< VirtualView > size (Value w, Value h)
 Fluent setSize; returns typed shared_ptr<Derived>.
std::shared_ptr< VirtualView > passThrough (bool p=true)
 Fluent setPassThrough; returns typed shared_ptr<Derived>.
std::shared_ptr< VirtualView > with (std::shared_ptr< Layout > c)
 Fluent addChild; returns typed shared_ptr<Derived>.
std::shared_ptr< VirtualView > self ()
 Return shared_from_this() statically cast to Derived.

Static Public Member Functions

static std::shared_ptr< VirtualView > create (ItemFactory factory, int count)
 Construct with an indexed factory.
static std::shared_ptr< VirtualView > create (std::function< std::shared_ptr< Layout >()> factory, int count)
 Construct with an index-agnostic factory.
Static Public Member Functions inherited from ZenitUI::TLayout< VirtualView, ScrollView >
static std::shared_ptr< VirtualView > create (Args &&...args)
 Construct a shared_ptr<Derived> and call onBuild().

Public Attributes

std::function< void(Layout *, int)> onItemCreated
 Fired each time an item node is materialized.

Protected Member Functions

void onBuild () override
 Hook: called once after create() finishes constructing the object.
void arrange (Rect space) override
Vec2 computeIntrinsicSize (float availW, float availH) override

Detailed Description

A virtualized list.

Style tag: "VirtualView". Extends ScrollView (Vertical). Item nodes are added as absolutely-positioned children of the view; the view itself does not lay them out — their top and height come from cumulativeHeights at materialization time.

Examples
14_desktop_workspace/main.cpp.

Constructor & Destructor Documentation

◆ VirtualView()

ZenitUI::UI::VirtualView::VirtualView ( ItemFactory factory,
int itemCount )

Construct with a factory and an item count.

Parameters
factoryCalled on demand for each item that enters the overscan window. Should be cheap; if it is expensive, cache externally.
itemCountTotal number of items. Negative is clamped to 0.

Member Function Documentation

◆ create() [1/2]

std::shared_ptr< VirtualView > ZenitUI::UI::VirtualView::create ( ItemFactory factory,
int count )
static

Construct with an indexed factory.

Note
This overload exists to satisfy TLayout::create's forwarding: it is not intended to be called directly. Use create(factory, count) instead.
Examples
00_demo/main.cpp.

◆ create() [2/2]

std::shared_ptr< VirtualView > ZenitUI::UI::VirtualView::create ( std::function< std::shared_ptr< Layout >()> factory,
int count )
static

Construct with an index-agnostic factory.

All items share the same factory (no index dispatch). Useful for placeholder skeletons or uniform rows.

◆ getFirstVisibleItem()

int ZenitUI::UI::VirtualView::getFirstVisibleItem ( ) const

First item whose range overlaps [scrollY, scrollY+height).

Returns
-1 if there are no items or the heights are not built.

◆ getLastVisibleItem()

int ZenitUI::UI::VirtualView::getLastVisibleItem ( ) const

Last visible item, inclusive.

-1 if none.

◆ onBuild()

void ZenitUI::UI::VirtualView::onBuild ( )
overrideprotectedvirtual

Hook: called once after create() finishes constructing the object.

Reimplemented from ZenitUI::TLayout< VirtualView, ScrollView >.

◆ removeAllChildren()

void ZenitUI::UI::VirtualView::removeAllChildren ( )
override

Clear the materialized children and the materialization bookkeeping.

Called automatically by Layout::removeAllChildren.

◆ scrollToItem()

void ZenitUI::UI::VirtualView::scrollToItem ( int index)

Scroll so that the given item is the first visible.

Does not account for the item's height: item index is placed at the top of the viewport. -1 and out-of-range indices are clamped to the nearest valid value.

◆ setItemCount()

void ZenitUI::UI::VirtualView::setItemCount ( int count)

Replace the item count.

Invalidates the cumulative heights; they are rebuilt on the next measure. If the count is unchanged, no-op.

◆ setUniformHeight()

void ZenitUI::UI::VirtualView::setUniformHeight ( float px)

Set a uniform height for all items.

When positive, disables measurement — this makes huge lists cheap (O(1) height computation), at the cost of assuming all items have the same height. 0 restores measurement mode.

Member Data Documentation

◆ onItemCreated

std::function<void(Layout *, int)> ZenitUI::UI::VirtualView::onItemCreated

Fired each time an item node is materialized.

Useful for wiring per-item callbacks that need to see the node identity. Fired again on re-materialization when scrolling back to an item that was previously unmaterialized.


The documentation for this class was generated from the following file: