Skip to main content

Module virtual_list

Module virtual_list 

Source
Expand description

A virtual list whose rows are as tall as their content.

A feed is long, grows at both ends, and no two notes are the same height. iced’s virtual List only renders the rows in view, but to know where they sit it builds and lays out every item whenever its content is new, and draws nothing until it has.

VirtualList never measures a row before it is in view. A row that has been shown has its real height; one that hasn’t has the height the caller estimates for its item. It only ever builds the rows in the viewport, so showing a feed costs the same whatever its length.

flowchart TB
    subgraph list["A list of 500 notes, scrolled some way down"]
        above["Rows above: measured when they went by. Not built"]
        subgraph viewport["The viewport"]
            r1["A row: built, laid out, measured"]
            r2["A row: built, laid out, measured"]
            r3["A row: built, laid out, measured"]
        end
        below["Rows below: never shown. Each as tall as its estimate. Not built"]
        above --- r1 --- r2 --- r3 --- below
    end
    heights[("RowHeights: by note id, in the model")]
    r2 -- "what each row measured" --> heights
    heights -- "where each row starts" --> above

Each frame, the list works out where every row starts from the heights it knows and the estimates for the rest (a sum, with no widget built), finds the first row that reaches into the viewport, and builds rows from there until one starts below it. Each row built is measured, and what it measured is kept. If a row turned out another height than it was laid out with, the list is laid out once more.

Scrolled from the top, everything above the viewport has been measured, so what is on screen never moves: only the scrollbar’s idea of the total changes as estimates below turn into measurements. After a jump over rows never shown (dragging the scrollbar), the rows above are still estimates; scrolling back up into them moves what is on screen by however far each estimate was off, so the closer the estimate, the steadier the list.

The estimate has to depend on the item and on how wide the list is, and on nothing else. Assuming the average of the rows measured so far looks better and is worse: every newly measured row would move the average, and with it every unmeasured row above the viewport, so after a jump the list would land on different rows frame after frame.

The list can say when it has been scrolled near its end, which is when a feed fetches its next page. It says so in answer to any event but a redraw (the pointer moving, a wheel turning), never while the window is being redrawn: a message published during a redraw makes the runtime take the window’s size afresh in the middle of the frame, and if the popup is being resized just then (as it is while it opens), the frame is presented into a buffer of the old size and iced_tiny_skia panics. That rules out the scrollable’s own on_scroll, which reports during redraws, and again each time a measured row changes the list’s height.

A row that is another height at its reader’s asking (a long note shown in full, or cut again) is the one row measured before it is in view: its list is told (RowHeights::measure_anew), and measures it in the layout that follows, so that the list is the right length at once.

Heights are remembered by row key, not index, in a RowHeights the caller owns. Rows added at the front leave the rest measured, and the measurements outlive the widget tree: a popup that is closed and opened again lays its list out exactly as it was.

Where the list is scrolled to outlives the widget tree the same way, but is not the list’s to keep: its scroller may hold more than the list. See ScrollPosition.

Structs§

Measured 🔒
Row 🔒
One row in view: which item it shows, where it sits, and its widget state.
RowHeights
The measured heights of a list’s rows, by row key, at one list width.
State 🔒
VirtualList
A virtual list of rows of any height. See the module docs.

Constants§

SAME_HEIGHT 🔒
How far two heights may differ and still be the same height.

Type Aliases§

Estimate 🔒
How tall the row of an item is assumed to be until it has been in view, in a list as wide as the second argument.