Skip to content

ui::ItemCount

Header: winui/core/ObservableVector.hpp.

How many items an index-based control shows, and the changes that lead from one count to the next. The typed views hand one to their controls, and an app uses one to drive a control by hand. It is either a plain number or ui::item_count() of a ui::ObservableVector or a ui::Property<std::vector<T>>. The second keeps the view in step with the collection, so no change has to be announced by hand.

Example:

ui::ObservableVector<std::string> names{"Ada", "Grace"};

auto list = std::make_unique<ui::VirtualListControl>();
list->set_item_factory([&names](std::size_t i) { return std::make_unique<ui::TextControl>(names[i]); });

ui::ItemCount count = ui::item_count(names);
list->set_item_count(count.initial());
list->add_subscription(count.subscribe(
    [raw = list.get()](const ui::CollectionChange& change) { raw->set_item_count(change.size); }));

names.push_back("Edsger"); // the list shows a third row

Attach

using ui::ItemCount::Attach = Callback<Subscription(Callback<void(const CollectionChange&)>)>

Subscribes a sink to the collection's changes.

ItemCount

ItemCount() = default

No items.

ItemCount

template <std::integral N>
ItemCount(N count)
    requires (!std::same_as<N, bool>)

A fixed number of items; a later change goes through the control's set_item_count(). A negative number is no items.

ItemCount

ItemCount(std::size_t count, Attach attach)

The live form, made by ui::item_count().

initial

std::size_t initial() const noexcept

The number of items when the spec is built.

reactive

bool reactive() const noexcept

True when the count follows a collection.

subscribe

Subscription subscribe(Callback<void(const CollectionChange&)> sink)

Starts following the collection; only valid when reactive().