Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

323 KB
Raw Blame
//! The page's state: one pane session, the canvas it is drawn on, where
//! the user is looking, what they selected, and who owns the tab.

use crate::fallback::{Capacity, FallbackBudget, Next, MIN_RETRY_MS};
use crate::glyphs::GlyphCache;
use crate::gpu::Gpu;
use crate::host::AppHost;
use crate::platform::{Link, Platform, PointerInput, WheelDelta, WheelInput};
use crate::viewport::{cell_at, max_scroll, visible_rows, visible_rows_px};
use anyhow::Result;
use codec::Pdu;
use std::cell::{Cell, RefCell};
use std::future::Future;
use std::rc::Rc;
use std::sync::Arc;
use termwiz::input::{KeyCode, Modifiers};
use thinkterm_font_core::FontShaper as _;
use thinkterm_i18n::tr;
use thinkterm_font_web::FontSet;
use thinkterm_proto::{PaneId, TabId};
use thinkterm_render::atlas::OutOfTextureSpace;
use thinkterm_render::pipeline::GpuTexture;
use thinkterm_render::quad::HeapQuadAllocator;
use thinkterm_render::vertex::Vertex;
use thinkterm_session::pane::PaneSession;
use thinkterm_plugin_channel::registry as plugins_api;
use thinkterm_snippets::wire as snippets_wire;
use wezterm_term::color::ColorPalette;
use wezterm_term::input::{MouseButton, MouseEvent, MouseEventKind};
use wezterm_term::{KeyModifiers, StableRowIndex, TerminalSize};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Pointer {
    Down,
    Move,
    Up,
}

/// A row of a thumbnail: runs of text with their foreground, as hex,
/// trailing blanks dropped.
#[derive(Debug, Clone, serde::Serialize)]
pub struct PreviewRow {
    pub runs: Vec<PreviewRun>,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct PreviewRun {
    pub text: String,
    pub fg: String,
}

/// A line's cells as runs of one colour: a thumbnail keeps the terminal's
/// colours, which is most of what makes it recognisable at that size.
fn preview_row(line: &termwiz::surface::Line, palette: &ColorPalette) -> PreviewRow {
    use wezterm_term::color::ColorAttribute;
    let mut runs: Vec<PreviewRun> = Vec::new();
    let mut at = 0usize;
    for cell in line.visible_cells() {
        let col = cell.cell_index();
        let attrs = cell.attrs();
        let fg = match attrs.foreground() {
            ColorAttribute::Default => palette.foreground,
            other => palette.resolve_fg(other),
        };
        let hex = fg.to_rgb_string();
        let mut text = String::new();
        while at < col {
            text.push(' ');
            at += 1;
        }
        text.push_str(cell.str());
        at += cell.width().max(1);
        match runs.last_mut() {
            Some(last) if last.fg == hex => last.text.push_str(&text),
            _ => runs.push(PreviewRun { text, fg: hex }),
        }
    }
    // Trailing blanks go: a thumbnail's row ends where its text does.
    while let Some(last) = runs.last_mut() {
        let trimmed = last.text.trim_end().len();
        if trimmed == 0 {
            runs.pop();
        } else {
            last.text.truncate(trimmed);
            break;
        }
    }
    PreviewRow { runs }
}

/// A tab as it is split, for a thumbnail of the whole tab: every pane on
/// show with its screen where it lies, in the tab's cells (`left`, `top`),
/// the dividers in the cells between them; a zoomed pane alone, filling it.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TabPreview {
    pub cols: usize,
    pub rows: usize,
    /// The colours of the pane asked about, for the tab round its panes.
    pub foreground: String,
    pub background: String,
    pub panes: Vec<PanePreview>,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct PanePreview {
    pub left: usize,
    pub top: usize,
    pub screen: ScreenPreview,
}

/// Where a pane lies in its tab, as one listing of the layout has it: its
/// left column and top row there, and its size.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Place {
    pane: PaneId,
    left: usize,
    top: usize,
    cols: usize,
    rows: usize,
}

/// The panes on show in the tab that holds `pane_id`, each where it lies:
/// a stack's pane in front, a zoomed pane alone.
fn tab_places(layout: &codec::ListPanesResponse, pane_id: PaneId) -> Option<Vec<Place>> {
    use thinkterm_proto::layout::{PaneEntry, PaneNode};
    fn shown(node: &PaneNode, out: &mut Vec<PaneEntry>) {
        match node {
            PaneNode::Empty => {}
            PaneNode::Leaf(entry) => out.push(entry.clone()),
            PaneNode::Stack(stack) => out.extend(stack.panes.get(stack.active).or(stack.panes.first()).cloned()),
            PaneNode::Split { left, right, .. } => {
                shown(left, out);
                shown(right, out);
            }
        }
    }
    let tab = layout.tabs.iter().find(|node| crate::attach::contains_pane(node, pane_id))?;
    let mut entries = Vec::new();
    shown(tab, &mut entries);
    let place = |e: &PaneEntry| Place { pane: e.pane_id, left: e.left_col, top: e.top_row, cols: e.size.cols, rows: e.size.rows };
    if let Some(zoomed) = entries.iter().find(|e| e.is_zoomed_pane) {
        return Some(vec![Place { left: 0, top: 0, ..place(zoomed) }]);
    }
    (!entries.is_empty()).then(|| entries.iter().map(place).collect())
}

/// A pane's whole screen, for a thumbnail with the terminal's shape: every
/// row of its viewport at its own size, in its own colours. A run's
/// background is `""` where it is the screen's own (`background`).
#[derive(Debug, Clone, serde::Serialize)]
pub struct ScreenPreview {
    pub cols: usize,
    pub rows: usize,
    pub foreground: String,
    pub background: String,
    pub lines: Vec<Vec<ScreenRun>>,
}

impl ScreenPreview {
    /// `cols` by `rows`, the size its place in the tab was listed at: of a
    /// pane resized since, what lies outside goes (a run across the edge
    /// is the picture's to clip), and rows it lacks are blank.
    fn fit(&mut self, cols: usize, rows: usize) {
        self.lines.resize(rows, Vec::new());
        for runs in &mut self.lines {
            runs.retain(|run| run.col < cols);
        }
        self.cols = cols;
        self.rows = rows;
    }
}

/// Text from column `col` across `width` columns: one wide character, or
/// narrow ones of one foreground and background, so a picture can put
/// every run where the terminal has it whatever its faces' widths.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ScreenRun {
    pub text: String,
    pub fg: String,
    pub bg: String,
    pub col: usize,
    pub width: usize,
    /// Narrow characters, a cell each: another can join them.
    #[serde(skip)]
    narrow: bool,
}

/// A line's cells as runs of one foreground and background, each wide
/// character a run of its own, in the colours the terminal draws them in
/// (`emit`): bold lifting the first eight to their bright twins, a
/// reversed cell, or every cell of a reversed screen, with the two swapped
/// (both, and they are as they were, a default ground left unpainted), a
/// concealed one blank. A background that is the screen's own is left
/// out, and trailing blanks on it go.
fn screen_row(line: &termwiz::surface::Line, palette: &ColorPalette, reversed: bool) -> Vec<ScreenRun> {
    use wezterm_term::color::ColorAttribute;
    let mut runs: Vec<ScreenRun> = Vec::new();
    let mut push = |text: &str, fg: String, bg: String, col: usize, width: usize| match runs.last_mut() {
        Some(last)
            if width == 1 && last.narrow && last.col + last.width == col && last.fg == fg && last.bg == bg =>
        {
            last.text.push_str(text);
            last.width += 1;
        }
        _ => runs.push(ScreenRun { text: text.to_string(), fg, bg, col, width, narrow: width == 1 }),
    };
    // The screen's own colours, as `ScreenPreview` has them.
    let (screen_fg, screen_bg) = if reversed {
        (palette.background, palette.foreground)
    } else {
        (palette.foreground, palette.background)
    };
    let (screen_fg, screen_bg) = (screen_fg.to_rgb_string(), screen_bg.to_rgb_string());
    let mut at = 0usize;
    for cell in line.visible_cells() {
        let col = cell.cell_index();
        for gap in at..col {
            push(" ", screen_fg.clone(), String::new(), gap, 1);
        }
        let attrs = cell.attrs();
        let fg = crate::emit::resolve_fg(attrs, attrs.foreground(), palette);
        let bg = palette.resolve_bg(attrs.background());
        let swapped = attrs.reverse() != reversed;
        let unpainted = !swapped && attrs.background() == ColorAttribute::Default;
        let (fg, bg) = if swapped { (bg, fg) } else { (fg, bg) };
        let (fg, bg) = (fg.to_rgb_string(), bg.to_rgb_string());
        let bg = if unpainted || bg == screen_bg { String::new() } else { bg };
        let width = cell.width().max(1);
        if attrs.invisible() {
            for at in col..col + width {
                push(" ", fg.clone(), bg.clone(), at, 1);
            }
        } else {
            push(cell.str(), fg, bg, col, width);
        }
        at = col + width;
    }
    while let Some(last) = runs.last_mut() {
        if !last.bg.is_empty() {
            break;
        }
        let trimmed = last.text.trim_end();
        if trimmed.is_empty() {
            runs.pop();
        } else {
            last.width -= last.text.chars().count() - trimmed.chars().count();
            last.text.truncate(trimmed.len());
            break;
        }
    }
    runs
}

/// See `App::threads_view`.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ThreadsView {
    pub space: String,
    pub threads: Vec<ThreadView>,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct ThreadView {
    pub id: String,
    pub name: String,
    pub project: String,
    pub project_id: String,
    pub status: crate::tree::Status,
    pub dot: crate::tree::Dot,
    pub live: bool,
    pub pinned: bool,
    pub unread: bool,
    pub current: bool,
    pub window: Option<thinkterm_proto::WindowId>,
    pub tabs: Vec<ThreadTab>,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct ThreadTab {
    pub tab: TabId,
    pub title: String,
    /// The pane a tap shows.
    pub target: PaneId,
    pub current: bool,
    pub panes: Vec<PaneRef>,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct PaneRef {
    pub pane: PaneId,
    pub title: String,
}

/// See `App::screen_text`.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ScreenText {
    pub cols: usize,
    pub rows: usize,
    pub text: String,
    /// The pane's content origin, in the viewport's units.
    pub origin: [f64; 2],
    /// One cell, in the viewport's units.
    pub cell: [f64; 2],
    /// The selection as two visible (row, col) cells, ordered, inclusive.
    pub selection: Option<[[usize; 2]; 2]>,
}

#[derive(Debug, Clone, Copy)]
pub struct Selection {
    anchor: (StableRowIndex, usize),
    head: (StableRowIndex, usize),
    /// 1 = cells, 2 = words, 3 = lines.
    mode: u8,
}

impl Selection {
    fn ordered(&self) -> ((StableRowIndex, usize), (StableRowIndex, usize)) {
        if self.anchor <= self.head {
            (self.anchor, self.head)
        } else {
            (self.head, self.anchor)
        }
    }
}

/// Everything the page hands the app once it is attached.
/// Reconnect backoff. The first attempt is immediate -- a socket that
/// dropped because a laptop's wifi blinked is usually back at once -- and
/// the wait doubles from there so a server that is down for an hour is not
/// hammered.
const RECONNECT_MIN_MS: f64 = 500.0;
const RECONNECT_MAX_MS: f64 = 15_000.0;
/// How long a call to the plugin host waits for its answer.
const PLUGIN_CALL_TIMEOUT_MS: f64 = 10_000.0;
/// How long the server's way to the plugin host is kept once nothing needs
/// it: Settings › Sidebar & Plugins opened again soon after, or a panel
/// switched back to, finds it there.
const PLUGIN_PIPE_LINGER_MS: f64 = 30_000.0;
/// How long a snippet's Run or Paste waits for its text: a command that
/// arrives later than this is not the one the press was for.
const SNIPPET_SEND_WAIT_MS: f64 = 3_000.0;
/// After this many failed attempts the status line stops saying
/// "reconnecting" and admits it may never work.
const RECONNECT_DOUBT_AFTER: u32 = 6;
/// How long a new connection has to last before the backoff is forgiven.
/// Shorter than the shortest useful outage and longer than the time a
/// server that is restarting in a loop stays up.
const RECONNECT_STABLE_MS: f64 = 5_000.0;
/// How long the close button waits for its second press.
const CLOSE_CONFIRM_MS: f64 = 3_000.0;

pub struct Setup<P: Platform, L: Link> {
    pub platform: Rc<P>,
    pub link: L,
    pub host: Arc<AppHost<P, L>>,
    pub images: Arc<thinkterm_session::Lock<thinkterm_session::images::ImageStore>>,
    pub remote_tab_id: Arc<std::sync::atomic::AtomicUsize>,
    /// The pane on show, already built with `build_session`.
    pub pane: PaneCell<P, L>,
    pub gpu: Gpu,
    pub glyphs: GlyphCache,
    pub fonts: Rc<FontSet>,
    pub pane_id: PaneId,
    pub tab_id: TabId,
    pub window_id: thinkterm_proto::WindowId,
    pub workspace: String,
    pub dpr: f64,
    pub cols: usize,
    pub rows: usize,
    /// `?font=` was given: the size is the user's, not the desktop's.
    pub font_pinned: bool,
    /// The browser's languages, for switching the locale by preference.
    pub languages: Vec<String>,
    /// The server's version, as it answered the handshake.
    pub server_version: String,
}

/// One pane on the page: its session and the state that is the page's
/// own for it. Everything here is per pane; what is per page lives on
/// `Inner`.
/// A point on the canvas, resolved to a pane and a cell within it.
#[derive(Debug, Clone, Copy)]
struct Hit {
    pane_id: PaneId,
    col: usize,
    row: usize,
    x_off: isize,
    y_off: isize,
}

pub struct PaneCell<P: Platform, L: Link> {
    pub session: Arc<PaneSession<AppHost<P, L>>>,
    pub scroll_from_bottom: usize,
    /// How far the top visible row is cut off at its top, in device px,
    /// always inside `[0, cell_h)`: the part of a row that smooth
    /// scrolling is through. Zero means the view sits on a row boundary,
    /// which is the only position stepped mode ever holds and the only
    /// one either end of the scrollback allows.
    pub scroll_px: f32,
    pub selection: Option<Selection>,
    /// The pane's own colours (OSC 4/10/11), pushed as `SetApplicationPalette`,
    /// exactly as they arrived. `None` means the base palette shows through.
    pub application: Option<ColorPalette>,
    /// What is drawn: the base palette under `application`, if any.
    pub palette: ColorPalette,
    pub title: String,
    /// A live-resize preview in force: its epoch, size, and when it
    /// began, so one the server never confirms is not kept for good.
    pub preview: Option<(u64, TerminalSize, f64)>,
    /// Parked through a reconnect: its rows may have moved on without
    /// this page hearing, so it is polled afresh when next shown.
    pub reconnect_stale: bool,
}

impl<P: Platform, L: Link> PaneCell<P, L> {
    pub fn new(session: Arc<PaneSession<AppHost<P, L>>>, title: &str) -> Self {
        Self {
            preview: None,
            reconnect_stale: false,
            session,
            scroll_from_bottom: 0,
            scroll_px: 0.0,
            selection: None,
            application: None,
            palette: ColorPalette::default(),
            title: title.to_string(),
        }
    }
}

pub struct Inner<P: Platform, L: Link> {
    link: L,
    platform: Rc<P>,
    /// The panes of the tab on show, by remote pane id. Never empty: the
    /// focused pane's cell stays (dead, if need be) until a listing puts
    /// something else in its place.
    panes: std::collections::BTreeMap<PaneId, PaneCell<P, L>>,
    /// Cells of tabs the page has shown and left: kept, and kept current
    /// by the server's pushes, so switching back shows them at once with
    /// their lines, cursor and colours rather than a blank round trip.
    parked: std::collections::BTreeMap<PaneId, PaneCell<P, L>>,
    frame_control: std::collections::BTreeMap<PaneId, (u64, Option<u64>)>,
    frame_control_epoch: u64,
    animation_clock: crate::graphics::AnimationClock,
    focused_pane: PaneId,
    host: Arc<AppHost<P, L>>,
    /// One per connection, shared by every session: a picture one pane
    /// fetched is not fetched again by another.
    images: Arc<thinkterm_session::Lock<thinkterm_session::images::ImageStore>>,
    /// One per shown tab, shared by its sessions, so a move updates all.
    remote_tab_id: Arc<std::sync::atomic::AtomicUsize>,
    gpu: Gpu,
    glyphs: GlyphCache,
    fonts: Rc<FontSet>,
    tab_id: TabId,
    /// Tabs this page has shown, most recent first: a thread opens on
    /// the tab it was left at, not the window's active one, since the
    /// page's tab switches are its own and never move the server's.
    recent_tabs: Vec<TabId>,
    window_id: thinkterm_proto::WindowId,
    workspace: String,
    dpr: f64,
    cols: usize,
    rows: usize,
    font_pinned: bool,
    /// The page's own size: what it shows while it holds the tab, and
    /// what the settings pin. Following the desktop's cell is for a
    /// follower only.
    base_size_pt: f64,
    /// The size the page started with: what "follow" goes back to.
    boot_size_pt: f64,
    languages: Vec<String>,
    /// What Settings › About names as the version: the server's, as it
    /// answered when the page attached.
    server_version: String,
    /// What leads each pane's terminal, as the server pushes it, for the
    /// tab icons. A pane the listing no longer has is let go with it.
    programs: std::collections::HashMap<PaneId, thinkterm_proto::ForegroundProgram>,
    /// The cards the page's server sent for its tabs (`tab_icons`).
    tab_icons: Option<crate::tab_icons::TabIcons>,
    /// Palette picks, most recent first; the page keeps them across loads.
    recent: Vec<String>,
    /// The page's preferences, as it handed them over.
    settings: crate::settings::WebSettings,
    /// The palette the server's own configuration resolves to, pushed as
    /// `DefaultPalette` right after the handshake.
    server_palette: Option<ColorPalette>,
    /// A scheme this browser picked, which outranks the server's.
    chosen_palette: Option<ColorPalette>,
    /// The background last published on the canvas as `data-bg`.
    published_bg: Option<String>,
    /// Word/line selection extends by units; the pointer is down.
    selecting: bool,
    /// The pane a drag started in; it keeps the drag until the release.
    drag_pane: Option<PaneId>,
    /// A divider being dragged: which, where the press was along its
    /// axis (device px), and the cells already sent to the server.
    drag_divider: Option<(usize, f64, isize)>,
    /// The layout as the divider drag began: every step is measured
    /// from it, not from the last step's result.
    drag_layout: Option<crate::layout::TabLayout>,
    /// The tab as this page last claimed it for its own grid, drawn until
    /// the server's layout answers: a phone's soft keyboard going away
    /// grows the grid at once, its top filled from the rows fetched
    /// ahead, where the old layout sat flush with the bottom and left the
    /// rest of the canvas blank for a round trip.
    claimed_layout: Option<crate::layout::TabLayout>,
    /// When `claimed_layout` was claimed, on the platform's clock.
    claimed_at: f64,
    /// A divider claim on the wire, and the step that arrived meanwhile
    /// to send when it answers: one round trip at a time, the newest
    /// position wins.
    divider_claim_busy: bool,
    divider_claim_next: Option<(usize, isize)>,
    /// Counts live-resize previews, so a late answer cannot end a newer one.
    preview_epoch: u64,
    /// A sidebar or panel edge is being dragged: the canvas follows it,
    /// the tab is reshaped once when it is let go.
    panel_drag: bool,
    /// Every pane's application palette the server has pushed, on the page
    /// or not: it is sent once per connection, so a pane drawn again after
    /// a tab switch takes its colours from here.
    overrides: std::collections::HashMap<PaneId, Option<ColorPalette>>,
    click_count: u8,
    /// The button a press put down, for the moves and the cancel that
    /// carry no button of their own.
    held_button: MouseButton,
    last_click_ms: f64,
    focused: bool,
    composing: bool,
    /// What the input method has composed so far, drawn at the cursor
    /// until it is committed or dropped (`emit::LineParams::composing`).
    preedit: String,
    /// Refreshed on layout/scroll changes, avoiding layout reads per glyph
    /// or per frame. The IME field itself uses fixed CSS positioning.
    canvas_rect: [f64; 4],
    ime_anchor: Option<crate::ime::Anchor>,
    /// Set while there is no live socket. Cleared by a reconnect, which is
    /// why it is no longer the end of the page's life.
    disconnected: Option<String>,
    /// How long to wait before the next attempt, and whether one is already
    /// scheduled. Attempts are counted only to change what the status line
    /// says after enough of them.
    reconnect_delay: f64,
    reconnect_pending: bool,
    /// The cursor blink's phase, while `cursor_blink` is on: shown, or not.
    blink_shown: bool,
    /// The blink's tick runs once started; it does nothing while off.
    blink_ticking: bool,
    reconnect_attempts: u32,
    /// When the current connection came up, until it has lasted long enough
    /// to be called good. `None` once the backoff has been forgiven.
    connected_since: Option<f64>,
    quads: HeapQuadAllocator,
    vertices: Vec<Vertex>,
    ground: HeapQuadAllocator,
    graphics: crate::graphics::Graphics,
    /// Where a row that hangs over the edge of its pane's content box is
    /// recorded before being replayed cropped. One row at a time, emptied
    /// and refilled: at most two rows per pane per frame go through it,
    /// and it is never allocated inside the paint.
    scratch: HeapQuadAllocator,
    /// Panes at their own font size (Cmd+= / Cmd+-), as on the desktop:
    /// each has its own glyph cache and atlas, and is drawn as its own
    /// batch. Absent means the page's size.
    pane_fonts: std::collections::BTreeMap<PaneId, PaneFont>,
    /// What to do about an atlas that has run out of room for good.
    capacity: Capacity,
    /// The server's tabs and panes as last listed, for the strip and for
    /// switching. Refreshed on the pushes that change it and on a timer.
    layout: Option<codec::ListPanesResponse>,
    /// Where the panes of the tab on show go, from that listing.
    tab_layout: Option<crate::layout::TabLayout>,
    /// Whether the page moves to whatever pane the desktop focuses. On by
    /// default: a page opened to "see my terminal" wants the one being
    /// used. Choosing a pane here turns it off.
    following: bool,
    /// The tab the page last claimed on its own, on showing it; see
    /// `show`. A tab is claimed once per showing, not on every listing.
    auto_claimed: Option<TabId>,
    /// The page is in its phone shape (`body[data-mobile]`), read at each
    /// resize: the grid is padded differently there.
    mobile: bool,
    /// The page's display layer, told once per animation frame that
    /// something it shows changed; it reads the views it wants.
    on_change: Option<Rc<dyn Fn()>>,
    notify_pending: Rc<Cell<bool>>,
    /// What the status views report: the current remark and the summary.
    toast: RefCell<Option<crate::views::Toast>>,
    summary: RefCell<String>,
    /// The timer that hides the passing remark, so a new one restarts it.
    tree: crate::tree::TreeModel,
    session_refresh_pending: bool,
    /// A thread's delete button was pressed once, and when.
    deleting: Option<(String, f64)>,
    editing: crate::sidebar::Editing,
    /// Why the last path typed into "Add workspace" was refused.
    new_project_error: Option<String>,
    /// A tab's close button was pressed once, and when.
    closing_tab: Option<(TabId, f64)>,
    layout_refresh_pending: bool,
    /// A switch is under way; a second request waits for the next push
    /// or click rather than racing it.
    switching: bool,
    /// The close button was pressed once; the second press, within a
    /// few seconds, is the one that closes.
    closing_since: Option<f64>,
    /// What the interaction that asked for the terminal wanted done once
    /// the server hands it over.
    after_take_over: Option<AfterTakeOver>,
    /// How the last ask for the terminal went, for the card to say.
    claim: Claim,
    /// The right panel's tab on show (`agents.rs` `panel_tabs`), and
    /// whether the page has the panel up at all.
    right_panel: String,
    right_panel_shown: bool,
    /// Agents whose sub-task list is open in the Agents panel; folded is
    /// the default, as on the desktop. Each look at the panel drops the
    /// ones with no sub-tasks left.
    open_subtasks: std::collections::HashSet<PaneId>,
    /// The plugin panel on show in the right panel, while one is.
    plugin_panel: Option<crate::plugin_panel::PanelModel>,
    /// What the page last said of the room a plugin panel has: its size,
    /// fonts and theme. A panel opens once there is this.
    plugin_panel_env: Option<thinkterm_plugin_panel::Env>,
    /// The panel's extended view, beside the right panel, while its frames
    /// ask for one, and what the page said of its room, which it opens
    /// once it has: the page gives it room while it is asked for.
    plugin_extended: Option<crate::plugin_panel::PanelModel>,
    plugin_extended_env: Option<thinkterm_plugin_panel::Env>,
    /// Numbers the page's openings of plugin panels.
    plugin_panel_views: u64,
    snippets: crate::snippets::SnippetsModel,
    /// The plugins as last heard, and what wants them.
    plugin_list: crate::plugin_list::PluginsModel,
    /// Calls to the plugin host waiting for their answers.
    plugin_calls: crate::plugins::PluginCalls,
    /// The server holds a way to the plugin host for this page: frames were
    /// sent since it was last closed.
    plugin_pipe: bool,
    /// Counts the lingers after the last need, so only the latest lets the
    /// way to the host go.
    plugin_pipe_linger: u64,
}

/// One pane's own font: its scale over the page's size, and what draws it.
struct PaneFont {
    scale: f64,
    glyphs: GlyphCache,
    quads: HeapQuadAllocator,
    vertices: Vec<Vertex>,
    vertex_layers: [std::ops::Range<usize>; 3],
}

/// Continuation of a take-over that an interaction started.
/// How many tabs' cells are kept parked when the page leaves them.
const PARKED_TABS: usize = 12;

/// The page's last ask for the terminal (Handoff mode): none, on the
/// wire, or turned down by the server.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Claim {
    Idle,
    Taking,
    Refused,
}

enum AfterTakeOver {
    Focus(PaneId),
    Key(KeyCode, Modifiers, bool),
}

/// Who hears of it when a frame for the plugin host does not reach it.
#[derive(Debug, Clone, Copy)]
enum FrameFor {
    Nobody,
    /// The call with this number, which fails.
    Call(u64),
    /// The page's plugin panel, or its extended view, opened under this
    /// number.
    Panel(u64),
}

pub struct App<P: Platform, L: Link> {
    pub platform: Rc<P>,
    inner: RefCell<Inner<P, L>>,
    frame_requested: Cell<bool>,
    /// A layout/input repaint must survive coalescing with parked-pane output.
    repaint_requested: Cell<bool>,
    /// The atlas backoff's own wake-up. Everything else here is driven by
    /// input or by output; this is the one thing that has to happen on a
    /// still screen.
    retry: RefCell<Option<(f64, crate::platform::Timeout)>>,
    animation_wake: RefCell<Option<(f64, crate::platform::Timeout)>>,
    animations_visible: Cell<bool>,
    /// When a timer set to come back to the plugin panel is due, while
    /// one is: to tell its plugin a size, or to open it again.
    panel_wake: Cell<Option<f64>>,
    /// Set once the App is let go: a tick that still finds it (an
    /// interval cannot be cancelled) does nothing more.
    retired: Cell<bool>,
}

/// How large the glyph atlas is allowed to get.
///
/// Not the GPU's maximum, which is commonly 8192: that is 268 MB of video
/// memory for glyphs alone, which is not a trade a terminal should make
/// silently. At this size it holds several screens' worth of distinct CJK,
/// and beyond it `Capacity` clears and reuses rather than growing -- which
/// is the whole reason that path exists.
const MAX_ATLAS_SIDE: usize = 4096;

/// What `grow_atlas` managed.
enum Grown {
    /// A bigger texture; try again.
    Larger,
    /// Already at the GPU's largest, and it is full. Only a clear frees
    /// space now, and `Capacity` decides when that is worth doing.
    AtCapacity,
}

/// A session for one pane, configured the way this page runs them.
pub fn build_session<P: Platform, L: Link>(
    host: &Arc<AppHost<P, L>>,
    images: &Arc<thinkterm_session::Lock<thinkterm_session::images::ImageStore>>,
    remote_tab_id: &Arc<std::sync::atomic::AtomicUsize>,
    pane_id: PaneId,
    dims: thinkterm_proto::RenderableDimensions,
    title: &str,
    alt_screen: bool,
) -> Arc<PaneSession<AppHost<P, L>>> {
    PaneSession::new(
        Arc::clone(host),
        Arc::clone(images),
        thinkterm_session::SessionConfig {
            scrollback_lines: 3500,
            local_echo_threshold_ms: Some(100),
            overlay_lag_indicator: false,
        },
        pane_id,
        Arc::clone(remote_tab_id),
        // The host's name for the pane is the server's: it is what the
        // session's events carry, and the page keys its panes by it.
        pane_id,
        dims,
        title,
        alt_screen,
    )
}

impl<P: Platform, L: Link> Inner<P, L> {
    /// The base palette every pane draws from: this browser's pick, else
    /// the server's configured scheme, else the stock palette.
    pub fn configured(&self) -> ColorPalette {
        crate::settings::configured_palette(
            self.chosen_palette.as_ref(),
            self.server_palette.as_ref(),
        )
    }

    /// Fold the base palette and each pane's application override back into
    /// what is drawn. Returns the panes whose palette moved.
    fn recompute_palettes(&mut self) -> Vec<PaneId> {
        let configured = self.configured();
        let mut changed = Vec::new();
        for (pane_id, cell) in self.panes.iter_mut() {
            if let Some(palette) = crate::settings::recomputed_palette(
                &cell.palette,
                &configured,
                cell.application.as_ref(),
            ) {
                cell.palette = palette;
                changed.push(*pane_id);
            }
        }
        changed
    }

    fn focused(&self) -> &PaneCell<P, L> {
        self.panes
            .get(&self.focused_pane)
            .expect("the focused pane has a cell")
    }

    fn focused_mut(&mut self) -> &mut PaneCell<P, L> {
        let id = self.focused_pane;
        self.panes.get_mut(&id).expect("the focused pane has a cell")
    }

    fn title(&self) -> &str {
        &self.focused().title
    }
}

/// The toast for a request that failed: `what` is a `web-act-*` key.
fn failed(what: &str, err: &dyn std::fmt::Display) -> String {
    let mut args = thinkterm_i18n::FluentArgs::new();
    args.set("what", tr(what));
    args.set("error", format!("{err:#}"));
    thinkterm_i18n::tr_args("web-toast-failed", &args)
}

impl<P: Platform, L: Link> App<P, L> {
    pub fn new(setup: Setup<P, L>) -> Rc<Self> {
        let mut panes = std::collections::BTreeMap::new();
        panes.insert(setup.pane_id, setup.pane);
        let inner = Inner {
            link: setup.link,
            platform: Rc::clone(&setup.platform),
            panes,
            parked: std::collections::BTreeMap::new(),
            frame_control: std::collections::BTreeMap::new(),
            frame_control_epoch: 0,
            animation_clock: crate::graphics::AnimationClock::default(),
            focused_pane: setup.pane_id,
            host: setup.host,
            images: setup.images,
            remote_tab_id: setup.remote_tab_id,
            gpu: setup.gpu,
            base_size_pt: setup.glyphs.size_pt,
            boot_size_pt: setup.glyphs.size_pt,
            glyphs: setup.glyphs,
            fonts: setup.fonts,
            tab_id: setup.tab_id,
            recent_tabs: vec![setup.tab_id],
            window_id: setup.window_id,
            workspace: setup.workspace,
            dpr: setup.dpr,
            cols: setup.cols,
            rows: setup.rows,
            selecting: false,
            drag_pane: None,
            drag_divider: None,
            drag_layout: None,
            claimed_layout: None,
            claimed_at: 0.0,
            divider_claim_busy: false,
            divider_claim_next: None,
            preview_epoch: 0,
            panel_drag: false,
            overrides: std::collections::HashMap::new(),
            click_count: 0,
            held_button: MouseButton::None,
            last_click_ms: 0.0,
            focused: true,
            composing: false,
            preedit: String::new(),
            canvas_rect: [0.0; 4],
            ime_anchor: None,
            disconnected: None,
            reconnect_delay: RECONNECT_MIN_MS,
            reconnect_pending: false,
            blink_shown: true,
            blink_ticking: false,
            reconnect_attempts: 0,
            connected_since: None,
            capacity: Capacity::new(),
            font_pinned: setup.font_pinned,
            languages: setup.languages,
            server_version: setup.server_version,
            programs: std::collections::HashMap::new(),
            tab_icons: None,
            recent: Vec::new(),
            settings: crate::settings::WebSettings::default(),
            server_palette: None,
            chosen_palette: None,
            published_bg: None,
            quads: HeapQuadAllocator::default(),
            vertices: Vec::new(),
            ground: HeapQuadAllocator::default(),
            graphics: crate::graphics::Graphics::default(),
            scratch: HeapQuadAllocator::default(),
            pane_fonts: Default::default(),
            layout: None,
            tab_layout: None,
            following: true,
            auto_claimed: None,
            mobile: false,
            on_change: None,
            notify_pending: Rc::new(Cell::new(false)),
            toast: RefCell::new(None),
            summary: RefCell::new(String::new()),
            tree: crate::tree::TreeModel::default(),
            session_refresh_pending: false,
            deleting: None,
            editing: crate::sidebar::Editing::None,
            new_project_error: None,
            closing_tab: None,
            layout_refresh_pending: false,
            switching: false,
            closing_since: None,
            after_take_over: None,
            claim: Claim::Idle,
            right_panel: "agents".to_string(),
            right_panel_shown: false,
            open_subtasks: Default::default(),
            plugin_panel: None,
            plugin_panel_env: None,
            plugin_extended: None,
            plugin_extended_env: None,
            plugin_panel_views: 0,
            snippets: Default::default(),
            plugin_list: Default::default(),
            plugin_calls: Default::default(),
            plugin_pipe: false,
            plugin_pipe_linger: 0,
        };
        let app = Rc::new(Self {
            platform: setup.platform,
            inner: RefCell::new(inner),
            frame_requested: Cell::new(false),
            repaint_requested: Cell::new(false),
            retry: RefCell::new(None),
            animation_wake: RefCell::new(None),
            animations_visible: Cell::new(true),
            panel_wake: Cell::new(None),
            retired: Cell::new(false),
        });
        let weak = Rc::downgrade(&app);
        app.platform.set_frame_handler(Box::new(move || {
            if let Some(app) = weak.upgrade() {
                app.frame_requested.set(false);
                app.frame_if_needed(false);
            }
        }));
        app.sync_frame_control();
        app
    }

    /// Run a future on the platform's executor for this thread.
    fn spawn(&self, fut: impl Future<Output = ()> + 'static) {
        self.platform.spawn(Box::pin(fut));
    }

    fn now_secs(&self) -> i64 {
        (self.platform.wall_ms() / 1000.0) as i64
    }

    fn new_id(&self, kind: &str) -> String {
        let platform = &self.platform;
        crate::tree::new_id(kind, || platform.random_u32())
    }

    /// Come back after `delay_ms` whether or not anything else asks for a
    /// frame.
    ///
    /// The atlas backoff is the only thing here that cannot wait to be
    /// asked: once the terminal goes quiet nothing requests another frame,
    /// so a retry that counted frames would never arrive -- and repainting a
    /// still screen thousands of times to reach a count would be waste.
    fn schedule_retry(self: &Rc<Self>, delay_ms: f64) {
        // A timer already set for sooner will do. One set for later will
        // not: dropping a nearer deadline on the floor is how a recovery
        // that should have taken a second takes eight.
        let due = self.platform.monotonic_ms() + delay_ms;
        if self.retry.borrow().as_ref().is_some_and(|(at, _)| *at <= due) {
            return;
        }
        let previous = self.retry.borrow_mut().take();
        drop(previous);
        let weak = Rc::downgrade(self);
        let timeout = self.platform.cancellable_timeout(
            delay_ms.max(0.0),
            Box::new(move || {
                if let Some(app) = weak.upgrade() {
                    let expired = app.retry.borrow_mut().take();
                    drop(expired);
                    app.request_frame();
                }
            }),
        );
        *self.retry.borrow_mut() = Some((due, timeout));
    }

