# Lists and virtual lists
Lists are where UI engines get slow: rebuilding every row when one changes, or building
100,000 rows to show twenty. RemedyUI's lists are keyed, so a change touches only the rows it
changes, and virtual, so only the rows near the viewport exist.
| A short list whose rows the app arranges itself | [`widgets::for_each`](crate::widgets::for_each) |
| A list people pick from with the pointer or arrow keys | [`widgets::list`](crate::widgets::list) |
| Many rows, or rows of unknown count | [`widgets::virtual_list`](crate::widgets::virtual_list) |
| Columns, sorting, editing cells | [`data::table`](crate::data::table) (feature `data`) |
| Folders and other nested items | [`data::tree_view`](crate::data::tree_view) |
| Icons or pictures in a grid | [`data::grid_view`](crate::data::grid_view) |
| Messages, newest at the bottom | [`data::chat_list`](crate::data::chat_list) |
`for_each`, `list` and `virtual_list` take the items as a `Signal<Vec<T>>` or a
[`SignalVec`](crate::reactive::SignalVec) (below), and follow it; the `data` views take a
`Signal<Vec<T>>`.
## Keyed children
[`widgets::for_each`](crate::widgets::for_each) keeps a parent's children in step with a list:
one element per key, built once. When the list changes, rows whose key stays keep their
element and whatever state it holds (a half-typed reply, a focus, a scroll position); rows
whose key goes are removed, and new keys are built. When the order changes, elements move.
```
use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::widgets as w;
use std::cell::Cell;
use std::rc::Rc;
#[derive(Clone)]
struct Task {
id: u32,
title: String,
}
let tasks = Signal::new(vec![
Task { id: 1, title: "Water the plants".into() },
Task { id: 2, title: "Call the bank".into() },
]);
let built = Rc::new(Cell::new(0));
let b = built.clone();
tasks.update(|t| t.insert(0, Task { id: 3, title: "Buy bread".into() }));
app.frames(1);
assert_eq!(built.get(), 3);
let names: Vec<String> = ui.reader_view().into_iter().filter_map(|n| n.name).collect();
assert_eq!(names, ["Buy bread", "Water the plants", "Call the bank"]);
```
The key must be stable and unique: an id, never the position. A row's element is built from
the item it first had; if an item's content changes under the same key, have the row read it
from a signal, or change the key.
## Selectable lists
[`widgets::list`](crate::widgets::list) adds selection on top: clicking a row or moving with
the arrow keys selects it, and the chosen key is in a `Signal<Option<K>>` the app owns.
Pressing a selected row again, or Enter, calls `on_open`. Screen readers hear a list named
by its label, and each row by what it shows, its place in the list ("2 of 3") and whether it
is selected: a list box of options (ARIA `listbox`, `option`).
```
use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::a11y::{Action, Role};
use remedy_ui::widgets as w;
let rooms = Signal::new(vec!["General".to_string(), "Music".to_string(), "Games".to_string()]);
let selected = Signal::new(None::<String>);
let opened = Signal::new(None::<String>);
let reader = ui.reader_view();
let Some(row) = reader.iter().find(|n| n.role == Role::Option && n.name.as_deref() == Some("Music")) else {
unreachable!("listed")
};
assert_eq!(row.position, Some((2, 3)), "second of three");
assert!(ui.accessibility_action(row.id, Action::Click));
app.frames(1);
assert_eq!(selected.get().as_deref(), Some("Music"));
// Clicking the selected row again opens it.
assert!(ui.accessibility_action(row.id, Action::Click));
assert_eq!(opened.get().as_deref(), Some("Music"));
```
## Virtual lists
[`widgets::virtual_list`](crate::widgets::virtual_list) builds only the rows near the
viewport, so its cost depends on the window's height, not the list's length. Rows may be any
height: `estimate` places rows not measured yet, and each row's real height replaces its
estimate once shown, keeping the scroll position anchored to what you were looking at.
Scrolling runs on the compositor; the UI thread only builds rows as they come into range.
```
use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::widgets as w;
let lines: Signal<Vec<String>> = Signal::new((1..=100_000).map(|i| format!("Line {i}")).collect());
});
let Some(ui) = app.ui() else { unreachable!("the window is open") };
app.frames(2);
```
The build closure gets each row's index and item. Keep rows cheap to build: they are built
again when they scroll back into range.
## Tables, trees and grids
The `data` feature ([`data`](crate::data)) has the heavier views, all virtual at any size:
- [`data::table`](crate::data::table): columns ([`data::Column`](crate::data::Column)) that
sort, resize and move, selection with the keyboard, cells edited in place, and a
[`data::TableState`](crate::data::TableState) holding what the person changed. Sorting
large tables runs off the UI thread.
- [`data::tree_view`](crate::data::tree_view): nested items loaded as they open, with drag
and drop.
- [`data::grid_view`](crate::data::grid_view): icons or pictures in rows that reflow with the
width. `Tiles::fluid(180.0, 1.5, 76.0, draw)` makes a poster grid like CSS
`repeat(auto-fill, minmax(180px, 1fr))`: as many columns as fit at 180 px, stretched to the
width, each tile 1.5 times its width tall plus 76 px for a caption.
[`data::grid_view_with`](crate::data::grid_view_with) takes `GridOptions`: `own_scroll:
false` puts the grid in the page's scroll (it grows to all its rows and builds only those
the window shows), and `interactive_tiles: true` keeps each card's buttons for the keyboard
and screen readers (Enter moves into the cursor's card, the arrows between its buttons,
Escape back to the grid) instead of reading the card as one item. Outside a virtual grid,
the layout's own `Track::Repeat(Repeat::AutoFill, ..)` does the same with grid tracks.
- [`data::property_grid`](crate::data::property_grid): names and values in groups, as in an
inspector.
- [`data::chat_list`](crate::data::chat_list): messages grouped by day, kept at the bottom
as new ones arrive, older ones loaded at the top without moving what you are reading. Like
the other lists, it takes a `Signal<Vec<T>>` or a [`SignalVec`](crate::reactive::SignalVec)
(push new messages, insert older ones at the front).
The file explorer and chat feed recipes use them ([`crate::guide::recipes`]).
## Lists that change in place
A `Signal<Vec<T>>` hands out the whole list when it changes, and the keyed views compare
keys to find what moved. For data that changes often in small steps (a chat, a log, a
queue), a [`SignalVec`](crate::reactive::SignalVec) records each change as an operation
(push, insert, remove, move, set). Give one to `for_each`, `list` or `virtual_list` and they
apply the operations as they come: a push builds one row, a removal or a move builds nothing,
and nothing is compared.
```
use remedy_ui::reactive::SignalVec;
use remedy_ui::testing::TestApp;
use remedy_ui::widgets as w;
use std::cell::Cell;
use std::rc::Rc;
let log = SignalVec::new(vec!["Started".to_string()]);
let built = Rc::new(Cell::new(0));
let b = built.clone();
let Ok(reader) = queue.reader() else { unreachable!("on the UI thread") };
let _ = reader.take_diffs(); // the first read is the whole list
let _ = queue.push("c.mp3".to_string());
let _ = queue.move_item(2, 0);
let Ok(diffs) = reader.take_diffs() else { unreachable!("on the UI thread") };
assert!(matches!(diffs.as_slice(), [VecDiff::Push(_), VecDiff::Move { from: 2, to: 0 }]));
```
## Rows that arrive, leave and move
[`widgets::for_each_with`](crate::widgets::for_each_with) is `for_each` with
[`ListTransitions`](crate::widgets::ListTransitions): rows that arrive play an `enter`
transition, rows that leave play an `exit` one (staying, inert, until it ends), and rows
that stay glide to their new places (`moves`). All of it is instant under reduced motion.
```
use remedy_ui::motion::{Pace, Transition};
use remedy_ui::reactive::Signal;
use remedy_ui::testing::TestApp;
use remedy_ui::widgets::{self as w, ListTransitions};
let tasks = Signal::new(vec!["Write".to_string(), "Review".to_string(), "Ship".to_string()]);