Skip to main content

scroll_list/
scroll_list.rs

1//! A pane with a list longer than it shows. The viewer scrolls the rows
2//! between a title and a line of key hints with its own motion: wheel and
3//! trackpad ticks followed smoothly, a finger's drag and glide, the rubber
4//! band at each end, and pixel motion where the terminal has it. The plugin
5//! paints every row and keeps a selection; it keeps no scroll offset and
6//! counts no wheel ticks.
7//!
8//! Manifest (`bundle/standard-plugin.json`):
9//!
10//! ```json
11//! {
12//!   "apiVersion": 2, "kind": "ui", "id": "scroll-list", "version": "0.1.0",
13//!   "module": "scroll_list.wasm",
14//!   "surfaces": [{ "id": "list", "anchor": "pane", "title": "List", "model": "cells" }],
15//!   "commands": [{ "id": "open", "title": "Open the list", "opens": "list" }],
16//!   "grants": {}
17//! }
18//! ```
19#![no_std]
20
21use standard_plugin::KeyPhase;
22use standard_plugin::prelude::*;
23
24/// The rows fixed at the top: a title.
25const TITLE_ROWS: u32 = 1;
26/// The rows fixed at the bottom: key hints.
27const HINT_ROWS: u32 = 1;
28const ITEMS: u32 = 200;
29
30#[standard_plugin::ui]
31struct ScrollList {
32    list: Surface<Cells>,
33    selected: u32,
34    dirty: bool,
35}
36
37impl ScrollList {
38    /// Paints the whole surface. The viewer picks the rows that show.
39    fn paint(&mut self) {
40        let (_, rows) = self.list.size();
41        if rows == 0 {
42            // No geometry yet: a resize will follow.
43            return;
44        }
45        self.dirty = false;
46        self.list.clear();
47        self.list.text(1, 0, "Items", Style::fg(Colour::FG).bold());
48        for item in 0..ITEMS {
49            let style = if item == self.selected {
50                Style::fg(Colour::ACCENT).bold()
51            } else {
52                Style::fg(Colour::FG)
53            };
54            let row = TITLE_ROWS + item;
55            self.list.text(1, row, &format!("Item {}", item + 1), style);
56        }
57        // The surface's last row, which is the bottom of the pane whenever
58        // the list is shorter than the pane: the surface is never shorter
59        // than where it shows.
60        self.list.text(
61            1,
62            rows - HINT_ROWS,
63            "Up, Down, Page Up and Page Down select",
64            Style::fg(Colour::RECEDE_FG),
65        );
66        let _ = self.list.commit();
67    }
68
69    /// Selects `item` and brings it into view. Rows in view stay put.
70    fn select(&mut self, item: u32) {
71        self.selected = item.min(ITEMS - 1);
72        let row = TITLE_ROWS + self.selected;
73        let _ = self.list.reveal(row, row);
74        self.dirty = true;
75    }
76
77    /// The items one page holds: the rows that scroll and show now.
78    fn page(&self) -> u32 {
79        match self.list.viewport() {
80            Ok(Some(viewport)) => viewport.rows.max(1),
81            _ => 1,
82        }
83    }
84}
85
86impl UiPlugin for ScrollList {
87    fn activate(_cx: &mut Context) -> Self {
88        let list = Surface::new("list");
89        // Call it again when the content's height changes. It stays in force.
90        let _ = list.set_scroll(Some(
91            Scroll::rows(TITLE_ROWS + ITEMS + HINT_ROWS).fixed(TITLE_ROWS, HINT_ROWS),
92        ));
93        Self {
94            list,
95            selected: 0,
96            dirty: true,
97        }
98    }
99
100    fn frame(&mut self, _frame: &Frame) {
101        if self.dirty {
102            self.paint();
103        }
104    }
105
106    fn event(&mut self, event: Event, cx: &mut Context) {
107        match event {
108            // The surface has a new, empty buffer.
109            Event::Resize { .. } => self.dirty = true,
110            Event::Key(key) if key.phase != KeyPhase::Release => match key.code {
111                KeyCode::Up => self.select(self.selected.saturating_sub(1)),
112                KeyCode::Down => self.select(self.selected.saturating_add(1)),
113                KeyCode::PageUp => self.select(self.selected.saturating_sub(self.page())),
114                KeyCode::PageDown => self.select(self.selected.saturating_add(self.page())),
115                KeyCode::Home => self.select(0),
116                KeyCode::End => self.select(ITEMS - 1),
117                _ => return,
118            },
119            // The pointer arrives in the surface's own rows, wherever the
120            // list is scrolled to, so the row names the item.
121            Event::Pointer(pointer) if pointer.kind == PointerKind::Down => {
122                let Some(item) = pointer.row.checked_sub(TITLE_ROWS) else {
123                    return;
124                };
125                if item >= ITEMS {
126                    return;
127                }
128                self.selected = item;
129                self.dirty = true;
130            }
131            _ => return,
132        }
133        cx.request_frame();
134    }
135}