    fn schedule_animation(self: &Rc<Self>, due: Option<f64>) {
        let due = due.filter(|_| self.animations_visible.get() && !self.retired.get());
        if self.animation_wake.borrow().as_ref().map(|(at, _)| *at) == due { return; }
        let previous = self.animation_wake.borrow_mut().take();
        drop(previous);
        let Some(due) = due else { return; };
        let weak = Rc::downgrade(self);
        let timeout = self.platform.cancellable_timeout(
            (due - self.platform.monotonic_ms()).max(0.0),
            Box::new(move || {
                if let Some(app) = weak.upgrade().filter(|app| !app.retired.get()) {
                    let expired = app.animation_wake.borrow_mut().take();
                    drop(expired);
                    app.request_frame();
                }
            }),
        );
        *self.animation_wake.borrow_mut() = Some((due, timeout));
    }

    pub fn visibility_changed(self: &Rc<Self>, visible: bool) {
        self.animations_visible.set(visible);
        if visible {
            // Coming back to the page is looking at the pane on show.
            self.mark_focused_program_status_seen();
            self.request_frame();
        } else {
            self.schedule_animation(None);
        }
    }

    pub fn wake(self: &Rc<Self>) -> Rc<dyn Fn()> {
        let weak = Rc::downgrade(self);
        Rc::new(move || {
            if let Some(app) = weak.upgrade() {
                app.schedule_frame();
            }
        })
    }

    pub fn request_frame(&self) {
        self.repaint_requested.set(true);
        self.schedule_frame();
    }

    fn schedule_frame(&self) {
        if self.frame_requested.get() {
            return;
        }
        self.frame_requested.set(true);
        self.platform.request_frame();
    }

    /// A passing remark, bottom right, gone after a few seconds -- the
    /// desktop has no status line, and neither does the page. It stays
    /// while the connection is down: that is not a remark but the state.
    fn set_status(inner: &Inner<P, L>, text: &str) {
        *inner.toast.borrow_mut() = Some(crate::views::Toast {
            text: text.to_string(),
            sticky: inner.disconnected.is_some(),
            at: inner.platform.monotonic_ms(),
        });
        if inner.disconnected.is_some() {
            // Not a remark but the state: probes read it there too.
            *inner.summary.borrow_mut() = text.to_string();
        }
        Self::notify(inner);
    }

    pub fn hide_status(&self) {
        let inner = self.inner.borrow();
        *inner.toast.borrow_mut() = None;
        Self::notify(&inner);
    }

    /// The desktop's surface over a terminal another device holds, or
    /// none when this page may type. It says who holds it, and after a
    /// press, whether the server let go.
    fn card(inner: &Inner<P, L>) -> Option<crate::views::Card> {
        use crate::views::Card;
        let lease = inner.link.lease();
        if !matches!(lease.mode, Some(codec::FrontendAccessMode::Handoff)) || lease.owns_viewport() {
            return None;
        }
        let host = lease.owner.as_ref().map(|o| o.hostname.trim().to_string()).unwrap_or_default();
        let taking = tr("web-card-take-over");
        Some(match (inner.claim, lease.owner.is_some()) {
            (Claim::Taking, _) => Card {
                title: tr("web-card-taking-title"),
                hint: tr("web-card-taking-hint"),
                state: "taking",
                action: taking,
            },
            (Claim::Refused, true) => Card {
                title: tr("web-card-refused-title"),
                hint: tr("web-card-refused-hint"),
                state: "refused",
                action: tr("web-card-try-again"),
            },
            (_, true) => Card {
                title: if host.is_empty() {
                    tr("web-card-busy-title")
                } else {
                    let mut args = thinkterm_i18n::FluentArgs::new();
                    args.set("host", host);
                    thinkterm_i18n::tr_args("web-card-busy-on", &args)
                },
                hint: tr("web-card-busy-hint"),
                state: "busy",
                action: taking,
            },
            (_, false) => Card {
                title: tr("web-card-free-title"),
                hint: tr("web-card-free-hint"),
                state: "free",
                action: tr("web-card-take-control"),
            },
        })
    }

    /// Tell the display layer that a view changed: once per task, so a
    /// burst of changes is one notice. A task rather than an animation
    /// frame, since the page's frames are the terminal's, and a notice
    /// that waited for one would be counted -- and paced -- as a paint.
    fn notify(inner: &Inner<P, L>) {
        let Some(cb) = inner.on_change.clone() else {
            return;
        };
        if inner.notify_pending.replace(true) {
            return;
        }
        let pending = Rc::clone(&inner.notify_pending);
        inner.platform.set_timeout(
            0.0,
            Box::new(move || {
                pending.set(false);
                cb();
            }),
        );
    }

    /// The language changed: every view carries text, so all are stale.
    pub fn locale_changed(&self) {
        let inner = self.inner.borrow();
        Self::render_strip(&inner);
    }

    pub fn set_on_change(&self, cb: Rc<dyn Fn()>) {
        let mut inner = self.inner.borrow_mut();
        inner.on_change = Some(cb);
        drop(inner);
        Self::notify(&self.inner.borrow());
    }

    pub fn sidebar_view(&self) -> crate::views::SidebarView {
        let inner = self.inner.borrow();
        crate::views::SidebarView {
            rows: Self::side_rows(&inner),
            editing: inner.editing.clone(),
            space: inner.tree.current_space().map(|s| s.id.clone()),
            new_project_error: inner.new_project_error.clone(),
            reveal: crate::sidebar::REVEAL,
            footer: Self::side_footer(),
            footer_label_min_width: crate::sidebar::FOOTER_LABEL_MIN_WIDTH,
        }
    }

    /// The footer the desktop's sidebar ends with: the gear and its label,
    /// then Live Overview, Remote Hosts and the thread search.
    fn side_footer() -> Vec<crate::sidebar::FooterAction> {
        // Only what the page can do: the desktop's Live Overview has no
        // browser counterpart, so no button for it. Remote Hosts is the
        // page's own machines panel.
        vec![
            crate::sidebar::FooterAction {
                id: "settings".into(),
                icon: "settings".into(),
                label: Some(thinkterm_i18n::tr("sidebar-settings")),
                tip: thinkterm_i18n::tr("tooltip-sidebar-settings"),
                enabled: true,
                trailing: false,
            },
            crate::sidebar::FooterAction {
                id: "machines".into(),
                icon: "globe".into(),
                label: None,
                tip: thinkterm_i18n::tr("tooltip-sidebar-ssh-hosts"),
                enabled: true,
                trailing: true,
            },
            crate::sidebar::FooterAction {
                id: "search".into(),
                icon: "search".into(),
                label: None,
                tip: thinkterm_i18n::tr("tooltip-sidebar-thread-search"),
                enabled: true,
                trailing: true,
            },
        ]
    }

    /// The threads of the Space on show with their tabs, in the sidebar's
    /// order (pinned first, then by project), for a phone that shows the
    /// desktop's sidebar layer and its tab strip as two rows. The current
    /// thread's tabs come from the strip (labels and panes as shown);
    /// another thread's from the session state.
    pub fn threads_view(&self) -> ThreadsView {
        let inner = self.inner.borrow();
        let space = inner
            .tree
            .current_space()
            .map(|s| s.name.clone())
            .unwrap_or_else(|| "Default".to_string());
        let strip = Self::strip_model(&inner).map(|(tabs, _)| tabs).unwrap_or_default();
        let mut threads = Vec::new();
        for row in Self::side_rows(&inner) {
            let crate::tree::Row::Thread(t) = row else {
                continue;
            };
            let Some(thread) = inner.tree.thread(&t.id) else {
                continue;
            };
            let project = inner
                .tree
                .project_of(&t.id)
                .map(|p| p.name.clone())
                .unwrap_or_default();
            let window = thread.tabs.first().map(|tab| tab.window_id);
            let tabs = thread
                .tabs
                .iter()
                .map(|tab| {
                    let shown = strip.iter().find(|s| s.tab_id == tab.tab_id);
                    let panes = match shown {
                        Some(s) => s.panes.iter().map(|p| PaneRef { pane: p.pane_id, title: p.title.clone() }).collect(),
                        None => tab
                            .pane_ids
                            .iter()
                            .map(|p| PaneRef { pane: *p, title: String::new() })
                            .collect(),
                    };
                    let target = shown.map(|s| s.target).or_else(|| tab.pane_ids.first().copied()).unwrap_or(0);
                    ThreadTab {
                        tab: tab.tab_id,
                        title: shown
                            .map(|s| s.label.clone())
                            .unwrap_or_else(|| crate::navbar::display_title(&tab.title).0.to_string()),
                        target,
                        current: t.selected && tab.tab_id == inner.tab_id,
                        panes,
                    }
                })
                .collect();
            threads.push(ThreadView {
                id: t.id.clone(),
                name: t.name.clone(),
                project,
                project_id: t.project_id.clone(),
                status: t.status,
                dot: t.dot,
                live: t.live,
                pinned: t.pinned,
                unread: t.unread,
                current: t.selected,
                window,
                tabs,
            });
        }
        ThreadsView { space, threads }
    }

    pub fn tabs_view(&self) -> crate::views::TabsView {
        let inner = self.inner.borrow();
        let (tabs, controls) = Self::strip_model(&inner).unwrap_or_default();
        let icon = Self::tab_icons(&inner).and_then(|icons| icons.terminal()).map(str::to_string);
        crate::views::TabsView { tabs, controls, icon }
    }

    /// The cards to dress the tabs with: none until the page has handed
    /// them over.
    fn tab_icons(inner: &Inner<P, L>) -> Option<&crate::tab_icons::TabIcons> {
        inner.tab_icons.as_ref()
    }

    /// The card heading a pane's tab: its agent's, else its program's,
    /// else the terminal's.
    fn pane_icon(inner: &Inner<P, L>, pane_id: PaneId) -> Option<String> {
        Self::tab_icons(inner)?
            .card(inner.tree.agent_id(pane_id), inner.programs.get(&pane_id))
            .map(str::to_string)
    }

    /// The cards this page's server sent for its tabs, as the page read
    /// them off the relay.
    pub fn set_tab_icons(self: &Rc<Self>, json: &str) -> Result<(), String> {
        let icons = crate::tab_icons::TabIcons::parse(json)?;
        let mut inner = self.inner.borrow_mut();
        inner.tab_icons = Some(icons);
        Self::notify(&inner);
        Ok(())
    }

    pub fn navs_view(&self) -> crate::views::NavsView {
        Self::nav_views(&self.inner.borrow())
    }

    pub fn status_view(&self) -> crate::views::StatusView {
        let inner = self.inner.borrow();
        let toast = inner.toast.borrow().clone();
        let summary = inner.summary.borrow().clone();
        crate::views::StatusView { toast, card: Self::card(&inner), summary }
    }

    /// The focused pane's visible rows as a document for a platform's text
    /// system (the phone's native selection): every row exactly `cols`
    /// UTF-16 units wide -- a wide glyph is followed by a word joiner for
    /// its second cell, an empty cell is a space, a glyph outside the BMP
    /// is a replacement character -- and rows joined by newlines, so a
    /// position is `row * (cols + 1) + col`. The text copied comes from
    /// `selection_text`, not from here.
    pub fn screen_text(&self) -> Option<ScreenText> {
        let inner = self.inner.borrow();
        let place = Self::focused_placement(&inner)?;
        let cell = inner.panes.get(&place.pane_id)?;
        let (cols, rows) = Self::shown_in(&inner, &place);
        if cols == 0 || rows == 0 {
            return None;
        }
        let dims = Self::shown_dims(&cell.session);
        let visible = visible_rows(&dims, rows, cell.scroll_from_bottom);
        let (first, lines) = cell.session.get_lines(visible.clone());
        let mut text = String::with_capacity(rows * (cols + 1));
        for i in 0..rows {
            let row = visible.start + i as StableRowIndex;
            let mut at = 0usize;
            if row >= first {
                if let Some(line) = lines.get((row - first) as usize) {
                    for c in line.visible_cells() {
                        let col = c.cell_index();
                        if col >= cols {
                            break;
                        }
                        while at < col {
                            text.push(' ');
                            at += 1;
                        }
                        let s = c.str();
                        let ch = s.chars().next().unwrap_or(' ');
                        text.push(if (ch as u32) > 0xffff { '\u{fffd}' } else { ch });
                        at += 1;
                        for _ in 1..c.width().max(1) {
                            if at < cols {
                                text.push('\u{2060}');
                                at += 1;
                            }
                        }
                    }
                }
            }
            while at < cols {
                text.push(' ');
                at += 1;
            }
            text.push('\n');
        }
        let (cw, ch) = Self::pane_cell(&inner, &place);
        let root = inner.glyphs.metrics.cell_size;
        let pad = Self::pad(&inner);
        let nav = Self::content_offset(&inner, &place) as f64;
        let [left, top, _, _] = inner.canvas_rect;
        let dpr = inner.dpr.max(0.1);
        let origin = [
            left + (pad.0 as f64 + place.frame.left as f64 * root.width as f64) / dpr,
            top + (pad.1 as f64 + place.frame.top as f64 * root.height as f64 + nav - cell.scroll_px as f64) / dpr,
        ];
        let selection = cell.selection.map(|sel| {
            let ((r0, c0), (r1, c1)) = sel.ordered();
            let clamp = |r: StableRowIndex, c: usize| {
                let r = (r - visible.start).clamp(0, rows as StableRowIndex - 1) as usize;
                [r, c.min(cols.saturating_sub(1))]
            };
            [clamp(r0, c0), clamp(r1, c1)]
        });
        Some(ScreenText {
            cols,
            rows,
            text,
            origin,
            cell: [cw / dpr, ch / dpr],
            selection,
        })
    }

    /// Select from one visible cell to another in the focused pane, as a
    /// platform's own selection handles ask; both ends inclusive.
    pub fn set_selection(self: &Rc<Self>, anchor: (usize, usize), head: (usize, usize)) {
        {
            let mut inner = self.inner.borrow_mut();
            let Some((pane_id, selection)) = Self::visible_selection(&inner, anchor, head) else {
                return;
            };
            let Some(cell) = inner.panes.get_mut(&pane_id) else {
                return;
            };
            cell.selection = Some(selection);
            inner.selecting = false;
        }
        self.request_frame();
    }

    /// The text between two visible cells of the focused pane, both ends
    /// inclusive, as a selection of them would copy it -- read without
    /// making one, so the pane's own selection, and a drag making it, stay.
    pub fn text_between(&self, anchor: (usize, usize), head: (usize, usize)) -> Option<String> {
        let inner = self.inner.borrow();
        let (_, selection) = Self::visible_selection(&inner, anchor, head)?;
        Self::text_of(&inner, &selection)
    }

    /// A cell selection between two visible cells of the focused pane, in
    /// the rows of its scrollback they show.
    fn visible_selection(inner: &Inner<P, L>, anchor: (usize, usize), head: (usize, usize)) -> Option<(PaneId, Selection)> {
        let place = Self::focused_placement(inner)?;
        let (_, rows) = Self::shown_in(inner, &place);
        let cell = inner.panes.get(&place.pane_id)?;
        let dims = Self::shown_dims(&cell.session);
        let visible = visible_rows(&dims, rows, cell.scroll_from_bottom);
        let selection = Selection {
            anchor: (visible.start + anchor.0 as StableRowIndex, anchor.1),
            head: (visible.start + head.0 as StableRowIndex, head.1),
            mode: 1,
        };
        Some((place.pane_id, selection))
    }

    pub fn clear_selection(self: &Rc<Self>) {
        {
            let mut inner = self.inner.borrow_mut();
            let focused = inner.focused_pane;
            if let Some(cell) = inner.panes.get_mut(&focused) {
                if cell.selection.take().is_none() {
                    return;
                }
            }
        }
        self.request_frame();
    }

    /// The `rows` rows of any pane on the server ending at its cursor's
    /// row, as text, for a thumbnail of a tab this page is not showing: a
    /// fresh shell's prompt sits at the top of a tall pane, and the rows
    /// under it are nothing to look at.
    pub fn preview_lines(self: &Rc<Self>, pane_id: PaneId, rows: usize) -> crate::platform::LocalFuture<Result<Vec<PreviewRow>>> {
        let link = self.inner.borrow().link.clone();
        // The pane's own colours if it is drawn here, else the scheme's.
        let palette = {
            let inner = self.inner.borrow();
            inner.panes.get(&pane_id).map(|c| c.palette.clone()).unwrap_or_else(|| inner.configured())
        };
        Box::pin(async move {
            let (_, lines) = Self::pane_rows(link, pane_id, |dims, cursor| {
                let bottom = cursor
                    .y
                    .saturating_add(1)
                    .min(dims.physical_top + dims.viewport_rows as StableRowIndex);
                let top = bottom
                    .saturating_sub(rows as StableRowIndex)
                    .max(dims.scrollback_top);
                top..bottom
            })
            .await?;
            let mut out: Vec<(StableRowIndex, PreviewRow)> = lines
                .into_iter()
                .map(|(row, line)| (row, preview_row(&line, &palette)))
                .collect();
            out.sort_by_key(|(row, _)| *row);
            Ok(out.into_iter().map(|(_, row)| row).collect())
        })
    }

    /// The whole screen of any pane on the server, every row of its
    /// viewport, for a thumbnail that has the terminal's shape and so can
    /// stand in for it (see `ScreenPreview`).
    pub fn preview_screen(self: &Rc<Self>, pane_id: PaneId) -> crate::platform::LocalFuture<Result<ScreenPreview>> {
        let link = self.inner.borrow().link.clone();
        let palette = {
            let inner = self.inner.borrow();
            inner.panes.get(&pane_id).map(|c| c.palette.clone()).unwrap_or_else(|| inner.configured())
        };
        Box::pin(async move {
            let (dims, lines) = Self::pane_rows(link, pane_id, |dims, _| {
                dims.physical_top..dims.physical_top + dims.viewport_rows as StableRowIndex
            })
            .await?;
            let (top, rows) = (dims.physical_top, dims.viewport_rows);
            let mut screen = vec![Vec::new(); rows];
            for (row, line) in lines {
                if let Some(slot) = usize::try_from(row - top).ok().and_then(|i| screen.get_mut(i)) {
                    *slot = screen_row(&line, &palette, dims.reverse_video);
                }
            }
            let (foreground, background) = if dims.reverse_video {
                (palette.background, palette.foreground)
            } else {
                (palette.foreground, palette.background)
            };
            Ok(ScreenPreview {
                cols: dims.cols,
                rows,
                foreground: foreground.to_rgb_string(),
                background: background.to_rgb_string(),
                lines: screen,
            })
        })
    }

    /// A pane's dimensions, then the rows `range` picks out of them, as
    /// the server sends them, each with its index: two round trips.
    async fn pane_rows(
        link: L,
        pane_id: PaneId,
        range: impl FnOnce(
            &thinkterm_proto::RenderableDimensions,
            &thinkterm_proto::StableCursorPosition,
        ) -> std::ops::Range<StableRowIndex>,
    ) -> Result<(thinkterm_proto::RenderableDimensions, Vec<(StableRowIndex, termwiz::surface::Line)>)> {
        let (dims, cursor) = thinkterm_session::host::request(
            &link,
            Pdu::GetPaneRenderableDimensions(codec::GetPaneRenderableDimensions { pane_id }),
            |pdu| match pdu {
                Pdu::GetPaneRenderableDimensionsResponse(r) => Ok((r.dimensions, r.cursor_position)),
                other => Err(other),
            },
        )
        .await?;
        if !dims.is_plausible() {
            anyhow::bail!("the pane reported impossible dimensions");
        }
        let lines = range(&dims, &cursor);
        let response = thinkterm_session::host::request(
            &link,
            Pdu::GetLines(codec::GetLines { pane_id, lines: vec![lines] }),
            |pdu| match pdu {
                Pdu::GetLinesResponse(r) => Ok(r),
                other => Err(other),
            },
        )
        .await?;
        Ok((dims, response.lines.extract_data().0))
    }

    /// Every pane on show in the tab that holds `pane_id`, each one's whole
    /// screen where it lies in the tab, for a thumbnail of the tab as it is
    /// split (see `TabPreview`). The panes' places and sizes are the layout
    /// as last listed; their screens are fetched together, two round trips
    /// each. A pane that cannot say (closed since) leaves its place blank.
    pub fn preview_tab(self: &Rc<Self>, pane_id: PaneId) -> crate::platform::LocalFuture<Result<TabPreview>> {
        let places = {
            let inner = self.inner.borrow();
            inner.layout.as_ref().and_then(|layout| tab_places(layout, pane_id))
        }
        // Not listed yet: the pane alone, at the size it says.
        .unwrap_or_else(|| vec![Place { pane: pane_id, left: 0, top: 0, cols: 0, rows: 0 }]);
        let screens: Vec<_> = places.iter().map(|place| self.preview_screen(place.pane)).collect();
        Box::pin(async move {
            let screens = futures::future::join_all(screens).await;
            let (mut cols, mut rows) = (0, 0);
            let mut asked = None;
            let mut failed = None;
            let mut panes = Vec::with_capacity(places.len());
            for (place, screen) in places.into_iter().zip(screens) {
                // The tab as large as listed, a blank place included.
                cols = cols.max(place.left + place.cols);
                rows = rows.max(place.top + place.rows);
                let mut screen = match screen {
                    Ok(screen) => screen,
                    Err(err) => {
                        failed = Some(err);
                        continue;
                    }
                };
                if place.cols > 0 && place.rows > 0 {
                    screen.fit(place.cols, place.rows);
                }
                if place.pane == pane_id {
                    asked = Some((screen.foreground.clone(), screen.background.clone()));
                }
                cols = cols.max(place.left + screen.cols);
                rows = rows.max(place.top + screen.rows);
                panes.push(PanePreview { left: place.left, top: place.top, screen });
            }
            if panes.is_empty() {
                return Err(failed.unwrap_or_else(|| anyhow::anyhow!("no pane to show")));
            }
            let (foreground, background) = asked
                .or_else(|| panes.first().map(|p| (p.screen.foreground.clone(), p.screen.background.clone())))
                .unwrap_or_default();
            Ok(TabPreview { cols, rows, foreground, background, panes })
        })
    }

    /// What is selected in the focused pane, for the platform's clipboard.
    pub fn selected_text(&self) -> Option<String> {
        Self::selection_text(&self.inner.borrow())
    }

    pub fn layout_view(&self) -> String {
        Self::layout_json(&self.inner.borrow())
    }

    fn apply_frame_selections(&self, state: codec::KittyFrameSelections, sent: Option<f64>) {
        let mut inner = self.inner.borrow_mut();
        let Some((_, known)) = inner.frame_control.get(&state.pane_id) else { return };
        if let Err(err) = state.validate() {
            log::warn!("ignoring invalid Kitty metadata: {err}");
            return;
        }
        let stale = known.is_some_and(|revision| revision > state.revision);
        let received = inner.platform.monotonic_ms();
        inner.animation_clock.observe(state.now_ms, received, sent);
        if !stale {
            let (reset, rows_changed) = inner.host.image_versions.borrow_mut().observe(state.pane_id, state.image_epoch, &state.selections);
            if reset {
                inner.images.lock().clear();
                let inner = &mut *inner;
                inner.graphics.clear_images(&mut inner.gpu);
                for cell in inner.panes.values().chain(inner.parked.values()) {
                    cell.session.make_all_stale();
                }
            } else if rows_changed {
                if let Some(cell) = inner.panes.get(&state.pane_id) { cell.session.make_all_stale(); }
            }
            inner.frame_control.get_mut(&state.pane_id).unwrap().1 = Some(state.revision);
            inner.graphics.set_selections(state.pane_id, &state.selections);
        }
        drop(inner);
        self.request_frame();
    }

    fn sync_frame_control(self: &Rc<Self>) {
        let (link, requests) = {
            let mut inner = self.inner.borrow_mut();
            let removed: Vec<PaneId> = inner.frame_control.keys().filter(|id| !inner.panes.contains_key(id)).copied().collect();
            let added: Vec<PaneId> = inner.panes.keys().filter(|id| !inner.frame_control.contains_key(id)).copied().collect();
            let mut requests = Vec::new();
            for pane_id in removed {
                inner.frame_control.remove(&pane_id);
                inner.host.image_versions.borrow_mut().forget(pane_id);
                inner.graphics.set_selections(pane_id, &[]);
                requests.push((pane_id, false, 0));
            }
            for pane_id in added {
                inner.frame_control_epoch += 1;
                let epoch = inner.frame_control_epoch;
                inner.frame_control.insert(pane_id, (epoch, None));
                requests.push((pane_id, true, epoch));
            }
            (inner.link.clone(), requests)
        };
        for (pane_id, subscribe, epoch) in requests {
            let link = link.clone();
            let weak = Rc::downgrade(self);
            self.spawn(async move {
                let Some(start) = weak.upgrade().map(|app| app.platform.monotonic_ms()) else { return; };
                let result = thinkterm_session::host::request(
                    &link, Pdu::GetKittyFrameSelections(codec::GetKittyFrameSelections { pane_id, subscribe }),
                    |pdu| match pdu {
                        Pdu::KittyFrameSelections(state) => Ok(Some(state)),
                        Pdu::UnitResponse(_) => Ok(None),
                        other => Err(other),
                    },
                ).await;
                if !subscribe { return; }
                let Some(app) = weak.upgrade().filter(|app| !app.retired.get()) else { return };
                if !app.inner.borrow().frame_control.get(&pane_id).is_some_and(|(current, _)| *current == epoch) { return; }
                match result {
                    Ok(Some(state)) if state.pane_id == pane_id => app.apply_frame_selections(state, Some(start)),
                    _ => {
                        // Older peers do not provide this extension. Pixel
                        // delivery still works through the existing protocol.
                        let mut inner = app.inner.borrow_mut();
                        if let Some((_, revision)) = inner.frame_control.get_mut(&pane_id) {
                            if revision.is_none() { *revision = Some(0); }
                        }
                        drop(inner);
                        app.request_frame();
                    }
                }
            });
        }
    }

    /// The server pushed something for us.
    pub fn on_push(self: &Rc<Self>, pdu: Pdu) {
        let inner = self.inner.borrow();
        match pdu {
            Pdu::KittyFrameSelections(state) => {
                drop(inner);
                self.apply_frame_selections(state, None);
            }
            // Every pane on the server pushes to every client; only the
            // ones on this page are wanted, and the rest are simply not
            // ours (never a reason to re-list: there are many).
            Pdu::GetPaneRenderChangesResponse(delta) => {
                if let Some(cell) = inner.panes.get(&delta.pane_id).or_else(|| inner.parked.get(&delta.pane_id)) {
                    cell.session.queue_render_delta(delta);
                }
            }
            Pdu::PaneRemoved(removed) if inner.parked.contains_key(&removed.pane_id) => {
                drop(inner);
                self.inner.borrow_mut().parked.remove(&removed.pane_id);
                // The strip and the sidebar list it still.
                self.refresh_layout_soon();
                self.refresh_session_soon();
            }
            Pdu::PaneRemoved(removed) if inner.panes.contains_key(&removed.pane_id) => {
                let cell = &inner.panes[&removed.pane_id];
                cell.session.set_dead(true);
                if removed.pane_id == inner.focused_pane {
                    Self::set_status(&inner, &tr("web-toast-pane-closed"));
                }
                // Something else to show, if the server has anything.
                drop(inner);
                self.refresh_layout();
            }
            // The desktop moved. Followed only while following; a page
            // that chose a pane is not dragged off it.
            Pdu::PaneFocused(focused) if inner.following && focused.pane_id != inner.focused_pane => {
                drop(inner);
                self.switch_to_pane(focused.pane_id, false);
            }
            // The strip is stale: a tab came or went, a pane closed
            // elsewhere, a title changed. Listed again, a moment later,
            // so a burst of these is one request.
            Pdu::PaneRemoved(_)
            | Pdu::TabAddedToWindow(_)
            | Pdu::TabTitleChanged(_)
            | Pdu::PaneLabelChanged(_)
            | Pdu::WindowTitleChanged(_)
            | Pdu::TabResized(_)
            | Pdu::WindowWorkspaceChanged(_) => {
                drop(inner);
                self.refresh_layout_soon();
                self.refresh_session_soon();
            }
            // Sent once per connection, before the first line change: a
            // program's OSC 4/10/11 colours. Dropping it would leave the
            // page on the stock palette for good.
            Pdu::SetApplicationPalette(codec::SetApplicationPalette { pane_id, palette }) => {
                drop(inner);
                let mut inner = self.inner.borrow_mut();
                inner.overrides.insert(pane_id, palette.clone());
                if let Some(cell) = inner.panes.get_mut(&pane_id) {
                    cell.application = palette;
                    let changed = inner.recompute_palettes();
                    drop(inner);
                    self.repaint_palettes(&changed);
                }
            }
            // The server's own `color_scheme`: the base under any override,
            // unless this browser picked a scheme of its own.
            Pdu::DefaultPalette(codec::DefaultPalette { palette }) => {
                drop(inner);
                let mut inner = self.inner.borrow_mut();
                if inner.server_palette.as_ref() == Some(&palette) {
                    return;
                }
                inner.server_palette = Some(palette);
                let changed = inner.recompute_palettes();
                drop(inner);
                self.repaint_palettes(&changed);
            }
            Pdu::ThinkTermTreeState(state) => {
                drop(inner);
                self.inner.borrow_mut().tree.apply_tree(state.tree);
                // The session view is not pushed with it: ask.
                self.refresh_session_soon();
                Self::notify(&self.inner.borrow());
            }
            Pdu::ThinkTermSessionState(state) => {
                drop(inner);
                let changed = self.inner.borrow_mut().tree.apply_session(state);
                if changed {
                    Self::notify(&self.inner.borrow());
                }
            }
            Pdu::AgentStatusChanged(codec::AgentStatusChanged { pane_id, status }) => {
                drop(inner);
                self.inner.borrow_mut().tree.apply_agent(pane_id, status.as_ref());
                Self::render_strip(&self.inner.borrow());
                // A result that lands in the pane on show is seen as it lands.
                self.mark_seen(pane_id);
            }
            Pdu::ForegroundProgramChanged(codec::ForegroundProgramChanged { pane_id, program }) => {
                drop(inner);
                let mut inner = self.inner.borrow_mut();
                match program {
                    Some(program) => {
                        inner.programs.insert(pane_id, program);
                    }
                    None => {
                        inner.programs.remove(&pane_id);
                    }
                }
                Self::notify(&inner);
            }
            Pdu::ClientViewportState(_) | Pdu::FrontendAccessState(_) => {
                drop(inner);
                // The desktop may have resized: the panes are listed again
                // and the page's report follows that listing (a report
                // from the old frames would not compose to the new size);
                // its cell is taken now.
                self.match_desktop_cell();
                self.refresh_layout_soon();
                self.refresh_status();
                self.request_frame();
            }
            // The plugin host, through the server; see `plugin_frame`.
            Pdu::PluginFrame(codec::PluginFrame { data }) => {
                drop(inner);
                self.plugin_frame(data);
            }
            Pdu::SetClipboard(clip) => {
                // Every pane on the server pushes to every client. Only a
                // pane this page shows may write the device's clipboard,
                // the same gate the render deltas above pass through: a
                // pane nobody is looking at is a program nobody chose.
                let shown = inner.panes.contains_key(&clip.pane_id)
                    || inner.parked.contains_key(&clip.pane_id);
                if let (true, Some(text)) = (shown, clip.clipboard) {
                    inner.platform.clipboard_write(&text);
                }
            }
            _ => {}
        }
    }

    pub fn on_close(self: &Rc<Self>, reason: String) {
        {
            let mut inner = self.inner.borrow_mut();
            if inner.disconnected.is_some() {
                return;
            }
            inner.disconnected = Some(reason.clone());
            // The server's connection to the plugin host went with it.
            inner.snippets.connection_lost();
            inner.plugin_list.connection_lost();
            inner.plugin_pipe = false;
            inner.plugin_calls.lost("the connection to the server closed");
            // Opened again as soon as the connection is back.
            let now = inner.platform.monotonic_ms();
            if let Some(panel) = inner.plugin_panel.as_mut() {
                panel.lost(now);
            }
            if let Some(extended) = inner.plugin_extended.as_mut() {
                extended.lost(now);
            }
            // The connection that just died settles its own backoff here:
            // one that held for a while earns the short delay back, one
            // that did not keeps the long one. Left set, a frame drawn
            // during the outage would forgive a connection that is gone.
            if let Some(at) = inner.connected_since.take() {
                if inner.platform.monotonic_ms() - at >= RECONNECT_STABLE_MS {
                    inner.reconnect_delay = RECONNECT_MIN_MS;
                }
            }
            // Nothing more will arrive on this socket: the watchdog must
            // stop asking, or the page keeps requesting lines that never
            // come. `reconnected` turns it back on.
            for cell in inner.panes.values().chain(inner.parked.values()) {
                cell.session.set_dead(true);
            }
            // An ask on this socket will not be answered; the card must
            // not sit at "taking" with its button disabled.
            if inner.claim == Claim::Taking {
                inner.claim = Claim::Idle;
                Self::notify(&inner);
            }
            log::warn!("connection lost: {reason}");
        }
        self.show_reconnect_status();
        self.schedule_reconnect(0.0);
    }

    /// What the status line says while there is no connection.
    ///
    /// The reason is shown as well as the count, because the page cannot
    /// tell the interesting cases apart: a browser is not told why an
    /// upgrade was refused, so a revoked link, an expired one and a server
    /// that is simply not running all arrive here as the same closed
    /// socket. After a while the message says so rather than counting up
    /// for ever in silence.
    fn show_reconnect_status(&self) {
        let inner = self.inner.borrow();
        let Some(reason) = inner.disconnected.clone() else {
            return;
        };
        let mut args = thinkterm_i18n::FluentArgs::new();
        args.set("reason", reason);
        let text = if inner.reconnect_attempts >= RECONNECT_DOUBT_AFTER {
            args.set("n", inner.reconnect_attempts);
            thinkterm_i18n::tr_args("web-toast-still-reconnecting", &args)
        } else {
            thinkterm_i18n::tr_args("web-toast-reconnecting", &args)
        };
        Self::set_status(&inner, &text);
    }

    /// Come back and try again. `0.0` means as soon as the browser will.
    fn schedule_reconnect(self: &Rc<Self>, delay_ms: f64) {
        {
            let mut inner = self.inner.borrow_mut();
            if inner.reconnect_pending || inner.disconnected.is_none() {
                return;
            }
            if !inner.settings.auto_reconnect {
                // Asked not to: the remark stays, sticky, until a reconnect
                // is asked for.
                let mut args = thinkterm_i18n::FluentArgs::new();
                args.set("reason", inner.disconnected.clone().unwrap_or_default());
                Self::set_status(
                    &inner,
                    &thinkterm_i18n::tr_args("web-toast-connection-lost", &args),
                );
                return;
            }
            inner.reconnect_pending = true;
        }
        let weak = Rc::downgrade(self);
        self.platform.set_timeout(
            delay_ms.max(0.0),
            Box::new(move || {
                // A client the page has closed does not come back.
                let Some(app) = weak.upgrade().filter(|app| !app.retired.get()) else { return };
                app.inner.borrow_mut().reconnect_pending = false;
                let task = Rc::clone(&app).try_reconnect();
                app.spawn(task);
            }),
        );
    }

    /// One attempt: reopen the socket, redo the handshake on the same pane,
    /// and refetch everything on screen.
    async fn try_reconnect(self: Rc<Self>) {
        let (link, pane_id, tab_id, size, nav_rows) = {
            let inner = self.inner.borrow();
            if inner.disconnected.is_none() {
                return;
            }
            let size = inner.link.lease().reported;
            (
                inner.link.clone(),
                inner.focused_pane,
                inner.tab_id,
                size,
                Self::nav_rows(&inner),
            )
        };
        let outcome = async {
            link.reconnect().await?;
            crate::attach::reattach(&link, tab_id, pane_id, size, nav_rows).await
        }
        .await;
        match outcome {
            Ok(list) => {
                self.reconnected();
                self.plugins_follow();
                // The tab is laid out again from the fresh listing: panes
                // that came or went while the socket was down are taken
                // up or let go, and the ones that stayed keep their cells.
                self.show(list, pane_id).await;
            }
            Err(err) => {
                // A server with nothing to show will not grow something by
                // being asked again, and neither will one whose protocol
                // this bundle cannot speak. Retrying either one for ever
                // would only hide the reason.
                let permanent = err.downcast_ref::<crate::attach::NoPanes>().is_some()
                    || err.to_string().contains("update the server or the bundle");
                if permanent {
                    // Nothing will read this socket again, and the server
                    // keeps a registered client and a TCP session for as
                    // long as one is open. Hand it back.
                    link.shutdown();
                    let inner = self.inner.borrow();
                    Self::set_status(&inner, &format!("{err:#}"));
                    log::error!("not reconnecting: {err:#}");
                    return;
                }
                let delay = {
                    let mut inner = self.inner.borrow_mut();
                    inner.reconnect_attempts += 1;
                    inner.reconnect_delay =
                        (inner.reconnect_delay * 2.0).clamp(RECONNECT_MIN_MS, RECONNECT_MAX_MS);
                    inner.reconnect_delay
                };
                log::warn!("reconnect failed, retrying in {delay:.0} ms: {err:#}");
                self.show_reconnect_status();
                self.schedule_reconnect(delay);
            }
        }
    }

    /// The socket is back. Everything on screen was fetched from a server
    /// that has since forgotten us, so none of it may be trusted: the rows
    /// go stale and are asked for again.
    fn reconnected(self: &Rc<Self>) {
        {
            let mut inner = self.inner.borrow_mut();
            inner.disconnected = None;
            inner.reconnect_attempts = 0;
            // The backoff is *not* reset here. A server that accepts and
            // then drops -- one restarting in a loop, a proxy closing idle
            // sockets -- would otherwise be hammered at the minimum delay
            // for ever, because every attempt "succeeds" for the moment it
            // takes to hand back a socket. `frame` clears it once the
            // connection has proved it can carry a frame.
            inner.connected_since = Some(inner.platform.monotonic_ms());
            inner.session_refresh_pending = false;
            inner.images.lock().clear();
            inner.host.image_versions.borrow_mut().reset();
            {
                let inner = &mut *inner;
                inner.graphics.clear(&mut inner.gpu);
            }
            let owners: Vec<PaneId> = inner.frame_control.keys().copied().collect();
            for pane in owners { inner.graphics.set_selections(pane, &[]); }
            inner.frame_control.clear();
            inner.animation_clock = crate::graphics::AnimationClock::default();
            for cell in inner.panes.values() {
                cell.session.set_dead(false);
                cell.session.make_all_stale();
            }
            // A parked pane's rows may have moved while the socket was
            // down; it is polled again when it comes back on show.
            for cell in inner.parked.values_mut() {
                cell.session.set_dead(false);
                cell.session.make_all_stale();
                cell.reconnect_stale = true;
            }
            log::info!("reconnected to pane {}", inner.focused_pane);
            // The outage's remark ("connection lost; reconnecting") was
            // sticky for as long as it was true; it is not any more.
            *inner.toast.borrow_mut() = None;
            // The server let the lease go with the socket. The listing
            // that follows is a fresh showing: it claims the tab again,
            // silently, unless another device took it meanwhile.
            inner.auto_claimed = None;
        }
        self.sync_frame_control();
        self.refresh_status();
        self.request_frame();
    }

    fn focused_placement(inner: &Inner<P, L>) -> Option<crate::layout::PanePlacement> {
        Self::placements(inner)
            .into_iter()
            .find(|p| p.pane_id == inner.focused_pane)
    }

    fn refresh_status(&self) {
        let inner = self.inner.borrow();
        if inner.disconnected.is_some() {
            return;
        }
        let owner = inner.link.lease().owns_viewport();
        let (cols, rows) = inner
            .tab_layout
            .as_ref()
            .map(|l| (l.cols, l.rows))
            .unwrap_or((inner.cols, inner.rows));
        let lease = inner.link.lease();
        let mut text = if owner {
            format!("{}  ·  {}x{}  ·  this browser has the terminal", inner.title(), cols, rows)
        } else if !lease.may_type() {
            format!(
                "{}  ·  {}x{}  ·  another device has the terminal (Ctrl+Shift+T to take over)",
                inner.title(), cols, rows
            )
        } else {
            format!("{}  ·  {}x{}  ·  mirroring the desktop", inner.title(), cols, rows)
        };
        drop(lease);
        if cols > inner.cols || rows > inner.rows {
            text.push_str(&format!("  ·  this window fits {}x{}", inner.cols, inner.rows));
        }
        // The one-line summary is for probes and tests; the page shows
        // the card and the strip's hint instead.
        *inner.summary.borrow_mut() = text;
        Self::render_strip(&inner);
    }

    fn spawn_drain(
        platform: &P,
        session: &Arc<PaneSession<AppHost<P, L>>>,
        start: Result<bool, thinkterm_session::input::InputQueueFull>,
    ) {
        match start {
            Ok(true) => {
                platform.spawn(Box::pin(Arc::clone(session).drain_inputs()));
                session.update_last_send();
            }
            Ok(false) => session.update_last_send(),
            Err(full) => log::warn!("input refused: {full}"),
        }
    }

    /// Returns true when the key was consumed (the browser must not act
    /// on it). `shift` is the key's real Shift state: for printable keys
    /// it is folded into the character and absent from `mods`, and the
    /// character's case cannot stand in for it (Caps Lock).
    pub fn key_down(self: &Rc<Self>, key: KeyCode, mods: Modifiers, shift: bool) -> bool {
        let mut inner = self.inner.borrow_mut();
        if inner.composing || inner.disconnected.is_some() {
            return false;
        }
        // Copy and paste chords belong to the page: Cmd+C/V on a Mac,
        // Ctrl+Shift+C/V elsewhere. Copy what is selected; let the paste
        // event carry the clipboard. Every other Cmd chord is the
        // browser's (reload, address bar, tabs), not the pane's.
        let cmd = mods == Modifiers::SUPER;
        let ctrl_shift = mods == Modifiers::CTRL && shift;
        match key {
            KeyCode::Char('c') | KeyCode::Char('C') if cmd || ctrl_shift => {
                if let Some(text) = Self::selection_text(&inner) {
                    inner.platform.clipboard_write(&text);
                }
                return true;
            }
            KeyCode::Char('v') | KeyCode::Char('V') if cmd || ctrl_shift => return false,
            // Ctrl+Shift+Enter splits to the right, Ctrl+Shift+\ below,
            // Ctrl+Shift+Z zooms; the same as the strip's buttons.
            KeyCode::Enter if ctrl_shift => {
                drop(inner);
                self.split(None, thinkterm_proto::SplitDirection::Horizontal);
                return true;
            }
            KeyCode::Char('\\') | KeyCode::Char('|') if ctrl_shift => {
                drop(inner);
                self.split(None, thinkterm_proto::SplitDirection::Vertical);
                return true;
            }
            KeyCode::Char('z') | KeyCode::Char('Z') if ctrl_shift => {
                drop(inner);
                self.toggle_zoom(None);
                return true;
            }
            // Ctrl+Shift+F fits the tab to this window; Ctrl+Shift+T
            // takes the terminal over from another device (Handoff mode).
            KeyCode::Char('f') | KeyCode::Char('F') if ctrl_shift => {
                drop(inner);
                self.fit(true);
                return true;
            }
            // Cmd+= / Cmd+- / Cmd+0 change the terminal's size here, as on
            // the desktop, not the browser's zoom.
            KeyCode::Char('=') | KeyCode::Char('+') if cmd => {
                drop(inner);
                self.step_font(1.0);
                return true;
            }
            KeyCode::Char('-') | KeyCode::Char('_') if cmd => {
                drop(inner);
                self.step_font(-1.0);
                return true;
            }
            KeyCode::Char('0') if cmd => {
                drop(inner);
                self.step_font(0.0);
                return true;
            }
            KeyCode::Char('t') | KeyCode::Char('T') if ctrl_shift => {
                drop(inner);
                self.take_over();
                return true;
            }
            // Ctrl+Shift+arrow moves the focus between the tab's panes,
            // locally: dragging the desktop along is a click's job.
            KeyCode::LeftArrow | KeyCode::RightArrow | KeyCode::UpArrow | KeyCode::DownArrow
                if mods == Modifiers::CTRL | Modifiers::SHIFT || ctrl_shift =>
            {
                let next = Self::neighbour(&inner, key);
                drop(inner);
                if let Some(next) = next {
                    self.focus_pane(next, false);
                }
                return true;
            }
            _ if mods.contains(Modifiers::SUPER) => return false,
            _ => {}
        }
        // After the chords: a key from a page that does not hold the
        // terminal asks for it, like a click on the desktop takes it back,
        // and is typed once the server hands it over.
        if inner.link.lease().needs_claim() {
            inner.after_take_over = Some(AfterTakeOver::Key(key, mods, shift));
            drop(inner);
            self.take_over();
            return true;
        }
        let serial = codec::InputSerial::from_millis(
            thinkterm_session::clock::Clock::wall_millis(&inner.host.clock),
        );
        let mods = KeyModifiers::from_bits_truncate(mods.bits());
        // Typing goes back to following the output.
        let platform = Rc::clone(&inner.platform);
        let cell = inner.focused_mut();
        cell.scroll_from_bottom = 0;
        cell.scroll_px = 0.0;
        cell.selection = None;
        let start = cell.session.key_down(serial, key, mods);
        Self::spawn_drain(&platform, &cell.session, start);
        drop(inner);
        self.request_frame();
        true
    }

    pub fn composing(self: &Rc<Self>, composing: bool) {
        {
            let mut inner = self.inner.borrow_mut();
            inner.composing = composing;
            if inner.preedit.is_empty() {
                return;
            }
            // A composition that starts afresh, or ends either way, leaves
            // nothing drawn at the cursor.
            inner.preedit.clear();
        }
        self.request_frame();
    }

    /// The input method's text so far, to draw at the cursor as the
    /// desktop does; the keys it took are not the terminal's yet.
    pub fn preedit(self: &Rc<Self>, text: &str) {
        {
            let mut inner = self.inner.borrow_mut();
            if inner.preedit == text {
                return;
            }
            inner.preedit.clear();
            inner.preedit.push_str(text);
        }
        self.request_frame();
    }

    /// Whether input may go out at all. In Handoff mode with the
    /// terminal in someone else's hands the server would drop it; the
    /// status line says so and offers to take over.
    fn may_type(inner: &Inner<P, L>) -> bool {
        if inner.link.lease().may_type() {
            return true;
        }
        Self::set_status(
            inner,
            "another device is using this terminal; press Ctrl+Shift+T to take it over",
        );
        false
    }

    /// Text the IME composed: bytes, not a paste, so no bracketing.
    pub fn text(&self, text: &str) {
        let mut inner = self.inner.borrow_mut();
        if inner.disconnected.is_some() || !Self::may_type(&inner) {
            return;
        }
        let platform = Rc::clone(&inner.platform);
        let cell = inner.focused_mut();
        cell.scroll_from_bottom = 0;
        cell.scroll_px = 0.0;
        let start = cell.session.write_bytes(text.as_bytes());
        Self::spawn_drain(&platform, &cell.session, start);
        drop(inner);
        self.request_frame();
    }

    pub fn paste(&self, text: &str) {
        let mut inner = self.inner.borrow_mut();
        if inner.disconnected.is_some() || !Self::may_type(&inner) {
            return;
        }
        let platform = Rc::clone(&inner.platform);
        let cell = inner.focused_mut();
        cell.scroll_from_bottom = 0;
        cell.scroll_px = 0.0;
        let start = cell.session.paste(text);
        Self::spawn_drain(&platform, &cell.session, start);
        drop(inner);
        self.request_frame();
    }

    fn set_cursor(inner: &Inner<P, L>, cursor: &str) {
        inner.platform.set_cursor(cursor);
    }

    /// The divider under a point on the canvas, and the point's position
    /// along the divider's axis in device px.
    fn divider_under(inner: &Inner<P, L>, client_x: f64, client_y: f64) -> Option<(usize, f64)> {
        let layout = inner.tab_layout.as_ref()?;
        let [left, top, _, _] = inner.canvas_rect;
        let pad = Self::pad(inner);
        let px = (client_x - left) * inner.dpr - pad.0 as f64;
        let py = (client_y - top) * inner.dpr - pad.1 as f64;
        let (cw, ch) = (
            inner.glyphs.metrics.cell_size.width as f64,
            inner.glyphs.metrics.cell_size.height as f64,
        );
        let (col, row) = cell_at(px.max(0.0), py.max(0.0), cw, ch);
        layout.dividers.iter().enumerate().find_map(|(i, d)| match *d {
            crate::layout::Divider::Col { col: c, top, rows } if col == c && row >= top && row < top + rows => Some((i, px)),
            crate::layout::Divider::Row { row: r, left, cols } if row == r && col >= left && col < left + cols => Some((i, py)),
            _ => None,
        })
    }

    /// A finger is no pointer: a press within a finger's width of a divider
    /// starts dragging it, as a press right on it does for a mouse. The
    /// drag is measured from the finger, so the line follows it from where
    /// it landed. The moves and the release are the pointer's own.
    pub fn touch_divider(&self, client_x: f64, client_y: f64) -> bool {
        const NEAR: [f64; 7] = [0.0, -6.0, 6.0, -12.0, 12.0, -18.0, 18.0];
        let mut inner = self.inner.borrow_mut();
        let dpr = inner.dpr;
        let hit = NEAR.iter().find_map(|d| {
            Self::divider_under(&inner, client_x, client_y + d)
                .filter(|(i, _)| matches!(inner.tab_layout.as_ref().and_then(|l| l.dividers.get(*i)), Some(crate::layout::Divider::Row { .. })))
                .map(|(i, at)| (i, at - d * dpr))
                .or_else(|| {
                    Self::divider_under(&inner, client_x + d, client_y)
                        .filter(|(i, _)| matches!(inner.tab_layout.as_ref().and_then(|l| l.dividers.get(*i)), Some(crate::layout::Divider::Col { .. })))
                        .map(|(i, at)| (i, at - d * dpr))
                })
        });
        let Some((idx, at)) = hit else {
            return false;
        };
        inner.drag_divider = Some((idx, at, 0));
        inner.drag_layout = inner.tab_layout.clone();
        true
    }

    /// A press on a divider starts dragging it; moves send whole cells
    /// to the server as they accrue; the release ends it. A move over a
    /// divider shows the resize cursor.
    fn divider_pointer(self: &Rc<Self>, ev: &PointerInput, what: Pointer) {
        let (x, y) = (ev.x, ev.y);
        match what {
            Pointer::Down => {
                let mut inner = self.inner.borrow_mut();
                if let Some((idx, at)) = Self::divider_under(&inner, x, y) {
                    inner.drag_divider = Some((idx, at, 0));
                    inner.drag_layout = inner.tab_layout.clone();
                }
            }
            Pointer::Move => {
                let step = {
                    let mut inner = self.inner.borrow_mut();
                    let Some((idx, start, sent)) = inner.drag_divider else {
                        let over = Self::divider_under(&inner, x, y).and_then(|(i, _)| inner.tab_layout.as_ref()?.dividers.get(i).copied());
                        let cursor = match over {
                            Some(crate::layout::Divider::Col { .. }) => "col-resize",
                            Some(crate::layout::Divider::Row { .. }) => "row-resize",
                            None => "default",
                        };
                        Self::set_cursor(&inner, cursor);
                        return;
                    };
                    let [left, top, _, _] = inner.canvas_rect;
                    let pad = Self::pad(&inner);
                    let divider = inner.tab_layout.as_ref().and_then(|l| l.dividers.get(idx).copied());
                    let (pos, cell) = match divider {
                        Some(crate::layout::Divider::Col { .. }) => (
                            (x - left) * inner.dpr - pad.0 as f64,
                            inner.glyphs.metrics.cell_size.width as f64,
                        ),
                        Some(crate::layout::Divider::Row { .. }) => (
                            (y - top) * inner.dpr - pad.1 as f64,
                            inner.glyphs.metrics.cell_size.height as f64,
                        ),
                        None => return,
                    };
                    let cells = ((pos - start) / cell.max(1.0)).round() as isize;
                    if cells == sent {
                        return;
                    }
                    inner.drag_divider = Some((idx, start, cells));
                    (idx, cells - sent, cells)
                };
                // The page's own claim, pane by pane, keeps every bar's rows
                // as the divider moves; a page that does not hold the tab
                // can only ask the server to grow a pane.
                if self.inner.borrow().link.lease().owns_viewport() {
                    self.drag_divider_to(step.0, step.2, false);
                } else {
                    self.adjust_divider(step.0, step.1);
                }
            }
            Pointer::Up => {
                let was = self.inner.borrow_mut().drag_divider.take();
                if let Some((idx, _, cells)) = was {
                    // Release mode held every step back: the lift is when
                    // the server hears where the divider ended up.
                    let settle = {
                        let inner = self.inner.borrow();
                        cells != 0
                            && inner.settings.resize_mode == crate::settings::ResizeMode::Release
                            && inner.link.lease().owns_viewport()
                    };
                    if settle {
                        self.drag_divider_to(idx, cells, true);
                    }
                }
                self.inner.borrow_mut().drag_layout = None;
                if was.is_some() {
                    // The held-back report goes out with a fresh listing,
                    // and not deduplicated: the drag put every pane at its
                    // whole frame, whatever this page reported last, even
                    // when the divider is back where it started.
                    self.inner.borrow().link.lease_mut().reported_viewport = None;
                    self.refresh_layout();
                }
            }
        }
    }

    /// Keep every drawn pane's rows on screen across a resize the server
    /// has yet to answer: a live-resize preview at the size each pane is
    /// expected to get. Rows are normalised to that width and refetched,
    /// never dropped, until a listing settles the sizes (`settle_previews`).
    fn preview_panes(inner: &mut Inner<P, L>, sizes: &[(PaneId, TerminalSize)]) {
        use thinkterm_session::lines::FrontendPreviewPolicy;
        inner.preview_epoch += 1;
        let epoch = inner.preview_epoch;
        for (pane, size) in sizes {
            if let Some(cell) = inner.panes.get_mut(pane) {
                cell.session.begin_frontend_preview(epoch, *size, FrontendPreviewPolicy::LiveResize);
                cell.preview = Some((epoch, *size, inner.platform.monotonic_ms()));
            }
        }
    }

    /// The layout with every frame scaled to a `cols` x `rows` grid, edges
    /// rounded so the frames still tile: what the tab looks like once it
    /// takes this window's grid, near enough to claim pane by pane.
    fn scaled_layout(inner: &Inner<P, L>, cols: usize, rows: usize) -> Option<crate::layout::TabLayout> {
        let layout = inner.tab_layout.as_ref()?;
        if layout.cols == 0 || layout.rows == 0 {
            return None;
        }
        let (cw, ch) = (
            inner.glyphs.metrics.cell_size.width as usize,
            inner.glyphs.metrics.cell_size.height as usize,
        );
        crate::layout::scaled(
            layout,
            TerminalSize {
                cols,
                rows,
                pixel_width: cols * cw,
                pixel_height: rows * ch,
                dpi: (inner.platform.units_per_inch() * inner.dpr) as u32,
            },
        )
    }

    /// The sizes the panes would have if the tab took this grid, scaled
    /// from their frames: near enough for a preview, which only has to
    /// keep rows on screen until the server says the exact size.
    #[allow(dead_code)]
    fn scaled_pane_sizes(inner: &Inner<P, L>, cols: usize, rows: usize) -> Vec<(PaneId, TerminalSize)> {
        let Some(layout) = inner.tab_layout.as_ref() else {
            return vec![];
        };
        if layout.cols == 0 || layout.rows == 0 {
            return vec![];
        }
        let nav_rows = Self::nav_rows(inner);
        let (cw, ch) = (
            inner.glyphs.metrics.cell_size.width as usize,
            inner.glyphs.metrics.cell_size.height as usize,
        );
        let dpi = (inner.platform.units_per_inch() * inner.dpr) as u32;
        layout
            .panes
            .iter()
            .map(|p| {
                let c = (p.frame.cols * cols + layout.cols / 2) / layout.cols;
                let r = (p.frame.rows * rows + layout.rows / 2) / layout.rows;
                let r = r.saturating_sub(nav_rows).max(1);
                let c = c.max(1);
                (p.pane_id, TerminalSize { cols: c, rows: r, pixel_width: c * cw, pixel_height: r * ch, dpi })
            })
            .collect()
    }

    /// A listing is the server's word on every pane's size: a preview it
    /// confirms ends; one it contradicts is moved to the listed size, rows
    /// kept, so the change never shows as a blank pane.
    /// How long a preview may wait for the server to confirm its size
    /// before the page gives it up and shows what the server has: a
    /// preview holds the pane's rows at a guess and turns away pushes
    /// at any other size, so one the server never meets would leave
    /// the pane painted wrong until something else redrew it.
    const PREVIEW_PATIENCE_MS: f64 = 1_500.0;

    fn settle_previews(inner: &mut Inner<P, L>) {
        use thinkterm_session::lines::FrontendPreviewPolicy;
        let Some(layout) = inner.tab_layout.clone() else {
            return;
        };
        let now = inner.platform.monotonic_ms();
        for place in &layout.panes {
            let Some(cell) = inner.panes.get_mut(&place.pane_id) else {
                continue;
            };
            let listed = place.size;
            match cell.preview {
                Some((epoch, size, _)) if cell.session.server_geometry_matches(size) => {
                    cell.session.end_frontend_preview(epoch, true);
                    cell.preview = None;
                }
                Some((epoch, _, at)) if now - at > Self::PREVIEW_PATIENCE_MS => {
                    log::info!("pane {}: the server never met the previewed size; showing its own", place.pane_id);
                    cell.session.end_frontend_preview(epoch, false);
                    cell.preview = None;
                }
                Some((_, size, at)) if size != listed => {
                    inner.preview_epoch += 1;
                    let epoch = inner.preview_epoch;
                    cell.session.begin_frontend_preview(epoch, listed, FrontendPreviewPolicy::LiveResize);
                    cell.preview = Some((epoch, listed, at));
                    if cell.session.server_geometry_matches(listed) {
                        cell.session.end_frontend_preview(epoch, true);
                        cell.preview = None;
                    }
                }
                _ => {}
            }
        }
    }

    /// See `Client::panel_drag`. Letting go reshapes the tab to the grid
    /// the canvas ended at, in one claim.
    pub fn set_panel_drag(self: &Rc<Self>, on: bool) {
        {
            let mut inner = self.inner.borrow_mut();
            if inner.panel_drag == on {
                return;
            }
            inner.panel_drag = on;
            if !on {
                inner.cols = 0;
            }
        }
        if !on {
            self.resize();
        }
    }

    /// Another pane in `pane`'s stack, as the desktop's bar `+` makes;
    /// it is shown and focused.
    pub fn new_in_stack(self: &Rc<Self>, pane: PaneId) {
        self.act(
            "web-act-new-tab",
            Pdu::SpawnPaneInStack(codec::SpawnPaneInStack {
                pane_id: pane,
                command: None,
                command_dir: None,
                domain: thinkterm_proto::SpawnTabDomain::CurrentPaneDomain,
            }),
            |app, answer| {
                if let Pdu::SpawnResponse(spawned) = answer {
                    app.switch_to_pane(spawned.pane_id, false);
                }
            },
        );
    }

    /// Claim the tab with divider `idx` moved `cells` from where the drag
    /// began (right/down positive): every pane touching the divider along
    /// its extent gives or takes the cells, none below one content row.
    /// `settle` is the lift of the finger: in Release mode the only step
    /// the server hears.
    fn drag_divider_to(self: &Rc<Self>, idx: usize, cells: isize, settle: bool) {
        let (link, tab_id) = {
            let inner = self.inner.borrow();
            let Some(layout) = inner.drag_layout.as_ref() else {
                return;
            };
            let Some(moved) = crate::layout::with_divider_moved(layout, idx, cells, Self::nav_rows(&inner)) else {
                return;
            };
            let panes = Self::native_panes(&inner, &moved);
            let sizes: Vec<(PaneId, TerminalSize)> = panes.iter().map(|p| (p.pane_id, p.size)).collect();
            let mut lease = inner.link.lease_mut();
            lease.native = panes;
            lease.native_root = Some(moved.size);
            drop(lease);
            let tab_id = layout.tab_id;
            let link = inner.link.clone();
            drop(inner);
            let mut inner = self.inner.borrow_mut();
            Self::preview_panes(&mut inner, &sizes);
            // The picture follows every step; the wire takes one step at
            // a time, and the latest when it is free again.
            if inner.divider_claim_busy {
                inner.divider_claim_next = Some((idx, cells));
                return;
            }
            if inner.settings.resize_mode == crate::settings::ResizeMode::Release && !settle {
                // The picture moves; the server hears when the finger lifts.
                return;
            }
            inner.divider_claim_busy = true;
            (link, tab_id)
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            if let Err(err) = link.report_viewport(tab_id).await {
                log::warn!("moving the divider: {err:#}");
            }
            let next = {
                let mut inner = app.inner.borrow_mut();
                inner.divider_claim_busy = false;
                inner.divider_claim_next.take().filter(|_| inner.drag_divider.is_some())
            };
            match next {
                Some((idx, cells)) => app.drag_divider_to(idx, cells, false),
                None => app.refresh_layout(),
            }
        });
    }

    /// Move divider `idx` by `delta` cells (right/down positive): the
    /// server grows the pane on one side of it in that direction, so the
    /// pane next to the divider is focused first. Nested splits are
    /// approximate: the server picks the nearest split of that
    /// orientation above the pane.
    fn adjust_divider(self: &Rc<Self>, idx: usize, delta: isize) {
        use thinkterm_proto::keyassignment::PaneDirection;
        let (pane, direction, link, palette) = {
            let inner = self.inner.borrow();
            let Some(layout) = inner.tab_layout.as_ref() else {
                return;
            };
            let Some(divider) = layout.dividers.get(idx).copied() else {
                return;
            };
            let pick = |before: bool| -> Option<PaneId> {
                layout
                    .panes
                    .iter()
                    .find(|p| match divider {
                        crate::layout::Divider::Col { col, top, rows } => {
                            let edge = if before { p.frame.left + p.frame.cols == col } else { p.frame.left == col + 1 };
                            edge && p.frame.top < top + rows && p.frame.top + p.frame.rows > top
                        }
                        crate::layout::Divider::Row { row, left, cols } => {
                            let edge = if before { p.frame.top + p.frame.rows == row } else { p.frame.top == row + 1 };
                            edge && p.frame.left < left + cols && p.frame.left + p.frame.cols > left
                        }
                    })
                    .map(|p| p.pane_id)
            };
            let horizontal = matches!(divider, crate::layout::Divider::Col { .. });
            let (pane, direction) = if delta > 0 {
                (pick(true), if horizontal { PaneDirection::Right } else { PaneDirection::Down })
            } else {
                (pick(false), if horizontal { PaneDirection::Left } else { PaneDirection::Up })
            };
            let Some(pane) = pane else {
                return;
            };
            (pane, direction, inner.link.clone(), inner.configured())
        };
        let amount = delta.unsigned_abs();
        let app = Rc::clone(self);
        self.spawn(async move {
            // The server resizes the tab's active pane: make it this one.
            let _ = thinkterm_session::host::request(
                &link,
                Pdu::SetFocusedPane(codec::SetFocusedPane { pane_id: pane, configured_palette: Some(palette) }),
                |p| match p {
                    Pdu::UnitResponse(_) => Ok(()),
                    other => Err(other),
                },
            )
            .await;
            app.focus_pane(pane, false);
            app.act(
                "web-act-resize-pane",
                Pdu::AdjustPaneSize(codec::AdjustPaneSize { pane_id: pane, direction, amount }),
                |_, _| {},
            );
        });
    }

    /// Where a point on the canvas lands: which pane, and the cell within
    /// it. `None` on a divider or past the tab.
    fn hit_under(inner: &Inner<P, L>, client_x: f64, client_y: f64) -> Option<Hit> {
        let [left, top, _, _] = inner.canvas_rect;
        let pad = Self::pad(inner);
        let px = (client_x - left) * inner.dpr - pad.0 as f64;
        let py = (client_y - top) * inner.dpr - pad.1 as f64;
        if px < 0.0 || py < 0.0 {
            return None;
        }
        let (cw, ch) = (
            inner.glyphs.metrics.cell_size.width as f64,
            inner.glyphs.metrics.cell_size.height as f64,
        );
        let (col, row) = cell_at(px, py, cw, ch);
        let place = match Self::drawn_layout(inner) {
            Some(layout) => crate::layout::hit(layout, col, row)?.clone(),
            None => Self::focused_placement(inner)?,
        };
        Some(Self::hit_in(inner, &place, px, py))
    }

    /// The cell of `place` under a canvas point (device px past the
    /// padding), clamped into the pane: a drag that leaves the pane keeps
    /// reporting its edge. The frame is in the page's cells, the cells
    /// within it are the pane's own.
    fn hit_in(inner: &Inner<P, L>, place: &crate::layout::PanePlacement, px: f64, py: f64) -> Hit {
        let (cols, rows) = Self::shown_in(inner, place);
        let (cw, ch) = Self::pane_cell(inner, place);
        let root = inner.glyphs.metrics.cell_size;
        let nav = Self::content_offset(inner, place) as f64;
        let origin = (place.frame.left as f64 * root.width as f64, place.frame.top as f64 * root.height as f64 + nav);
        // Smooth scrolling draws the rows shifted up by `scroll_px`, with
        // one more row filling the strip that leaves at the bottom; the
        // row under a pixel moves with them, and the extra row is one the
        // pointer can reach. `row` stays an offset from the first row
        // drawn, which is what every caller adds to `visible_rows`'s start.
        let scroll_px = inner
            .panes
            .get(&place.pane_id)
            .map(|cell| (cell.scroll_px as f64).clamp(0.0, ch))
            .unwrap_or(0.0);
        let last = if scroll_px > 0.0 && rows > 0 { rows } else { rows.saturating_sub(1) };
        let local_col = (((px - origin.0) / cw).floor().max(0.0) as usize).min(cols.saturating_sub(1));
        let local_y = py - origin.1 + scroll_px;
        let local_row = ((local_y / ch).floor().max(0.0) as usize).min(last);
        let x_off = (px - origin.0 - local_col as f64 * cw).clamp(0.0, cw) as isize;
        let y_off = (local_y - local_row as f64 * ch).clamp(0.0, ch) as isize;
        Hit {
            pane_id: place.pane_id,
            col: local_col,
            row: local_row,
            x_off,
            y_off,
        }
    }

    /// Focus a pane that is on the page. `advise` tells the server, so
    /// the desktop follows: a click does when the page is following, a
    /// focus push (which came from the server) never does.
    pub fn focus_pane(self: &Rc<Self>, pane_id: PaneId, advise: bool) {
        let link = {
            let mut inner = self.inner.borrow_mut();
            if !inner.panes.contains_key(&pane_id) {
                return;
            }
            if inner.focused_pane == pane_id {
                drop(inner);
                self.mark_seen(pane_id);
                return;
            }
            inner.focused_pane = pane_id;
            inner.selecting = false;
            inner.drag_pane = None;
            inner.ime_anchor = None;
            inner.platform.set_title(inner.title());
            Self::render_strip(&inner);
            (inner.link.clone(), inner.configured())
        };
        self.refresh_status();
        self.request_frame();
        if advise {
            Self::advise_focus(&self.platform, link.0, pane_id, link.1);
        }
        self.mark_seen(pane_id);
    }

    /// A machine or tab has come on show, including one whose focus stayed put.
    pub fn mark_focused_program_status_seen(self: &Rc<Self>) {
        let focused = self.inner.borrow().focused_pane;
        self.mark_seen(focused);
    }

    /// Tell the server a pane has been looked at, when a program in it left
    /// a finished or failed result: only the mux that owns the pane keeps
    /// those, and it hears of a look no other way. Sent and forgotten.
    fn mark_seen(self: &Rc<Self>, pane_id: PaneId) {
        if self.retired.get() || !self.animations_visible.get() || !self.platform.shown() {
            return;
        }
        let link = {
            let inner = self.inner.borrow();
            if inner.focused_pane != pane_id
                || !inner.panes.contains_key(&pane_id)
                || !inner.tree.has_unseen_result(pane_id)
            {
                return;
            }
            inner.link.clone()
        };
        self.platform.spawn(Box::pin(async move {
            let pdu = Pdu::MarkProgramStatusSeen(codec::MarkProgramStatusSeen { pane_id });
            if let Err(err) = thinkterm_session::host::request(&link, pdu, |p| match p {
                Pdu::UnitResponse(_) => Ok(()),
                other => Err(other),
            })
            .await
            {
                log::warn!("result not marked seen: {err:#}");
            }
        }));
    }

    /// Tell the server (and so the desktop) which pane has the focus.
    /// Sent and forgotten: the desktop may ignore it for a few seconds
    /// after a focus move of its own, and the echo is the pane already
    /// focused here.
    /// The page's base palette goes with it: the server paints a pane
    /// with the colours of whoever focused it last, as it does for the
    /// desktop, so a pane the page uses shows the page's scheme.
    fn advise_focus(platform: &P, link: L, pane_id: PaneId, palette: ColorPalette) {
        platform.spawn(Box::pin(async move {
            let pdu = Pdu::SetFocusedPane(codec::SetFocusedPane {
                pane_id,
                configured_palette: Some(palette),
            });
            if let Err(err) = thinkterm_session::host::request(&link, pdu, |p| match p {
                Pdu::UnitResponse(_) => Ok(()),
                other => Err(other),
            })
            .await
            {
                log::warn!("focus not advised: {err:#}");
            }
        }));
    }

    /// The pane next to the focused one in `direction`: the nearest whose
    /// frame lies past the focused frame's edge on that side.
    fn neighbour(inner: &Inner<P, L>, direction: KeyCode) -> Option<PaneId> {
        let layout = inner.tab_layout.as_ref()?;
        let me = layout.panes.iter().find(|p| p.pane_id == inner.focused_pane)?;
        let (mx, my) = (
            me.frame.left as isize + me.frame.cols as isize / 2,
            me.frame.top as isize + me.frame.rows as isize / 2,
        );
        layout
            .panes
            .iter()
            .filter(|p| p.pane_id != me.pane_id)
            .filter(|p| match direction {
                KeyCode::LeftArrow => p.frame.left + p.frame.cols <= me.frame.left,
                KeyCode::RightArrow => p.frame.left >= me.frame.left + me.frame.cols,
                KeyCode::UpArrow => p.frame.top + p.frame.rows <= me.frame.top,
                KeyCode::DownArrow => p.frame.top >= me.frame.top + me.frame.rows,
                _ => false,
            })
            .min_by_key(|p| {
                let (x, y) = (
                    p.frame.left as isize + p.frame.cols as isize / 2,
                    p.frame.top as isize + p.frame.rows as isize / 2,
                );
                (x - mx).abs() + (y - my).abs()
            })
            .map(|p| p.pane_id)
    }

    pub fn pointer(self: &Rc<Self>, ev: &PointerInput, what: Pointer) {
        if what == Pointer::Down {
            // Focus first, outside any borrow: focus() dispatches events
            // synchronously and a listener may look at the app.
            self.platform.focus_input();
        }
        // A divider under the press is dragged, as on the desktop.
        if what != Pointer::Down && self.inner.borrow().drag_divider.is_some() {
            self.divider_pointer(ev, what);
            return;
        }
        // A press lands on the pane under it and focuses it; a drag stays
        // with the pane it started in.
        let hit = {
            let inner = self.inner.borrow();
            if inner.disconnected.is_some() {
                return;
            }
            match (what, inner.drag_pane) {
                (Pointer::Down, _) => Self::hit_under(&inner, ev.x, ev.y),
                (_, Some(drag)) => {
                    let place = Self::placements(&inner).into_iter().find(|p| p.pane_id == drag);
                    place.map(|place| {
                        let [left, top, _, _] = inner.canvas_rect;
                        let pad = Self::pad(&inner);
                        let px = (ev.x - left) * inner.dpr - pad.0 as f64;
                        let py = (ev.y - top) * inner.dpr - pad.1 as f64;
                        Self::hit_in(&inner, &place, px, py)
                    })
                }
                (_, None) => Self::hit_under(&inner, ev.x, ev.y),
            }
        };
        let Some(hit) = hit else {
            self.divider_pointer(ev, what);
            return;
        };
        if what == Pointer::Move {
            Self::set_cursor(&self.inner.borrow(), "text");
        }
        if what == Pointer::Down {
            let (advise, claim) = {
                let inner = self.inner.borrow();
                let claim = inner.link.lease().needs_claim();
                (inner.following, claim)
            };
            if claim {
                // The press takes the terminal over; the desktop is told
                // about the focus once the server has handed it over.
                self.focus_pane(hit.pane_id, false);
                self.inner.borrow_mut().after_take_over =
                    advise.then_some(AfterTakeOver::Focus(hit.pane_id));
                self.take_over();
                return;
            }
            self.focus_pane(hit.pane_id, advise);
        }
        let mut inner = self.inner.borrow_mut();
        if what == Pointer::Down && !Self::may_type(&inner) {
            return;
        }
        let Some(cell) = inner.panes.get(&hit.pane_id) else {
            return;
        };
        let session = Arc::clone(&cell.session);
        let scroll = cell.scroll_from_bottom;
        let dims = Self::shown_dims(&session);
        let rows_shown = Self::placements(&inner)
            .into_iter()
            .find(|p| p.pane_id == hit.pane_id)
            .map(|p| Self::shown_in(&inner, &p).1)
            .unwrap_or(inner.rows);
        let visible = visible_rows(&dims, rows_shown, scroll);
        let stable_row = visible.start + hit.row as StableRowIndex;
        let button = match ev.button {
            0 => MouseButton::Left,
            1 => MouseButton::Middle,
            2 => MouseButton::Right,
            255 => inner.held_button,
            _ => MouseButton::None,
        };
        match what {
            Pointer::Down => inner.held_button = button,
            Pointer::Up => inner.held_button = MouseButton::None,
            Pointer::Move => {}
        }

        // A program that asked for the mouse gets it, unless Shift holds
        // the event back for the page's own selection.
        if session.is_mouse_grabbed() && !ev.mods.contains(KeyModifiers::SHIFT) {
            let kind = match what {
                Pointer::Down => MouseEventKind::Press,
                Pointer::Up => MouseEventKind::Release,
                Pointer::Move => MouseEventKind::Move,
            };
            // A follower taller than the pane shows rows above its top;
            // those are nowhere for a program, as on the desktop.
            let event = MouseEvent {
                kind,
                x: hit.col,
                y: (stable_row - dims.physical_top).max(0) as i64,
                x_pixel_offset: hit.x_off,
                y_pixel_offset: hit.y_off,
                button: if what == Pointer::Move && !ev.buttons_down { MouseButton::None } else { button },
                modifiers: ev.mods,
            };
            let start = session.mouse_event(event);
            Self::spawn_drain(&inner.platform, &session, start);
            return;
        }

        match what {
            Pointer::Down if button == MouseButton::Left => {
                let t = inner.platform.monotonic_ms();
                inner.click_count = if t - inner.last_click_ms < 400.0 {
                    (inner.click_count % 3) + 1
                } else {
                    1
                };
                inner.last_click_ms = t;
                let mode = inner.click_count;
                if let Some(cell) = inner.panes.get_mut(&hit.pane_id) {
                    cell.selection = Some(Selection {
                        anchor: (stable_row, hit.col),
                        head: (stable_row, hit.col),
                        mode,
                    });
                }
                inner.selecting = true;
                inner.drag_pane = Some(hit.pane_id);
                // A click is a real interaction: it takes the terminal over.
                let link = inner.link.clone();
                let tab_id = inner.tab_id;
                self.spawn(async move {
                    let _ = link.ensure_owner(tab_id).await;
                });
            }
            Pointer::Move if inner.selecting => {
                if let Some(sel) = inner.panes.get_mut(&hit.pane_id).and_then(|c| c.selection.as_mut()) {
                    sel.head = (stable_row, hit.col);
                }
            }
            Pointer::Up if inner.selecting => {
                inner.selecting = false;
                inner.drag_pane = None;
                if let Some(sel) = inner.panes.get_mut(&hit.pane_id).and_then(|c| c.selection.as_mut()) {
                    sel.head = (stable_row, hit.col);
                }
                let empty = inner
                    .panes
                    .get(&hit.pane_id)
                    .and_then(|c| c.selection)
                    .map(|s| s.mode == 1 && s.anchor == s.head)
                    .unwrap_or(true);
                if empty {
                    if let Some(cell) = inner.panes.get_mut(&hit.pane_id) {
                        cell.selection = None;
                    }
                } else if let Some(text) = Self::selection_text(&inner) {
                    inner.platform.clipboard_write(&text);
                }
            }
            _ => return,
        }
        drop(inner);
        self.request_frame();
    }

    /// The selected cells of `row`, from the selection's shape and mode.
    fn selection_on_row(width: usize, sel: &Selection, row: StableRowIndex, line: &termwiz::surface::Line) -> std::ops::Range<usize> {
        let ((r0, c0), (r1, c1)) = sel.ordered();
        if row < r0 || row > r1 {
            return 0..0;
        }
        let cols = width.max(line.len());
        match sel.mode {
            3 => 0..cols,
            2 => {
                let word = |col: usize| -> std::ops::Range<usize> {
                    match line.compute_double_click_range(col, |s| {
                        s.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == '.' || c == '/')
                    }) {
                        termwiz::surface::line::DoubleClickRange::Range(r) => r,
                        termwiz::surface::line::DoubleClickRange::RangeWithWrap(r) => r,
                    }
                };
                let start = if row == r0 { word(c0).start } else { 0 };
                let end = if row == r1 { word(c1).end } else { cols };
                start..end
            }
            _ => {
                let start = if row == r0 { c0 } else { 0 };
                let end = if row == r1 { c1 + 1 } else { cols };
                start..end.max(start)
            }
        }
    }

    fn selection_text(inner: &Inner<P, L>) -> Option<String> {
        let sel = inner.focused().selection?;
        Self::text_of(inner, &sel)
    }

    /// What a selection covers in the focused pane, each row's trailing
    /// blanks dropped.
    fn text_of(inner: &Inner<P, L>, sel: &Selection) -> Option<String> {
        let ((r0, _), (r1, _)) = sel.ordered();
        let (first, lines) = inner.focused().session.get_lines(r0..r1 + 1);
        let width = Self::focused_placement(inner).map(|p| Self::shown(&p).0).unwrap_or(inner.cols);
        let mut out = Vec::new();
        for (i, line) in lines.iter().enumerate() {
            let row = first + i as StableRowIndex;
            let range = Self::selection_on_row(width, sel, row, line);
            let mut text = String::new();
            for cell in line.visible_cells() {
                if range.contains(&cell.cell_index()) {
                    text.push_str(cell.str());
                }
            }
            out.push(text.trim_end().to_string());
        }
        Some(out.join("\n"))
    }

    /// Returns true when the page acted on the wheel (the browser must
    /// not). The pane under the pointer scrolls, as on the desktop. A
    /// program that has the mouse gets every wheel with its real
    /// modifiers; otherwise Ctrl+wheel and pinch are the browser's zoom.
    pub fn wheel(self: &Rc<Self>, ev: &WheelInput) -> bool {
        let mut inner = self.inner.borrow_mut();
        if inner.disconnected.is_some() {
            return false;
        }
        let Some(hit) = Self::hit_under(&inner, ev.x, ev.y) else {
            return false;
        };
        // Scrolling asks for the terminal like a click does; this notch
        // is spent on that.
        if inner.link.lease().needs_claim() {
            inner.after_take_over = inner.following.then_some(AfterTakeOver::Focus(hit.pane_id));
            drop(inner);
            self.focus_pane(hit.pane_id, false);
            self.take_over();
            return true;
        }
        let place = Self::placements(&inner).into_iter().find(|p| p.pane_id == hit.pane_id);
        let rows_shown = place.as_ref().map(|p| Self::shown_in(&inner, p).1).unwrap_or(inner.rows);
        let cell_h = place
            .as_ref()
            .map(|p| Self::pane_cell(&inner, p).1)
            .unwrap_or(inner.glyphs.metrics.cell_size.height as f64);
        let cell_h_css = cell_h / inner.dpr;
        let lines = match ev.delta {
            WheelDelta::Lines(n) => n,
            WheelDelta::Pages(n) => n * rows_shown as f64,
            WheelDelta::Pixels(px) => px / cell_h_css,
        };
        let notches = lines.abs().round().max(if lines == 0.0 { 0.0 } else { 1.0 }) as usize;
        if notches == 0 {
            return false;
        }
        let smooth = inner.settings.scroll_mode.is_smooth();
        let Some(cell) = inner.panes.get_mut(&hit.pane_id) else {
            return false;
        };
        let session = Arc::clone(&cell.session);
        if session.is_alt_screen() || session.is_mouse_grabbed() {
            let button = if lines < 0.0 { MouseButton::WheelUp(notches) } else { MouseButton::WheelDown(notches) };
            let dims = Self::shown_dims(&session);
            let visible = visible_rows(&dims, rows_shown, cell.scroll_from_bottom);
            let event = MouseEvent {
                kind: MouseEventKind::Press,
                x: hit.col,
                y: (visible.start + hit.row as StableRowIndex - dims.physical_top).max(0) as i64,
                x_pixel_offset: hit.x_off,
                y_pixel_offset: hit.y_off,
                button,
                modifiers: ev.mods,
            };
            let start = session.mouse_event(event);
            Self::spawn_drain(&inner.platform, &session, start);
            return true;
        }
        if ev.ctrl {
            // A real Ctrl+wheel (a trackpad pinch on the desktop) stays the
            // browser's zoom. The page synthesises one for a two-finger
            // pinch on the canvas (touch.ts), where the browser's zoom is
            // switched off; that one changes the pane's font instead.
            if ev.trusted {
                return false;
            }
            let pane_id = hit.pane_id;
            let step = if lines < 0.0 { 1.0 } else { -1.0 };
            drop(inner);
            self.focus_pane(pane_id, false);
            self.step_font(step);
            return true;
        }
        let max = max_scroll(&Self::shown_dims(&session), rows_shown);
        if smooth {
            // Down the page (a positive delta) is towards the newest row,
            // which is the way `normalize_scroll_px` counts. A trackpad or
            // a finger hands over a fraction of a row at a time and keeps
            // it: nothing is rounded up to a notch.
            let (scroll, px) = crate::viewport::normalize_scroll_px(
                cell.scroll_from_bottom,
                cell.scroll_px,
                (lines * cell_h) as f32,
                cell_h as f32,
                max,
            );
            cell.scroll_from_bottom = scroll;
            cell.scroll_px = px;
        } else {
            cell.scroll_from_bottom = if lines < 0.0 {
                (cell.scroll_from_bottom + notches).min(max)
            } else {
                cell.scroll_from_bottom.saturating_sub(notches)
            };
            cell.scroll_px = 0.0;
        }
        // The scroll position is in the layout view: a scrollbar reads it.
        Self::notify(&inner);
        drop(inner);
        self.request_frame();
        true
    }

    pub fn focus(&self, focused: bool) {
        self.inner.borrow_mut().focused = focused;
        self.request_frame();
    }

    /// What the page has laid out, as JSON on the canvas element for the
    /// smoke tests and anyone else curious: `canvas.dataset.layout`.
    /// Rewritten whenever the strip is, which is whenever it changes.
    fn publish_layout(inner: &Inner<P, L>) {
        let json = Self::layout_json(inner);
        inner.platform.publish("layout", &json);
    }

    /// The layout as a probe reads it.
    fn layout_json(inner: &Inner<P, L>) -> String {
        let lease = inner.link.lease();
        let dpr = inner.dpr.max(0.1);
        let (cw, ch) = (
            inner.glyphs.metrics.cell_size.width as f64 / dpr,
            inner.glyphs.metrics.cell_size.height as f64 / dpr,
        );
        // The desktop's cell in CSS px, from the canonical size's pixels.
        let desktop_cell = lease
            .canonical_size
            .filter(|s| s.rows > 0 && s.dpi > 0)
            .map(|s| s.pixel_height as f64 / s.rows as f64 * inner.platform.units_per_inch() / s.dpi as f64)
            .unwrap_or(0.0);
        let mut json = format!(
            "{{\"tab\":{},\"canvas\":[{},{}],\"cell\":[{cw:.3},{ch:.3}],\"pad\":[{:.3},{:.3}],\"font_pt\":{},\"desktop_cell\":{desktop_cell:.3},\"focused\":{},\"owner\":{},\"may_type\":{},\"fit\":{},\"following\":{}",
            inner.tab_id,
            inner.cols,
            inner.rows,
            Self::pad(inner).0 as f64 / dpr,
            Self::pad(inner).1 as f64 / dpr,
            inner.glyphs.size_pt,
            inner.focused_pane,
            lease.owns_viewport(),
            lease.may_type(),
            lease.fit,
            inner.following,
        );
        // Where the focused pane is in its scrollback: rows above the
        // bottom, and rows there are to scroll through.
        if let Some(cell) = inner.panes.get(&inner.focused_pane) {
            let rows = match Self::drawn_layout(inner) {
                Some(layout) => layout.panes.iter().find(|place| place.pane_id == inner.focused_pane)
                    .map(|place| Self::shown_in(inner, place).1),
                None => Self::focused_placement(inner).as_ref().map(|place| Self::shown_in(inner, place).1),
            }.unwrap_or(inner.rows);
            let max = max_scroll(&Self::shown_dims(&cell.session), rows);
            // With the fraction of a row a smooth scroll is part way
            // through, so a bar following it moves as the rows do rather
            // than a row at a time.
            let cell_h = inner
                .pane_fonts
                .get(&inner.focused_pane)
                .map(|f| f.glyphs.metrics.cell_size.height as f32)
                .unwrap_or(inner.glyphs.metrics.cell_size.height as f32)
                .max(1.0);
            let above = cell.scroll_from_bottom as f32 + cell.scroll_px / cell_h;
            json.push_str(&format!(",\"scroll\":[{above:.3},{max}]"));
        }
        match Self::drawn_layout(inner) {
            Some(layout) => {
                json.push_str(&format!(",\"cols\":{},\"rows\":{},\"zoomed\":", layout.cols, layout.rows));
                match layout.zoomed {
                    Some(id) => json.push_str(&id.to_string()),
                    None => json.push_str("null"),
                }
                json.push_str(",\"panes\":[");
                for (i, p) in layout.panes.iter().enumerate() {
                    if i > 0 {
                        json.push(',');
                    }
                    let shown = Self::shown_in(inner, p);
                    json.push_str(&format!(
                        "{{\"id\":{},\"left\":{},\"top\":{},\"cols\":{},\"rows\":{},\"content\":[{},{}],\"shown\":[{},{}]}}",
                        p.pane_id, p.frame.left, p.frame.top, p.frame.cols, p.frame.rows, p.content.0, p.content.1, shown.0, shown.1
                    ));
                }
                json.push_str("],\"dividers\":[");
                for (i, d) in layout.dividers.iter().enumerate() {
                    if i > 0 {
                        json.push(',');
                    }
                    match *d {
                        crate::layout::Divider::Col { col, top, rows } => {
                            json.push_str(&format!("{{\"col\":{col},\"top\":{top},\"rows\":{rows}}}"))
                        }
                        crate::layout::Divider::Row { row, left, cols } => {
                            json.push_str(&format!("{{\"row\":{row},\"left\":{left},\"cols\":{cols}}}"))
                        }
                    }
                }
                json.push(']');
            }
            None => json.push_str(",\"cols\":null,\"rows\":null,\"zoomed\":null,\"panes\":[],\"dividers\":[]"),
        }
        json.push('}');
        json
    }

    /// Draw the tab strip from what the page knows.
    /// The desktop's cell in CSS px, from the canonical size's pixels.
    fn desktop_cell_css(inner: &Inner<P, L>) -> Option<f64> {
        inner
            .link
            .lease()
            .canonical_size
            .filter(|s| s.rows > 0 && s.dpi > 0)
            .map(|s| s.pixel_height as f64 / s.rows as f64 * inner.platform.units_per_inch() / s.dpi as f64)
    }

    /// The bar above each pane, in CSS px.
    fn nav_css(inner: &Inner<P, L>) -> f64 {
        if inner.platform.is_mobile() {
            return crate::navbar::MOBILE_NAV_CSS;
        }
        let cell_css = inner.glyphs.metrics.cell_size.height as f64 / inner.dpr.max(0.1);
        crate::navbar::nav_css(cell_css, Self::desktop_cell_css(inner))
    }

    /// The bar's height in device px. The pane gives up the rounded-up
    /// count of rows for it either way. A page in the desktop's shape
    /// draws the bar at its own height with the rows a pad under it
    /// (`content_offset`), as the desktop does: drawn to the rows it pays
    /// for, it left the rounding as a blank band under the capsules. A
    /// phone draws it to those rows, so its rows stay flush with the
    /// bottom of the screen (`pad`).
    fn nav_dev(inner: &Inner<P, L>) -> f32 {
        let rows = Self::nav_rows(inner);
        if inner.mobile || rows == 0 {
            return rows as f32 * inner.glyphs.metrics.cell_size.height as f32;
        }
        (Self::nav_css(inner) * inner.dpr) as f32
    }

    /// Where the grid starts in the canvas, in device px: the desktop's
    /// window padding of a cell left and right and half a cell top and
    /// bottom.
    fn pad(inner: &Inner<P, L>) -> (f32, f32) {
        let cw = inner.glyphs.metrics.cell_size.width as f32;
        let ch = inner.glyphs.metrics.cell_size.height as f32;
        if inner.mobile {
            // A phone has no room to give the grid a margin above and
            // below, and a gap under the last row reads as a cut-off
            // prompt: the rows sit flush with the bottom of the canvas,
            // and whatever part of a row the height cannot fit is the gap
            // under the tab row instead, where it reads as a margin.
            // Flush with the tab's own rows while the server has yet to
            // give it the canvas's: the soft keyboard coming or going cuts
            // the canvas a round trip before the new layout arrives, and
            // the old one drawn from the top jumped the prompt away from
            // the keyboard's edge and back.
            let height = inner.gpu.size().1 as f32;
            let rows = match Self::drawn_layout(inner) {
                Some(layout) if layout.rows > 0 => layout.rows as f32,
                _ => (height / ch.max(1.0)).floor(),
            };
            return (cw, height - rows * ch);
        }
        (cw, ch / 2.0)
    }

    /// What the grid's rows cannot use of the canvas's height: the pad
    /// above and below on a desktop, nothing on a phone (the leftover is
    /// the pad there, see `pad`).
    fn pad_y_total(inner: &Inner<P, L>) -> u32 {
        if inner.mobile {
            0
        } else {
            (2.0 * Self::pad(inner).1) as u32
        }
    }

    /// Rows of a frame the bar takes from the pane.
    fn nav_rows(inner: &Inner<P, L>) -> usize {
        Self::nav_rows_for(inner, inner.tab_layout.as_ref().map_or(1, |l| l.panes.len()))
    }

    /// The bar's rows for a tab of `panes` panes. The phone app shows a
    /// lone pane bare, as its terminal apps do, the bar appearing with a
    /// split, when there is something to tell apart; a page on a phone
    /// keeps the bar, as the desktop does (`Platform::bare_lone_pane`).
    fn nav_rows_for(inner: &Inner<P, L>, panes: usize) -> usize {
        if inner.platform.is_mobile()
            && (!inner.settings.pane_bars || (panes <= 1 && inner.platform.bare_lone_pane()))
        {
            return 0;
        }
        crate::navbar::nav_rows(Self::nav_css(inner) * inner.dpr, inner.glyphs.metrics.cell_size.height as f64)
    }

    /// One bar per drawn pane, as the page shows them.
    fn nav_views(inner: &Inner<P, L>) -> Vec<crate::navbar::NavView> {
        let mut views = Vec::new();
        // No rows for the bars, no bars: a phone shows a lone pane bare.
        // A bar reaches from above the pane down to its rows, so without
        // this the padding alone drew a sliver of one.
        if Self::nav_rows(inner) == 0 {
            return views;
        }
        if let Some(layout) = &inner.tab_layout {
            let dpr = inner.dpr.max(0.1);
            let cell_css = (
                inner.glyphs.metrics.cell_size.width as f64 / dpr,
                inner.glyphs.metrics.cell_size.height as f64 / dpr,
            );
            let closing = inner
                .closing_since
                .is_some_and(|since| inner.platform.monotonic_ms() - since < CLOSE_CONFIRM_MS);
            let pad = Self::pad(inner);
            let pad_css = (pad.0 as f64 / dpr, pad.1 as f64 / dpr);
            let width_css = inner.gpu.size().0 as f64 / dpr;
            let rows_below = |p: &crate::layout::PanePlacement| Self::content_offset(inner, p) as f64 / dpr;
            // A phone's bar reaches down to its rows; the desktop's leaves
            // the pad between them.
            let gap_css = if inner.mobile { 0.0 } else { pad_css.1 };
            for (rect, place) in crate::navbar::rects(layout, cell_css, pad_css, width_css, gap_css, rows_below)
                .into_iter()
                .zip(&layout.panes)
            {
                let members = place
                    .stack
                    .iter()
                    .map(|m| {
                        // The page's own copy of a drawn pane's title is fresher.
                        let raw = inner
                            .panes
                            .get(&m.pane_id)
                            .map(|c| c.title.as_str())
                            .unwrap_or(&m.title);
                        let (title, busy) = crate::navbar::display_title(raw);
                        let busy = busy || inner.tree.agents.get(&m.pane_id) == Some(&thinkterm_proto::AgentState::Working);
                        // A label someone gave the pane names it, as it is
                        // in the desktop's pane bar.
                        let title = inner
                            .layout
                            .as_ref()
                            .and_then(|layout| layout.pane_labels.get(&m.pane_id).cloned())
                            .unwrap_or(title);
                        crate::navbar::CapsuleView {
                            pane_id: m.pane_id,
                            title,
                            busy,
                            current: m.pane_id == place.pane_id,
                            icon: Self::pane_icon(inner, m.pane_id),
                        }
                    })
                    .collect();
                let focused = place.pane_id == inner.focused_pane;
                views.push(crate::navbar::NavView {
                    rect,
                    members,
                    focused,
                    zoomed: layout.zoomed.is_some(),
                    closing: closing && focused,
                });
            }
        }
        views
    }

    /// The sidebar's rows from the model, with the pending delete shown.
    fn side_rows(inner: &Inner<P, L>) -> Vec<crate::tree::Row> {
        inner.tree.rows_with(inner.tab_id, &inner.workspace, inner.window_id, Self::deleting(inner))
    }

    fn deleting(inner: &Inner<P, L>) -> Option<&str> {
        inner
            .deleting
            .as_ref()
            .filter(|(_, since)| inner.platform.monotonic_ms() - since < CLOSE_CONFIRM_MS)
            .map(|(id, _)| id.as_str())
    }

    /// Every Space with its projects and threads: the TUI's sidebar tree.
    pub fn tree_view(&self) -> crate::tree::TreeView {
        let inner = self.inner.borrow();
        inner.tree.tree(inner.tab_id, &inner.workspace, Self::deleting(&inner))
    }

    /// Ask the server for its tree, its session view and the agents in
    /// its panes: at the start, and again after a reconnect.
    pub fn fetch_tree(self: &Rc<Self>) {
        let link = self.inner.borrow().link.clone();
        let app = Rc::clone(self);
        self.spawn(async move {
            let tree = thinkterm_session::host::request(&link, Pdu::GetThinkTermTree(codec::GetThinkTermTree {}), |p| match p {
                Pdu::ThinkTermTreeState(s) => Ok(s.tree),
                other => Err(other),
            })
            .await;
            match tree {
                Ok(tree) => app.inner.borrow_mut().tree.apply_tree(tree),
                Err(err) => log::warn!("fetching the tree: {err:#}"),
            }
            let agents = thinkterm_session::host::request(&link, Pdu::GetAgentStatuses(codec::GetAgentStatuses {}), |p| match p {
                Pdu::GetAgentStatusesResponse(r) => Ok(r.statuses),
                other => Err(other),
            })
            .await;
            match agents {
                Ok(entries) => {
                    let mut inner = app.inner.borrow_mut();
                    inner.tree.clear_agents();
                    for e in entries {
                        inner.tree.apply_agent(e.pane_id, Some(&e.status));
                        inner.tree.record_agent_details(e.pane_id, &e.status.agent_id, &e.title);
                    }
                }
                Err(err) => log::warn!("fetching agent statuses: {err:#}"),
            }
            app.mark_focused_program_status_seen();
            let programs = thinkterm_session::host::request(&link, Pdu::GetForegroundPrograms(codec::GetForegroundPrograms {}), |p| match p {
                Pdu::GetForegroundProgramsResponse(r) => Ok(r.programs),
                other => Err(other),
            })
            .await;
            match programs {
                Ok(entries) => app.inner.borrow_mut().programs = entries.into_iter().map(|e| (e.pane_id, e.program)).collect(),
                Err(err) => log::warn!("fetching foreground programs: {err:#}"),
            }
            app.fetch_session().await;
            Self::render_strip(&app.inner.borrow());
        });
    }

    async fn fetch_session(self: &Rc<Self>) {
        let link = self.inner.borrow().link.clone();
        let state = thinkterm_session::host::request(&link, Pdu::GetThinkTermSessionState(codec::GetThinkTermSessionState {}), |p| match p {
            Pdu::ThinkTermSessionState(s) => Ok(s),
            other => Err(other),
        })
        .await;
        match state {
            Ok(state) => {
                self.inner.borrow_mut().tree.apply_session(state);
                Self::notify(&self.inner.borrow());
            }
            Err(err) => log::warn!("fetching the session view: {err:#}"),
        }
    }

    /// The session view is not pushed when the tree or the panes change;
    /// it is asked for, a moment after the last such push.
    fn refresh_session_soon(self: &Rc<Self>) {
        {
            let mut inner = self.inner.borrow_mut();
            if inner.session_refresh_pending || inner.disconnected.is_some() {
                return;
            }
            inner.session_refresh_pending = true;
        }
        let weak = Rc::downgrade(self);
        self.platform.set_timeout(
            150.0,
            Box::new(move || {
                let Some(app) = weak.upgrade() else { return };
                app.inner.borrow_mut().session_refresh_pending = false;
                let task = {
                    let app = Rc::clone(&app);
                    async move { app.fetch_session().await }
                };
                app.spawn(task);
            }),
        );
    }

    /// A click in the sidebar.
    pub fn on_side_click(self: &Rc<Self>, click: crate::sidebar::SideClick) {
        use crate::sidebar::SideClick;
        if !matches!(click, SideClick::Delete(_)) {
            self.inner.borrow_mut().deleting = None;
        }
        if !matches!(click, SideClick::RenameThread(_) | SideClick::RenameProject(_) | SideClick::NewProject)
            && self.inner.borrow().editing != crate::sidebar::Editing::None
        {
            let inner = &mut *self.inner.borrow_mut();
            inner.editing = crate::sidebar::Editing::None;
            inner.new_project_error = None;
        }
        match click {
            SideClick::Thread(id) => self.activate_thread(id),
            SideClick::Window(window_id) => {
                let pane = {
                    let inner = self.inner.borrow();
                    // The tab this page last showed in that window, else its first.
                    inner.layout.as_ref().and_then(|l| {
                        let tabs: Vec<_> = l
                            .tabs
                            .iter()
                            .filter_map(|t| Some((t.window_and_tab_ids()?, t)))
                            .filter(|((w, _), _)| *w == window_id)
                            .collect();
                        let recent = inner
                            .recent_tabs
                            .iter()
                            .find_map(|r| tabs.iter().find(|((_, id), _)| id == r));
                        recent
                            .or(tabs.first())
                            .and_then(|(_, t)| crate::chrome::active_pane(t))
                            .map(|e| e.pane_id)
                    })
                };
                if let Some(pane) = pane {
                    self.switch_to_pane(pane, true);
                }
            }
            SideClick::ToggleProject(id) => {
                let inner = &mut *self.inner.borrow_mut();
                if !inner.tree.collapsed.remove(&id) {
                    inner.tree.collapsed.insert(id);
                }
                Self::notify(inner);
            }
            SideClick::ToggleArchived => {
                let inner = &mut *self.inner.borrow_mut();
                inner.tree.archived_open = !inner.tree.archived_open;
                Self::notify(inner);
            }
            other => self.on_side_write(other),
        }
    }

    /// Send a write, take the tree that comes back, check the intent
    /// against it (a refusal is a remark, never an error), then ask for
    /// the session view the server does not push, and go on.
    fn mutate(self: &Rc<Self>, intent: crate::tree::Intent, then: impl FnOnce(&Rc<Self>) + 'static) {
        self.mutate_or(intent, then, |app, why| Self::set_status(&app.inner.borrow(), &why));
    }

    /// `mutate`, with the refusal handed to a caller that shows it itself
    /// rather than letting it pass by as a remark.
    fn mutate_or(
        self: &Rc<Self>,
        intent: crate::tree::Intent,
        then: impl FnOnce(&Rc<Self>) + 'static,
        refused: impl FnOnce(&Rc<Self>, String) + 'static,
    ) {
        let link = self.inner.borrow().link.clone();
        let app = Rc::clone(self);
        self.spawn(async move {
            let tree = thinkterm_session::host::request(
                &link,
                Pdu::MutateThinkTermTree(codec::MutateThinkTermTree { ops: intent.ops }),
                |p| match p {
                    Pdu::ThinkTermTreeState(s) => Ok(s.tree),
                    other => Err(other),
                },
            )
            .await;
            let tree = match tree {
                Ok(tree) => tree,
                Err(err) => {
                    refused(&app, format!("the server refused: {err:#}"));
                    return;
                }
            };
            let verdict = intent.expect.check(&tree);
            app.inner.borrow_mut().tree.apply_tree(tree);
            app.fetch_session().await;
            match verdict {
                Ok(()) => then(&app),
                Err(why) => refused(&app, why),
            }
        });
    }

    /// Every pane a thread has, for ending its programs.
    fn thread_panes(inner: &Inner<P, L>, id: &str) -> Vec<PaneId> {
        inner
            .tree
            .thread(id)
            .map(|t| t.tabs.iter().flat_map(|tab| tab.pane_ids.iter().copied()).collect())
            .unwrap_or_default()
    }

    fn kill_panes(self: &Rc<Self>, panes: Vec<PaneId>) {
        for pane_id in panes {
            self.act("web-act-close-pane", Pdu::KillPane(codec::KillPane { pane_id }), |_, _| {});
        }
    }

    /// The sidebar's writes: the same ops the desktop sends.
    fn on_side_write(self: &Rc<Self>, click: crate::sidebar::SideClick) {
        use crate::sidebar::{Editing, SideClick};
        if !matches!(click, SideClick::RenameThread(_) | SideClick::RenameProject(_) | SideClick::NewProject) {
            let inner = &mut *self.inner.borrow_mut();
            inner.editing = Editing::None;
            inner.new_project_error = None;
        }
        match click {
            SideClick::NewThread(project) => {
                let (project, size) = {
                    let inner = self.inner.borrow();
                    let project = project.or_else(|| inner.tree.default_project(inner.tab_id, &inner.workspace));
                    let size = inner.link.lease().canonical_size.unwrap_or_default();
                    (project, size)
                };
                match project {
                    Some(project) => {
                        let id = self.new_id("thread");
                        let intent = crate::tree::create_thread(&project, id.clone(), self.now_secs());
                        self.mutate(intent, move |app| app.activate_thread(id));
                    }
                    None => {
                        // No project yet: the server's landing thread
                        // (Default / Home / main) is the first one.
                        let app = Rc::clone(self);
                        self.spawn(async move {
                            let link = app.inner.borrow().link.clone();
                            let ensured = thinkterm_session::host::request(
                                &link,
                                Pdu::EnsureThinkTermThread(codec::EnsureThinkTermThread { preferred_thread_id: None, size }),
                                |p| match p {
                                    Pdu::EnsureThinkTermThreadResponse(r) => Ok(r),
                                    other => Err(other),
                                },
                            )
                            .await;
                            match ensured {
                                Ok(r) => {
                                    app.fetch_tree();
                                    app.activate_thread(r.thread_id);
                                }
                                Err(err) => Self::set_status(&app.inner.borrow(), &failed("web-act-start-thread", &err)),
                            }
                        });
                    }
                }
            }
            SideClick::NewProject => {
                let inner = &mut *self.inner.borrow_mut();
                inner.editing = Editing::NewProject;
                inner.new_project_error = None;
                Self::notify(inner);
            }
            SideClick::RenameThread(id) => {
                let inner = &mut *self.inner.borrow_mut();
                inner.editing = Editing::Thread(id);
                Self::notify(inner);
            }
            SideClick::RenameProject(id) => {
                let inner = &mut *self.inner.borrow_mut();
                inner.editing = Editing::Project(id);
                Self::notify(inner);
            }
            SideClick::Pin(id, on) => {
                self.mutate(crate::tree::set_pinned(&id, on, self.now_secs()), |_| {});
            }
            SideClick::Delete(id) => {
                // On the first press, as the desktop's menu item is: no
                // second press to confirm.
                let confirmed = {
                    self.inner.borrow_mut().deleting = None;
                    true
                };
                if confirmed {
                    let panes = Self::thread_panes(&self.inner.borrow(), &id);
                    self.kill_panes(panes);
                    self.mutate(crate::tree::delete_thread(&id), |_| {});
                }
            }
            SideClick::Archive(id) => {
                let panes: Vec<PaneId> = {
                    let inner = self.inner.borrow();
                    inner
                        .tree
                        .session
                        .as_ref()
                        .and_then(|s| s.projects.iter().find(|p| p.id == id))
                        .map(|p| p.threads.iter().flat_map(|t| t.tabs.iter()).flat_map(|t| t.pane_ids.iter().copied()).collect())
                        .unwrap_or_default()
                };
                // The flag lands first; the programs end once it has.
                self.mutate(crate::tree::archive_project(&id, true, self.now_secs()), move |app| app.kill_panes(panes));
            }
            SideClick::Unarchive(id) => {
                self.mutate(crate::tree::archive_project(&id, false, self.now_secs()), |_| {});
            }
            // The page opens the Space menu itself (`context_menu("space")`).
            SideClick::SpaceMenu => {}
            _ => {}
        }
    }

    /// A key in the sidebar's text field: Enter commits, Escape cancels.
    pub fn on_side_key(self: &Rc<Self>, key: &str, value: String) {
        use crate::sidebar::Editing;
        let editing = self.inner.borrow().editing.clone();
        match key {
            "Escape" => {
                let inner = &mut *self.inner.borrow_mut();
                inner.editing = Editing::None;
                inner.new_project_error = None;
                Self::notify(inner);
                inner.platform.focus_input();
            }
            "Enter" if editing == Editing::NewProject => self.add_workspace(value.trim()),
            "Enter" => {
                {
                    let inner = &mut *self.inner.borrow_mut();
                    inner.editing = Editing::None;
                    Self::notify(inner);
                    inner.platform.focus_input();
                }
                let value = value.trim().to_string();
                if value.is_empty() {
                    return;
                }
                match editing {
                    Editing::Thread(id) => self.mutate(crate::tree::rename_thread(&id, &value, self.now_secs()), |_| {}),
                    Editing::Project(id) => self.mutate(crate::tree::rename_project(&id, &value), |_| {}),
                    Editing::Space(id) => self.mutate(crate::tree::rename_space(&id, &value), |_| {}),
                    Editing::NewProject | Editing::None => {}
                }
            }
            _ => {}
        }
    }

    /// Keep the "Add workspace" field open with `why` beside it, so the
    /// path that was refused can be corrected rather than retyped.
    fn refuse_workspace(self: &Rc<Self>, why: String) {
        let inner = &mut *self.inner.borrow_mut();
        inner.editing = crate::sidebar::Editing::NewProject;
        inner.new_project_error = Some(why);
        Self::notify(inner);
    }

    /// A path typed into "Add workspace". An empty or relative one is
    /// refused here; a directory the tree already has is opened rather
    /// than added twice, as the desktop's `create_project_from_path`
    /// does; anything else is a project and its `main` thread.
    fn add_workspace(self: &Rc<Self>, path: &str) {
        if path.is_empty() {
            self.refuse_workspace(thinkterm_i18n::tr("web-add-workspace-empty"));
            return;
        }
        if !(path.starts_with('/') || path.starts_with('~')) {
            self.refuse_workspace(thinkterm_i18n::tr("web-add-workspace-relative"));
            return;
        }
        let trimmed = path.trim_end_matches('/');
        let (space, existing) = {
            let inner = self.inner.borrow();
            let space = inner.tree.current_space().map(|s| s.id.clone());
            let existing = inner.tree.session.as_ref().and_then(|s| {
                s.projects
                    .iter()
                    .filter(|p| space.as_deref().is_none_or(|id| p.space_id == id))
                    .find(|p| p.path.trim_end_matches('/') == trimmed && !trimmed.is_empty())
                    .and_then(|p| p.threads.first())
                    .map(|t| t.id.clone())
            });
            (space, existing)
        };
        {
            let inner = &mut *self.inner.borrow_mut();
            inner.editing = crate::sidebar::Editing::None;
            inner.new_project_error = None;
            Self::notify(inner);
            inner.platform.focus_input();
        }
        if let Some(thread) = existing {
            self.activate_thread(thread);
            return;
        }
        let project = self.new_id("project");
        let thread = self.new_id("thread");
        let intent = crate::tree::create_project(space.as_deref(), project, thread.clone(), path, self.now_secs());
        self.mutate_or(
            intent,
            move |app| app.activate_thread(thread),
            |app, why| app.refuse_workspace(why),
        );
    }

    /// Show a thread: its active tab when it has terminals, else have the
    /// server make them (`EnsureThinkTermThread`) and show what it made.
    /// Either way the thread is touched and read, as the desktop does.
    pub fn activate_thread(self: &Rc<Self>, id: String) {
        let (live_tab, size) = {
            let inner = self.inner.borrow();
            // A thread the model has not seen yet (just created) is
            // opened like a cold one: the server knows it.
            let tab = inner.tree.thread(&id).and_then(|thread| {
                inner
                    .recent_tabs
                    .iter()
                    .find(|recent| thread.tabs.iter().any(|t| t.tab_id == **recent))
                    .copied()
                    .or_else(|| thread.tabs.iter().find(|t| t.is_active).or(thread.tabs.first()).map(|t| t.tab_id))
            });
            let size = inner
                .link
                .lease()
                .canonical_size
                .or_else(|| inner.tab_layout.as_ref().map(|l| l.size))
                .unwrap_or_default();
            (tab, size)
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            let link = app.inner.borrow().link.clone();
            let mut workspace = None;
            if live_tab.is_none() {
                let ensured = thinkterm_session::host::request(
                    &link,
                    Pdu::EnsureThinkTermThread(codec::EnsureThinkTermThread {
                        preferred_thread_id: Some(id.clone()),
                        size,
                    }),
                    |p| match p {
                        Pdu::EnsureThinkTermThreadResponse(r) => Ok(r),
                        other => Err(other),
                    },
                )
                .await;
                match ensured {
                    Ok(r) => workspace = Some(r.workspace),
                    Err(err) => {
                        log::warn!("opening thread {id}: {err:#}");
                        Self::set_status(&app.inner.borrow(), &failed("web-act-open-thread", &err));
                        return;
                    }
                }
            }
            let Some(list) = app.list_panes().await else {
                return;
            };
            let pane = list
                .tabs
                .iter()
                .filter(|t| match (&live_tab, &workspace) {
                    (Some(tab), _) => t.window_and_tab_ids().is_some_and(|(_, id)| id == *tab),
                    (None, Some(ws)) => crate::layout::leaves(t).first().is_some_and(|e| &e.workspace == ws),
                    _ => false,
                })
                .find_map(crate::chrome::active_pane)
                .map(|e| e.pane_id);
            if let Some(pane) = pane {
                app.show(list, pane).await;
            }
            let now = app.now_secs();
            let _ = thinkterm_session::host::request(
                &link,
                Pdu::MutateThinkTermTree(codec::MutateThinkTermTree {
                    ops: vec![
                        codec::TreeOp::TouchThread { thread_id: id.clone(), at: now },
                        codec::TreeOp::SetThreadUnread { thread_id: id.clone(), unread: false },
                    ],
                }),
                |p| match p {
                    Pdu::ThinkTermTreeState(s) => Ok(s.tree),
                    other => Err(other),
                },
            )
            .await;
            app.fetch_session().await;
        });
    }

    /// Something the chrome shows changed: the probe's attribute is
    /// rewritten and the page is told.
    fn render_strip(inner: &Inner<P, L>) {
        Self::publish_layout(inner);
        Self::notify(inner);
    }

    /// The strip's tabs and state, once there is a listing.
    fn strip_model(inner: &Inner<P, L>) -> Option<(Vec<crate::chrome::TabView>, crate::chrome::Controls)> {
        let layout = inner.layout.as_ref()?;
        let tabs = crate::chrome::model(layout, inner.focused_pane, inner.title(), Some(inner.window_id));
        let (cols, rows) = inner
            .tab_layout
            .as_ref()
            .map(|l| (l.cols, l.rows))
            .unwrap_or((inner.cols, inner.rows));
        let controls = crate::chrome::Controls {
            following: inner.following,
            fit: inner.link.lease().fit,
            closing_tab: inner
                .closing_tab
                .filter(|(_, since)| inner.platform.monotonic_ms() - since < CLOSE_CONFIRM_MS)
                .map(|(tab, _)| tab),
            clipped: (cols > inner.cols || rows > inner.rows).then_some((inner.cols, inner.rows)),
        };
        Some((tabs, controls))
    }

    /// List the server's panes again, redraw the strip, and bring the tab
    /// on show up to date: new panes get sessions, gone ones lose them. If
    /// the focused pane is gone, the first tab's pane is shown instead.
    pub fn refresh_layout(self: &Rc<Self>) {
        {
            let mut inner = self.inner.borrow_mut();
            if inner.layout_refresh_pending || inner.disconnected.is_some() {
                return;
            }
            inner.layout_refresh_pending = true;
        }
        let app = Rc::clone(self);
        self.spawn(async move {
            let listed = app.list_panes().await;
            app.inner.borrow_mut().layout_refresh_pending = false;
            let Some(list) = listed else {
                return;
            };
            let want = app.inner.borrow().focused_pane;
            app.show(list, want).await;
        });
    }

    async fn list_panes(&self) -> Option<codec::ListPanesResponse> {
        let link = self.inner.borrow().link.clone();
        match crate::attach::list_panes(&link).await {
            Ok(list) => Some(list),
            Err(err) => {
                log::warn!("listing panes: {err:#}");
                None
            }
        }
    }

    /// Put the tab holding `want` on the canvas with `want` focused, from
    /// a fresh listing. The same tab is reconciled in place; another tab
    /// is reported to the server first, since the lease is per tab.
    async fn show(self: &Rc<Self>, list: codec::ListPanesResponse, want: PaneId) {
        let node = crate::layout::tab_containing(&list, want)
            .or_else(|| list.tabs.iter().find(|t| crate::layout::layout(t).is_some()));
        // The rows the bars take depend on the tab about to be shown, not
        // the one on screen: a split's second pane brings the bars with it.
        let nav_rows = Self::nav_rows_for(&self.inner.borrow(), node.map_or(1, |n| crate::layout::leaves(n).len()));
        let lay = |node: &thinkterm_proto::layout::PaneNode| {
            let tab_size = node
                .window_and_tab_ids()
                .and_then(|(_, tab_id)| self.inner.borrow().link.lease().tab_sizes.get(&tab_id).copied());
            crate::layout::layout_in(node, tab_size, nav_rows)
        };
        let Some(layout) = node.and_then(lay) else {
            let mut inner = self.inner.borrow_mut();
            inner.layout = Some(list);
            Self::render_strip(&inner);
            Self::set_status(&inner, &tr("web-toast-no-panes"));
            return;
        };
        let (link, tab_changed) = {
            let mut inner = self.inner.borrow_mut();
            if inner.switching || inner.disconnected.is_some() {
                return;
            }
            let tab_changed = layout.tab_id != inner.tab_id;
            inner.switching = tab_changed;
            (inner.link.clone(), tab_changed)
        };
        if tab_changed {
            // The lease is per tab: a follower's report of the tab's own
            // size, never a claim. Typing or clicking claims, as at attach.
            let outcome: Result<()> = async {
                {
                    // Computed before the lease is held: it reads the lease.
                    let panes = Self::native_panes(&self.inner.borrow(), &layout);
                    let mut lease = link.lease_mut();
                    lease.tab_id = Some(layout.tab_id);
                    lease.tab_owner = None;
                    lease.canonical_size = Some(layout.size);
                    lease.reported_viewport = None;
                    lease.native = panes;
                    lease.native_root = Some(layout.size);
                    lease.fit = false;
                }
                link.report_viewport(layout.tab_id).await
            }
            .await;
            let mut inner = self.inner.borrow_mut();
            inner.switching = false;
            if let Err(err) = outcome {
                log::warn!("switching to tab {}: {err:#}", layout.tab_id);
                Self::set_status(&inner, &failed("web-act-switch-tabs", &err));
                return;
            }
        }
        let tab_id = layout.tab_id;
        let (fresh, changed, first_layout) = {
            let mut inner = self.inner.borrow_mut();
            inner.tree.apply_panes(&list);
            let listed: std::collections::HashSet<PaneId> = list
                .tabs
                .iter()
                .flat_map(|node| crate::layout::leaves(node).into_iter().map(|e| e.pane_id))
                .collect();
            inner.programs.retain(|pane, _| listed.contains(pane));
            inner.layout = Some(list);
            let first_layout = inner.tab_layout.is_none();
            let before = (inner.focused_pane, inner.tab_layout.clone());
            let fresh = Self::apply_layout(&mut inner, layout, want);
            // What a keystroke would claim follows the drawn layout.
            if let Some(layout) = &inner.tab_layout {
                let panes = Self::native_panes(&inner, layout);
                let mut lease = link.lease_mut();
                lease.native = panes;
                lease.native_root = Some(layout.size);
            }
            let changed = tab_changed
                || !fresh.is_empty()
                || before.0 != inner.focused_pane
                || before.1 != inner.tab_layout;
            if tab_changed || first_layout {
                inner.selecting = false;
                inner.ime_anchor = None;
                // Every showing claims afresh: the desktop may have taken
                // this tab since the page last had it.
                inner.auto_claimed = None;
                // Forces `resize` to see a change: the tab is reflowed to
                // this grid if the page owns it, or reported against it.
                inner.cols = 0;
            }
            inner.platform.set_title(inner.title());
            Self::render_strip(&inner);
            (fresh, changed, first_layout)
        };
        self.mark_focused_program_status_seen();
        // A pane's first push comes when something asks after it: one
        // liveness poll each, and the answer is not waited for.
        self.sync_frame_control();
        for pane_id in fresh {
            let link = link.clone();
            self.spawn(async move {
                let _ = thinkterm_session::host::request(
                    &link,
                    Pdu::GetPaneRenderChanges(codec::GetPaneRenderChanges { pane_id }),
                    |pdu| match pdu {
                        Pdu::LivenessResponse(_) | Pdu::UnitResponse(_) => Ok(()),
                        other => Err(other),
                    },
                )
                .await;
            });
        }
        if tab_changed || first_layout {
            self.match_desktop_cell();
            self.resize();
        }
        // What the page would claim follows the listing; the report is
        // skipped when it is what the server already has, and held back
        // while a divider is being dragged (each step would otherwise
        // reshape the panes twice: the server's resize, then ours).
        let dragging = self.inner.borrow().drag_divider.is_some();
        if !dragging {
            if let Err(err) = link.report_viewport(tab_id).await {
                log::warn!("reporting the tab shape: {err:#}");
                Self::set_status(&self.inner.borrow(), &failed("web-act-report-shape", &err));
            }
        }
        // A listing that changed nothing (the timer's, mostly) is not a
        // reason to paint.
        if changed {
            self.refresh_status();
            self.request_frame();
        }
        // The page holds the terminal it shows: taken at this window's
        // shape as soon as a tab is on screen, and taken back by the
        // desktop only when someone interacts there.
        // A showing the page already owns (the report took an ownerless
        // tab) is handled too: the desktop taking it later is not undone
        // by the next listing.
        let claim = {
            let mut inner = self.inner.borrow_mut();
            // A machine's terminal behind another's on the page is not on
            // show: it takes nothing from the host's own desktop. It claims
            // when it comes on show (the page lists it again then).
            let showing = inner.platform.shown()
                && inner.auto_claimed != Some(tab_id)
                && inner.disconnected.is_none();
            if showing {
                inner.auto_claimed = Some(tab_id);
            }
            let lease = inner.link.lease();
            // Handoff is exclusive: a terminal someone holds is taken by a
            // press on the card, never by a page merely opening.
            let held_exclusively = lease.needs_claim() && lease.owner.is_some();
            showing && !lease.owns_viewport() && !held_exclusively
        };
        if claim {
            self.take_over_with(true);
        }
    }

    /// Make `inner.panes` match `layout`, focusing `want` if it is drawn
    /// and the tab's active pane otherwise. Returns the panes that are
    /// new to the page.
    fn apply_layout(inner: &mut Inner<P, L>, layout: crate::layout::TabLayout, want: PaneId) -> Vec<PaneId> {
        if layout.tab_id != inner.tab_id {
            // Another tab is another lease and another tab id for every
            // session under it.
            inner.remote_tab_id = Arc::new(std::sync::atomic::AtomicUsize::new(layout.tab_id));
            let mut leaving = std::mem::take(&mut inner.panes);
            // A preview in force would turn away every push while the
            // tab is parked; it is over now, the server's size stands.
            for cell in leaving.values_mut() {
                if let Some((epoch, _, _)) = cell.preview.take() {
                    cell.session.end_frontend_preview(epoch, false);
                }
            }
            inner.parked.extend(leaving);
            inner.tab_id = layout.tab_id;
            // Kept for the tabs recently shown; the rest are built again
            // when next shown, as they were the first time.
            let kept: Vec<TabId> = inner.recent_tabs.iter().take(PARKED_TABS).copied().collect();
            inner.parked.retain(|_, cell| kept.contains(&cell.session.remote_tab_id()));
        }
        inner.recent_tabs.retain(|t| *t != layout.tab_id);
        inner.recent_tabs.insert(0, layout.tab_id);
        inner.recent_tabs.truncate(64);
        inner.window_id = layout.window_id;
        inner.workspace = layout.workspace.clone();
        let drawn: std::collections::BTreeSet<PaneId> =
            layout.panes.iter().map(|p| p.pane_id).collect();
        inner.panes.retain(|id, _| drawn.contains(id));
        let mut fresh = Vec::new();
        for place in &layout.panes {
            if inner.panes.contains_key(&place.pane_id) {
                continue;
            }
            // A parked pane comes back with what it had, unless it has
            // moved to another tab meanwhile: its session is bound to the
            // old tab's lease, so it is built afresh, as a stranger is.
            if let Some(mut cell) = inner.parked.remove(&place.pane_id) {
                if cell.session.remote_tab_id() == layout.tab_id {
                    // Its program may have set or reset its colours while
                    // the tab was away; only `overrides` heard.
                    cell.application = inner.overrides.get(&place.pane_id).cloned().flatten();
                    if std::mem::take(&mut cell.reconnect_stale) {
                        fresh.push(place.pane_id);
                    }
                    inner.panes.insert(place.pane_id, cell);
                    continue;
                }
            }
            let rows = place.size.rows;
            let dims = thinkterm_proto::RenderableDimensions {
                cols: place.size.cols,
                viewport_rows: rows,
                scrollback_rows: rows,
                physical_top: place.physical_top,
                scrollback_top: place.physical_top,
                dpi: place.size.dpi,
                pixel_width: place.size.pixel_width,
                pixel_height: place.size.pixel_height,
                reverse_video: false,
            };
            let session = build_session(
                &inner.host,
                &inner.images,
                &inner.remote_tab_id,
                place.pane_id,
                dims,
                &place.title,
                place.alt_screen,
            );
            let mut cell = PaneCell::new(session, &place.title);
            cell.application = inner.overrides.get(&place.pane_id).cloned().flatten();
            inner.panes.insert(place.pane_id, cell);
            fresh.push(place.pane_id);
        }
        // A fresh cell starts on the stock palette; the base and any
        // override it has are folded in before it is first drawn.
        inner.recompute_palettes();
        inner.focused_pane = if drawn.contains(&want) {
            want
        } else {
            layout
                .panes
                .iter()
                .find(|p| p.is_active)
                .or(layout.panes.first())
                .map(|p| p.pane_id)
                .unwrap_or(want)
        };
        // A layout sent before the claim -- the server's word on an earlier
        // size -- leaves the claimed one drawn; the answer to it, another
        // tab, or a claim nobody met in time puts the server's back.
        let answered = inner.claimed_layout.as_ref().is_none_or(|claimed| {
            claimed.tab_id != layout.tab_id
                || (claimed.cols, claimed.rows) == (layout.cols, layout.rows)
                || inner.platform.monotonic_ms() - inner.claimed_at > Self::PREVIEW_PATIENCE_MS
        });
        inner.tab_layout = Some(layout);
        // Nor do the panes' previews give way to it.
        if answered {
            inner.claimed_layout = None;
            Self::settle_previews(inner);
        }
        fresh
    }

    /// `refresh_layout`, a moment from now, so a burst of pushes is one
    /// listing.
    fn refresh_layout_soon(self: &Rc<Self>) {
        let weak = Rc::downgrade(self);
        self.platform.set_timeout(
            150.0,
            Box::new(move || {
                if let Some(app) = weak.upgrade() {
                    app.refresh_layout();
                }
            }),
        );
    }

    /// Ask the server again every so often, whatever the pushes said:
    /// pane titles elsewhere change without one.
    pub fn poll_layout(self: &Rc<Self>, every_ms: i32) {
        let weak = Rc::downgrade(self);
        self.platform.set_interval(
            every_ms as f64,
            Box::new(move || {
                if let Some(app) = weak.upgrade() {
                    if !app.retired.get() {
                        app.refresh_layout();
                    }
                }
            }),
        );
    }

    /// A click on the strip.
    pub fn on_chrome_click(self: &Rc<Self>, click: crate::chrome::Click) {
        use crate::chrome::Click;
        // Any other click withdraws a pending close.
        if !matches!(click, Click::Close | Click::ClosePane(_)) {
            self.inner.borrow_mut().closing_since = None;
        }
        if !matches!(click, Click::CloseTab(_)) {
            self.inner.borrow_mut().closing_tab = None;
        }
        match click {
            Click::Pane(pane_id) => {
                let hidden = self
                    .inner
                    .borrow()
                    .tab_layout
                    .as_ref()
                    .is_some_and(|l| l.hidden.contains(&pane_id));
                if hidden {
                    // A stack member behind the drawn one: the server
                    // brings it to the front, then it is focused.
                    self.act(
                        "web-act-activate-pane",
                        Pdu::ActivatePaneInStack(codec::ActivatePaneInStack { pane_id }),
                        move |app, _| app.switch_to_pane(pane_id, true),
                    );
                } else {
                    self.switch_to_pane(pane_id, true);
                }
            }
            Click::CloseTab(tab_id) => {
                let panes: Vec<PaneId> = {
                    // On the first press, as the desktop's × is.
                    let mut inner = self.inner.borrow_mut();
                    inner.closing_tab = None;
                    inner
                        .layout
                        .as_ref()
                        .map(|l| {
                            l.tabs
                                .iter()
                                .flat_map(crate::layout::leaves)
                                .filter(|e| e.tab_id == tab_id)
                                .map(|e| e.pane_id)
                                .collect()
                        })
                        .unwrap_or_default()
                };
                for pane_id in panes {
                    self.act(
                        "web-act-close-tab",
                        Pdu::KillPane(codec::KillPane { pane_id }),
                        |_, _| {},
                    );
                }
            }
            Click::ClosePane(pane_id) => {
                let (focused, following) = {
                    let inner = self.inner.borrow();
                    (inner.focused_pane, inner.following)
                };
                if pane_id != focused {
                    self.inner.borrow_mut().closing_since = None;
                    self.focus_pane(pane_id, following);
                }
                self.on_chrome_click(Click::Close);
            }
            Click::Follow => {
                let mut inner = self.inner.borrow_mut();
                inner.following = !inner.following;
                Self::render_strip(&inner);
                inner.platform.focus_input();
            }
            Click::NewTab => self.new_tab(),
            Click::NewInStack(pane) => self.new_in_stack(pane),
            Click::SplitRight(pane) => self.split(pane, thinkterm_proto::SplitDirection::Horizontal),
            Click::SplitBelow(pane) => self.split(pane, thinkterm_proto::SplitDirection::Vertical),
            Click::Zoom(pane) => self.toggle_zoom(pane),
            Click::Close => {
                // On the first press, as the desktop's × is.
                let confirmed = {
                    self.inner.borrow_mut().closing_since = None;
                    true
                };
                if confirmed {
                    self.close_pane();
                } else {
                    // Back to a plain × when the moment passes.
                    let weak = Rc::downgrade(self);
                    self.platform.set_timeout(
                        CLOSE_CONFIRM_MS + 50.0,
                        Box::new(move || {
                            if let Some(app) = weak.upgrade() {
                                let inner = app.inner.borrow();
                                Self::render_strip(&inner);
                            }
                        }),
                    );
                }
            }
        }
    }

    /// One request to the server, its refusal shown rather than logged,
    /// and the layout listed again afterwards; the server's own pushes
    /// list it too, which is harmless.
    fn act<F>(self: &Rc<Self>, what: &'static str, pdu: Pdu, done: F)
    where
        F: FnOnce(&Rc<Self>, Pdu) + 'static,
    {
        let link = self.inner.borrow().link.clone();
        let app = Rc::clone(self);
        self.spawn(async move {
            match thinkterm_session::host::request(&link, pdu, Ok).await {
                Ok(answer) => {
                    done(&app, answer);
                    app.refresh_layout();
                }
                Err(err) => {
                    log::warn!("{what}: {err:#}");
                    let inner = app.inner.borrow();
                    Self::set_status(&inner, &failed(what, &err));
                }
            }
            app.platform.focus_input();
        });
    }

    /// A new tab in the window this tab is in, at the tab's own size.
    pub fn new_tab(self: &Rc<Self>) {
        self.new_tab_in(None);
    }

    /// A new tab in the named domain of the server, rather than the one
    /// the focused pane is in: a plain ssh terminal on a machine the page
    /// could not reach with ThinkTerm.
    pub fn new_tab_in(self: &Rc<Self>, domain: Option<String>) {
        let (window_id, workspace, size) = {
            let inner = self.inner.borrow();
            let size = inner
                .link
                .lease()
                .canonical_size
                .or_else(|| inner.tab_layout.as_ref().map(|l| l.size))
                .unwrap_or_default();
            (inner.window_id, inner.workspace.clone(), size)
        };
        self.act(
            "web-act-new-tab",
            Pdu::SpawnV2(codec::SpawnV2 {
                domain: match domain {
                    Some(name) => thinkterm_proto::SpawnTabDomain::DomainName(name),
                    None => thinkterm_proto::SpawnTabDomain::CurrentPaneDomain,
                },
                window_id: Some(window_id),
                command: None,
                command_dir: None,
                size,
                workspace,
            }),
            |app, answer| {
                if let Pdu::SpawnResponse(spawned) = answer {
                    // The page moves with it; the desktop already has.
                    app.switch_to_pane(spawned.pane_id, false);
                }
            },
        );
    }

    /// Split the focused pane; the new pane takes the focus.
    pub fn split(self: &Rc<Self>, pane: Option<PaneId>, direction: thinkterm_proto::SplitDirection) {
        self.split_at(pane, direction, true);
    }

    /// Split with the new pane after (`second`) or before the target.
    pub fn split_at(self: &Rc<Self>, pane: Option<PaneId>, direction: thinkterm_proto::SplitDirection, second: bool) {
        let pane_id = self.target_pane(pane);
        self.act(
            "web-act-split-pane",
            Pdu::SplitPane(codec::SplitPane {
                pane_id,
                split_request: thinkterm_proto::SplitRequest {
                    direction,
                    target_is_second: second,
                    top_level: false,
                    size: thinkterm_proto::SplitSize::Percent(50),
                },
                command: None,
                command_dir: None,
                domain: thinkterm_proto::SpawnTabDomain::CurrentPaneDomain,
                move_pane_id: None,
            }),
            |app, answer| {
                if let Pdu::SpawnResponse(spawned) = answer {
                    app.switch_to_pane(spawned.pane_id, false);
                }
            },
        );
    }

    /// A dropped tab: put it at `index` among its window's tabs. The
    /// listing that follows redraws the strip; the drop is refused for a
    /// tab the page does not know.
    pub fn move_tab(self: &Rc<Self>, tab: TabId, index: usize) -> bool {
        let window_id = {
            let inner = self.inner.borrow();
            let Some((tabs, _)) = Self::strip_model(&inner) else {
                return false;
            };
            let Some(view) = tabs.iter().find(|t| t.tab_id == tab) else {
                return false;
            };
            if tabs.iter().position(|t| t.tab_id == tab) == Some(index) {
                return true;
            }
            view.window_id
        };
        self.act("web-act-move-tab", Pdu::MoveTab(codec::MoveTab { window_id, tab_id: tab, index }), |_, _| {});
        true
    }

    /// A pane dropped on another: it leaves its place and becomes the
    /// target's neighbour in `direction`, after it (`second`) or before.
    /// Dropping a pane on itself is nothing.
    pub fn move_pane(
        self: &Rc<Self>,
        pane: PaneId,
        target: PaneId,
        direction: thinkterm_proto::SplitDirection,
        second: bool,
    ) -> bool {
        if pane == target {
            return false;
        }
        self.act(
            "web-act-move-pane",
            Pdu::SplitPane(codec::SplitPane {
                pane_id: target,
                split_request: thinkterm_proto::SplitRequest {
                    direction,
                    target_is_second: second,
                    top_level: false,
                    size: thinkterm_proto::SplitSize::Percent(50),
                },
                command: None,
                command_dir: None,
                domain: thinkterm_proto::SpawnTabDomain::CurrentPaneDomain,
                move_pane_id: Some(pane),
            }),
            move |app, _| app.switch_to_pane(pane, false),
        );
        true
    }

    /// A thread dropped among its project's unpinned threads, before
    /// `before` (None: last); refused across projects and for pinned rows.
    pub fn move_thread(self: &Rc<Self>, id: &str, before: Option<&str>) -> bool {
        let intent = {
            let inner = self.inner.borrow();
            let Some(project) = inner.tree.project_of(id) else {
                return false;
            };
            let unpinned: Vec<String> =
                project.threads.iter().filter(|t| !t.is_pinned).map(|t| t.id.clone()).collect();
            if !unpinned.iter().any(|t| t == id) || before.is_some_and(|b| !unpinned.iter().any(|t| t == b)) {
                return false;
            }
            crate::tree::move_thread_before(&project.id, id, before, &unpinned)
        };
        self.mutate(intent, |_| {});
        true
    }

    /// A project dropped among its Space's projects, before `before`
    /// (None: last); refused across Spaces.
    pub fn move_project(self: &Rc<Self>, id: &str, before: Option<&str>) -> bool {
        let intent = {
            let inner = self.inner.borrow();
            let Some(session) = inner.tree.session.as_ref() else {
                return false;
            };
            let Some(space) = session.projects.iter().find(|p| p.id == id).map(|p| p.space_id.clone()) else {
                return false;
            };
            let order: Vec<String> =
                session.projects.iter().filter(|p| p.space_id == space).map(|p| p.id.clone()).collect();
            if before.is_some_and(|b| !order.iter().any(|p| p == b)) {
                return false;
            }
            crate::tree::move_project_before(&space, id, before, &order)
        };
        self.mutate(intent, |_| {});
        true
    }

    /// Whether the connection is up: false from a drop until the reconnect
    /// that ends it.
    pub fn connected(&self) -> bool {
        self.inner.borrow().disconnected.is_none()
    }

    /// Every Space of this server, for a page that lists several servers'
    /// Spaces in one menu.
    pub fn spaces(&self) -> Vec<crate::tree::SpaceEntry> {
        self.inner.borrow().tree.spaces()
    }

    /// A context menu for something on the page: `kind` names what was
    /// clicked, `id` which one. Empty when there is nothing to offer.
    pub fn context_menu(&self, kind: &str, id: &str) -> Vec<crate::menu::MenuItem> {
        use crate::menu;
        let inner = self.inner.borrow();
        let menu = match kind {
            "pane" => id.parse().ok().map(|pane| menu::for_pane(pane, inner.link.lease().mode)),
            "tab" => id.parse().ok().and_then(|tab: TabId| {
                let (tabs, _) = Self::strip_model(&inner)?;
                let index = tabs.iter().position(|t| t.tab_id == tab)?;
                Some(menu::for_tab(tab, index, tabs.len(), tabs[index].target))
            }),
            "thread" => Self::side_rows(&inner).into_iter().find_map(|row| match row {
                crate::tree::Row::Thread(t) if t.id == id => Some(menu::for_thread(&t)),
                _ => None,
            }),
            "project" => Some(menu::for_project(id, inner.tree.live_panes_of_project(id).len())).map(|items| {
                if inner.platform.is_mobile() {
                    // The phone's sidebar folds on its own; the core's
                    // fold would only hide the threads from the overview.
                    items.into_iter().filter(|item| !item.id.starts_with("collapse")).collect()
                } else {
                    items
                }
            }),
            "archived-project" => Some(menu::for_archived_project(id, inner.tree.thread_count_of_project(id))),
            "sidebar-options" => Some(menu::for_sidebar_options(inner.tree.archived_open, inner.tree.archived_count())),
            "space" => Some(menu::for_space(&inner.tree.spaces())),
            "notifications" => Some(menu::for_notifications()),
            _ => None,
        };
        menu.unwrap_or_default()
    }

    /// The Agents panel: every pane an agent runs in, and the summary line.
    pub fn agents_view(&self) -> crate::agents::AgentsView {
        let mut inner = self.inner.borrow_mut();
        let inner = &mut *inner;
        // An open list lasts as long as its sub-tasks: the next batch
        // starts folded, as on the desktop.
        let tree = &inner.tree;
        inner
            .open_subtasks
            .retain(|pane| tree.agent_report(*pane).is_some_and(|r| !r.children.is_empty()));
        let shared: &Inner<P, L> = inner;
        let rows = crate::agents::rows(
            &shared.tree,
            shared.window_id,
            |pane| shared.panes.get(&pane).map(|c| crate::navbar::display_title(&c.title).0),
            |pane| Self::pane_icon(shared, pane),
            |pane| shared.open_subtasks.contains(&pane),
        );
        let summary = crate::agents::summary(&rows);
        let tabs = Self::panel_tabs(&inner);
        let labeled = crate::agents::labeled(&tabs);
        crate::agents::AgentsView { rows, summary, tabs, active: inner.right_panel.clone(), labeled }
    }

    fn panel_tabs(inner: &Inner<P, L>) -> Vec<crate::agents::PanelTab> {
        crate::agents::panel_tabs(Self::snippets_offered(inner), &inner.plugin_list.panels())
    }

    /// Show a tab of the right panel by its id. False for one the page
    /// does not offer.
    pub fn set_right_panel(self: &Rc<Self>, id: &str) -> bool {
        let tabs = Self::panel_tabs(&self.inner.borrow());
        let Some(tab) = tabs.into_iter().find(|tab| tab.id == id && tab.available) else {
            return false;
        };
        self.inner.borrow_mut().right_panel = tab.id;
        self.plugins_follow();
        true
    }

    /// The page put the right panel up, or took it down. While it is up
    /// the plugin list is followed: its Snippets tab is there only while
    /// the Snippets plugin is on.
    pub fn set_right_panel_shown(self: &Rc<Self>, shown: bool) {
        {
            let inner = &mut *self.inner.borrow_mut();
            inner.right_panel_shown = shown;
            inner.plugin_list.want("panel", shown);
        }
        self.plugins_follow();
    }

    /// Whether the right panel offers Snippets: unless the plugin list, as
    /// last heard, has its plugin off or broken.
    fn snippets_offered(inner: &Inner<P, L>) -> bool {
        inner.plugin_list.usable(snippets_wire::PLUGIN) != Some(false)
    }

    /// Something on the page needs the plugin list, or no longer does:
    /// `who` is `settings` or `panel`.
    pub fn plugins_want(self: &Rc<Self>, who: &str, on: bool) {
        let who = match who {
            "settings" => "settings",
            "panel" => "panel",
            _ => return,
        };
        self.inner.borrow_mut().plugin_list.want(who, on);
        self.plugins_follow();
    }

    /// Whether the Snippets tab is on show, and so the snippets followed.
    fn snippets_wanted(inner: &Inner<P, L>) -> bool {
        inner.right_panel_shown && inner.right_panel == "snippets"
    }

    /// Whether anything needs the server's way to the plugin host: a tab or
    /// section on show, or a call waiting for its answer.
    fn plugin_pipe_needed(inner: &Inner<P, L>) -> bool {
        Self::snippets_wanted(inner)
            || inner.plugin_panel.is_some()
            || inner.plugin_list.wanted()
            || inner.plugin_calls.waiting()
    }

    /// Follow what the page shows of the plugins: the snippets while their
    /// tab is on show, the plugin list while something needs it. The
    /// server's way to the plugin host is let go a while after nothing needs
    /// it any more.
    fn plugins_follow(self: &Rc<Self>) {
        let (ask_snippets, closing, sends, extended_sends, panel_later) = {
            let inner = &mut *self.inner.borrow_mut();
            if !Self::snippets_offered(inner) && inner.right_panel == "snippets" {
                inner.right_panel = "agents".to_string();
            }
            // A plugin's panel goes from the selector with its plugin, once
            // the list says so.
            let offered = !inner.plugin_list.known()
                || Self::panel_tabs(inner).iter().any(|tab| tab.id == inner.right_panel);
            if !offered {
                inner.right_panel = "agents".to_string();
            }
            let (closing, sends, panel_later) = Self::follow_plugin_panel(inner);
            let (closing_extended, extended_sends) = Self::follow_plugin_extended(inner);
            let closing: Vec<Vec<u8>> = closing.into_iter().chain(closing_extended).collect();
            let snippets_wanted = Self::snippets_wanted(inner);
            let mut ask_snippets = false;
            if !snippets_wanted {
                inner.snippets.close();
            } else if inner.disconnected.is_none() {
                // Asked for once the connection is back, otherwise.
                inner.snippets.open();
                ask_snippets = true;
            }
            (ask_snippets, closing, sends, extended_sends, panel_later)
        };
        for frame in closing {
            self.send_plugin_frame(frame, FrameFor::Nobody);
        }
        self.send_panel_frames(sends, false);
        self.send_panel_frames(extended_sends, true);
        if let Some(delay) = panel_later {
            self.wake_plugin_panel(delay);
        }
        if ask_snippets {
            self.snippets_list();
        }
        self.plugins_list();
        self.linger_plugin_pipe();
        Self::notify(&self.inner.borrow());
    }

    /// Opens the plugin panel the right panel shows, closes one it no
    /// longer does, and opens again one whose time to be has come: the
    /// frame that closes the last, those for the one on show, and how long
    /// until that time when it has not come yet. A panel opens once the
    /// page has said how much room it has; one waiting for the way to the
    /// host keeps what it shows.
    fn follow_plugin_panel(inner: &mut Inner<P, L>) -> (Option<Vec<u8>>, Vec<Vec<u8>>, Option<f64>) {
        let wanted = inner
            .right_panel_shown
            .then(|| inner.right_panel.strip_prefix("plugin:"))
            .flatten()
            .map(str::to_string);
        let mut sends = Vec::new();
        let mut closing = None;
        let other = inner
            .plugin_panel
            .as_ref()
            .is_some_and(|panel| Some(panel.plugin()) != wanted.as_deref());
        if other {
            let panel = inner.plugin_panel.take().expect("checked above");
            if !panel.waiting() && inner.disconnected.is_none() {
                closing = Some(panel.close());
            }
        }
        if inner.disconnected.is_some() {
            return (closing, sends, None);
        }
        let now = inner.platform.monotonic_ms();
        let mut later = None;
        match (&mut inner.plugin_panel, wanted, &inner.plugin_panel_env) {
            (Some(panel), _, _) => match panel.again_in(now) {
                Some(wait) if wait > 0.0 => later = Some(wait),
                Some(_) => {
                    inner.plugin_panel_views += 1;
                    sends.extend(panel.reopen(inner.plugin_panel_views, None, now));
                }
                None => {}
            },
            (None, Some(plugin), Some(env)) => {
                inner.plugin_panel_views += 1;
                let views = inner.plugin_panel_views;
                let (panel, open) = crate::plugin_panel::PanelModel::open(&plugin, views, None, env.clone(), now);
                inner.plugin_panel = Some(panel);
                sends.extend(open);
            }
            _ => {}
        }
        (closing, sends, later)
    }

    /// Opens the extended view of the plugin panel on show while the
    /// panel's frames ask for one and the page gave it room -- as that of
    /// the panel's opening, once the panel is drawn on it; one of an earlier
    /// opening shows what it had until then -- and closes it once they stop
    /// asking: the frame that closes it, and those for it. One the host
    /// stopped serving waits for the panel's next opening.
    fn follow_plugin_extended(inner: &mut Inner<P, L>) -> (Option<Vec<u8>>, Vec<Vec<u8>>) {
        let Some(panel) = inner.plugin_panel.as_ref().filter(|panel| panel.wants_extended()) else {
            // What the page said of its room stands until the page takes
            // the room away (`plugin_extended_gone`): asked for again before
            // the page has looked, it opens in the room still there.
            let closing = inner
                .plugin_extended
                .take()
                .filter(|extended| !extended.waiting() && inner.disconnected.is_none())
                .map(|extended| extended.close());
            return (closing, Vec::new());
        };
        let (plugin, opening) = (panel.plugin().to_string(), panel.view());
        if !panel.drawn() || inner.disconnected.is_some() {
            return (None, Vec::new());
        }
        let Some(env) = inner.plugin_extended_env.clone() else {
            return (None, Vec::new());
        };
        let now = inner.platform.monotonic_ms();
        match inner.plugin_extended.as_mut() {
            Some(extended) if extended.extends() == Some(opening) => (None, Vec::new()),
            Some(extended) => {
                inner.plugin_panel_views += 1;
                (None, extended.reopen(inner.plugin_panel_views, Some(opening), now))
            }
            None => {
                inner.plugin_panel_views += 1;
                let views = inner.plugin_panel_views;
                let (extended, open) = crate::plugin_panel::PanelModel::open(&plugin, views, Some(opening), env, now);
                inner.plugin_extended = Some(extended);
                (None, open)
            }
        }
    }

    /// Comes back to the plugin panel after `delay_ms`, to tell its plugin a
    /// size or to open it again. A timer already set for sooner will do.
    fn wake_plugin_panel(self: &Rc<Self>, delay_ms: f64) {
        let due = self.platform.monotonic_ms() + delay_ms;
        if self.panel_wake.get().is_some_and(|at| at <= due) {
            return;
        }
        self.panel_wake.set(Some(due));
        let weak = Rc::downgrade(self);
        self.platform.set_timeout(
            delay_ms.max(0.0),
            Box::new(move || {
                if let Some(app) = weak.upgrade() {
                    if app.panel_wake.get() == Some(due) {
                        app.panel_wake.set(None);
                    }
                    app.plugin_panel_tell_env();
                    app.plugins_follow();
                }
            }),
        );
    }

    /// What the host said about the page's panel `view`, or its extended
    /// view.
    fn plugin_panel_heard(self: &Rc<Self>, view: u64, event: thinkterm_plugin_channel::wire::PanelEvent) {
        let (sends, closing, extended_sends, again) = {
            let inner = &mut *self.inner.borrow_mut();
            let now = inner.platform.monotonic_ms();
            if let Some(panel) = inner.plugin_panel.as_mut().filter(|panel| panel.view() == view) {
                let sends = panel.heard(event, now);
                let again = panel.again_in(now);
                // Its extended view follows what it drew.
                let (closing, extended_sends) = Self::follow_plugin_extended(inner);
                (sends, closing, extended_sends, again)
            } else if let Some(extended) = inner.plugin_extended.as_mut().filter(|extended| extended.view() == view) {
                (Vec::new(), None, extended.heard(event, now), None)
            } else {
                // An opening the page has let go of since.
                return;
            }
        };
        if let Some(frame) = closing {
            self.send_plugin_frame(frame, FrameFor::Nobody);
        }
        self.send_panel_frames(sends, false);
        self.send_panel_frames(extended_sends, true);
        if let Some(delay) = again {
            self.wake_plugin_panel(delay);
        }
        Self::notify(&self.inner.borrow());
    }

    /// How much room the page gives a plugin panel, or its extended view,
    /// its fonts and theme: an `Env`, which says whether the page has room
    /// for an extended view. The first opens the view on show.
    pub fn plugin_panel_env(self: &Rc<Self>, extended: bool, mut env: thinkterm_plugin_panel::Env) {
        {
            let inner = &mut *self.inner.borrow_mut();
            env.locale = thinkterm_i18n::current_locale().to_string();
            env.cwd = Self::focused_cwd(inner);
            env.terminal = Self::focused_terminal(inner);
            // Its fields are the page's own; the keyboard comes to a panel
            // through them, and no key of the page's gives it.
            env.features = vec![
                thinkterm_plugin_panel::feature::FIELDS.to_string(),
                thinkterm_plugin_panel::feature::WRAP.to_string(),
            ];
            if extended {
                env.can_extend = false;
                inner.plugin_extended_env = Some(env.clone());
                if let Some(extended) = inner.plugin_extended.as_mut() {
                    extended.set_env(env);
                }
            } else {
                inner.plugin_panel_env = Some(env.clone());
                if let Some(panel) = inner.plugin_panel.as_mut() {
                    panel.set_env(env);
                }
            }
        }
        self.plugin_panel_tell_env();
        self.plugins_follow();
    }

    /// Tells the panel's plugin where the pane in focus is, and the page's
    /// language, when either moved since it was told.
    fn plugin_panel_follow_place(self: &Rc<Self>) {
        let moved = {
            let inner = &mut *self.inner.borrow_mut();
            let cwd = Self::focused_cwd(inner);
            let terminal = Self::focused_terminal(inner);
            let locale = thinkterm_i18n::current_locale();
            let mut moved = false;
            let views = [
                (&mut inner.plugin_panel_env, &mut inner.plugin_panel),
                (&mut inner.plugin_extended_env, &mut inner.plugin_extended),
            ];
            for (env, model) in views {
                // The pane in focus not known for the moment, the one told
                // stays.
                let terminal = terminal.clone().or_else(|| env.as_ref().and_then(|env| env.terminal.clone()));
                match env.as_mut() {
                    Some(env) if env.cwd != cwd || env.terminal != terminal || env.locale != locale => {
                        env.cwd = cwd.clone();
                        env.terminal = terminal;
                        env.locale = locale.to_string();
                        if let Some(model) = model.as_mut() {
                            model.set_env(env.clone());
                        }
                        moved = true;
                    }
                    _ => {}
                }
            }
            moved
        };
        if moved {
            self.plugin_panel_tell_env();
        }
    }

    /// The pane in focus as `thinkterm cli` on the server's machine reaches
    /// it, where the plugins the page reaches run: by the number the server
    /// gave it, through the server, which the page knows no path for.
    fn focused_terminal(inner: &Inner<P, L>) -> Option<thinkterm_plugin_panel::Terminal> {
        inner.panes.contains_key(&inner.focused_pane).then(|| thinkterm_plugin_panel::Terminal {
            pane: inner.focused_pane as u64,
            socket: None,
        })
    }

    /// The directory of the pane in focus, as a path on the server's
    /// machine: where the plugins the page reaches run, and the terminal.
    fn focused_cwd(inner: &Inner<P, L>) -> Option<String> {
        let url = inner.panes.get(&inner.focused_pane)?.session.working_dir()?;
        if url.scheme() != "file" {
            return None;
        }
        let path = percent_encoding::percent_decode_str(url.path())
            .decode_utf8()
            .ok()?;
        // `/C:/Users/...` from a Windows server.
        let path = match path.strip_prefix('/') {
            Some(rest) if rest.as_bytes().get(1) == Some(&b':') => rest,
            _ => &path,
        };
        (!path.is_empty()).then(|| path.to_string())
    }

    /// Tells the panel's plugin the size the page gave it last, if it is
    /// still to be told and the last was told long enough ago; comes back
    /// when that is, if it is not.
    fn plugin_panel_tell_env(self: &Rc<Self>) {
        let (sends, extended_sends, later) = {
            let inner = &mut *self.inner.borrow_mut();
            let now = inner.platform.monotonic_ms();
            if inner.disconnected.is_some() {
                return;
            }
            let (sends, later) = inner.plugin_panel.as_mut().map_or((Vec::new(), None), |panel| panel.tell_env(now));
            let (extended_sends, extended_later) =
                inner.plugin_extended.as_mut().map_or((Vec::new(), None), |extended| extended.tell_env(now));
            let later = match (later, extended_later) {
                (Some(a), Some(b)) => Some(a.min(b)),
                (a, b) => a.or(b),
            };
            (sends, extended_sends, later)
        };
        self.send_panel_frames(sends, false);
        self.send_panel_frames(extended_sends, true);
        if let Some(delay) = later {
            self.wake_plugin_panel(delay);
        }
    }

    /// The plugin panel on show, or its extended view: JSON `PanelView`, or
    /// `null`.
    pub fn plugin_panel_view(&self, extended: bool) -> Option<crate::plugin_panel::PanelView> {
        let inner = self.inner.borrow();
        Self::plugin_view(&inner, extended).map(|panel| panel.view_json())
    }

    /// Changes when `plugin_panel_view` would.
    pub fn plugin_panel_revision(&self, extended: bool) -> u64 {
        let inner = self.inner.borrow();
        Self::plugin_view(&inner, extended).map_or(0, |panel| (panel.view() << 32) | (panel.revision() & 0xffff_ffff))
    }

    /// Whether the page is to give the plugin panel on show room beside
    /// the right panel for its extended view: its frames ask for one.
    pub fn plugin_panel_extended(&self) -> bool {
        self.inner.borrow().plugin_panel.as_ref().is_some_and(|panel| panel.wants_extended())
    }

    /// The extended view's close button pressed: the view goes at once, its
    /// plugin is told the user closed it, and the panel gets none until its
    /// frames have stopped asking for one.
    pub fn plugin_extended_close(self: &Rc<Self>) {
        let frames = {
            let inner = &mut *self.inner.borrow_mut();
            if let Some(panel) = inner.plugin_panel.as_mut() {
                panel.dismiss();
            }
            match inner.plugin_extended.take() {
                Some(extended) if !extended.waiting() && inner.disconnected.is_none() => {
                    vec![extended.closed_by_user(), extended.close()]
                }
                _ => Vec::new(),
            }
        };
        // For a view that is gone: nothing is to hear of them.
        for frame in frames {
            self.send_plugin_frame(frame, FrameFor::Nobody);
        }
        Self::notify(&self.inner.borrow());
    }

    /// The page took the extended view's room away -- nothing asks for it,
    /// or the window grew too narrow for it: it is closed, and opened again
    /// once the page gives it room.
    pub fn plugin_extended_gone(self: &Rc<Self>) {
        let closing = {
            let inner = &mut *self.inner.borrow_mut();
            inner.plugin_extended_env = None;
            inner
                .plugin_extended
                .take()
                .filter(|extended| !extended.waiting() && inner.disconnected.is_none())
                .map(|extended| extended.close())
        };
        if let Some(frame) = closing {
            self.send_plugin_frame(frame, FrameFor::Nobody);
        }
        Self::notify(&self.inner.borrow());
    }

    /// The pointer over the plugin panel, or its extended view, in its
    /// units. True when the page is to paint it again.
    pub fn plugin_panel_pointer(&self, extended: bool, x: f32, y: f32) -> bool {
        let mut inner = self.inner.borrow_mut();
        Self::plugin_view_mut(&mut inner, extended).is_some_and(|panel| panel.pointer(x, y))
    }

    pub fn plugin_panel_leave(&self, extended: bool) -> bool {
        let mut inner = self.inner.borrow_mut();
        Self::plugin_view_mut(&mut inner, extended).is_some_and(|panel| panel.leave())
    }

    /// A press in the plugin panel, or its extended view. True when it went
    /// to the plugin.
    #[allow(clippy::too_many_arguments)]
    pub fn plugin_panel_click(
        self: &Rc<Self>,
        extended: bool,
        x: f32,
        y: f32,
        button: thinkterm_plugin_panel::Button,
        count: u32,
        mods: thinkterm_plugin_panel::Mods,
    ) -> bool {
        let sends = {
            let mut inner = self.inner.borrow_mut();
            match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) => panel.click(x, y, button, count, mods),
                None => return false,
            }
        };
        let sent = !sends.is_empty();
        self.send_panel_frames(sends, extended);
        sent
    }

    /// Whether the plugin panel, or its extended view, has the keyboard: one
    /// of its fields is the page's field with it.
    pub fn plugin_panel_has_keyboard(&self, extended: bool) -> bool {
        Self::plugin_view(&self.inner.borrow(), extended).is_some_and(|panel| panel.has_keyboard())
    }

    /// The user put the keyboard in field `id` of the plugin panel, or its
    /// extended view: pressed in it, or tabbed to it. The other view lets go
    /// of it.
    pub fn plugin_panel_field_focus(self: &Rc<Self>, extended: bool, id: &str) {
        let (sends, others) = {
            let mut inner = self.inner.borrow_mut();
            let others = match Self::plugin_view_mut(&mut inner, !extended) {
                Some(other) if other.has_keyboard() => other.blur(),
                _ => Vec::new(),
            };
            let sends = match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) => panel.focus_field(id),
                None => Vec::new(),
            };
            (sends, others)
        };
        self.send_panel_frames(others, !extended);
        self.send_panel_frames(sends, extended);
    }

    /// What field `id` of the plugin panel, or its extended view, holds now
    /// the user edited it: what the page's field is to hold instead, when
    /// the panel's takes less.
    pub fn plugin_panel_field_text(self: &Rc<Self>, extended: bool, id: &str, text: &str) -> Option<String> {
        let (sends, held) = {
            let mut inner = self.inner.borrow_mut();
            match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) => panel.edit_field(id, text),
                None => return None,
            }
        };
        self.send_panel_frames(sends, extended);
        held
    }

    /// The user submitted field `id` of the plugin panel, or its extended
    /// view.
    pub fn plugin_panel_field_submit(self: &Rc<Self>, extended: bool, id: &str) {
        let sends = {
            let mut inner = self.inner.borrow_mut();
            match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) => panel.submit_field(id),
                None => return,
            }
        };
        self.send_panel_frames(sends, extended);
    }

    /// The keyboard went from the plugin panel, or its extended view.
    pub fn plugin_panel_blur(self: &Rc<Self>, extended: bool) {
        let sends = {
            let mut inner = self.inner.borrow_mut();
            match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) if panel.has_keyboard() => panel.blur(),
                _ => return,
            }
        };
        self.send_panel_frames(sends, extended);
    }

    /// A key the page's field with the keyboard does not use, in the plugin
    /// panel or its extended view, named as the browser names it: true when
    /// it was the panel's, for the page to keep it to itself.
    pub fn plugin_panel_key(
        self: &Rc<Self>,
        extended: bool,
        key: &str,
        mods: thinkterm_plugin_panel::Mods,
    ) -> bool {
        let (taken, sends) = {
            let mut inner = self.inner.borrow_mut();
            match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) => panel.key(key, mods),
                None => return false,
            }
        };
        self.send_panel_frames(sends, extended);
        taken
    }

    /// The wheel over the plugin panel, or its extended view, `dy` of its
    /// units down. True when something in it scrolled, and the page is to
    /// paint it again.
    pub fn plugin_panel_wheel(self: &Rc<Self>, extended: bool, x: f32, y: f32, dx: f32, dy: f32) -> bool {
        let (moved, sends) = {
            let mut inner = self.inner.borrow_mut();
            match Self::plugin_view_mut(&mut inner, extended) {
                Some(panel) => panel.wheel(x, y, dx, dy),
                None => return false,
            }
        };
        self.send_panel_frames(sends, extended);
        moved
    }

    fn plugin_view(inner: &Inner<P, L>, extended: bool) -> Option<&crate::plugin_panel::PanelModel> {
        match extended {
            true => inner.plugin_extended.as_ref(),
            false => inner.plugin_panel.as_ref(),
        }
    }

    fn plugin_view_mut(inner: &mut Inner<P, L>, extended: bool) -> Option<&mut crate::plugin_panel::PanelModel> {
        match extended {
            true => inner.plugin_extended.as_mut(),
            false => inner.plugin_panel.as_mut(),
        }
    }

    /// Once nothing needs the server's way to the plugin host, it is let go
    /// after a while: looked at whenever a need may have ended -- something
    /// went off show, or a call was answered.
    fn linger_plugin_pipe(self: &Rc<Self>) {
        let linger = {
            let inner = &mut *self.inner.borrow_mut();
            if Self::plugin_pipe_needed(inner) || !inner.plugin_pipe {
                return;
            }
            inner.plugin_pipe_linger += 1;
            inner.plugin_pipe_linger
        };
        let weak = Rc::downgrade(self);
        self.platform.set_timeout(
            PLUGIN_PIPE_LINGER_MS,
            Box::new(move || {
                if let Some(app) = weak.upgrade() {
                    app.plugin_pipe_let_go(linger);
                }
            }),
        );
    }

    /// The linger after the last need is over: the way to the host is let
    /// go, unless something needed it meanwhile or a later linger runs.
    fn plugin_pipe_let_go(self: &Rc<Self>, linger: u64) {
        let close = {
            let inner = &mut *self.inner.borrow_mut();
            let close = inner.plugin_pipe_linger == linger
                && inner.plugin_pipe
                && inner.disconnected.is_none()
                && !Self::plugin_pipe_needed(inner);
            if close {
                inner.plugin_pipe = false;
                inner.plugin_list.connection_lost();
            }
            close
        };
        if close {
            self.send_plugin_frame(Vec::new(), FrameFor::Nobody);
        }
    }

    /// Ask the plugin host for the plugin list, if something needs it and it
    /// is out of date: one request at a time.
    fn plugins_list(self: &Rc<Self>) {
        let locale = {
            let inner = &mut *self.inner.borrow_mut();
            if inner.disconnected.is_some() {
                return;
            }
            let Some(locale) = inner.plugin_list.next_list(thinkterm_i18n::current_locale()) else {
                return;
            };
            locale
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            let request = plugins_api::Request::List { locale };
            let body = serde_json::to_value(request).expect("a plugin request always serialises");
            let answer = app.plugin_call(plugins_api::PLUGIN, body).await;
            app.inner.borrow_mut().plugin_list.listed(answer);
            // A change may have come meanwhile, and the Snippets tab
            // follows its plugin's switch.
            app.plugins_follow();
        });
    }

    /// The plugin list in Settings › Sidebar & Plugins: JSON `PluginsView`.
    pub fn plugins_view(&self) -> crate::plugin_list::PluginsView {
        self.inner.borrow().plugin_list.view()
    }

    pub fn plugins_revision(&self) -> u64 {
        self.inner.borrow().plugin_list.revision()
    }

    /// Turn a plugin on or off. The switch shows the change until the
    /// host answers; false when it refused.
    pub async fn plugin_set_enabled(self: &Rc<Self>, id: String, enabled: bool) -> bool {
        self.inner.borrow_mut().plugin_list.switching(&id, enabled);
        Self::notify(&self.inner.borrow());
        let request = plugins_api::Request::SetEnabled { id: id.clone(), enabled };
        let body = serde_json::to_value(request).expect("a plugin request always serialises");
        let answer = self.plugin_call(plugins_api::PLUGIN, body).await.map(drop);
        let done = answer.is_ok();
        self.inner.borrow_mut().plugin_list.switched(&id, answer);
        self.plugins_follow();
        done
    }

    /// Let plugin `id` run unused as `background` says -- `always`,
    /// `briefly` or `never` -- its manifest's own being kept as no choice at
    /// all.
    pub async fn plugin_set_background(self: &Rc<Self>, id: String, background: String) -> bool {
        let Some(background) = crate::plugin_list::background_named(&background) else {
            return false;
        };
        let default = {
            let inner = &mut *self.inner.borrow_mut();
            inner.plugin_list.choosing(&id, background);
            inner.plugin_list.background_default(&id)
        };
        Self::notify(&self.inner.borrow());
        let request = plugins_api::Request::SetBackground {
            id: id.clone(),
            background: (Some(background) != default).then_some(background),
        };
        let body = serde_json::to_value(request).expect("a plugin request always serialises");
        let answer = self.plugin_call(plugins_api::PLUGIN, body).await.map(drop);
        let done = answer.is_ok();
        self.inner.borrow_mut().plugin_list.chose(&id, answer);
        self.plugins_follow();
        done
    }

    /// Look for new and removed plugins on the server's machine, and
    /// restart the running ones.
    pub async fn plugins_reload(self: &Rc<Self>) -> bool {
        let request = plugins_api::Request::Reload { id: None };
        let body = serde_json::to_value(request).expect("a plugin request always serialises");
        let answer = self.plugin_call(plugins_api::PLUGIN, body).await;
        let done = answer.is_ok();
        if let Err(why) = answer {
            self.inner.borrow_mut().plugin_list.refused(why);
        }
        self.plugins_follow();
        done
    }

    /// One frame for the plugin host, through the server, on the wire now
    /// and so behind every frame sent before it. The call it carries, if
    /// any, fails when the server cannot pass it on.
    fn send_plugin_frame(self: &Rc<Self>, data: Vec<u8>, sender: FrameFor) {
        let answer = {
            let inner = &mut *self.inner.borrow_mut();
            if inner.disconnected.is_some() {
                // A panel hears of it from `on_close`.
                if let FrameFor::Call(call) = sender {
                    inner.plugin_calls.fail(call, "not connected to the server");
                }
                return;
            }
            // A frame opens the server's way to the host; an empty one
            // closes it.
            inner.plugin_pipe = !data.is_empty();
            inner.link.request(Pdu::PluginFrame(codec::PluginFrame { data }))
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            let refused = match answer.await {
                Ok(Pdu::UnitResponse(_)) => return,
                Ok(Pdu::ErrorResponse(err)) => err.reason,
                Ok(other) => format!("unexpected answer {}", other.pdu_name()),
                Err(err) => format!("{err:#}"),
            };
            log::warn!("plugin channel: {refused}");
            match sender {
                FrameFor::Nobody => {}
                FrameFor::Call(call) => app.inner.borrow_mut().plugin_calls.fail(call, &refused),
                FrameFor::Panel(view) => app.plugin_panel_unsent(view, &refused),
            }
        });
    }

    /// Sends frames for the page's plugin panel, or its extended view: one
    /// that does not reach the host has the view say why, and a panel open
    /// again in a while.
    fn send_panel_frames(self: &Rc<Self>, frames: Vec<Vec<u8>>, extended: bool) {
        if frames.is_empty() {
            return;
        }
        let view = Self::plugin_view(&self.inner.borrow(), extended).map(|panel| panel.view());
        for frame in frames {
            self.send_plugin_frame(frame, view.map_or(FrameFor::Nobody, FrameFor::Panel));
        }
    }

    /// A frame for the page's panel `view` did not reach the host -- the
    /// server could not reach it, say, and closes no way to it, having
    /// opened none: the panel says why, and is opened again in a while.
    fn plugin_panel_unsent(self: &Rc<Self>, view: u64, why: &str) {
        let later = {
            let inner = &mut *self.inner.borrow_mut();
            let now = inner.platform.monotonic_ms();
            // An extended view says why, and waits for its panel's next
            // opening.
            if let Some(extended) = inner.plugin_extended.as_mut().filter(|extended| extended.view() == view) {
                extended.unsent(why, now);
            }
            let panel = inner.plugin_panel.as_mut().filter(|panel| panel.view() == view);
            panel.and_then(|panel| panel.unsent(why, now))
        };
        if let Some(delay) = later {
            self.wake_plugin_panel(delay);
        }
        Self::notify(&self.inner.borrow());
    }

    /// Ask a plugin on the server's machine: its answer, or why there is
    /// none. A host that does not answer in time is given up on, so a save
    /// or a list never waits for good.
    fn plugin_call(self: &Rc<Self>, plugin: &str, body: serde_json::Value) -> impl Future<Output = crate::plugins::Answer> {
        self.plugin_call_within(plugin, body, PLUGIN_CALL_TIMEOUT_MS)
    }

    /// [`plugin_call`](Self::plugin_call), waiting `wait_ms` for the answer.
    /// The call is on its way when this returns, before its answer is
    /// awaited: whatever the page does next finds it waiting, and keeps the
    /// way to the host for it.
    fn plugin_call_within(
        self: &Rc<Self>,
        plugin: &str,
        body: serde_json::Value,
        wait_ms: f64,
    ) -> impl Future<Output = crate::plugins::Answer> {
        let (id, frame, answer) = self.inner.borrow_mut().plugin_calls.call(plugin, body);
        self.send_plugin_frame(frame, FrameFor::Call(id));
        let weak = Rc::downgrade(self);
        let timeout = weak.clone();
        self.platform.set_timeout(
            wait_ms,
            Box::new(move || {
                if let Some(app) = timeout.upgrade() {
                    app.inner.borrow_mut().plugin_calls.fail(id, "the plugin host did not answer in time");
                }
            }),
        );
        async move {
            let answer = answer.await.unwrap_or_else(|_| Err("the call was dropped".into()));
            // The way to the host was kept for this call; it may be the last
            // thing that needed it.
            if let Some(app) = weak.upgrade() {
                app.linger_plugin_pipe();
            }
            answer
        }
    }

    /// A frame from the plugin host, through the server: an answer, an
    /// event, or -- empty -- word that the server's connection to the host
    /// closed. The snippets are then asked for again, after a pause that
    /// grows while the host keeps going away.
    fn plugin_frame(self: &Rc<Self>, data: Vec<u8>) {
        if data.is_empty() {
            let delay = {
                let inner = &mut *self.inner.borrow_mut();
                inner.plugin_pipe = false;
                let snippets_again = inner.snippets.host_gone();
                inner.plugin_list.connection_lost();
                let delay = inner.snippets.retry_delay_ms().max(inner.plugin_list.retry_delay_ms());
                // Opened again when the rest is asked for again.
                let at = inner.platform.monotonic_ms() + delay;
                let panel_again = match inner.plugin_panel.as_mut() {
                    Some(panel) => {
                        panel.lost(at);
                        panel.waiting()
                    }
                    None => false,
                };
                if let Some(extended) = inner.plugin_extended.as_mut() {
                    extended.lost(at);
                }
                let again = (snippets_again || panel_again || inner.plugin_list.wanted()).then_some(delay);
                inner.plugin_calls.lost("the plugin host went away");
                again
            };
            if let Some(delay) = delay {
                let weak = Rc::downgrade(self);
                self.platform.set_timeout(
                    delay,
                    Box::new(move || {
                        if let Some(app) = weak.upgrade() {
                            app.inner.borrow_mut().plugin_list.retry();
                            app.plugins_follow();
                        }
                    }),
                );
            }
            Self::notify(&self.inner.borrow());
            return;
        }
        let heard = self.inner.borrow_mut().plugin_calls.heard(&data);
        let (plugin, body) = match heard {
            Some(crate::plugins::Heard::Event(plugin, body)) => (plugin, body),
            Some(crate::plugins::Heard::Panel(view, event)) => return self.plugin_panel_heard(view, event),
            None => return,
        };
        if plugin == plugins_api::PLUGIN {
            match serde_json::from_value(body) {
                Ok(plugins_api::Event::Changed) => {
                    self.inner.borrow_mut().plugin_list.list_changed();
                    self.plugins_list();
                }
                Err(err) => log::warn!("a plugin list event that does not read: {err}"),
            }
            return;
        }
        let changed = plugin == snippets_wire::PLUGIN
            && matches!(serde_json::from_value(body), Ok(snippets_wire::Event::Changed));
        if changed {
            self.inner.borrow_mut().snippets.snippets_changed();
            self.snippets_list();
        }
    }

    /// Ask the plugin host for the rows the tab's search matches: one
    /// request at a time, the newest search asked for once it lands.
    fn snippets_list(self: &Rc<Self>) {
        let Some(query) = self.inner.borrow_mut().snippets.next_list() else {
            return;
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            let answer = app.snippets_call(snippets_wire::Request::List { query: query.clone() }).await;
            app.inner.borrow_mut().snippets.listed(query, answer);
            // The search may have moved on while this was out.
            app.snippets_list();
            Self::notify(&app.inner.borrow());
        });
    }

    async fn snippets_call(self: &Rc<Self>, request: snippets_wire::Request) -> crate::plugins::Answer {
        let body = serde_json::to_value(request).expect("a snippets request always serialises");
        self.plugin_call(snippets_wire::PLUGIN, body).await
    }

    /// The Snippets tab: JSON `SnippetsView`.
    pub fn snippets_view(&self) -> crate::snippets::SnippetsView {
        self.inner.borrow().snippets.view()
    }

    pub fn snippets_revision(&self) -> u64 {
        self.inner.borrow().snippets.revision()
    }

    /// The search box: the plugin host is asked what it matches.
    pub fn snippets_search(self: &Rc<Self>, query: &str) {
        self.inner.borrow_mut().snippets.set_query(query);
        self.snippets_list();
        Self::notify(&self.inner.borrow());
    }

    /// A snippet whole, for the editor: JSON `Snippet`, or `null`.
    pub async fn snippet(self: &Rc<Self>, id: String) -> String {
        match self.snippets_call(snippets_wire::Request::Get { id }).await {
            Ok(body) => body.to_string(),
            Err(why) => {
                log::warn!("snippets: {why}");
                "null".to_string()
            }
        }
    }

    /// Save what the editor holds, as the plugin host decides: JSON
    /// `Saved`, or an `error` outcome with its message.
    pub async fn snippet_save(self: &Rc<Self>, id: Option<String>, title: String, body: String) -> String {
        match self.snippets_call(snippets_wire::Request::Save { id, title, body }).await {
            Ok(body) => body.to_string(),
            Err(message) => serde_json::json!({ "outcome": "error", "message": message }).to_string(),
        }
    }

    pub async fn snippet_delete(self: &Rc<Self>, id: String) -> bool {
        match self.snippets_call(snippets_wire::Request::Delete { id }).await {
            Ok(serde_json::Value::Bool(deleted)) => deleted,
            Ok(_) => false,
            Err(why) => {
                log::warn!("snippets: {why}");
                false
            }
        }
    }

    /// A snippet into the focused pane, as the plugin host says to send
    /// it: pasted, or typed and run. Into the pane focused at the press,
    /// never whichever is focused when the answer comes, and not at all
    /// once that pane is off the page or the answer comes too late to still
    /// be what was meant.
    pub async fn snippet_paste(self: &Rc<Self>, id: String, run: bool) -> bool {
        let (pane, pressed) = {
            let inner = self.inner.borrow();
            (inner.focused_pane, inner.platform.monotonic_ms())
        };
        let text = match self.snippets_call(snippets_wire::Request::Text { id, run }).await {
            Ok(serde_json::Value::String(text)) => text,
            Ok(_) => return false,
            Err(why) => {
                log::warn!("snippets: {why}");
                return false;
            }
        };
        if self.platform.monotonic_ms() - pressed > SNIPPET_SEND_WAIT_MS {
            log::warn!("not sending a snippet: its text came too late");
            return false;
        }
        self.send_text_to(pane, &text, !run)
    }

    /// `text` into `pane`, if it is still on the page and this page may type:
    /// as a paste, or as typed keys.
    fn send_text_to(&self, pane: PaneId, text: &str, paste: bool) -> bool {
        let mut inner = self.inner.borrow_mut();
        if inner.disconnected.is_some() || !Self::may_type(&inner) {
            return false;
        }
        let platform = Rc::clone(&inner.platform);
        let Some(cell) = inner.panes.get_mut(&pane) else {
            return false;
        };
        cell.scroll_from_bottom = 0;
        cell.scroll_px = 0.0;
        let start = if paste { cell.session.paste(text) } else { cell.session.write_bytes(text.as_bytes()) };
        Self::spawn_drain(&platform, &cell.session, start);
        drop(inner);
        self.request_frame();
        true
    }

    /// Open or fold an agent's sub-task list in the Agents panel.
    pub fn agent_toggle_subtasks(&self, pane: PaneId) -> bool {
        let mut inner = self.inner.borrow_mut();
        if !inner.open_subtasks.remove(&pane) {
            inner.open_subtasks.insert(pane);
        }
        true
    }

    /// Bring an agent's pane on show: switch to it here, or open the
    /// thread that holds it when it is in another window.
    pub fn agent_reveal(self: &Rc<Self>, pane: PaneId) -> bool {
        let (here, thread) = {
            let inner = self.inner.borrow();
            let here = inner.tab_layout.as_ref().is_some_and(|l| l.panes.iter().any(|p| p.stack.iter().any(|m| m.pane_id == pane)))
                || inner.layout.as_ref().is_some_and(|l| {
                    l.tabs
                        .iter()
                        .filter(|t| t.window_and_tab_ids().is_some_and(|(w, _)| w == inner.window_id))
                        .any(|t| crate::layout::leaves(t).iter().any(|e| e.pane_id == pane))
                });
            let thread = inner.tree.session.as_ref().and_then(|s| {
                s.projects.iter().flat_map(|p| p.threads.iter()).find(|t| t.tabs.iter().any(|tab| tab.pane_ids.contains(&pane))).map(|t| t.id.clone())
            });
            (here, thread)
        };
        if here {
            self.switch_to_pane(pane, true);
            return true;
        }
        match thread {
            Some(id) => {
                let space = self.inner.borrow().tree.project_of(&id).map(|p| p.space_id.clone());
                if let Some(space) = space {
                    self.set_space(&space);
                }
                self.activate_thread(id);
                true
            }
            None => {
                // A window no thread claims: its listing knows the pane.
                self.switch_to_pane(pane, true);
                true
            }
        }
    }

    /// Re-upload and repaint the panes whose palette moved: every glyph and
    /// the ground behind it are coloured from it.
    fn repaint_palettes(self: &Rc<Self>, changed: &[PaneId]) {
        if changed.is_empty() {
            return;
        }
        {
            let inner = self.inner.borrow();
            for pane_id in changed {
                if let Some(cell) = inner.panes.get(pane_id) {
                    cell.session.make_all_stale();
                }
            }
        }
        self.request_frame();
    }

    /// The colours of the scheme this browser picked, as the page read them
    /// out of `schemes.json`; `None` follows the server's configuration.
    pub fn set_terminal_palette(self: &Rc<Self>, palette: Option<ColorPalette>) {
        let changed = {
            let mut inner = self.inner.borrow_mut();
            if inner.chosen_palette == palette {
                return;
            }
            inner.chosen_palette = palette;
            inner.recompute_palettes()
        };
        self.repaint_palettes(&changed);
        self.advise_palette();
        Self::notify(&self.inner.borrow());
    }

    /// The pane in use takes the scheme on the server too -- on show only:
    /// telling a host's server which pane has focus moves its desktop's tab
    /// when the desktop follows the page. A scheme chosen off show is told
    /// with this once the page is shown again.
    pub fn advise_palette(self: &Rc<Self>) {
        if !self.platform.shown() {
            return;
        }
        let (link, pane, palette) = {
            let inner = self.inner.borrow();
            (inner.link.clone(), inner.focused_pane, inner.configured())
        };
        Self::advise_focus(&self.platform, link, pane, palette);
    }

    /// The page's preferences, as the model holds them.
    pub fn settings_view(&self) -> crate::settings::WebSettings {
        self.inner.borrow().settings.clone()
    }

    /// The server's version, as it answered when the page attached.
    pub fn server_version(&self) -> String {
        self.inner.borrow().server_version.clone()
    }

    /// The page's stored preferences, at boot: applied whole.
    pub fn apply_settings(self: &Rc<Self>, json: &str) -> Result<(), String> {
        let settings = crate::settings::WebSettings::parse(json)?;
        self.inner.borrow_mut().settings = settings.clone();
        self.apply_language(&settings.language);
        self.set_font_mode(settings.font);
        if settings.terminal_scheme == crate::settings::FOLLOW_DESKTOP {
            self.set_terminal_palette(None);
        }
        Ok(())
    }

    /// One preference changed on the page.
    pub fn set_setting(self: &Rc<Self>, key: &str, value: &str) -> Result<(), String> {
        let (language, font, scheme) = {
            let mut inner = self.inner.borrow_mut();
            inner.settings.set(key, value)?;
            (
                inner.settings.language.clone(),
                inner.settings.font,
                inner.settings.terminal_scheme.clone(),
            )
        };
        match key {
            "language" => self.apply_language(&language),
            "font" => self.set_font_mode(font),
            // Back to the desktop's scheme: the colours the page sends with
            // a named pick are simply dropped.
            "terminal-scheme" if scheme == crate::settings::FOLLOW_DESKTOP => {
                self.set_terminal_palette(None)
            }
            // Stepped mode holds no part of a row: whatever a pane was
            // through goes, rather than staying frozen until it is scrolled.
            "scroll-mode" => {
                for cell in self.inner.borrow_mut().panes.values_mut() {
                    cell.scroll_px = 0.0;
                }
                Self::notify(&self.inner.borrow());
                self.request_frame();
            }
            "cursor-blink" => {
                // Read first: `set_blinking` borrows mutably.
                let on = self.inner.borrow().settings.cursor_blink;
                self.set_blinking(on);
            }
            "cursor-style" | "min-contrast" | "pane-bars" => {
                if key == "pane-bars" {
                    // The rows the bars take changed: the grid is laid out again.
                    self.inner.borrow_mut().cols = 0;
                    self.resize();
                }
                self.request_frame();
            }
            _ => Self::notify(&self.inner.borrow()),
        }
        Ok(())
    }

    /// Blink the cursor: a half-second tick that hides it every other
    /// beat. Intervals cannot be cancelled, so the tick starts once and
    /// idles while blinking is off. It holds the App weakly: a strong
    /// reference here would keep a retired App (and its GPU state)
    /// alive for the life of the platform.
    fn set_blinking(self: &Rc<Self>, on: bool) {
        let mut inner = self.inner.borrow_mut();
        inner.blink_shown = true;
        if on && !inner.blink_ticking {
            inner.blink_ticking = true;
            let weak = Rc::downgrade(self);
            inner.platform.set_interval(
                530.0,
                Box::new(move || {
                    let Some(app) = weak.upgrade() else { return };
                    if app.retired.get() {
                        return;
                    }
                    let mut inner = app.inner.borrow_mut();
                    if !inner.settings.cursor_blink || inner.panes.is_empty() {
                        return;
                    }
                    inner.blink_shown = !inner.blink_shown;
                    drop(inner);
                    app.request_frame();
                }),
            );
        }
        drop(inner);
        self.request_frame();
    }

    fn apply_language(&self, preference: &str) {
        let languages = self.inner.borrow().languages.clone();
        thinkterm_i18n::activate_preference(preference, &languages);
        self.locale_changed();
    }

    /// The base font: a size of the page's own, or the desktop's cell
    /// again. Pinning rasterises at that size and refits the grid.
    pub fn set_font_mode(self: &Rc<Self>, mode: crate::settings::FontMode) {
        use crate::settings::FontMode;
        match mode {
            FontMode::Pinned { pt } => {
                let pt = pt.clamp(6.0, 72.0);
                let mut inner = self.inner.borrow_mut();
                inner.font_pinned = true;
                inner.base_size_pt = pt;
                if (inner.glyphs.size_pt - pt).abs() >= 0.125 {
                    let dpi = (inner.platform.units_per_inch() * inner.dpr) as u32;
                    Self::rerasterise(&mut inner, pt, dpi);
                    inner.cols = 0;
                }
                drop(inner);
                self.resize();
                self.request_frame();
            }
            FontMode::Follow => {
                let mut inner = self.inner.borrow_mut();
                inner.font_pinned = false;
                inner.base_size_pt = inner.boot_size_pt;
                drop(inner);
                self.match_desktop_cell();
                self.request_frame();
            }
        }
    }

    /// What the palette can find, from the model, ranked for `query`.
    pub fn palette(&self, query: &str) -> crate::palette::Results {
        use crate::palette::{path_terms, Entry, Group};
        let inner = self.inner.borrow();
        let mut entries = Vec::new();
        if let Some(session) = &inner.tree.session {
            let space_name = |id: &str| session.spaces.iter().find(|s| s.id == id).map(|s| s.name.clone()).unwrap_or_default();
            for project in &session.projects {
                for thread in &project.threads {
                    let icon = match inner.tree.status_of(thread) {
                        crate::tree::Status::Running => "loader-circle",
                        crate::tree::Status::NeedsAttention => "circle-alert",
                        crate::tree::Status::Done => "circle-check",
                        crate::tree::Status::Idle => "square-terminal",
                    };
                    entries.push(Entry {
                        id: format!("thread:{}", thread.id),
                        title: thread.name.clone(),
                        subtitle: project.name.clone(),
                        icon,
                        accessory: space_name(&project.space_id),
                        group: Group::Threads,
                        terms: path_terms(&project.path),
                    });
                }
            }
        }
        if let Some((tabs, _)) = Self::strip_model(&inner) {
            for tab in tabs {
                entries.push(Entry {
                    id: format!("tab:{}", tab.tab_id),
                    title: tab.label,
                    subtitle: String::new(),
                    icon: "square-terminal",
                    accessory: String::new(),
                    group: Group::Tabs,
                    terms: tab.title,
                });
            }
        }
        if let Some(layout) = &inner.tab_layout {
            for place in &layout.panes {
                for member in &place.stack {
                    let raw = inner.panes.get(&member.pane_id).map(|c| c.title.as_str()).unwrap_or(&member.title);
                    let (title, _) = crate::navbar::display_title(raw);
                    entries.push(Entry {
                        id: format!("pane:{}", member.pane_id),
                        title,
                        subtitle: String::new(),
                        icon: "square-terminal",
                        accessory: String::new(),
                        group: Group::Panes,
                        terms: raw.to_string(),
                    });
                }
            }
        }
        let spaces = inner.tree.spaces();
        if spaces.len() > 1 {
            for space in spaces {
                entries.push(Entry {
                    id: format!("space:{}", space.id),
                    title: space.name,
                    subtitle: String::new(),
                    icon: if space.default { "house" } else { "layers" },
                    accessory: String::new(),
                    group: Group::Spaces,
                    terms: String::new(),
                });
            }
        }
        entries.extend(crate::palette::commands());
        crate::palette::results(query, entries, &inner.recent)
    }

    /// The page's remembered picks, given back at boot.
    pub fn set_recent(&self, ids: Vec<String>) {
        self.inner.borrow_mut().recent = ids;
    }

    /// Do what a palette pick asks. `page` names what only the page can
    /// do (its sidebar, its settings panel, remembering the language).
    pub fn palette_run(self: &Rc<Self>, id: &str) -> crate::views::PaletteOutcome {
        use crate::sidebar::SideClick;
        let mut outcome = crate::views::PaletteOutcome { handled: true, page: None, recent: Vec::new() };
        let (kind, arg) = id.split_once(':').unwrap_or((id, ""));
        match (kind, arg) {
            ("thread", id) => {
                // A thread in another Space: the sidebar follows it there.
                let space = self.inner.borrow().tree.project_of(id).map(|p| p.space_id.clone());
                if let Some(space) = space {
                    self.set_space(&space);
                }
                self.activate_thread(id.to_string());
            }
            ("tab", id) => {
                let target = id.parse().ok().and_then(|tab: TabId| {
                    let inner = self.inner.borrow();
                    Self::strip_model(&inner)?.0.into_iter().find(|t| t.tab_id == tab).map(|t| t.target)
                });
                match target {
                    Some(pane) => self.switch_to_pane(pane, true),
                    None => outcome.handled = false,
                }
            }
            ("pane", id) => match id.parse() {
                Ok(pane) => self.switch_to_pane(pane, true),
                Err(_) => outcome.handled = false,
            },
            ("space", id) => outcome.handled = self.set_space(id),
            ("cmd", "new-thread") => self.on_side_click(SideClick::NewThread(None)),
            ("cmd", "new-tab") => self.new_tab(),
            ("cmd", "split-right") => self.split(None, thinkterm_proto::SplitDirection::Horizontal),
            ("cmd", "split-down") => self.split(None, thinkterm_proto::SplitDirection::Vertical),
            ("cmd", "zoom") => self.toggle_zoom(None),
            ("cmd", "close-pane") => self.close_pane(),
            ("cmd", "take-over") => self.take_over(),
            ("cmd", "follow") => self.on_chrome_click(crate::chrome::Click::Follow),
            ("cmd", "toggle-sidebar") | ("cmd", "settings") | ("cmd", "remote-hosts") | ("cmd", "add-remote-host") => {
                outcome.page = Some(arg.to_string())
            }
            ("cmd", "font-up") => self.step_font(1.0),
            ("cmd", "font-down") => self.step_font(-1.0),
            ("cmd", "font-reset") => self.step_font(0.0),
            _ => outcome.handled = false,
        }
        if outcome.handled {
            let inner = &mut *self.inner.borrow_mut();
            crate::palette::remember(&mut inner.recent, id);
            outcome.recent = inner.recent.clone();
        }
        outcome
    }

    /// Show a Space in the sidebar; false when the server has no such Space.
    pub fn set_space(&self, id: &str) -> bool {
        let inner = &mut *self.inner.borrow_mut();
        let known = inner.tree.set_space(id);
        if known {
            inner.editing = crate::sidebar::Editing::None;
            inner.new_project_error = None;
            Self::notify(inner);
        }
        known
    }

    /// Every pane of a tab, from the last listing.
    fn tab_panes(inner: &Inner<P, L>, tab: TabId) -> Vec<PaneId> {
        inner
            .layout
            .as_ref()
            .and_then(|l| l.tabs.iter().find(|t| t.window_and_tab_ids().is_some_and(|(_, id)| id == tab)))
            .map(|node| crate::layout::leaves(node).iter().map(|e| e.pane_id).collect())
            .unwrap_or_default()
    }

    /// Do what a menu row asks. Copy hands the selection back for the
    /// page to put on the clipboard; Paste asks the page to read it.
    pub fn menu_action(self: &Rc<Self>, id: &str) -> crate::views::MenuOutcome {
        use crate::menu::{MenuAction, Side};
        use crate::sidebar::SideClick;
        let mut outcome = crate::views::MenuOutcome { handled: true, copy: None, paste: false };
        let Some(action) = MenuAction::parse(id) else {
            outcome.handled = false;
            return outcome;
        };
        match action {
            MenuAction::Copy => outcome.copy = Self::selection_text(&self.inner.borrow()),
            MenuAction::Paste => outcome.paste = true,
            MenuAction::Split { pane, side } => {
                use thinkterm_proto::SplitDirection::{Horizontal, Vertical};
                let (direction, second) = match side {
                    Side::Right => (Horizontal, true),
                    Side::Left => (Horizontal, false),
                    Side::Down => (Vertical, true),
                    Side::Up => (Vertical, false),
                };
                self.split_at(Some(pane), direction, second);
            }
            MenuAction::FrontendAccess(mode) => {
                let (tab_id, viewport) = {
                    let inner = self.inner.borrow();
                    let viewport = inner.link.lease().claim_viewport();
                    (inner.tab_id, viewport)
                };
                match viewport {
                    Some(viewport) => self.act(
                        "web-act-set-access",
                        Pdu::SetFrontendAccessMode(codec::SetFrontendAccessMode { mode, tab_id, viewport }),
                        |_, _| {},
                    ),
                    None => outcome.handled = false,
                }
            }
            MenuAction::CloseTabsLeft(tab) | MenuAction::CloseTabsRight(tab) | MenuAction::CloseOtherTabs(tab) => {
                let panes: Vec<PaneId> = {
                    let inner = self.inner.borrow();
                    let tabs = Self::strip_model(&inner).map(|(t, _)| t).unwrap_or_default();
                    let at = tabs.iter().position(|t| t.tab_id == tab).unwrap_or(0);
                    tabs.iter()
                        .enumerate()
                        .filter(|(i, t)| match action {
                            MenuAction::CloseTabsLeft(_) => *i < at,
                            MenuAction::CloseTabsRight(_) => *i > at,
                            _ => t.tab_id != tab,
                        })
                        .flat_map(|(_, t)| Self::tab_panes(&inner, t.tab_id))
                        .collect()
                };
                self.kill_panes(panes);
            }
            MenuAction::NewTabRight => self.new_tab(),
            MenuAction::Zoom(pane) => self.toggle_zoom(Some(pane)),
            MenuAction::Pin(id, on) => self.on_side_click(SideClick::Pin(id, on)),
            MenuAction::RenameThread(id) => self.on_side_click(SideClick::RenameThread(id)),
            MenuAction::DeleteThread(id) => {
                // The desktop's menu deletes at once; the two presses are
                // the sidebar button's.
                let panes = Self::thread_panes(&self.inner.borrow(), &id);
                self.kill_panes(panes);
                self.mutate(crate::tree::delete_thread(&id), |_| {});
            }
            MenuAction::MarkUnread(id) => self.mutate(crate::tree::set_unread(&id, true), |_| {}),
            MenuAction::RenameProject(id) => self.on_side_click(SideClick::RenameProject(id)),
            MenuAction::NewThread(id) => self.on_side_click(SideClick::NewThread(Some(id))),
            MenuAction::ToggleCollapsed(id) => self.on_side_click(SideClick::ToggleProject(id)),
            MenuAction::ArchiveProject(id) => self.on_side_click(SideClick::Archive(id)),
            MenuAction::UnarchiveProject(id) => self.on_side_click(SideClick::Unarchive(id)),
            MenuAction::RemoveProject(id) => {
                let panes = self.inner.borrow().tree.live_panes_of_project(&id);
                self.kill_panes(panes);
                self.mutate(crate::tree::remove_project(&id), |_| {});
            }
            MenuAction::ShowArchived => self.on_side_click(SideClick::ToggleArchived),
            MenuAction::SwitchSpace(id) => outcome.handled = self.set_space(&id),
            MenuAction::NewSpace => {
                let id = self.new_id("space");
                let count = self.inner.borrow().tree.spaces().len();
                let name = format!("Space {}", count + 1);
                let chosen = id.clone();
                self.mutate(crate::tree::create_space(id, &name), move |app| {
                    app.set_space(&chosen);
                });
            }
            MenuAction::RenameSpace(id) => {
                let inner = &mut *self.inner.borrow_mut();
                inner.editing = crate::sidebar::Editing::Space(id);
                Self::notify(inner);
            }
            MenuAction::DeleteSpace(id) => {
                let panes: Vec<PaneId> = {
                    let inner = self.inner.borrow();
                    inner
                        .tree
                        .session
                        .as_ref()
                        .map(|s| {
                            s.projects
                                .iter()
                                .filter(|p| p.space_id == id)
                                .flat_map(|p| inner.tree.live_panes_of_project(&p.id))
                                .collect()
                        })
                        .unwrap_or_default()
                };
                self.kill_panes(panes);
                self.mutate(crate::tree::delete_space(&id), |app| {
                    let first = app.inner.borrow().tree.spaces().first().map(|s| s.id.clone());
                    if let Some(first) = first {
                        app.set_space(&first);
                    }
                });
            }
        }
        outcome
    }

    /// The pane a bar's button or a chord acts on: the bar's pane, which
    /// the page focuses first as the desktop does, else the focused one.
    fn target_pane(self: &Rc<Self>, pane: Option<PaneId>) -> PaneId {
        match pane {
            Some(pane_id) => {
                self.focus_pane(pane_id, true);
                pane_id
            }
            None => self.inner.borrow().focused_pane,
        }
    }

    /// Zoom the focused pane to the whole tab, or back.
    pub fn toggle_zoom(self: &Rc<Self>, pane: Option<PaneId>) {
        let pane_id = self.target_pane(pane);
        let (tab_id, zoomed) = {
            let inner = self.inner.borrow();
            let zoomed = inner.tab_layout.as_ref().is_some_and(|l| l.zoomed.is_some());
            (inner.tab_id, zoomed)
        };
        self.act(
            "web-act-zoom-pane",
            Pdu::SetPaneZoomed(codec::SetPaneZoomed {
                containing_tab_id: tab_id,
                pane_id,
                zoomed: !zoomed,
            }),
            |_, _| {},
        );
    }

    /// Close the focused pane, ending its program.
    pub fn close_pane(self: &Rc<Self>) {
        let pane_id = self.inner.borrow().focused_pane;
        self.act(
            "web-act-close-pane",
            Pdu::KillPane(codec::KillPane { pane_id }),
            |_, _| {},
        );
    }

    /// The pane at `pane_id`, and the page's focus with it. `chosen` is a
    /// person's click, which also stops the page following the desktop;
    /// a focus push is not.
    pub fn switch_to_pane(self: &Rc<Self>, pane_id: PaneId, chosen: bool) {
        // `chosen` is a person's click: the desktop is told, as with a
        // click in a pane, so both screens keep one focus.
        let _ = chosen;
        // Already drawn: a focus change and nothing else. Not advised to
        // the server: a strip click has just pinned the page, and a focus
        // push is the server's own news.
        if self.inner.borrow().panes.contains_key(&pane_id) {
            self.focus_pane(pane_id, false);
            self.platform.focus_input();
            return;
        }
        // Elsewhere: listed again either way, since the entry carries the
        // pane's size and screen state and the listing may be seconds old.
        let app = Rc::clone(self);
        self.spawn(async move {
            let Some(list) = app.list_panes().await else {
                return;
            };
            if crate::layout::tab_containing(&list, pane_id).is_none() {
                log::warn!("pane {pane_id} is not on the server");
                let mut inner = app.inner.borrow_mut();
                inner.layout = Some(list);
                Self::render_strip(&inner);
                return;
            }
            app.show(list, pane_id).await;
        });
    }

    /// Reshape the tab to this window's grid (`on`), or go back to the
    /// desktop's size (`off`): a claim either way, at the size the lease
    /// then picks.
    pub fn fit(self: &Rc<Self>, on: bool) {
        let (link, tab_id) = {
            let inner = self.inner.borrow();
            inner.link.lease_mut().fit = on;
            (inner.link.clone(), inner.tab_id)
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            match link.claim(tab_id).await {
                Ok(_) => {
                    app.refresh_status();
                    app.request_frame();
                }
                Err(err) => {
                    let inner = app.inner.borrow();
                    Self::set_status(&inner, &failed("web-act-resize-tab", &err));
                }
            }
        });
    }

    /// Take the terminal from whichever device holds it (Handoff mode).
    /// There is no giving it back: the desktop takes it by interacting.
    pub fn take_over(self: &Rc<Self>) {
        self.take_over_with(false);
    }

    /// `quiet`: the page's own claim on showing a tab, not a person's
    /// action, so a refusal is logged rather than remarked on.
    fn take_over_with(self: &Rc<Self>, quiet: bool) {
        let (link, tab_id) = {
            let mut inner = self.inner.borrow_mut();
            if !quiet {
                // The card says it is on the wire; a toast would sit under
                // the card.
                inner.claim = Claim::Taking;
                Self::notify(&inner);
            }
            // Taken by an interaction here: at this page's own shape.
            inner.link.lease_mut().fit = true;
            (inner.link.clone(), inner.tab_id)
        };
        let app = Rc::clone(self);
        self.spawn(async move {
            let after = match link.claim(tab_id).await {
                Ok(true) => {
                    // Back to the page's own size, and the grid with it.
                    app.match_desktop_cell();
                    app.refresh_status();
                    app.request_frame();
                    let mut inner = app.inner.borrow_mut();
                    inner.claim = Claim::Idle;
                    Self::notify(&inner);
                    inner.after_take_over.take()
                }
                Ok(false) => {
                    let mut inner = app.inner.borrow_mut();
                    inner.after_take_over = None;
                    if quiet {
                        log::info!("the server did not hand the terminal over on showing the tab");
                    } else {
                        inner.claim = Claim::Refused;
                        Self::notify(&inner);
                    }
                    None
                }
                Err(err) => {
                    let mut inner = app.inner.borrow_mut();
                    inner.after_take_over = None;
                    if quiet {
                        log::warn!("claiming the tab on showing it: {err:#}");
                    } else {
                        inner.claim = Claim::Refused;
                        Self::notify(&inner);
                        Self::set_status(&inner, &failed("web-act-take-over", &err));
                    }
                    None
                }
            };
            match after {
                Some(AfterTakeOver::Focus(pane_id)) => {
                    let (link, palette) = {
                        let inner = app.inner.borrow();
                        (inner.link.clone(), inner.configured())
                    };
                    Self::advise_focus(&app.platform, link, pane_id, palette);
                }
                Some(AfterTakeOver::Key(key, mods, shift)) => {
                    app.key_down(key, mods, shift);
                }
                None => {}
            }
        });
    }

    /// The GPU state, for a platform whose surface comes and goes.
    pub fn with_gpu<T>(&self, f: impl FnOnce(&mut Gpu) -> T) -> T {
        f(&mut self.inner.borrow_mut().gpu)
    }

    /// Let the link go, ahead of `into_gpu`. The link's handlers hold the
    /// App and the App holds the link; cutting that, and telling the
    /// link's pending requests they will not be answered, lets every task
    /// on the wire finish once the executor runs again. Ticks that still
    /// reach the App afterwards do nothing.
    pub fn retire(&self) {
        self.retired.set(true);
        let animation = self.animation_wake.borrow_mut().take();
        drop(animation);
        let retry = self.retry.borrow_mut().take();
        drop(retry);
        let inner = &mut *self.inner.borrow_mut();
        inner.graphics.clear(&mut inner.gpu);
        inner.link.set_push_handler(Box::new(|_| {}));
        inner.link.set_close_handler(Box::new(|_| {}));
        inner.link.shutdown();
    }

    /// Take the GPU state back, for a platform that keeps its device
    /// across connections. Fails (handing the App back) while something
    /// else still holds it -- a task on the wire, say; `retire` first and
    /// run the executor to give those a chance to finish.
    pub fn into_gpu(app: Rc<Self>) -> std::result::Result<Gpu, Rc<Self>> {
        app.retire();
        Rc::try_unwrap(app).map(|app| app.inner.into_inner().gpu)
    }

    /// See `GlyphCache::warm`. Called once, after the first frame.
    pub fn warm_glyph_canvas(&self) {
        self.inner.borrow_mut().glyphs.warm();
    }

    /// Rasterise for another size or density: a fresh cache on a fresh
    /// texture. A new cell size makes "what fits" a different question,
    /// so the atlas backoff starts over rather than carrying a grudge
    /// from the old one.
    fn rerasterise(inner: &mut Inner<P, L>, size_pt: f64, dpi: u32) {
        let side = inner.glyphs.atlas.size() as u32;
        match GpuTexture::new(&inner.gpu.device, Arc::clone(&inner.gpu.queue), side, side).and_then(
            |t| {
                GlyphCache::new(
                    Rc::clone(&inner.fonts),
                    size_pt,
                    dpi,
                    Rc::new(t),
                    Rc::clone(&inner.glyphs.families),
                    Rc::clone(&inner.glyphs.platform),
                )
            },
        ) {
            Ok(glyphs) => inner.glyphs = glyphs,
            Err(err) => log::error!("glyph cache at {size_pt} pt, {dpi} dpi: {err:#}"),
        }
        inner.capacity.reset();
        Self::rebuild_pane_fonts(inner);
    }

    /// The scaled panes' caches follow the page's size, density and atlas
    /// side; a cache that cannot be built leaves the pane at the page's
    /// size.
    fn rebuild_pane_fonts(inner: &mut Inner<P, L>) {
        let side = inner.glyphs.atlas.size() as u32;
        let (size_pt, dpi) = (inner.glyphs.size_pt, inner.glyphs.dpi);
        let mut failed = Vec::new();
        for (pane_id, font) in inner.pane_fonts.iter_mut() {
            let built = GpuTexture::new(&inner.gpu.device, Arc::clone(&inner.gpu.queue), side, side).and_then(|t| {
                GlyphCache::new(
                    Rc::clone(&inner.fonts),
                    size_pt * font.scale,
                    dpi,
                    Rc::new(t),
                    Rc::clone(&inner.glyphs.families),
                    Rc::clone(&inner.glyphs.platform),
                )
            });
            match built {
                Ok(glyphs) => font.glyphs = glyphs,
                Err(err) => {
                    log::error!("glyph cache for pane {pane_id} at scale {}: {err:#}", font.scale);
                    failed.push(*pane_id);
                }
            }
        }
        for pane_id in failed {
            inner.pane_fonts.remove(&pane_id);
        }
    }

    /// Whether a pane's own font is what the page draws it with. It is
    /// while this page holds the terminal (its claim shapes the pane to
    /// that cell), and otherwise only when the pane, as the server has
    /// it, fits its frame at that cell -- a follower never clips a pane.
    fn pane_font_shown(inner: &Inner<P, L>, place: &crate::layout::PanePlacement) -> bool {
        let Some(font) = inner.pane_fonts.get(&place.pane_id) else {
            return false;
        };
        if inner.link.lease().owns_viewport() {
            return true;
        }
        let root = inner.glyphs.metrics.cell_size;
        let m = font.glyphs.metrics.cell_size;
        let nav = Self::nav_dev(inner) as f64;
        place.content.0 as f64 * m.width as f64 <= place.frame.cols as f64 * root.width as f64
            && place.content.1 as f64 * m.height as f64 + nav <= place.frame.rows as f64 * root.height as f64
    }

    /// A pane's cell in device px: its own font's when that is shown.
    fn pane_cell(inner: &Inner<P, L>, place: &crate::layout::PanePlacement) -> (f64, f64) {
        let m = if Self::pane_font_shown(inner, place) {
            inner.pane_fonts[&place.pane_id].glyphs.metrics
        } else {
            inner.glyphs.metrics
        };
        (m.cell_size.width as f64, m.cell_size.height as f64)
    }

    /// The grid a placement shows at its own cell: the pane's own size,
    /// or less where its frame cannot hold it.
    fn shown_in(inner: &Inner<P, L>, place: &crate::layout::PanePlacement) -> (usize, usize) {
        let (cw, ch) = Self::pane_cell(inner, place);
        let root = inner.glyphs.metrics.cell_size;
        let frame_w = place.frame.cols as f64 * root.width as f64;
        let frame_h = place.frame.rows as f64 * root.height as f64;
        let offset = Self::content_offset(inner, place) as f64;
        (
            place.content.0.min((frame_w / cw).floor() as usize),
            place.content.1.min(((frame_h - offset) / ch).floor().max(0.0) as usize),
        )
    }

    /// How far below its frame's top a pane's content starts: a pad under
    /// the bar, which on a desktop-shaped page starts above the frame
    /// (`navbar::rows_offset_css`), in whole device px so the text stays
    /// sharp. A pane the server has at its whole frame (a layout nobody
    /// with a bar has claimed yet) loses its last rows under the frame's
    /// bottom rather than its first under the bar -- the prompt is at the
    /// top, and the next claim from here makes room.
    fn content_offset(inner: &Inner<P, L>, place: &crate::layout::PanePlacement) -> f32 {
        let nav = Self::nav_dev(inner);
        if inner.mobile || nav <= 0.0 {
            return nav;
        }
        let dpr = inner.dpr.max(0.1);
        let ch = inner.glyphs.metrics.cell_size.height as f64 / dpr;
        let pad_top = Self::pad(inner).1 as f64 / dpr;
        (crate::navbar::rows_offset_css(place.frame.top, ch, pad_top, nav as f64 / dpr) * dpr).round() as f32
    }

    /// The panes as this page would claim them: the desktop's rule for
    /// each, at its own cell where it has its own font.
    fn native_panes(inner: &Inner<P, L>, layout: &crate::layout::TabLayout) -> Vec<codec::ClientPaneViewport> {
        // The bar's rows for *this* layout: on a switch the one on show is
        // still the outgoing tab's, and a phone shows a lone pane bare.
        let nav_rows = Self::nav_rows_for(inner, layout.panes.len());
        let mut panes = layout.viewport(nav_rows);
        let root = inner.glyphs.metrics.cell_size;
        let nav = Self::nav_dev(inner) as f64;
        for pane in panes.iter_mut() {
            let Some(font) = inner.pane_fonts.get(&pane.pane_id) else {
                continue;
            };
            let Some(place) = layout.panes.iter().find(|p| p.pane_id == pane.pane_id) else {
                continue;
            };
            let (cw, ch) = (font.glyphs.metrics.cell_size.width as f64, font.glyphs.metrics.cell_size.height as f64);
            let cols = ((place.frame.cols as f64 * root.width as f64) / cw).floor().max(1.0) as usize;
            let rows = ((place.frame.rows as f64 * root.height as f64 - nav) / ch).floor().max(1.0) as usize;
            pane.size = TerminalSize {
                cols,
                rows,
                pixel_width: cols * cw as usize,
                pixel_height: rows * ch as usize,
                dpi: font.glyphs.dpi,
            };
        }
        panes
    }

    /// Cmd+= / Cmd+- scale the focused pane's font by a tenth, as the
    /// desktop does; Cmd+0 takes it back to the page's size. The pane's
    /// grid follows when this page holds the terminal.
    pub fn step_font(self: &Rc<Self>, by: f64) {
        let pane_id = self.inner.borrow().focused_pane;
        {
            let mut inner = self.inner.borrow_mut();
            let current = inner.pane_fonts.get(&pane_id).map(|f| f.scale).unwrap_or(1.0);
            let scale = if by == 0.0 { 1.0 } else { (current * 1.1f64.powf(by)).clamp(0.25, 4.0) };
            if (scale - 1.0).abs() < 1e-6 {
                inner.pane_fonts.remove(&pane_id);
            } else {
                let side = inner.glyphs.atlas.size() as u32;
                let (size_pt, dpi) = (inner.glyphs.size_pt, inner.glyphs.dpi);
                let built = GpuTexture::new(&inner.gpu.device, Arc::clone(&inner.gpu.queue), side, side).and_then(|t| {
                    GlyphCache::new(Rc::clone(&inner.fonts), size_pt * scale, dpi, Rc::new(t), Rc::clone(&inner.glyphs.families), Rc::clone(&inner.glyphs.platform))
                });
                match built {
                    Ok(glyphs) => {
                        inner.pane_fonts.insert(
                            pane_id,
                            PaneFont { scale, glyphs, quads: HeapQuadAllocator::default(), vertices: Vec::new(), vertex_layers: std::array::from_fn(|_| 0..0) },
                        );
                    }
                    Err(err) => {
                        Self::set_status(&inner, &failed("web-act-size-font", &err));
                        return;
                    }
                }
            }
            if let Some(layout) = &inner.tab_layout {
                let panes = Self::native_panes(&inner, layout);
                let mut lease = inner.link.lease_mut();
                lease.native = panes;
                lease.native_root = Some(layout.size);
            }
        }
        self.request_frame();
        // The pane's grid changes with its cell: claimed when this page
        // drives, and a page that does not takes the terminal first, as
        // any interaction here does.
        let (link, tab_id, owns) = {
            let inner = self.inner.borrow();
            let owns = inner.link.lease().owns_viewport();
            (inner.link.clone(), inner.tab_id, owns)
        };
        if owns {
            let app = Rc::clone(self);
            self.spawn(async move {
                if let Err(err) = link.claim(tab_id).await {
                    log::warn!("resizing the pane for its font: {err:#}");
                }
                app.refresh_layout();
            });
        } else {
            self.take_over();
        }
    }

    /// The font size this page wants (`viewport::choose_size_pt`): the
    /// desktop's cell, and for a follower no larger than lets the whole
    /// tab fit the canvas. `None` when the size is right or pinned by
    /// `?font=`.
    fn desired_size_pt(inner: &Inner<P, L>, dev_w: u32, dev_h: u32) -> Option<f64> {
        // A canvas with no room -- one machine's terminal behind another's
        // -- says nothing about the size anyone wants.
        if inner.font_pinned || dev_w <= 1 || dev_h <= 1 {
            return None;
        }
        // Holding the tab, the page shows its own size; the desktop is
        // the one following now.
        if inner.link.lease().owns_viewport() {
            let base = inner.base_size_pt;
            return ((inner.glyphs.size_pt - base).abs() >= 0.125).then_some(base);
        }
        let (desktop, fit) = {
            let lease = inner.link.lease();
            (lease.canonical_size, lease.fit)
        };
        let desktop = desktop?;
        let page_dpi = inner.platform.units_per_inch() * inner.dpr;
        let dpi = page_dpi as u32;
        let fonts = Rc::clone(&inner.fonts);
        let cell = |pt: f64| {
            fonts.metrics(pt, dpi).ok().map(|m| {
                let r = crate::glyphs::RenderMetrics::with_font_metrics(&m);
                (r.cell_size.width as f64, r.cell_size.height as f64)
            })
        };
        let desktop_cell_h = (desktop.rows > 0 && desktop.dpi > 0 && desktop.pixel_height > 0)
            .then(|| desktop.pixel_height as f64 / desktop.rows as f64 / desktop.dpi as f64 * page_dpi);
        // Holding the terminal at this page's own shape, the tab takes the
        // grid this window has at the desktop's cell size; only a follower
        // shrinks the cell to show all of the desktop's tab.
        let grid = (!fit && desktop.cols > 0 && desktop.rows > 0).then_some((desktop.cols, desktop.rows));
        crate::viewport::choose_size_pt(cell, inner.glyphs.size_pt, desktop_cell_h, grid, (dev_w as f64, dev_h as f64))
    }

    /// The desktop's size changed: settle the font, and only then the
    /// grid. Nothing happens when the size is already right -- a forced
    /// pass through `resize` would re-claim the tab on every push.
    pub fn match_desktop_cell(self: &Rc<Self>) {
        let wanted = {
            let inner = self.inner.borrow();
            let (w, h) = inner.gpu.size();
            Self::desired_size_pt(&inner, w, h)
        };
        if wanted.is_some() {
            self.inner.borrow_mut().cols = 0;
            self.resize();
            self.request_frame();
        }
    }

    /// Fit the grid to the canvas's CSS box at the device pixel ratio, and
    /// tell the server if this browser owns the viewport.
    pub fn resize(self: &Rc<Self>) {
        let mut inner = self.inner.borrow_mut();
        let viewport = inner.platform.viewport();
        let dpr = viewport.dpr;
        inner.canvas_rect = [viewport.left, viewport.top, viewport.width, viewport.height];
        if (dpr - inner.dpr).abs() > f64::EPSILON {
            // Another monitor: glyphs are rasterised for the new density.
            inner.dpr = dpr;
            let size_pt = inner.glyphs.size_pt;
            let dpi = (inner.platform.units_per_inch() * dpr) as u32;
            Self::rerasterise(&mut inner, size_pt, dpi);
        }
        let dev_w = (viewport.width * dpr).floor().max(1.0) as u32;
        let dev_h = (viewport.height * dpr).floor().max(1.0) as u32;
        // Setting the backing store's size clears it: the frame is
        // painted again right away below, not at the next animation
        // frame, or the page's ground shows through for a frame.
        let cleared = inner.platform.set_backing_size(dev_w, dev_h);
        inner.gpu.resize(dev_w, dev_h);
        // The font follows the desktop's cell and the window's box.
        if let Some(size_pt) = Self::desired_size_pt(&inner, dev_w, dev_h) {
            log::info!("font {} pt -> {size_pt} pt for the desktop's cell in this window", inner.glyphs.size_pt);
            let dpi = (inner.platform.units_per_inch() * inner.dpr) as u32;
            Self::rerasterise(&mut inner, size_pt, dpi);
            inner.cols = 0;
        }
        let (cw, ch) = (
            inner.glyphs.metrics.cell_size.width as u32,
            inner.glyphs.metrics.cell_size.height as u32,
        );
        inner.mobile = inner.platform.is_mobile();
        let pad = Self::pad(&inner);
        let Some((cols, rows)) = grid_for(
            dev_w.saturating_sub(2 * pad.0 as u32),
            dev_h.saturating_sub(Self::pad_y_total(&inner)),
            cw,
            ch,
        ) else {
            // A box too small for a cell is a hidden or collapsed page,
            // not a size anyone asked for: keep the last real grid rather
            // than resize every client's shell to it.
            return;
        };
        // In the desktop's shape what fits under the bars decides the rows.
        let rows = if inner.mobile {
            rows
        } else {
            crate::navbar::grid_rows(dev_h as f64, Self::nav_dev(&inner) as f64, ch as f64, pad.1 as f64, inner.dpr)
        };
        let changed = cols != inner.cols || rows != inner.rows;
        inner.cols = cols;
        inner.rows = rows;
        inner.host.config.saw_rows(rows);
        // The bars are placed from the cell size, which may just have moved.
        Self::notify(&inner);
        if changed {
            // The holder shapes the tab: a page that has the tab claims
            // its new grid, as a claim, because that is what changing the
            // tab's size is. A follower keeps the desktop's size and
            // letterboxes it.
            let size = TerminalSize {
                rows,
                cols,
                pixel_width: cols * cw as usize,
                pixel_height: rows * ch as usize,
                dpi: (inner.platform.units_per_inch() * dpr) as u32,
            };
            let fitting = {
                let mut lease = inner.link.lease_mut();
                lease.reported = Some(size);
                // Before the first listing there is no split layout to
                // reserve the bars in. Claiming a bare grid here would
                // resize twice and can discard image rows in between.
                lease.owns_viewport() && !inner.panel_drag && inner.tab_layout.is_some()
            };
            if fitting {
                // Pane by pane, at frames scaled to the new grid: one
                // claim that reserves every bar's rows, rather than a bare
                // grid the listing then corrects (two reflows on screen).
                if let Some(mut scaled) = Self::scaled_layout(&inner, cols, rows) {
                    let panes = Self::native_panes(&inner, &scaled);
                    let sizes: Vec<(PaneId, TerminalSize)> = panes.iter().map(|p| (p.pane_id, p.size)).collect();
                    let mut lease = inner.link.lease_mut();
                    lease.native = panes;
                    lease.native_root = Some(size);
                    drop(lease);
                    Self::preview_panes(&mut inner, &sizes);
                    // Drawn at the claimed grid until the server answers.
                    for place in &mut scaled.panes {
                        if let Some((_, claimed)) = sizes.iter().find(|(pane, _)| *pane == place.pane_id) {
                            place.content = (claimed.cols, claimed.rows);
                            place.size = *claimed;
                        }
                    }
                    inner.claimed_layout = Some(scaled);
                    inner.claimed_at = inner.platform.monotonic_ms();
                }
                let link = inner.link.clone();
                let tab_id = inner.tab_id;
                let app = Rc::clone(self);
                self.spawn(async move {
                    if let Err(err) = link.claim(tab_id).await {
                        log::warn!("fitting the tab to this window: {err:#}");
                        // Nothing will answer it: back to the server's layout.
                        app.inner.borrow_mut().claimed_layout = None;
                        app.request_frame();
                    }
                });
            }
        }
        drop(inner);
        self.refresh_status();
        if cleared {
            self.frame();
        } else {
            self.request_frame();
        }
    }

    /// Paint. Lines the session does not have yet come back blank and are
    /// fetched; their arrival marks the page dirty again.
    pub fn frame(self: &Rc<Self>) {
        self.frame_if_needed(true);
    }

    fn frame_if_needed(self: &Rc<Self>, immediate: bool) {
        let requested = self.repaint_requested.replace(false) || immediate;
        let mut inner = self.inner.borrow_mut();
        // A connection that has carried frames for a while has earned the
        // short delay back. Done here rather than on connect, because
        // "the socket opened" is not evidence a server is healthy.
        if inner
            .connected_since
            .is_some_and(|at| inner.platform.monotonic_ms() - at >= RECONNECT_STABLE_MS)
        {
            inner.connected_since = None;
            inner.reconnect_delay = RECONNECT_MIN_MS;
        }
        // Output in a parked tab keeps its cell current but changes
        // nothing on screen: no paint for it alone.
        let dirty = inner.host.events.take_dirty();
        if !requested && !dirty.is_empty() && dirty.iter().all(|pane| !inner.panes.contains_key(pane)) {
            return;
        }
        let title = inner.focused().session.title();
        if title != inner.focused().title {
            inner.focused_mut().title = title;
            Self::render_strip(&inner);
            inner.platform.set_title(inner.title());
            drop(inner);
            self.refresh_status();
            inner = self.inner.borrow_mut();
        }
        // A plugin's panel is told where the pane in focus is as it moves:
        // a `cd`, another pane.
        if inner.plugin_panel.is_some() {
            drop(inner);
            self.plugin_panel_follow_place();
            inner = self.inner.borrow_mut();
        }
        // One budget for the whole frame, not one per attempt. The loop
        // below calls `paint` more than once, so a fresh budget each time
        // would let a single browser callback draw several times the cap --
        // which is the thing the cap is for. The deadline inside it is set
        // once, here, for the same reason.
        let mut budget = FallbackBudget::new(inner.platform.monotonic_ms());
        let (mut owed, mut declined, mut painted) = (0u32, 0u32, false);
        // Whether the atlas is what has been failing, and whether the budget
        // has already been given back once this frame.
        let (mut atlas_failed, mut refunded) = (false, false);
        const ATTEMPTS: usize = 4;
        for attempt in 0..ATTEMPTS {
            // The last attempt always declines allocations, so it cannot
            // fail on the atlas and the frame reaches the screen. Leaving
            // that to `grow_atlas` saying `AtCapacity` was not enough: two
            // growths that both succeed and still do not fit left the last
            // attempt allocating normally, and its failure fell out of the
            // loop with nothing painted, no repaint requested and no timer
            // armed -- the last frame stayed up until unrelated input
            // arrived, which looks exactly like a hang.
            if attempt + 1 == ATTEMPTS && atlas_failed && !inner.capacity.frozen() {
                let now = inner.platform.monotonic_ms();
                inner.capacity.at_capacity(now);
            }
            match Self::paint(&mut inner, &mut budget) {
                Ok((deferred, refused)) => {
                    owed = deferred;
                    declined = refused;
                    painted = true;
                    break;
                }
                Err(err) if attempt + 1 < ATTEMPTS => {
                    let Some(full) = err.root_cause().downcast_ref::<OutOfTextureSpace>() else {
                        log::error!("frame failed: {err:#}");
                        break;
                    };
                    atlas_failed = true;
                    match Self::grow_atlas(&mut inner, full.size) {
                        // The cache was rebuilt, so every glyph this frame
                        // has already drawn is gone with it. The retry gets
                        // the count back: charging it twice for the same
                        // graphemes left the rows at the bottom of the
                        // screen -- where a terminal's action is -- as
                        // boxes, and made `owed` a sum over attempts rather
                        // than what this frame still owes.
                        Ok(Grown::Larger) => {
                            // Once only, and the deadline is never given
                            // back -- see `refund`. The rebuild honestly
                            // threw away what the earlier attempt drew, but
                            // three refunds would let one frame do four
                            // times the work, which is the opposite of what
                            // the budget is for.
                            if !refunded {
                                refunded = true;
                                budget.refund();
                            }
                        }
                        Ok(Grown::AtCapacity) => {
                            let now = inner.platform.monotonic_ms();
                            inner.capacity.at_capacity(now);
                        }
                        Err(err) => {
                            log::error!("atlas could not grow: {err:#}");
                            break;
                        }
                    }
                }
                Err(err) => {
                    log::error!("frame failed: {err:#}");
                    break;
                }
            }
        }

        // Lines still in flight, a stalled fetch, or glyphs the budget put
        // off: come back for them.
        // Exactly the rows shown: asking about rows past a shorter page's
        // bottom, which may not exist, would repaint forever.
        // Called first, and never behind a `||`. This is not a predicate:
        // it re-issues line fetches that timed out and resets the poll
        // interval. Short-circuiting it on a frame that deferred a glyph
        // meant a dropped `GetLines` was never retried, and those rows
        // stayed blank for the rest of the stream. Every pane, every frame.
        let mut stalled = false;
        for place in Self::placements(&inner) {
            let Some(cell) = inner.panes.get(&place.pane_id) else {
                continue;
            };
            let dims = Self::shown_dims(&cell.session);
            // The extra row a fractional scroll draws is one whose fetch can
            // stall like any other; asking about the range that was painted
            // is what keeps a dropped `GetLines` for it being retried.
            let visible =
                visible_rows_px(&dims, Self::shown_in(&inner, &place).1, cell.scroll_from_bottom, cell.scroll_px);
            stalled |= cell.session.render_looks_stalled_in(visible);
        }
        let mut again = owed > 0 || stalled;

        let mut retry_in = None;
        if !painted {
            // Nothing reached the screen. Come back on the backoff's own
            // timer rather than waiting to be asked.
            retry_in = Some(MIN_RETRY_MS);
        } else if inner.capacity.frozen() {
            let now = inner.platform.monotonic_ms();
            match inner.capacity.submitted_frozen(now, owed, declined) {
                Next::Recovered => {}
                Next::Clear => match Self::clear_atlas(&mut inner) {
                    Ok(()) => again = true,
                    Err(err) => {
                        // `submitted_frozen` unfroze on the promise of a
                        // clear that did not happen. Put it back, or the
                        // frame stands still against a full atlas with
                        // nothing scheduled to try again.
                        log::error!("the atlas could not be cleared: {err:#}");
                        let now = inner.platform.monotonic_ms();
                        inner.capacity.at_capacity(now);
                        retry_in = Some(MIN_RETRY_MS);
                    }
                },
                Next::RetryAt(at) => retry_in = Some((at - inner.platform.monotonic_ms()).max(0.0)),
            }
        } else if owed == 0 && declined == 0 {
            // Owing nothing is the whole condition. A frame whose budget
            // ran out returns successfully too, and taking that for "the
            // demand is satisfied" would end the emergency after refilling
            // only part of the screen.
            inner.capacity.drew_everything();
        }

        let animation_at = painted.then(|| inner.graphics.next_animation_at()).flatten()
            .map(|at| inner.animation_clock.local_time(at));
        drop(inner);
        self.schedule_animation(animation_at);
        if again {
            self.request_frame();
        }
        if let Some(delay) = retry_in {
            self.schedule_retry(delay);
        } else if painted && !again {
            let retry = self.retry.borrow_mut().take();
            drop(retry);
        }
    }

    /// Grow the atlas past what would not fit.
    ///
    /// `wanted` is the size the allocator says would hold the sprite, which
    /// is not always twice the current side: one sprite larger than that
    /// does not fit after a single doubling, and the frame only retries so
    /// many times.
    fn grow_atlas(inner: &mut Inner<P, L>, wanted: Option<usize>) -> Result<Grown> {
        let current = inner.glyphs.atlas.size();
        let side = wanted
            .unwrap_or(current * 2)
            .max(current * 2)
            .next_power_of_two()
            .min(MAX_ATLAS_SIDE)
            .min(inner.gpu.max_texture_dimension() as usize);
        if side <= current {
            return Ok(Grown::AtCapacity);
        }
        let texture = Rc::new(GpuTexture::new(
            &inner.gpu.device,
            Arc::clone(&inner.gpu.queue),
            side as u32,
            side as u32,
        )?);
        Self::rebuild_glyphs(inner, texture)?;
        Self::rebuild_pane_fonts(inner);
        Ok(Grown::Larger)
    }

    /// Empty the atlas by rebuilding the cache over the same texture:
    /// `Atlas::new` zeroes it and resets the allocator.
    ///
    /// Everything drawn so far is lost, which is the point. There is no
    /// per-sprite eviction, so this is the only way to get space back, and
    /// the flicker it causes is a great deal better than the alternative --
    /// which, before this existed, was a terminal that never drew again.
    fn clear_atlas(inner: &mut Inner<P, L>) -> Result<()> {
        log::warn!(
            "the atlas is at the GPU's largest texture ({}) and still full; clearing it",
            inner.glyphs.atlas.size()
        );
        let texture = inner.glyphs.texture_rc();
        Self::rebuild_glyphs(inner, texture)?;
        Self::rebuild_pane_fonts(inner);
        Ok(())
    }

    fn rebuild_glyphs(inner: &mut Inner<P, L>, texture: Rc<GpuTexture>) -> Result<()> {
        inner.glyphs = GlyphCache::new(
            Rc::clone(&inner.fonts),
            inner.glyphs.size_pt,
            inner.glyphs.dpi,
            texture,
            Rc::clone(&inner.glyphs.families),
            Rc::clone(&inner.glyphs.platform),
        )?;
        Ok(())
    }

    /// The panes to draw and where. Before the first listing there is
    /// one, at the canvas's origin and the page's own grid.
    fn placements(inner: &Inner<P, L>) -> Vec<crate::layout::PanePlacement> {
        if let Some(layout) = Self::drawn_layout(inner) {
            return layout.panes.clone();
        }
        let cell = inner.focused();
        let size = Self::shown_dims(&cell.session);
        vec![crate::layout::PanePlacement {
            pane_id: inner.focused_pane,
            tab_id: inner.tab_id,
            window_id: inner.window_id,
            frame: crate::layout::Rect { left: 0, top: 0, cols: inner.cols, rows: inner.rows },
            content: (inner.cols, inner.rows),
            is_active: true,
            is_zoomed: false,
            title: cell.title.clone(),
            alt_screen: false,
            physical_top: size.physical_top,
            size: TerminalSize::default(),
            workspace: inner.workspace.clone(),
            stack: vec![],
        }]
    }

    /// The tab as it is drawn and pointed at: the grid this page claimed,
    /// while the server has yet to answer the claim, else the server's.
    fn drawn_layout(inner: &Inner<P, L>) -> Option<&crate::layout::TabLayout> {
        inner.claimed_layout.as_ref().or(inner.tab_layout.as_ref())
    }

    /// A pane's dimensions as it is drawn and pointed at: a preview of
    /// another height placed where the server will put its rows
    /// (`viewport::previewed`).
    fn shown_dims(session: &PaneSession<AppHost<P, L>>) -> thinkterm_proto::RenderableDimensions {
        crate::viewport::previewed(
            session.dimensions(),
            &session.server_dimensions(),
            session.cursor_position().y,
            session.is_alt_screen(),
        )
    }

    /// The grid a placement shows: its frame, or less when the pane's own
    /// grid is smaller (the desktop reserves rows of a frame for chrome).
    fn shown(place: &crate::layout::PanePlacement) -> (usize, usize) {
        (place.frame.cols.min(place.content.0), place.frame.rows.min(place.content.1))
    }

    /// Returns what this frame owes: glyphs the budget put off, and sprites
    /// a full atlas declined.
    fn paint(inner: &mut Inner<P, L>, budget: &mut FallbackBudget) -> Result<(u32, u32)> {
        let now = inner.animation_clock.server_now(inner.platform.monotonic_ms());
        inner.graphics.begin_frame(now);
        inner.ground.recycle();
        let frozen = inner.capacity.frozen();
        inner.glyphs.begin_frame(frozen);
        for font in inner.pane_fonts.values_mut() {
            font.glyphs.begin_frame(frozen);
            font.quads.recycle();
        }
        let (w, h) = inner.gpu.size();
        let surface = (w as f32, h as f32);
        let root_w = inner.glyphs.metrics.cell_size.width as f32;
        let root_h = inner.glyphs.metrics.cell_size.height as f32;
        let owns = inner.link.lease().owns_viewport();
        let placements = Self::placements(inner);
        let focused_palette = inner.focused().palette.clone();
        let pad = Self::pad(inner);
        let tab = inner.tab_layout.as_ref().map(|l| (l.cols, l.rows)).unwrap_or((usize::MAX, usize::MAX));
        inner.quads.recycle();
        let smooth = inner.settings.scroll_mode.is_smooth();
        for place in &placements {
            let (cols_shown, rows_shown) = Self::shown_in(inner, place);
            let offset = Self::content_offset(inner, place);
            // The pane's own cell height, before its glyph cache is borrowed
            // to draw with: what a fractional scroll offset is measured in.
            let pane_cell_h = Self::pane_cell(inner, place).1 as f32;
            let Some(cell) = inner.panes.get_mut(&place.pane_id) else {
                continue;
            };
            let session = Arc::clone(&cell.session);
            let dims = Self::shown_dims(&session);
            let max = max_scroll(&dims, rows_shown);
            if cell.scroll_from_bottom > max {
                cell.scroll_from_bottom = max;
                cell.scroll_px = 0.0;
            }
            // The offset only means anything inside the row it is measured
            // against, and only above the tail: a font change, a switch to
            // stepped mode or a scroll back to the newest row all leave it
            // behind, and a whole row is what each of those wants.
            if !smooth
                || cell.scroll_from_bottom == 0
                || !(cell.scroll_px > 0.0)
                || cell.scroll_px >= pane_cell_h
            {
                cell.scroll_px = 0.0;
            }
            let px = cell.scroll_px;
            let visible = visible_rows_px(&dims, rows_shown, cell.scroll_from_bottom, px);
            let (first, lines) = session.get_lines(visible);
            // No cursor before the server has placed one: a fresh cell's
            // sits at the origin, which is nowhere real.
            let mut cursor = session.cursor_position();
            if !session.has_received() {
                cursor.y = StableRowIndex::MIN;
            }
            let palette = cell.palette.clone();
            let selection = cell.selection;
            let is_focused = place.pane_id == inner.focused_pane;
            let hsv = if is_focused {
                None
            } else {
                Some(wezterm_color_types::HsbTransform::for_inactive_pane(palette.background))
            };
            // A pane with its own font draws from its own cache, into its
            // own batch.
            let own_font = Self::pane_font_shown(inner, place);
            let (glyphs, quads): (&mut GlyphCache, &mut HeapQuadAllocator) = match inner.pane_fonts.get_mut(&place.pane_id) {
                Some(font) if own_font => (&mut font.glyphs, &mut font.quads),
                _ => (&mut inner.glyphs, &mut inner.quads),
            };
            let scratch = &mut inner.scratch;
            let cell_w = glyphs.metrics.cell_size.width as f32;
            let cell_h = glyphs.metrics.cell_size.height as f32;
            let frame_origin = (
                pad.0 + place.frame.left as f32 * root_w,
                pad.1 + place.frame.top as f32 * root_h,
            );
            // The content sits below the pane's bar, as on the desktop, and
            // a fractional scroll lifts the rows by the part of the top one
            // that is cut off; the extra row fetched above fills the strip
            // that leaves at the bottom.
            let content_top = frame_origin.1 + offset;
            let origin = (frame_origin.0, content_top - px);
            let clip = (cols_shown as f32 * cell_w, rows_shown as f32 * cell_h);
            // The content box, in the quads' own centre-relative space: what
            // the two rows hanging over its edges are cropped to.
            let content_clip = thinkterm_render::quad::QuadClipRect::from_top_left_pixels(
                frame_origin.0,
                content_top,
                frame_origin.0 + clip.0,
                content_top + clip.1,
                &thinkterm_render::geom::Dimensions {
                    pixel_width: w as usize,
                    pixel_height: h as usize,
                    dpi: dims.dpi as usize,
                },
            );
            // The pane's own ground: its frame, plus the padding out to the
            // canvas edge where it touches the tab's edge and half a cell
            // into the divider where it does not, so the panes tile the
            // canvas and an inactive pane's dimming shows no rim.
            let (tab_cols, tab_rows) = (tab.0, tab.1);
            let x0 = if place.frame.left == 0 { 0.0 } else { frame_origin.0 - root_w / 2.0 };
            let y0 = if place.frame.top == 0 { 0.0 } else { frame_origin.1 - root_h / 2.0 };
            let x1 = if place.frame.left + place.frame.cols >= tab_cols {
                surface.0
            } else {
                frame_origin.0 + place.frame.cols as f32 * root_w + root_w / 2.0
            };
            let y1 = if place.frame.top + place.frame.rows >= tab_rows {
                surface.1
            } else {
                frame_origin.1 + place.frame.rows as f32 * root_h + root_h / 2.0
            };
            crate::emit::fill_rect(
                glyphs,
                &mut inner.ground,
                0,
                surface,
                (x0, y0),
                (x1 - x0, y1 - y0),
                palette.background.to_linear(),
                hsv,
            )?;
            if is_focused {
                let cursor_line = cursor
                    .y
                    .checked_sub(first)
                    .and_then(|row| usize::try_from(row).ok())
                    .and_then(|row| lines.get(row));
                let width_scale = if cursor_line.is_some_and(|line| !line.is_single_width()) { 2.0 } else { 1.0 };
                let height_scale = if cursor_line.is_some_and(|line| line.is_double_height_top()) { 2.0 } else { 1.0 };
                let cw = cell_w as f64 * width_scale;
                let anchor = crate::ime::anchor(
                    inner.canvas_rect,
                    (w, h),
                    (
                        origin.0 as f64 + cursor.x as f64 * cw,
                        origin.1 as f64 + cursor.y.saturating_sub(first) as f64 * cell_h as f64,
                    ),
                    (cw, cell_h as f64 * height_scale),
                );
                if let Some(anchor) = anchor.filter(|a| Some(*a) != inner.ime_anchor) {
                    inner.ime_anchor = Some(anchor);
                    inner.platform.set_ime_anchor(anchor);
                }
            }
            let last = lines.len().saturating_sub(1);
            if let Some(revision) = inner.frame_control.get(&place.pane_id).and_then(|(_, revision)| *revision) {
                let viewport = crate::graphics::RelativeViewport {
                    seqno: session.current_seqno() as u64,
                    first: first as i64, alternate: session.is_alt_screen(), origin,
                    cell: (cell_w, cell_h),
                    source_cell: ((dims.pixel_width / dims.cols.max(1)) as u32,
                        (dims.pixel_height / dims.viewport_rows.max(1)) as u32),
                    clip: [frame_origin.0, content_top, frame_origin.0 + clip.0, content_top + clip.1],
                };
                let versions = inner.host.image_versions.borrow();
                for (id, hash, generation, views) in versions.relative_images(place.pane_id) {
                    let mut image = None;
                    for view in views {
                        let Some(slice) = crate::graphics::relative_slice(view, &viewport) else { continue };
                        if image.is_none() { image = session.scene_image(id, hash, generation, revision); }
                        let Some(image) = &image else { break };
                        let hsv = hsv.unwrap_or_default();
                        inner.graphics.collect_relative(&mut inner.gpu, place.pane_id, image, id, view,
                            slice, [hsv.hue, hsv.saturation, hsv.brightness])?;
                    }
                }
            }
            for (i, line) in lines.iter().enumerate() {
                let row = first + i as StableRowIndex;
                let sel_range = match &selection {
                    Some(sel) => Self::selection_on_row(cols_shown, sel, row, line),
                    None => 0..0,
                };
                let params = crate::emit::LineParams {
                    line,
                    stable_row: row,
                    top_pixel_y: i as f32 * cell_h,
                    cursor: &cursor,
                    palette: &palette,
                    selection: sel_range,
                    focused: inner.focused && owns,
                    reverse_video: dims.reverse_video,
                    surface,
                    origin,
                    clip,
                    hsv,
                    draw_cursor: is_focused,
                    cursor_shape: match inner.settings.cursor_style {
                        crate::settings::CursorStyle::Auto => None,
                        crate::settings::CursorStyle::Block => Some(termwiz::surface::CursorShape::SteadyBlock),
                        crate::settings::CursorStyle::Bar => Some(termwiz::surface::CursorShape::SteadyBar),
                        crate::settings::CursorStyle::Underline => Some(termwiz::surface::CursorShape::SteadyUnderline),
                    },
                    cursor_hidden: inner.settings.cursor_blink && !inner.blink_shown,
                    min_contrast: inner.settings.min_contrast,
                    composing: (inner.composing && !inner.preedit.is_empty()).then_some(inner.preedit.as_str()),
                };
                if line.has_images() && inner.frame_control.get(&place.pane_id).is_some_and(|(_, revision)| revision.is_some()) {
                    inner.graphics.collect_line(
                        &mut inner.gpu,
                        place.pane_id,
                        &params,
                        (cell_w, cell_h),
                        (
                            if dims.pixel_width > 0 { cell_w * dims.cols as f32 / dims.pixel_width as f32 } else { 1.0 },
                            if dims.pixel_height > 0 { cell_h * dims.viewport_rows as f32 / dims.pixel_height as f32 } else { 1.0 },
                        ),
                        [frame_origin.0, content_top, frame_origin.0 + clip.0, content_top + clip.1],
                    )?;
                }
                let edge = i == 0 || i == last || (line.is_double_height_top() && i + 1 == last);
                if edge && (px > 0.0 || placements.len() > 1) {
                    // Fractional scrolling clips the partial rows. A split
                    // also clips glyph overhang so drawing pane grounds first
                    // cannot expose pixels in a neighbouring pane.
                    scratch.recycle();
                    crate::emit::emit_line(glyphs, scratch, budget, &params)?;
                    scratch.apply_to_clipped_at(&mut *quads, 0.0, 0.0, content_clip, 1.0)?;
                } else {
                    crate::emit::emit_line(glyphs, quads, budget, &params)?;
                }
            }
        }
        let (cell_w, cell_h) = (root_w, root_h);
        // Dividers, centred in the gap cell like the desktop's.
        if let Some(layout) = inner.claimed_layout.as_ref().or(inner.tab_layout.as_ref()) {
            let t = (inner.glyphs.metrics.underline_height.max(1)) as f32;
            let colour = focused_palette.split.to_linear();
            // Centred in the gap cell, and as long as the desktop's: half a
            // cell into the neighbouring gaps, to the canvas edge where the
            // gap reaches the tab's edge (`paint_split`).
            for divider in &layout.dividers {
                let (x0, y0, x1, y1) = match *divider {
                    crate::layout::Divider::Col { col, top, rows } => {
                        let x = pad.0 + col as f32 * cell_w + (cell_w - t) / 2.0;
                        let y0 = if top == 0 { 0.0 } else { pad.1 + top as f32 * cell_h - cell_h / 2.0 };
                        let y1 = if top + rows >= layout.rows { surface.1 } else { pad.1 + (top + rows) as f32 * cell_h + cell_h / 2.0 };
                        (x, y0, x + t, y1)
                    }
                    crate::layout::Divider::Row { row, left, cols } => {
                        let y = pad.1 + row as f32 * cell_h + (cell_h - t) / 2.0;
                        let x0 = if left == 0 { 0.0 } else { pad.0 + left as f32 * cell_w - cell_w / 2.0 };
                        let x1 = if left + cols >= layout.cols { surface.0 } else { pad.0 + (left + cols) as f32 * cell_w + cell_w / 2.0 };
                        (x0, y, x1, y + t)
                    }
                };
                crate::emit::fill_rect(&inner.glyphs, &mut inner.quads, 0, surface, (x0, y0), (x1 - x0, y1 - y0), colour, None)?;
            }
            // A follower larger than the tab sees the desktop's faint
            // grid over what the tab does not reach.
            let lease = inner.link.lease();
            let follower = matches!(lease.mode, Some(codec::FrontendAccessMode::TmuxLatest)) && !lease.owns_viewport();
            drop(lease);
            if follower && (inner.cols > layout.cols || inner.rows > layout.rows) {
                let faint = wezterm_color_types::LinearRgba::with_srgba(0x8e, 0x8e, 0x93, 26);
                let (tab_w, tab_h) = (layout.cols as f32 * cell_w, layout.rows as f32 * cell_h);
                let (page_w, page_h) = (inner.cols as f32 * cell_w, inner.rows as f32 * cell_h);
                let mut line = |x: f32, y: f32, w: f32, h: f32| {
                    crate::emit::fill_rect(&inner.glyphs, &mut inner.quads, 0, surface, (pad.0 + x, pad.1 + y), (w, h), faint, None)
                };
                for c in layout.cols..=inner.cols {
                    line(c as f32 * cell_w, 0.0, 1.0, page_h)?;
                }
                for r in layout.rows..=inner.rows {
                    line(0.0, r as f32 * cell_h, page_w, 1.0)?;
                }
                for r in 0..layout.rows {
                    line(tab_w, r as f32 * cell_h, page_w - tab_w, 1.0)?;
                }
                for c in 0..layout.cols {
                    line(c as f32 * cell_w, tab_h, 1.0, page_h - tab_h)?;
                }
            }
        }
        inner.vertices.clear();
        inner.ground.extract_vertices(&mut inner.vertices);
        let ground_end = inner.vertices.len();
        let vertex_layers = inner.quads.extract_layer_vertices(&mut inner.vertices);
        inner.graphics.finish_frame(&mut inner.gpu, surface);
        let mut declined = inner.glyphs.declined();
        for font in inner.pane_fonts.values_mut() {
            font.vertices.clear();
            font.vertex_layers = font.quads.extract_layer_vertices(&mut font.vertices);
            declined += font.glyphs.declined();
        }
        // The cleared ground, for anyone reading the canvas from the page:
        // the pixels themselves are behind WebGPU.
        let hex = focused_palette.background.to_rgb_string();
        if inner.published_bg.as_deref() != Some(hex.as_str()) {
            inner.platform.publish("bg", &hex);
            inner.published_bg = Some(hex);
        }
        let bg = focused_palette.background.to_linear().tuple();
        let millis = (inner.platform.wall_ms() % (u32::MAX as f64)) as u32;
        let mut batches: Vec<(&[Vertex], &GpuTexture)> = Vec::new();
        if inner.graphics.is_empty() {
            batches.push((&inner.vertices, inner.glyphs.texture()));
            for font in inner.pane_fonts.values() {
                batches.push((&font.vertices, font.glyphs.texture()));
            }
        } else {
            batches.push((&inner.vertices[..ground_end], inner.glyphs.texture()));
            for layer in 0..3 {
                inner.graphics.append_batches(layer, &mut batches);
                batches.push((&inner.vertices[vertex_layers[layer].clone()], inner.glyphs.texture()));
                for font in inner.pane_fonts.values() {
                    batches.push((&font.vertices[font.vertex_layers[layer].clone()], font.glyphs.texture()));
                }
            }
        }
        inner.gpu.draw_batches(&batches, [bg.0, bg.1, bg.2, bg.3], millis)?;
        Ok((budget.deferred(), declined))
    }
}

/// The grid a canvas of `w`x`h` device pixels holds, or `None` when it
/// cannot hold even one cell: a 2x1 shell is never what a 0x0 box means.
pub fn grid_for(w: u32, h: u32, cell_w: u32, cell_h: u32) -> Option<(usize, usize)> {
    if cell_w == 0 || cell_h == 0 || w < cell_w || h < cell_h {
        return None;
    }
    Some(((w / cell_w).max(2) as usize, (h / cell_h).max(1) as usize))
}

#[cfg(test)]
mod tests {
    use super::grid_for;

    #[test]
    fn a_degenerate_box_yields_no_grid() {
        assert_eq!(grid_for(0, 0, 10, 20), None);
        assert_eq!(grid_for(1, 1, 10, 20), None);
        assert_eq!(grid_for(800, 19, 10, 20), None);
        assert_eq!(grid_for(800, 400, 10, 20), Some((80, 20)));
        assert_eq!(grid_for(15, 20, 10, 20), Some((2, 1)));
    }

    #[test]
    fn a_tab_is_placed_as_it_is_split() {
        use super::tab_places;
        use thinkterm_proto::layout::{PaneEntry, PaneNode, PaneStackEntry};
        use thinkterm_proto::split::{SplitDirection, SplitDirectionAndSize};
        use wezterm_term::TerminalSize;

        let pane = |tab_id, pane_id, left_col, top_row, is_zoomed_pane| PaneEntry {
            window_id: 0,
            tab_id,
            pane_id,
            title: String::new(),
            size: TerminalSize { rows: 12, cols: 40, ..TerminalSize::default() },
            working_dir: None,
            is_active_pane: false,
            is_zoomed_pane,
            alt_screen: false,
            workspace: "default".to_string(),
            cursor_pos: Default::default(),
            physical_top: 0,
            top_row,
            left_col,
            tty_name: None,
        };
        let split = |left, right| PaneNode::Split {
            left: Box::new(left),
            right: Box::new(right),
            node: SplitDirectionAndSize {
                direction: SplitDirection::Horizontal,
                first: TerminalSize::default(),
                second: TerminalSize::default(),
            },
        };
        let stack = PaneStackEntry { active: 1, panes: vec![pane(2, 22, 41, 13, false), pane(2, 23, 41, 13, false)], pane_stack_id: None };
        let layout = codec::ListPanesResponse {
            tabs: vec![
                PaneNode::Leaf(pane(1, 10, 0, 0, false)),
                split(
                    PaneNode::Leaf(pane(2, 20, 0, 0, false)),
                    split(PaneNode::Leaf(pane(2, 21, 41, 0, false)), PaneNode::Stack(stack)),
                ),
                split(PaneNode::Leaf(pane(3, 30, 0, 0, false)), PaneNode::Leaf(pane(3, 31, 41, 0, true))),
            ],
            tab_titles: Vec::new(),
            window_titles: Default::default(),
            pane_labels: Default::default(),
            pane_machines: Default::default(),
        };
        let at = |pane_id| tab_places(&layout, pane_id).map(|places| places.iter().map(|p| (p.pane, p.left, p.top)).collect::<Vec<_>>());
        assert_eq!(at(10), Some(vec![(10, 0, 0)]));
        assert_eq!(
            at(22),
            Some(vec![(20, 0, 0), (21, 41, 0), (23, 41, 13)]),
            "every pane on show, a stack's one in front"
        );
        assert_eq!(at(30), Some(vec![(31, 0, 0)]), "a zoomed pane alone");
        assert_eq!(at(99), None);
        // Each at the size the same listing has it.
        let place = tab_places(&layout, 21).unwrap()[1];
        assert_eq!((place.cols, place.rows), (40, 12));
    }

    #[test]
    fn a_pane_fits_the_place_listed_for_it() {
        use super::{ScreenPreview, ScreenRun};

        let run = |text: &str, col| ScreenRun {
            text: text.to_string(),
            fg: String::new(),
            bg: String::new(),
            col,
            width: text.len(),
            narrow: true,
        };
        // Wider and taller than its place: resized since it was listed.
        let mut screen = ScreenPreview {
            cols: 6,
            rows: 3,
            foreground: String::new(),
            background: String::new(),
            lines: vec![vec![run("ab", 0), run("cd", 4)], vec![run("x", 0)], vec![run("gone", 0)]],
        };
        screen.fit(4, 2);
        let seen: Vec<Vec<&str>> = screen.lines.iter().map(|runs| runs.iter().map(|r| r.text.as_str()).collect()).collect();
        assert_eq!((screen.cols, screen.rows, seen), (4, 2, vec![vec!["ab"], vec!["x"]]));
        // Shorter: the rows it lacks are blank.
        screen.fit(4, 3);
        assert_eq!(screen.lines.len(), 3);
        assert!(screen.lines[2].is_empty());
    }

    #[test]
    fn a_screen_row_keeps_its_backgrounds_and_reversed_cells() {
        use super::screen_row;
        use termwiz::cell::{Cell, CellAttributes, Intensity};
        use termwiz::color::AnsiColor;
        use termwiz::surface::{Line, SEQ_ZERO};
        use wezterm_term::color::{ColorAttribute, ColorPalette};

        let palette = ColorPalette::default();
        let fg = palette.foreground.to_rgb_string();
        let bg = palette.background.to_rgb_string();
        let plain = CellAttributes::default();
        let mut line = Line::from_text("ab  ", &plain, SEQ_ZERO, None);
        let mut inverse = CellAttributes::default();
        inverse.set_reverse(true);
        line.set_cell(1, Cell::new('b', inverse.clone()), SEQ_ZERO);
        let mut on_red = CellAttributes::default();
        on_red.set_background(AnsiColor::Maroon);
        let red = palette.resolve_bg(on_red.background()).to_rgb_string();
        line.set_cell(4, Cell::new(' ', on_red), SEQ_ZERO);

        let runs = screen_row(&line, &palette, false);
        let seen: Vec<(&str, &str, &str, usize, usize)> =
            runs.iter().map(|r| (r.text.as_str(), r.fg.as_str(), r.bg.as_str(), r.col, r.width)).collect();
        assert_eq!(
            seen,
            vec![
                ("a", fg.as_str(), "", 0, 1),
                ("b", bg.as_str(), fg.as_str(), 1, 1),
                ("  ", fg.as_str(), "", 2, 2),
                (" ", fg.as_str(), red.as_str(), 4, 1),
            ]
        );

        // A wide character is a run of its own, and what follows it
        // starts where the terminal has it.
        let wide = Line::from_text("a界b", &plain, SEQ_ZERO, None);
        let runs = screen_row(&wide, &palette, false);
        let seen: Vec<(&str, usize, usize)> = runs.iter().map(|r| (r.text.as_str(), r.col, r.width)).collect();
        assert_eq!(seen, vec![("a", 0, 1), ("界", 1, 2), ("b", 3, 1)]);

        // Blanks on the screen's own background end a row, a reversed
        // screen's too; a cell reversed on one is as it was, its ground
        // left unpainted, as the terminal leaves it.
        let short = Line::from_text("x   ", &plain, SEQ_ZERO, None);
        let runs = screen_row(&short, &palette, false);
        assert_eq!((runs.len(), runs[0].text.as_str()), (1, "x"));
        let mut both = Line::from_text("x   ", &plain, SEQ_ZERO, None);
        both.set_cell(2, Cell::new('y', inverse), SEQ_ZERO);
        let runs = screen_row(&both, &palette, true);
        let seen: Vec<(&str, &str, &str, usize)> =
            runs.iter().map(|r| (r.text.as_str(), r.fg.as_str(), r.bg.as_str(), r.col)).collect();
        assert_eq!(seen, vec![("x ", bg.as_str(), "", 0), ("y", fg.as_str(), "", 2)]);

        // Bold lifts the first eight colours to their bright twins, as the
        // terminal draws them.
        let mut bold = CellAttributes::default();
        bold.set_intensity(Intensity::Bold);
        bold.set_foreground(AnsiColor::Navy);
        let loud = Line::from_text("d", &bold, SEQ_ZERO, None);
        let runs = screen_row(&loud, &palette, false);
        assert_eq!(runs[0].fg, palette.resolve_fg(ColorAttribute::PaletteIndex(12)).to_rgb_string());

        // A wide character of two (a kana and its voicing mark) is a run of
        // its own as well: nothing narrow joins it.
        let marked = Line::from_text("か\u{3099}b", &plain, SEQ_ZERO, None);
        let runs = screen_row(&marked, &palette, false);
        let seen: Vec<(&str, usize, usize)> = runs.iter().map(|r| (r.text.as_str(), r.col, r.width)).collect();
        assert_eq!(seen, vec![("か\u{3099}", 0, 2), ("b", 2, 1)]);

        // Concealed text is not drawn; its background is.
        let mut hidden = CellAttributes::default();
        hidden.set_invisible(true);
        hidden.set_background(AnsiColor::Maroon);
        let mut secret = Line::from_text("a", &plain, SEQ_ZERO, None);
        secret.set_cell(1, Cell::new('s', hidden), SEQ_ZERO);
        let runs = screen_row(&secret, &palette, false);
        let seen: Vec<(&str, &str, usize)> = runs.iter().map(|r| (r.text.as_str(), r.bg.as_str(), r.col)).collect();
        assert_eq!(seen, vec![("a", "", 0), (" ", red.as_str(), 1)]);
    }
}