GitCorner

Code

ossartcrafteffectcraft🌐
665bfac0b339
//! The Settings dialog (After Effects' Preferences): the page list on the left, the page on the
//! right, OK / Cancel / Previous / Next. Pages are built from the engine's schema
//! (`effectcraft_engine::prefs::pages`), so the dialog, `prefs.pages` and the docs agree. Edits
//! apply live; Cancel restores the settings from when the dialog opened, OK saves them.
//!
//! Automation ids: `settings.page.<id>`, `settings.<key>` (e.g. `settings.general.undoLevels`),
//! `settings.labels.<n>.name` / `.color`, `settings.button.<label>`, `settings.ok`,
//! `settings.cancel`, `settings.previous`, `settings.next`, `settings.disk.cacheLimit` (the disk
//! cache's limit in effect, a label).

use crate::i18n::{tr, tr_args};
use effectcraft_engine::prefs::{APPEARANCE_MODES, DARK_THEMES, Item, Kind, LIGHT_THEMES, Page, pages};
use effectcraft_engine::segment::Task;
use egui::{Color32, RichText, vec2};
use serde_json::{Value, json};

use crate::a11y;
use crate::theme::{ThemeKind, Tokens};
use crate::{Dialog, EffectcraftApp};

/// Open the dialog on a page, remembering the settings for Cancel.
pub fn open(app: &mut EffectcraftApp, page: &str) {
    if app.dialog != Some(Dialog::Settings) {
        app.dialog_state.prefs_snapshot = Some(app.session.prefs.clone());
    }
    app.dialog_state.settings_page = page.to_string();
    app.dialog = Some(Dialog::Settings);
}

enum Act {
    Set(String, Value),
    Run(String, Value),
    PickFolder(String),
    PickProject(String),
    /// Install from File (a downloaded weights file) for a task's models.
    PickModel(Task),
}

fn hex(c: [u8; 3]) -> String {
    format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2])
}

/// sRGB components (0–1) as bytes.
fn srgb_bytes(c: [f32; 3]) -> [u8; 3] {
    let col = crate::widgets::srgb32(c);
    [col.r(), col.g(), col.b()]
}

fn reg(app: &mut EffectcraftApp, id: &str, r: &egui::Response, label: &str) {
    app.auto.add(id, r.rect, label);
}

/// One page's rows.
fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value, t: &Tokens, acts: &mut Vec<Act>) {
    let get = |key: &str| cur.pointer(&format!("/{}", key.replace('.', "/"))).cloned().unwrap_or(Value::Null);
    let label_w = 250.0;
    for item in &page.items {
        match *item {
            Item::Section(title) => {
                ui.add_space(6.0);
                ui.label(RichText::new(tr(title)).font(Tokens::semibold(12.5)).color(t.tab_text_active));
                ui.separator();
            }
            Item::Note(text) => {
                ui.label(RichText::new(tr(text)).color(t.text_dim));
            }
            Item::Button { label, command, params } => {
                let r = ui.button(tr(label));
                reg(app, &format!("settings.button.{}", label.trim_end_matches('.')), &r, label);
                if r.clicked() {
                    acts.push(Act::Run(command.into(), serde_json::from_str(params).unwrap_or(json!({}))));
                }
            }
            Item::Labels => labels_ui(app, ui, cur, acts),
            Item::BrowserStorage => browser_storage_ui(app, ui, t, acts),
            Item::Models(task) => models_ui(app, ui, t, acts, task),
            Item::PluginLibrary => library_ui(app, ui, t, acts),
            Item::AudioDevices { key } => {
                let devices: Vec<String> = app.hooks.audio_devices.as_ref().map(|f| f()).unwrap_or_default();
                let sel = get(key).as_str().unwrap_or("").to_string();
                ui.horizontal(|ui| {
                    ui.add_sized(vec2(label_w, 18.0), egui::Label::new(tr("Default Output:")));
                    let shown = if sel.is_empty() { tr("System Default").to_string() } else { sel.clone() };
                    let shown_name = shown.clone();
                    let r = craft_ui::ComboBox::from_id_salt(("settings", key)).selected_text(shown).width(260.0).show_ui(ui, |ui| {
                        if ui.selectable_label(sel.is_empty(), tr("System Default")).clicked() {
                            acts.push(Act::Set(key.into(), json!("")));
                        }
                        for d in &devices {
                            if ui.selectable_label(*d == sel, d).clicked() {
                                acts.push(Act::Set(key.into(), json!(d)));
                            }
                        }
                    });
                    a11y::combo_box(&r.response, tr("Default Output:").trim_end_matches(':'), &shown_name, true);
                    reg(app, &format!("settings.{key}"), &r.response, "Default Output");
                });
                if devices.is_empty() {
                    ui.label(RichText::new(tr("No device list from this host; the system default output is used.")).color(t.text_dim));
                }
            }
            // Appearance Mode with the light and dark theme cards (the per-mode themes are
            // chosen on the cards).
            Item::Setting { key: "appearance.appearanceMode", .. } => appearance_ui(app, ui, cur, t, acts),
            Item::Setting { key: "appearance.lightTheme" | "appearance.darkTheme", .. } => {}
            Item::Setting { key, label, kind, live } => {
                let v = get(key);
                let id = format!("settings.{key}");
                let tip = (!live).then(|| tr("Stored, but not used by EffectCraft yet"));
                let mut resp: Option<egui::Response> = None;
                // What the control is set to, for assistive technology.
                let mut value_text = String::new();
                match kind {
                    Kind::Bool => {
                        let mut b = v.as_bool().unwrap_or(false);
                        let r = crate::widgets::labelled_checkbox(ui, &mut b, tr(label));
                        if r.changed() {
                            acts.push(Act::Set(key.into(), json!(b)));
                        }
                        resp = Some(r);
                    }
                    _ => {
                        ui.horizontal(|ui| {
                            ui.add_sized(vec2(label_w, 18.0), egui::Label::new(format!("{}:", tr(label))).truncate());
                            let r = match kind {
                                Kind::Int(lo, hi, unit) => {
                                    // A dragged value applies on release, and a typed one once
                                    // entered: UI Scale doesn't resize the interface under the
                                    // pointer mid-drag (#2582) or at a typed number's first digit.
                                    let held = egui::Id::unique(("settings-drag", key));
                                    let mut x = ui.data(|d| d.get_temp::<i64>(held)).unwrap_or_else(|| v.as_i64().unwrap_or(0));
                                    let drag = egui::DragValue::new(&mut x).range(lo..=hi).speed(0.25).update_while_editing(false);
                                    let r = ui.add(crate::widgets::drag_value(drag));
                                    if !unit.is_empty() {
                                        ui.label(RichText::new(tr(unit)).color(t.text_dim));
                                    }
                                    if r.dragged() {
                                        ui.data_mut(|d| d.insert_temp(held, x));
                                    } else if r.changed() || r.drag_stopped() {
                                        ui.data_mut(|d| d.remove::<i64>(held));
                                        acts.push(Act::Set(key.into(), json!(x)));
                                    }
                                    r
                                }
                                Kind::Float(lo, hi, unit) => {
                                    let mut x = v.as_f64().unwrap_or(0.0);
                                    let r = ui.add(crate::widgets::drag_value(egui::DragValue::new(&mut x).range(lo..=hi).speed(0.1).max_decimals(3)));
                                    if !unit.is_empty() {
                                        ui.label(RichText::new(tr(unit)).color(t.text_dim));
                                    }
                                    if r.changed() {
                                        acts.push(Act::Set(key.into(), json!(x)));
                                    }
                                    r
                                }
                                Kind::Slider(lo, hi) => {
                                    let mut x = v.as_f64().unwrap_or(0.0);
                                    ui.label(RichText::new(tr("Darker")).color(t.text_dim));
                                    let r = ui.add(egui::Slider::new(&mut x, lo..=hi).show_value(false));
                                    ui.label(RichText::new(tr("Lighter")).color(t.text_dim));
                                    if ui.small_button(tr("Default")).clicked() {
                                        acts.push(Act::Set(key.into(), json!(0.0)));
                                    }
                                    if r.changed() {
                                        acts.push(Act::Set(key.into(), json!(x)));
                                    }
                                    r
                                }
                                Kind::Choice(opts) => {
                                    let curv = v.as_str().map_or_else(|| v.to_string(), str::to_string);
                                    let shown = opts.iter().find(|o| o.1 == curv).map(|o| o.0).unwrap_or(curv.as_str()).to_string();
                                    value_text = tr(&shown).to_string();
                                    craft_ui::ComboBox::from_id_salt(("settings", key))
                                        .selected_text(tr(&shown))
                                        .width(220.0)
                                        .show_ui(ui, |ui| {
                                            for (l, val) in opts {
                                                if ui.selectable_label(*val == curv, tr(l)).clicked() {
                                                    acts.push(Act::Set(key.into(), json!(val)));
                                                }
                                            }
                                        })
                                        .response
                                }
                                Kind::Text | Kind::Path => {
                                    let mut s = v.as_str().unwrap_or("").to_string();
                                    let r = ui.add(egui::TextEdit::singleline(&mut s).desired_width(if kind == Kind::Path { 230.0 } else { 260.0 }));
                                    value_text = s.clone();
                                    if r.changed() {
                                        acts.push(Act::Set(key.into(), json!(s)));
                                    }
                                    if kind == Kind::Path {
                                        let b = ui.button(tr("Choose..."));
                                        reg(app, &format!("{id}.choose"), &b, "Choose...");
                                        if b.clicked() {
                                            acts.push(if key.ends_with("Path") { Act::PickProject(key.into()) } else { Act::PickFolder(key.into()) });
                                        }
                                    }
                                    r
                                }
                                Kind::Color => {
                                    let c = crate::panels::viewer::hex_rgb(v.as_str().unwrap_or("")).unwrap_or([128, 128, 128]);
                                    let mut f = c.map(|v| v as f32 / 255.0);
                                    let r = crate::widgets::srgb_color_button(ui, &mut f);
                                    let rgb = if r.changed() { srgb_bytes(f) } else { c };
                                    value_text = hex(rgb);
                                    ui.label(RichText::new(hex(rgb)).monospace().color(t.text_dim));
                                    if rgb != c {
                                        acts.push(Act::Set(key.into(), json!(hex(rgb))));
                                    }
                                    r
                                }
                                // Checkboxes are drawn above; nothing to edit here.
                                Kind::Bool => ui.label(""),
                            };
                            resp = Some(r);
                        });
                    }
                }
                if let Some(r) = &resp {
                    // The field's label is drawn beside it, not on it: give the control its name.
                    let name = tr(label);
                    match kind {
                        Kind::Bool => {}
                        Kind::Choice(_) => a11y::combo_box(r, name, &value_text, true),
                        Kind::Text | Kind::Path => a11y::text_input(r, name, &value_text),
                        Kind::Color => a11y::name_value(r, name, &value_text),
                        _ => a11y::name(r, name),
                    }
                }
                if let Some(r) = resp {
                    let r = match tip {
                        Some(tip) => r.on_hover_text(tip),
                        None => r,
                    };
                    reg(app, &id, &r, label);
                }
                if key == "disk.diskCacheMaxGb" {
                    disk_cache_limit_ui(app, ui, t);
                }
            }
        }
    }
}

/// Settings ▸ Disk, under Maximum Disk Cache Size: the limit in effect, and why it is lower when
/// free disk space holds the cache below the setting (it never fills the disk). Automation id
/// `settings.disk.cacheLimit`, labelled `limitMb=… maxMb=… reserveMb=… spaceLimited=… writes=…`
/// (machine-readable, the same in every interface language). Nothing without an open disk cache.
/// The maximum is the one applied: an edit applies at once (a dragged value on release).
fn disk_cache_limit_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens) {
    let Some(dc) = app.session.disk_cache.clone() else { return };
    // (atomic reads: drawn every frame)
    let (max, limit, reserve, writes) = (dc.max_bytes(), dc.limit_bytes(), dc.reserve_bytes(), dc.accepts_writes());
    let lines = disk_cache_limit_lines(max, limit, reserve, writes);
    let color = if limit < max { t.warning } else { t.text_dim };
    let r = ui
        .vertical(|ui| {
            for line in &lines {
                ui.label(RichText::new(line).color(color));
            }
        })
        .response;
    let label =
        format!("limitMb={} maxMb={} reserveMb={} spaceLimited={} writes={writes}", limit.min(max) >> 20, max >> 20, reserve.unwrap_or(0) >> 20, limit < max);
    app.auto.add("settings.disk.cacheLimit", r.rect, &label);
}

/// The lines [`disk_cache_limit_ui`] shows for a disk cache of `max` bytes (Maximum Disk Cache
/// Size) whose limit in effect is `limit` bytes, leaving `reserve` bytes of its disk free (when
/// known), and taking frame and layer writes (`writes`, [`DiskCache::accepts_writes`]).
///
/// [`DiskCache::accepts_writes`]: effectcraft_engine::render::disk_cache::DiskCache::accepts_writes
pub(crate) fn disk_cache_limit_lines(max: u64, limit: u64, reserve: Option<u64>, writes: bool) -> Vec<String> {
    use effectcraft_engine::render::disk_cache::{MAX_DISK_RESERVE, MIN_DISK_RESERVE};
    const GB: f64 = (1u64 << 30) as f64;
    let mut lines = vec![tr_args("Disk cache limit in effect: {} MB", &[&(limit.min(max) >> 20)])];
    if limit < max {
        lines.push(match reserve {
            Some(r) => tr_args("Limited by free disk space: EffectCraft keeps {} GB of this disk free.", &[&format!("{:.1}", r as f64 / GB)]),
            None => tr_args(
                "Limited by free disk space: EffectCraft keeps 5% of the disk free (at least {} GB, at most {} GB).",
                &[&(MIN_DISK_RESERVE >> 30), &(MAX_DISK_RESERVE >> 30)],
            ),
        });
    }
    if !writes {
        lines.push(tr("No room left: nothing is written to the disk cache until disk space is freed.").to_string());
    }
    lines
}

/// A small EffectCraft window painted with theme `kind`'s tokens: the Tools bar, the Project
/// panel, the Composition panel, the Preview and Properties column and the Timeline with its
/// layer bars.
fn theme_preview(ui: &mut egui::Ui, kind: ThemeKind, width: f32) {
    use egui::{Rect, pos2};
    let p = Tokens::for_kind(kind);
    let (rect, _) = ui.allocate_exact_size(vec2(width, 118.0), egui::Sense::hover());
    let painter = ui.painter_at(rect);
    painter.rect_filled(rect, 3.0, p.app_bg);
    let bar = Rect::from_min_size(rect.min, vec2(rect.width(), 13.0));
    painter.rect_filled(bar, 0.0, p.header_bg);
    // The brand mark, the tools (the Selection tool is active) and the Discord pill.
    painter.rect_filled(Rect::from_min_size(bar.min + vec2(4.0, 3.0), vec2(7.0, 7.0)), 2.0, p.accent);
    for i in 0..7 {
        let c = if i == 0 { p.accent } else { p.icon };
        painter.rect_filled(Rect::from_min_size(bar.min + vec2(16.0 + i as f32 * 8.0, 4.0), vec2(5.0, 5.0)), 1.0, c);
    }
    painter.rect_filled(
        Rect::from_min_max(pos2(bar.right() - 22.0, bar.top() + 3.0), pos2(bar.right() - 4.0, bar.bottom() - 3.0)),
        3.5,
        egui::Color32::from_rgb(0x58, 0x65, 0xf2),
    );
    let gap = 2.0;
    let body = Rect::from_min_max(pos2(rect.left() + gap, bar.bottom() + gap), pos2(rect.right() - gap, rect.bottom() - gap));
    let right_w = (body.width() * 0.22).round();
    let timeline_h = (body.height() * 0.42).round();
    let left_w = (body.width() * 0.2).round();
    let upper = Rect::from_min_max(body.min, pos2(body.right() - right_w - gap, body.bottom() - timeline_h - gap));
    let panel = |r: Rect, title_w: f32| {
        painter.rect_filled(r, 2.0, p.panel_bg);
        painter.rect_filled(Rect::from_min_size(r.min + vec2(4.0, 3.0), vec2(title_w, 2.0)), 1.0, p.tab_text_active);
    };
    // Project panel: rows with label swatches.
    let project = Rect::from_min_max(upper.min, pos2(upper.left() + left_w, upper.bottom()));
    panel(project, 14.0);
    for (i, label) in [5usize, 5, 2, 2].into_iter().enumerate() {
        let y = project.top() + 10.0 + i as f32 * 6.0;
        painter.rect_filled(Rect::from_min_size(pos2(project.left() + 4.0, y), vec2(3.0, 3.0)), 0.5, p.labels.get(label).copied().unwrap_or(p.icon));
        painter.rect_filled(Rect::from_min_size(pos2(project.left() + 9.0, y + 0.5), vec2(project.width() * 0.45, 2.0)), 1.0, p.text_dim);
    }
    // Composition panel: the pasteboard and the comp frame with a title.
    let comp = Rect::from_min_max(pos2(project.right() + gap, upper.top()), upper.max);
    panel(comp, 22.0);
    let paste = Rect::from_min_max(comp.min + vec2(2.0, 8.0), comp.max - vec2(2.0, 2.0));
    painter.rect_filled(paste, 0.0, p.pasteboard);
    let frame_h = (paste.height() - 6.0).max(4.0);
    let frame = Rect::from_center_size(paste.center(), vec2((frame_h * 16.0 / 9.0).min(paste.width() - 6.0), frame_h));
    painter.rect_filled(frame, 0.0, egui::Color32::from_rgb(0x14, 0x1c, 0x4a));
    painter.circle_stroke(frame.center(), frame.height() * 0.36, egui::Stroke::new(0.8, egui::Color32::from_rgb(0x4a, 0x7c, 0xe8)));
    painter.rect_filled(Rect::from_center_size(frame.center(), vec2(frame.width() * 0.5, 2.5)), 1.0, egui::Color32::WHITE);
    // Preview and Properties.
    let column = Rect::from_min_max(pos2(body.right() - right_w, body.top()), body.max);
    let preview = Rect::from_min_max(column.min, pos2(column.right(), column.top() + column.height() * 0.3));
    panel(preview, 12.0);
    painter.rect_filled(Rect::from_min_size(preview.left_bottom() + vec2(4.0, -5.0), vec2(preview.width() * 0.25, 1.5)), 0.5, p.cache_green);
    let props = Rect::from_min_max(pos2(column.left(), preview.bottom() + gap), column.max);
    panel(props, 18.0);
    for i in 0..5 {
        let y = props.top() + 10.0 + i as f32 * 6.0;
        painter.rect_filled(Rect::from_min_size(pos2(props.left() + 4.0, y), vec2(props.width() * 0.35, 2.0)), 1.0, p.text_dim);
        painter.rect_filled(Rect::from_min_size(pos2(props.left() + props.width() * 0.55, y), vec2(props.width() * 0.25, 2.0)), 1.0, p.hot_text);
    }
    // Timeline: the layer list and the time graph, with coloured layer bars and the time marker.
    let timeline = Rect::from_min_max(pos2(body.left(), upper.bottom() + gap), pos2(upper.right(), body.bottom()));
    panel(timeline, 18.0);
    painter.rect_filled(Rect::from_min_size(timeline.min + vec2(4.0, 8.0), vec2(14.0, 3.0)), 1.0, p.timecode);
    let graph = Rect::from_min_max(pos2(timeline.left() + timeline.width() * 0.38, timeline.top() + 8.0), timeline.max - vec2(2.0, 2.0));
    painter.rect_filled(graph, 0.0, p.tl_bg);
    painter.rect_filled(Rect::from_min_size(graph.min, vec2(graph.width(), 4.0)), 0.0, p.tl_ruler_bg);
    for (i, label) in [1usize, 1, 8, 8, 1].into_iter().enumerate() {
        let y = graph.top() + 7.0 + i as f32 * 5.5;
        if y + 3.5 > graph.bottom() {
            break;
        }
        let swatch = p.labels.get(label).copied().unwrap_or(p.icon);
        let row_l = timeline.left() + 4.0;
        if i == 1 {
            painter.rect_filled(Rect::from_min_max(pos2(timeline.left(), y - 1.0), pos2(graph.left(), y + 4.5)), 0.0, p.row_selected);
        }
        painter.rect_filled(Rect::from_min_size(pos2(row_l, y), vec2(3.0, 3.0)), 0.5, swatch);
        painter.rect_filled(Rect::from_min_size(pos2(row_l + 6.0, y + 0.5), vec2(graph.left() - row_l - 14.0, 2.0)), 1.0, p.text_dim);
        let bar = Rect::from_min_max(pos2(graph.left() + 1.0, y), pos2(graph.right() - 1.0, y + 3.5));
        painter.rect_filled(bar, 1.0, swatch.gamma_multiply(0.8));
    }
    let cti = graph.left() + graph.width() * 0.3;
    painter.line_segment([pos2(cti, graph.top()), pos2(cti, graph.bottom())], egui::Stroke::new(1.0, p.cti));
}

/// What [`theme_card`] shows: the setting `key`, the translated `title`, whether the family is
/// `active`, the `cur`rent theme and the `options` (value, label) to pick from.
struct ThemeFamily<'a> {
    key: &'a str,
    title: &'a str,
    active: bool,
    cur: &'a str,
    options: &'a [(&'a str, &'a str)],
}

/// One theme family's card: its (translated) name, Active when the appearance shows it now, a preview of the
/// chosen theme and a radio button per theme.
fn theme_card(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, fam: ThemeFamily, acts: &mut Vec<Act>) {
    let ThemeFamily { key, title, active, cur, options } = fam;
    egui::Frame::new()
        .fill(t.field_bg)
        .stroke(egui::Stroke::new(1.0, if active { t.accent } else { t.field_border }))
        .corner_radius(6.0)
        .inner_margin(egui::Margin::same(8))
        .show(ui, |ui| {
            ui.set_width(ui.available_width());
            ui.horizontal(|ui| {
                ui.label(RichText::new(title).font(Tokens::semibold(12.5)).color(t.text));
                if active {
                    ui.label(RichText::new(tr("Active")).color(t.accent).small());
                }
            });
            ui.add_space(4.0);
            let kind = ThemeKind::from_name(cur).unwrap_or(if key.ends_with("lightTheme") { ThemeKind::Light } else { ThemeKind::Dark });
            theme_preview(ui, kind, ui.available_width());
            ui.add_space(4.0);
            for (label, value) in options {
                let row = egui::Rect::from_min_size(ui.cursor().min, vec2(ui.available_width(), 24.0));
                let hovered = ui.ctx().pointer_hover_pos().is_some_and(|p| row.contains(p));
                ui.horizontal(|ui| {
                    let r = ui.radio(cur == *value, tr(label));
                    reg(app, &format!("settings.{key}.{value}"), &r, label);
                    if r.clicked() && cur != *value {
                        acts.push(Act::Set(key.into(), json!(value)));
                    }
                    ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                        let mut favorites = app.session.prefs.appearance.favorite_themes.clone();
                        let favorite = favorites.iter().any(|id| id == value);
                        let tip = tr(if favorite { "Remove from favorites" } else { "Add to favorites" });
                        let r = ui.add_visible(favorite || hovered, egui::Button::new("").min_size(vec2(24.0, 24.0)).frame(false)).on_hover_text(tip);
                        r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, format!("{}: {}", tip, tr(label))));
                        reg(app, &format!("settings.appearance.favoriteThemes.{value}"), &r, tip);
                        if favorite || hovered {
                            crate::icons::paint(
                                ui.painter(),
                                r.rect.shrink(4.0),
                                if favorite { crate::icons::Icon::StarFilled } else { crate::icons::Icon::Star },
                                if favorite { t.accent } else { t.text_dim },
                            );
                        }
                        if r.clicked() {
                            if favorite {
                                favorites.retain(|id| id != value);
                            } else {
                                favorites.push((*value).into());
                            }
                            acts.push(Act::Set("appearance.favoriteThemes".into(), json!(favorites)));
                        }
                    });
                });
            }
        });
}

/// Settings ▸ Appearance: Appearance Mode (Sync with System, Dark, Light) above the light and dark
/// theme cards.
fn appearance_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, cur: &Value, t: &Tokens, acts: &mut Vec<Act>) {
    let get = |key: &str| cur.pointer(&format!("/appearance/{key}")).and_then(Value::as_str).unwrap_or_default().to_string();
    let (mode, light, dark) = (get("appearanceMode"), get("lightTheme"), get("darkTheme"));
    ui.horizontal(|ui| {
        ui.add_sized(vec2(250.0, 18.0), egui::Label::new(format!("{}:", tr("Appearance Mode"))).truncate());
        let shown = APPEARANCE_MODES.iter().find(|(_, v)| *v == mode).map_or(mode.as_str(), |(l, _)| *l);
        let r = craft_ui::ComboBox::from_id_salt(("settings", "appearance.appearanceMode"))
            .selected_text(tr(shown))
            .width(220.0)
            .show_ui(ui, |ui| {
                for (l, v) in APPEARANCE_MODES {
                    if ui.selectable_label(*v == mode, tr(l)).clicked() {
                        acts.push(Act::Set("appearance.appearanceMode".into(), json!(v)));
                    }
                }
            })
            .response;
        a11y::combo_box(&r, tr("Appearance Mode"), tr(shown), true);
        reg(app, "settings.appearance.appearanceMode", &r, "Appearance Mode");
    });
    ui.add_space(6.0);
    let system_light = app.system_light(ui.ctx());
    let light_active = mode == "light" || (mode == "auto" && system_light == Some(true));
    ui.columns(2, |cols| {
        let [l, d] = cols else { return };
        theme_card(
            app,
            l,
            t,
            ThemeFamily { key: "appearance.lightTheme", title: tr("Light Theme"), active: light_active, cur: &light, options: LIGHT_THEMES },
            acts,
        );
        theme_card(
            app,
            d,
            t,
            ThemeFamily { key: "appearance.darkTheme", title: tr("Dark Theme"), active: !light_active, cur: &dark, options: DARK_THEMES },
            acts,
        );
    });
    ui.add_space(6.0);
}

/// Bytes as "12.3 MB".
pub fn human_bytes(b: u64) -> String {
    let units = ["bytes", "KB", "MB", "GB", "TB"];
    let mut v = b as f64;
    let mut u = 0;
    while v >= 1000.0 && u + 1 < units.len() {
        v /= 1000.0;
        u += 1;
    }
    if u == 0 { format!("{b} bytes") } else { format!("{v:.1} {}", units[u]) }
}

/// Settings ▸ Roto Brush and Settings ▸ Face Tracking: a task's models (`roto.models`,
/// `face.models`): choose one; download, install from a file or remove the trained ones; who made
/// them, their licence, size and source.
fn models_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, acts: &mut Vec<Act>, task: Task) {
    let (prefix, user) = match task {
        Task::Mask => ("roto", "Roto Brush"),
        Task::Face => ("face", "Face tracking"),
    };
    let Ok(info) = app.session.execute(&format!("{prefix}.models"), json!({})) else { return };
    let web = cfg!(target_arch = "wasm32");
    let busy = info["busy"].as_str().map(str::to_string);
    for m in info["models"].as_array().into_iter().flatten() {
        let id = m["id"].as_str().unwrap_or_default().to_string();
        let name = m["name"].as_str().unwrap_or_default();
        let classic = id == effectcraft_engine::segment::CLASSICAL;
        let (installed, selected, active) =
            (m["installed"].as_bool().unwrap_or(false), m["selected"].as_bool().unwrap_or(false), m["active"].as_bool().unwrap_or(false));
        ui.add_space(6.0);
        egui::Frame::new().fill(t.field_bg).corner_radius(6.0).inner_margin(egui::Margin::same(10)).show(ui, |ui| {
            ui.set_width(ui.available_width());
            ui.horizontal(|ui| {
                let r = ui.radio(selected, RichText::new(name).font(Tokens::semibold(12.5)));
                reg(app, &format!("settings.{prefix}.model.{id}"), &r, name);
                if r.clicked() && !selected {
                    acts.push(Act::Run(format!("{prefix}.model.select"), json!({"id": id})));
                }
                let state = match (active, installed, selected) {
                    (true, _, _) => "In use".to_string(),
                    _ if classic => "Built in".to_string(),
                    (false, true, true) if busy.is_some() => "Loading…".to_string(),
                    (false, true, _) => "Installed".to_string(),
                    (false, false, true) => format!("Not installed: {user} uses the classic engine"),
                    _ => "Not installed".to_string(),
                };
                ui.label(RichText::new(state).color(if active { t.accent } else { t.text_dim }));
            });
            ui.label(RichText::new(m["description"].as_str().unwrap_or_default()).color(t.text_dim));
            if let Some(hint) = m["hint"].as_str() {
                ui.label(RichText::new(hint).color(if m["recommended"].as_bool().unwrap_or(false) { t.accent } else { t.text_dim }));
            }
            if let Some(by) = m["authors"].as_str() {
                ui.label(RichText::new(format!("By {by}")).color(t.text_dim));
            }
            ui.horizontal_wrapped(|ui| {
                ui.label(RichText::new(tr_args("Licence: {}", &[&(m["licence"].as_str().unwrap_or_default())])).color(t.text_dim));
                if let Some(size) = m["size"].as_u64() {
                    ui.label(RichText::new(format!("·  {}", human_bytes(size))).color(t.text_dim));
                }
                for (label, key) in [("Project page", "homepage"), ("Licence", "licenceUrl")] {
                    if let Some(url) = m[key].as_str() {
                        let r = ui.link(label);
                        if r.clicked() {
                            ui.ctx().open_url(egui::OpenUrl::new_tab(url));
                        }
                    }
                }
                if let (Some(source), Some(file)) = (m["source"].as_str(), m["sourceFile"].as_str()) {
                    let response = ui.link(file).on_hover_text(crate::i18n::tr("Source"));
                    reg(app, &format!("settings.{prefix}.source.{id}"), &response, file);
                    if response.clicked() {
                        ui.ctx().open_url(egui::OpenUrl::new_tab(source));
                    }
                }
            });
            if classic {
                return;
            }
            ui.horizontal(|ui| {
                if !installed && !web && m["url"].as_str().is_some_and(|url| !url.is_empty()) {
                    let r = ui.add_enabled(busy.is_none(), egui::Button::new(tr("Download")));
                    reg(app, &format!("settings.{prefix}.download.{id}"), &r, "Download");
                    if r.clicked() {
                        acts.push(Act::Run(format!("{prefix}.model.download"), json!({"id": id})));
                    }
                }
                if !web {
                    let r = ui.add_enabled(busy.is_none(), egui::Button::new(tr("Install from File…")));
                    reg(app, &format!("settings.{prefix}.install.{id}"), &r, "Install from File");
                    if r.clicked() {
                        acts.push(Act::PickModel(task));
                    }
                }
                if installed {
                    let r = ui.add_enabled(busy.is_none(), egui::Button::new(tr("Remove")));
                    reg(app, &format!("settings.{prefix}.remove.{id}"), &r, "Remove");
                    if r.clicked() {
                        acts.push(Act::Run(format!("{prefix}.model.remove"), json!({"id": id})));
                    }
                }
            });
        });
    }
    if let Some(b) = &busy {
        ui.horizontal(|ui| {
            ui.spinner();
            ui.label(b);
        });
        ui.ctx().request_repaint_after(std::time::Duration::from_millis(250));
    }
    if let Some(e) = info["error"].as_str() {
        ui.label(RichText::new(e).color(t.danger));
    }
    if web {
        ui.label(RichText::new(tr("Trained models are available in the desktop app; the browser uses the classic engine.")).color(t.text_dim));
    } else if let Some(f) = info["folder"].as_str() {
        ui.label(RichText::new(tr_args("Models folder: {}", &[&f])).color(t.text_faint));
    }
}

/// Settings ▸ Plug-in Library (`library.*`): search the community catalog, install, update and
/// remove packages. Automation ids: `settings.library.query`, `settings.library.refresh`,
/// `settings.library.updateAll`, `settings.library.<install|update|remove>.<package id>`.
fn library_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, acts: &mut Vec<Act>) {
    let query = app.dialog_state.library_query.clone();
    let Ok(info) = app.session.execute("library.list", json!({"query": query})) else { return };
    if !info["available"].as_bool().unwrap_or(false) {
        ui.label(RichText::new(tr("The Plug-in Library is available in the desktop app.")).color(t.text_dim));
        return;
    }
    let busy = info["busy"].as_str().map(str::to_string);
    let packages = info["packages"].as_array().cloned().unwrap_or_default();
    let has_catalog = info["catalog"].is_string();
    ui.add_space(6.0);
    library_toolbar_ui(app, ui, &query, &packages, has_catalog, busy.is_none(), acts);
    if let Some(when) = info["fetched"].as_str() {
        let when = when.get(..16).unwrap_or(when).replace('T', " ");
        ui.label(RichText::new(tr_args("Catalog fetched {} (UTC)", &[&when])).color(t.text_faint));
    }
    // A plug-in author's test catalog stays in use until Refresh fetches the community one.
    if let Some(url) = info["catalog"].as_str().filter(|u| *u != effectcraft_engine::library::CATALOG_URL) {
        ui.label(RichText::new(tr_args("Test catalog: {} (Refresh returns to the community catalog)", &[&url])).color(t.warning));
    }
    if !has_catalog {
        ui.label(RichText::new(tr("Nothing is downloaded until you fetch the catalog.")).color(t.text_dim));
    }
    for p in &packages {
        ui.add_space(6.0);
        library_card_ui(app, ui, t, p, busy.is_none(), acts);
    }
    if has_catalog && packages.is_empty() {
        let note = if query.is_empty() { tr("The catalog lists no plug-ins yet.") } else { tr("No plug-ins match the search.") };
        ui.label(RichText::new(note).color(t.text_dim));
    }
    if let Some(b) = &busy {
        ui.horizontal(|ui| {
            ui.spinner();
            ui.label(b);
        });
        ui.ctx().request_repaint_after(std::time::Duration::from_millis(250));
    }
    if let Some(e) = info["error"].as_str() {
        ui.label(RichText::new(e).color(t.danger));
    }
    if let Some(f) = info["folder"].as_str() {
        ui.label(RichText::new(tr_args("Plug-ins folder: {}", &[&f])).color(t.text_faint));
    }
}

/// The Plug-in Library's search field, Refresh (or Fetch Catalog) and Update All.
fn library_toolbar_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, query: &str, packages: &[Value], has_catalog: bool, idle: bool, acts: &mut Vec<Act>) {
    ui.horizontal(|ui| {
        let mut q = query.to_string();
        let r = ui.add(egui::TextEdit::singleline(&mut q).hint_text(tr("Search plug-ins and effects")).desired_width(260.0));
        reg(app, "settings.library.query", &r, "Search");
        if q != query {
            app.dialog_state.library_query = q;
        }
        let label = if has_catalog { tr("Refresh") } else { tr("Fetch Catalog") };
        let r = ui.add_enabled(idle, egui::Button::new(label));
        reg(app, "settings.library.refresh", &r, "Refresh");
        if r.clicked() {
            acts.push(Act::Run("library.refresh".into(), json!({})));
        }
        let updates = packages.iter().filter(|p| p["updateAvailable"].as_bool().unwrap_or(false)).count();
        if updates > 0 {
            let r = ui.add_enabled(idle, egui::Button::new(tr_args("Update All ({})", &[&updates])));
            reg(app, "settings.library.updateAll", &r, "Update All");
            if r.clicked() {
                acts.push(Act::Run("library.update".into(), json!({})));
            }
        }
    });
}

/// One package of the Plug-in Library (`library.list`'s entry `p`): name, state, details, links,
/// actions and, when shown, its preview.
fn library_card_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, p: &Value, idle: bool, acts: &mut Vec<Act>) {
    let id = p["id"].as_str().unwrap_or_default().to_string();
    let s = |k: &str| p[k].as_str().unwrap_or_default().to_string();
    let installed = p["installed"].as_str().map(str::to_string);
    let (update, restart, newer_app) =
        (p["updateAvailable"].as_bool().unwrap_or(false), p["restartNeeded"].as_bool().unwrap_or(false), p["needsNewerApp"].as_bool().unwrap_or(false));
    egui::Frame::new().fill(t.field_bg).corner_radius(6.0).inner_margin(egui::Margin::same(10)).show(ui, |ui| {
        ui.set_width(ui.available_width());
        ui.horizontal(|ui| {
            ui.label(RichText::new(s("name")).font(Tokens::semibold(12.5)));
            if let Some(v) = p["latest"].as_str() {
                ui.label(RichText::new(v).color(t.text_dim));
            }
            let state = match (&installed, restart, update) {
                (_, true, _) => tr("Restart EffectCraft to finish").to_string(),
                (Some(v), _, true) => tr_args("Installed {}: update available", &[v]),
                (Some(v), _, _) => tr_args("Installed {}", &[v]),
                (None, _, _) if newer_app && p["latest"].is_null() => tr("Needs a newer EffectCraft").to_string(),
                _ => String::new(),
            };
            ui.label(RichText::new(state).color(if installed.is_some() { t.accent } else { t.text_dim }));
        });
        if !s("description").is_empty() {
            ui.label(RichText::new(s("description")).color(t.text_dim));
        }
        library_links_ui(app, ui, t, p, &id);
        let effects: Vec<&str> = p["effects"].as_array().into_iter().flatten().filter_map(Value::as_str).collect();
        if !effects.is_empty() {
            ui.label(RichText::new(tr_args("Effects: {}", &[&effects.join(", ")])).color(t.text_faint));
        }
        library_actions_ui(app, ui, p, &id, idle, acts);
        if app.dialog_state.library_preview.as_deref() == Some(id.as_str()) {
            library_preview_ui(app, ui, t, &id);
        }
    });
}

/// A package's author, licence, size, and links to its source and video.
fn library_links_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, p: &Value, id: &str) {
    let s = |k: &str| p[k].as_str().unwrap_or_default().to_string();
    ui.horizontal_wrapped(|ui| {
        if !s("author").is_empty() {
            ui.label(RichText::new(tr_args("By {}", &[&s("author")])).color(t.text_dim));
        }
        if !s("license").is_empty() {
            ui.label(RichText::new(tr_args("Licence: {}", &[&s("license")])).color(t.text_dim));
        }
        if let Some(size) = p["size"].as_u64() {
            ui.label(RichText::new(format!("·  {}", human_bytes(size))).color(t.text_dim));
        }
        let repo = s("repo");
        if repo.starts_with("https://") {
            let label = if repo.starts_with("https://github.com/") { tr("Open on GitHub") } else { tr("Source") };
            let r = ui.link(label);
            reg(app, &format!("settings.library.repo.{id}"), &r, "Source");
            if r.clicked() {
                ui.ctx().open_url(egui::OpenUrl::new_tab(repo));
            }
        }
        let video = s("video");
        if video.starts_with("https://") {
            let r = ui.link(tr("Watch video"));
            reg(app, &format!("settings.library.video.{id}"), &r, "Watch video");
            if r.clicked() {
                ui.ctx().open_url(egui::OpenUrl::new_tab(video));
            }
        }
    });
}

/// A package's Preview / Install / Update / Remove buttons.
fn library_actions_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &Value, id: &str, idle: bool, acts: &mut Vec<Act>) {
    let installed = p["installed"].is_string();
    let available = !p["latest"].is_null();
    let showing = app.dialog_state.library_preview.as_deref() == Some(id);
    ui.horizontal(|ui| {
        if available {
            let r = ui.add_enabled(idle || showing, egui::Button::new(if showing { tr("Hide Preview") } else { tr("Preview") }).selected(showing));
            reg(app, &format!("settings.library.preview.{id}"), &r, "Preview");
            if r.clicked() {
                if showing {
                    app.dialog_state.library_preview = None;
                } else {
                    app.dialog_state.library_preview = Some(id.to_string());
                    app.dialog_state.library_split = 0.5;
                    acts.push(Act::Run("library.preview".into(), json!({"id": id})));
                }
            }
        }
        let buttons = [
            (!installed && available, tr("Install"), "Install", "install", "library.install"),
            (p["updateAvailable"].as_bool().unwrap_or(false), tr("Update"), "Update", "update", "library.update"),
            (installed, tr("Remove"), "Remove", "remove", "library.uninstall"),
        ];
        for (shown, label, name, key, command) in buttons {
            if !shown {
                continue;
            }
            let r = ui.add_enabled(idle, egui::Button::new(label));
            reg(app, &format!("settings.library.{key}.{id}"), &r, name);
            if r.clicked() {
                acts.push(Act::Run(command.into(), json!({"id": id})));
            }
        }
    });
}

/// A package's live preview (`library.preview`): its effect at default settings on the sample
/// frame, playing, with a Before/After split the user drags (automation id
/// `settings.library.previewImage`), and the effect's parameters.
fn library_preview_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, id: &str) {
    let status = app.session.library.status();
    let Some(pv) = status.preview.filter(|v| v.id == id) else {
        // A failure shows under the list (the library's error).
        if status.busy.is_none() {
            return;
        }
        ui.horizontal(|ui| {
            ui.spinner();
            ui.label(RichText::new(tr("Rendering a preview in the sandbox…")).color(t.text_dim));
        });
        return;
    };
    let ctx = ui.ctx().clone();
    // Textures: the sample frame, then the effect's frames. One set, tagged with its package
    // version and replaced when another is previewed (the engine keeps only the last preview).
    let key = egui::Id::unique("library-preview");
    let tag = (pv.id.clone(), pv.version.clone());
    let size = [pv.width as usize, pv.height as usize];
    // Cached per preview: its textures, whether it moves, whether it changes the frame at all.
    let cached = ctx.data(|d| d.get_temp::<((String, String), Vec<egui::TextureHandle>, bool, bool)>(key));
    let (tex, animated, unchanged) = match cached {
        Some((t, v, animated, unchanged)) if t == tag => (v, animated, unchanged),
        _ => {
            let v: Vec<egui::TextureHandle> = std::iter::once(&pv.before)
                .chain(&pv.frames)
                .enumerate()
                .filter(|(_, px)| px.len() == size[0] * size[1] * 4)
                .map(|(i, px)| {
                    ctx.load_texture(format!("library-preview-{i}"), egui::ColorImage::from_rgba_unmultiplied(size, px), egui::TextureOptions::LINEAR)
                })
                .collect();
            let animated = pv.frames.windows(2).any(|w| w.first() != w.last());
            let unchanged = pv.frames.iter().all(|f| *f == pv.before);
            ctx.data_mut(|d| d.insert_temp(key, (tag, v.clone(), animated, unchanged)));
            (v, animated, unchanged)
        }
    };
    let (Some(before), true) = (tex.first(), tex.len() > 1) else { return };
    let frames = tex.get(1..).unwrap_or_default();
    let index = if animated { (ctx.input(|i| i.time) * pv.fps) as usize % frames.len() } else { 0 };
    let Some(after) = frames.get(index) else { return };
    if animated {
        ctx.request_repaint_after(std::time::Duration::from_secs_f64(1.0 / pv.fps.max(1.0)));
    }
    ui.add_space(6.0);
    let w = ui.available_width().min(480.0);
    let (rect, r) = ui.allocate_exact_size(vec2(w, w * pv.height as f32 / pv.width.max(1) as f32), egui::Sense::click_and_drag());
    reg(app, "settings.library.previewImage", &r, "Preview: drag to compare");
    let split = &mut app.dialog_state.library_split;
    if let Some(pos) = r.interact_pointer_pos() {
        *split = ((pos.x - rect.left()) / rect.width()).clamp(0.0, 1.0);
    }
    let x = rect.left() + rect.width() * *split;
    let uv = egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0));
    let painter = ui.painter_at(rect);
    painter.image(after.id(), rect, uv, Color32::WHITE);
    let left = egui::Rect::from_min_max(rect.min, egui::pos2(x, rect.bottom()));
    painter.with_clip_rect(left).image(before.id(), rect, uv, Color32::WHITE);
    painter.line_segment([egui::pos2(x, rect.top()), egui::pos2(x, rect.bottom())], egui::Stroke::new(2.0, Color32::WHITE));
    let font = egui::FontId::proportional(11.0);
    painter.text(rect.left_top() + vec2(6.0, 4.0), egui::Align2::LEFT_TOP, tr("Before"), font.clone(), Color32::WHITE);
    painter.text(rect.right_top() + vec2(-6.0, 4.0), egui::Align2::RIGHT_TOP, tr("After"), font, Color32::WHITE);
    let effect = &pv.manifest;
    let params: Vec<&str> = effect["params"].as_array().into_iter().flatten().filter_map(|p| p["name"].as_str()).collect();
    let mut line =
        tr_args("{} ({}) at default settings, on a sample frame", &[&effect["name"].as_str().unwrap_or(id), &effect["category"].as_str().unwrap_or("")]);
    if !params.is_empty() {
        line = format!("{line} · {}", tr_args("Parameters: {}", &[&params.join(", ")]));
    }
    ui.label(RichText::new(line).color(t.text_faint));
    if unchanged {
        ui.label(RichText::new(tr("At its default settings this effect leaves the sample frame unchanged.")).color(t.text_dim));
    }
}

/// Settings ▸ Disk ▸ Browser Storage (the web app's storage manager, `storage.*`): where the
/// data lives, the origin's usage and quota, persistent storage, and Clear buttons.
fn browser_storage_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, acts: &mut Vec<Act>) {
    let Some(host) = app.session.storage.clone() else { return };
    let info = host.info();
    let u = |v: &Value| v.as_u64().unwrap_or(0);
    ui.add_space(6.0);
    ui.label(RichText::new(tr("Browser Storage")).font(Tokens::semibold(12.5)).color(t.tab_text_active));
    ui.separator();
    let backend = match info["backend"].as_str().unwrap_or("") {
        "opfs" => "Origin Private File System",
        "indexeddb" => "IndexedDB",
        "memory" => "Memory only: nothing is kept after a reload",
        "" => "Unknown",
        other => other,
    };
    let r = ui.label(tr_args("Stored in: {}", &[&backend]));
    reg(app, "settings.storage.backend", &r, backend);
    let (usage, quota) = (u(&info["usage"]), u(&info["quota"]));
    let frac = if quota > 0 { usage as f32 / quota as f32 } else { 0.0 };
    let text = if quota > 0 { format!("{} of {} used ({:.1} %)", human_bytes(usage), human_bytes(quota), frac * 100.0) } else { "Usage unknown".into() };
    let r = ui
        .horizontal(|ui| {
            ui.add(egui::ProgressBar::new(frac.clamp(0.0, 1.0)).desired_width(220.0).desired_height(8.0));
            ui.label(&text);
        })
        .response;
    reg(app, "settings.storage.usage", &r, &text);
    let f = &info["files"];
    let row = |what: &str, k: &str| format!("{what}: {} ({})", u(&f[k]["count"]), human_bytes(u(&f[k]["bytes"])));
    ui.label(
        RichText::new(format!("{} · {} · {}", row(tr("Projects"), "projects"), row(tr("Imported media"), "media"), row(tr("Auto-saves"), "autoSaves")))
            .color(t.text_dim),
    );
    let d = &info["diskCache"];
    let dc = if d["enabled"].as_bool().unwrap_or(false) {
        format!(
            "Disk cache: {} frames, {} of {} (hits {}, misses {})",
            u(&d["entries"]),
            human_bytes(u(&d["bytes"])),
            human_bytes(u(&d["maxBytes"])),
            u(&d["hits"]),
            u(&d["misses"])
        )
    } else {
        "Disk cache: off".into()
    };
    let r = ui.label(RichText::new(&dc).color(t.text_dim));
    reg(app, "settings.storage.diskCache", &r, &dc);
    ui.horizontal(|ui| {
        let persisted = info["persisted"].as_bool().unwrap_or(false);
        let state = if persisted { "Persistent storage: granted" } else { "Persistent storage: not granted (the browser may clear it when space runs low)" };
        ui.label(state);
        if !persisted {
            let r = ui.button(tr("Request Persistent Storage"));
            reg(app, "settings.storage.persist", &r, "Request Persistent Storage");
            if r.clicked() {
                acts.push(Act::Run("storage.persist".into(), json!({})));
            }
        }
    });
    ui.horizontal_wrapped(|ui| {
        for (what, label) in
            [("diskCache", "Clear Disk Cache"), ("media", "Clear Imported Media"), ("autoSaves", "Clear Auto-Saves"), ("projects", "Clear Saved Projects")]
        {
            let r = ui.button(label);
            reg(app, &format!("settings.storage.clear.{what}"), &r, label);
            if r.clicked() {
                acts.push(Act::Run("storage.clear".into(), json!({"what": what})));
            }
        }
    });
    // The numbers refresh in the background.
    ui.ctx().request_repaint_after(std::time::Duration::from_secs(1));
}

/// Draw the editable label colors and names in Settings.
fn labels_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, cur: &Value, acts: &mut Vec<Act>) {
    ui.label(tr("Label Colors and Names:"));
    ui.add_space(4.0);
    let labels = cur.get("labels").and_then(Value::as_array).cloned().unwrap_or_default();
    egui::Grid::new("settings-labels").num_columns(4).spacing([10.0, 4.0]).show(ui, |ui| {
        for (i, l) in labels.iter().enumerate() {
            let c = crate::panels::viewer::hex_rgb(l["color"].as_str().unwrap_or("")).unwrap_or([128, 128, 128]);
            let mut f = c.map(|v| v as f32 / 255.0);
            let r = crate::widgets::srgb_color_button(ui, &mut f);
            let rgb = if r.changed() { srgb_bytes(f) } else { c };
            a11y::name_value(&r, &tr_args("Label {} color", &[&(i + 1)]), &hex(rgb));
            app.auto.add(&format!("settings.labels.{i}.color"), r.rect, "label colour");
            if rgb != c {
                acts.push(Act::Set(format!("labels.{i}.color"), json!(hex(rgb))));
            }
            let mut name = l["name"].as_str().unwrap_or("").to_string();
            let r = ui.add_sized(vec2(160.0, 20.0), egui::TextEdit::singleline(&mut name));
            a11y::text_input(&r, &tr_args("Label {} name", &[&(i + 1)]), &name);
            app.auto.add(&format!("settings.labels.{i}.name"), r.rect, "label name");
            if r.changed() {
                acts.push(Act::Set(format!("labels.{i}.name"), json!(name)));
            }
            if i % 2 == 1 {
                ui.end_row();
            }
        }
    });
    ui.add_space(8.0);
    let r = ui.button(tr("Reset Labels"));
    app.auto.add("settings.button.Reset Labels", r.rect, "Reset Labels");
    if r.clicked() {
        acts.push(Act::Run("prefs.reset".into(), json!({"page": "labels"})));
    }
}

/// Draw the Settings dialog, including its translated page list and the selected page's controls.
pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
    let all = pages();
    let mut page = app.dialog_state.settings_page.clone();
    if !all.iter().any(|p| p.id == page) {
        page = "general".into();
    }
    let idx = all.iter().position(|p| p.id == page).unwrap_or(0);
    let cur = serde_json::to_value(&app.session.prefs).unwrap_or_default();
    let mut acts: Vec<Act> = vec![];
    let (mut ok, mut cancel) = (false, false);
    super::dialogs::modal_fit(ctx, "Settings", vec2(820.0, 600.0), t, |ui, height| {
        // The buttons go first and the pages get the height they leave: at a high UI Scale the
        // pages scroll and the buttons stay in view.
        let (mut body, body_h) = super::dialogs::above_footer(ui, height, |foot| {
            foot.horizontal(|ui| {
                let r = ui.add_enabled(idx > 0, egui::Button::new(tr("Previous")));
                app.auto.add("settings.previous", r.rect, "Previous");
                if r.clicked() {
                    page = all[idx - 1].id.to_string();
                }
                let r = ui.add_enabled(idx + 1 < all.len(), egui::Button::new(tr("Next")));
                app.auto.add("settings.next", r.rect, "Next");
                if r.clicked() {
                    page = all[idx + 1].id.to_string();
                }
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    let r = ui.add(egui::Button::new(RichText::new(tr("   OK   ")).color(Color32::WHITE)).fill(t.accent));
                    app.auto.add("settings.ok", r.rect, "OK");
                    ok |= r.clicked();
                    let r = ui.button(tr("Cancel"));
                    app.auto.add("settings.cancel", r.rect, "Cancel");
                    cancel |= r.clicked();
                });
            });
            foot.separator();
        });
        {
            let ui = &mut body;
            // Page list (left, like After Effects).
            let widest_page = all
                .iter()
                .map(|p| ui.painter().layout_no_wrap(crate::i18n::settings_page_title(p.title).to_owned(), Tokens::ui(12.5), t.text).size().x)
                .fold(0.0_f32, f32::max);
            let page_width = (widest_page + 20.0).max(176.0).min((ui.available_width() * 0.42).min(300.0));
            ui.vertical(|ui| {
                ui.set_width(page_width + 4.0);
                egui::ScrollArea::vertical().id_salt("settings-pages").max_height(body_h).auto_shrink([false, false]).show(ui, |ui| {
                    // One Tab stop for the list (the open page); Up / Down / Home / End move
                    // between the pages and the focus goes with them.
                    // The rows touch, so the pointer never falls between two of them.
                    ui.spacing_mut().item_spacing.y = 0.0;
                    let ctx = ui.ctx().clone();
                    let page_item = |id: &str| egui::Id::unique(("settings-page", id.to_string()));
                    let on_list = all.iter().any(|q| ctx.memory(|m| m.has_focus(page_item(q.id))));
                    for (n, p) in all.iter().enumerate() {
                        let sel = page == p.id;
                        let r = ui.allocate_space(vec2(page_width, 24.0)).1;
                        let resp = ui.interact(r, page_item(p.id), if sel { egui::Sense::click() } else { egui::Sense::CLICK });
                        a11y::list_item(&resp, crate::i18n::settings_page_title(p.title), sel, n + 1, all.len());
                        if a11y::take_pending_focus(&ctx, "settings", resp.id) {
                            resp.request_focus();
                            resp.scroll_to_me(None);
                        }
                        if sel {
                            ui.painter().rect_filled(r, 3.0, t.accent);
                        } else if resp.hovered() {
                            ui.painter().rect_filled(r, 3.0, t.hover);
                        }
                        crate::widgets::text_fit(
                            ui.painter(),
                            r.left_center() + vec2(10.0, 0.0),
                            egui::Align2::LEFT_CENTER,
                            crate::i18n::settings_page_title(p.title),
                            Tokens::ui(12.5),
                            (page_width - 20.0).max(0.0),
                            if sel { Color32::WHITE } else { t.text },
                        );
                        if resp.has_focus() {
                            ui.painter().rect_stroke(r.shrink(1.0), 3.0, egui::Stroke::new(2.0, t.text), egui::StrokeKind::Inside);
                        }
                        app.auto.add(&format!("settings.page.{}", p.id), r, p.title);
                        if resp.on_hover_text(crate::i18n::settings_page_title(p.title)).clicked() {
                            page = p.id.to_string();
                        }
                    }
                    if on_list {
                        let at = all.iter().position(|q| q.id == page).unwrap_or(0);
                        let last = all.len().saturating_sub(1);
                        let mut to = at;
                        for key in ctx.input(|i| {
                            i.events
                                .iter()
                                .filter_map(|e| match e {
                                    egui::Event::Key { key, pressed: true, modifiers, .. } if !modifiers.any() => Some(*key),
                                    _ => None,
                                })
                                .collect::<Vec<_>>()
                        }) {
                            to = match key {
                                egui::Key::ArrowDown => to.saturating_add(1).min(last),
                                egui::Key::ArrowUp => to.saturating_sub(1),
                                egui::Key::Home => 0,
                                egui::Key::End => last,
                                _ => to,
                            };
                        }
                        if to != at
                            && let Some(q) = all.get(to)
                        {
                            page = q.id.to_string();
                            a11y::focus_next_pass(&ctx, "settings", page_item(q.id));
                        }
                        // The arrows are the list's, not egui's spatial focus moves.
                        ctx.memory_mut(|m| m.move_focus(egui::FocusDirection::None));
                    }
                });
            });
            ui.separator();
            ui.vertical(|ui| {
                // The rest of the dialog's width: a fixed width ran past it, so the whole
                // dialog scrolled sideways a little (#284).
                ui.set_width(ui.available_width());
                let p = &all[idx];
                ui.label(RichText::new(crate::i18n::settings_page_title(p.title)).font(Tokens::semibold(14.0)).color(t.tab_text_active));
                ui.add_space(6.0);
                egui::ScrollArea::vertical().max_height((body_h - 30.0).max(0.0)).auto_shrink([false, false]).show(ui, |ui| {
                    page_ui(app, ui, p, &cur, t, &mut acts);
                });
            });
        }
    });
    app.dialog_state.settings_page = page;
    for a in acts {
        match a {
            Act::Set(k, v) => {
                if let Err(e) = app.session.prefs.set(&k, v) {
                    app.ui.status = e;
                }
                app.session.prefs_changed();
            }
            Act::Run(cmd, params) => {
                if let Err(e) = crate::menus::invoke(app, ctx, &cmd, params) {
                    app.ui.status = e;
                }
                // Buttons such as Reset Settings change the baseline Cancel returns to.
                if cmd.starts_with("prefs.") {
                    app.dialog_state.prefs_snapshot = Some(app.session.prefs.clone());
                }
                if app.dialog != Some(Dialog::Settings) && app.dialog != Some(Dialog::Info) {
                    app.dialog = Some(Dialog::Settings);
                }
            }
            Act::PickFolder(k) => {
                if let Some(f) = app.hooks.pick_folder.as_ref().and_then(|f| f()) {
                    let _ = app.session.prefs.set(&k, json!(f));
                    app.session.prefs_changed();
                }
            }
            Act::PickProject(k) => {
                if let Some(f) = app.hooks.pick_open_project.as_ref().and_then(|f| f()) {
                    let _ = app.session.prefs.set(&k, json!(f));
                    app.session.prefs_changed();
                }
            }
            Act::PickModel(task) => {
                // The registry's file types for the task (`.pt`, `.task`).
                let mut exts: Vec<&str> = effectcraft_engine::segment::models(task).filter_map(|m| m.file_name.rsplit_once('.').map(|x| x.1)).collect();
                exts.dedup();
                let picked = app.hooks.pick_files.as_ref().map(|f| f(&exts)).unwrap_or_default();
                let cmd = if task == Task::Face { "face.model.install" } else { "roto.model.install" };
                if let Some(path) = picked.into_iter().next()
                    && let Err(e) = crate::menus::invoke(app, ctx, cmd, json!({"path": path}))
                {
                    app.ui.status = e;
                }
            }
        }
    }
    if cancel {
        if let Some(p) = app.dialog_state.prefs_snapshot.take() {
            app.session.prefs = p;
            app.session.prefs_changed();
        }
        app.dialog = None;
    } else if ok {
        app.dialog_state.prefs_snapshot = None;
        app.session.save_prefs();
        app.dialog = None;
    }
}

/// Crash recovery: the previous session didn't exit cleanly; offer its latest auto-save.
pub fn recovery(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
    let Some(r) = app.recovery.clone() else {
        app.dialog = None;
        return;
    };
    let mut choice: Option<&str> = None;
    super::dialogs::modal(ctx, "Recover Project", vec2(560.0, 230.0), t, |ui| {
        ui.label(tr("EffectCraft didn't quit normally last time."));
        ui.add_space(6.0);
        if let Some(p) = &r.project {
            ui.label(tr_args("Project: {}", &[&p]));
        }
        if let Some(a) = &r.autosave {
            let when = std::fs::metadata(a).and_then(|m| m.modified()).ok().and_then(|m| m.elapsed().ok()).map(|d| {
                let m = d.as_secs() / 60;
                if m < 1 {
                    "less than a minute ago".to_string()
                } else if m < 120 {
                    tr_args("{} minutes ago", &[&m])
                } else {
                    tr_args("{} hours ago", &[&(m / 60)])
                }
            });
            ui.label(tr_args("Latest auto-save: {}", &[&a]));
            if let Some(w) = when {
                ui.label(RichText::new(tr_args("Saved {}", &[&w])).color(t.text_dim));
            }
        }
        ui.add_space(14.0);
        ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
            let b = ui.add(egui::Button::new(RichText::new(tr(" Open Auto-Save ")).color(Color32::WHITE)).fill(t.accent));
            app.auto.add("recovery.openAutoSave", b.rect, "Open Auto-Save");
            if b.clicked() {
                choice = Some("autosave");
            }
            if r.project.is_some() {
                let b = ui.button(tr("Open Last Saved Project"));
                app.auto.add("recovery.openProject", b.rect, "Open Last Saved Project");
                if b.clicked() {
                    choice = Some("project");
                }
            }
            let b = ui.button(tr("Don't Recover"));
            app.auto.add("recovery.dismiss", b.rect, "Don't Recover");
            if b.clicked() {
                choice = Some("none");
            }
        });
    });
    if choice.is_none() && super::dialogs::ui_enter(ctx) {
        choice = Some("autosave");
    }
    let Some(c) = choice else { return };
    app.dialog = None;
    app.recovery = None;
    let path = match c {
        "autosave" => r.autosave,
        "project" => r.project,
        _ => None,
    };
    if let Some(p) = path
        && let Err(e) = crate::menus::invoke(app, ctx, "file.open", json!({"path": p}))
    {
        app.ui.status = e;
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;

    /// Install-only models must not offer a download that the command will reject.
    #[test]
    fn sam2_settings_offer_file_installation_without_downloads() {
        let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
        let ctx = egui::Context::default();
        let tokens = app.tokens;
        let mut actions = vec![];
        let mut output = ctx
            .run_ui(egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(900.0, 2200.0))), ..Default::default() }, |ui| {
                models_ui(&mut app, ui, &tokens, &mut actions, Task::Mask)
            });
        output.textures_delta.clear();
        for id in ["sam2.1-hiera-tiny", "sam2.1-hiera-small", "sam2.1-hiera-base-plus", "sam2.1-hiera-large"] {
            assert!(app.auto.find(&format!("settings.roto.download.{id}")).is_none());
            assert!(app.auto.find(&format!("settings.roto.install.{id}")).is_some());
            assert_eq!(app.auto.find(&format!("settings.roto.source.{id}")).unwrap().label, "model.safetensors");
        }
        assert!(actions.is_empty());
    }

    /// Settings ▸ Disk shows the disk cache's limit in effect under Maximum Disk Cache Size, and
    /// says when free disk space holds it lower (a 20 GB cache held at 0 showed nothing).
    #[test]
    fn the_disk_page_shows_the_limit_free_space_allows() {
        use effectcraft_engine::render::disk_cache::DiskCache;
        const GB: u64 = 1 << 30;
        let dir = std::env::temp_dir().join(format!("ec-settings-disk-limit-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
        let dc = DiskCache::open(&dir, 20 * GB).unwrap();
        app.session.disk_cache = Some(dc.clone());
        let page = pages().into_iter().find(|p| p.id == "disk").unwrap();
        let shown = |app: &mut EffectcraftApp| {
            // (one frame's elements: `find` returns the first match)
            app.auto = Default::default();
            let ctx = egui::Context::default();
            let (tokens, cur) = (app.tokens, serde_json::to_value(&app.session.prefs).unwrap());
            let mut actions = vec![];
            let mut output = ctx
                .run_ui(egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(900.0, 1200.0))), ..Default::default() }, |ui| {
                    page_ui(app, ui, &page, &cur, &tokens, &mut actions)
                });
            output.textures_delta.clear();
            app.auto.find("settings.disk.cacheLimit").map(|e| e.label.clone())
        };
        let text = || disk_cache_limit_lines(dc.max_bytes(), dc.limit_bytes(), dc.reserve_bytes(), dc.accepts_writes()).join(" ");
        // A 4 TB disk with 190 GB free: the 20 GB setting holds.
        dc.set_disk_space(Some((4096 * GB, 190 * GB)));
        let label = shown(&mut app).expect("the limit is shown");
        assert_eq!(label, "limitMb=20480 maxMb=20480 reserveMb=10240 spaceLimited=false writes=true");
        assert_eq!(text(), "Disk cache limit in effect: 20480 MB");
        // 3 GB free on a 4 TB disk: 10 GB stay free, nothing is left for the cache.
        dc.set_disk_space(Some((4096 * GB, 3 * GB)));
        assert_eq!(shown(&mut app).unwrap(), "limitMb=0 maxMb=20480 reserveMb=10240 spaceLimited=true writes=false");
        assert_eq!(
            text(),
            "Disk cache limit in effect: 0 MB Limited by free disk space: EffectCraft keeps 10.0 GB of this disk free. No room left: nothing is written to the disk cache until disk space is freed."
        );
        // 12 GB free: 2 GB for the cache.
        dc.set_disk_space(Some((4096 * GB, 12 * GB)));
        assert_eq!(shown(&mut app).unwrap(), "limitMb=2048 maxMb=20480 reserveMb=10240 spaceLimited=true writes=true");
        assert_eq!(text(), "Disk cache limit in effect: 2048 MB Limited by free disk space: EffectCraft keeps 10.0 GB of this disk free.");
        // A 100 GB disk keeps 5 GB.
        dc.set_disk_space(Some((100 * GB, 6 * GB)));
        assert_eq!(shown(&mut app).unwrap(), "limitMb=1024 maxMb=20480 reserveMb=5120 spaceLimited=true writes=true");
        // Without the disk's size, the rule from the constants.
        assert_eq!(
            disk_cache_limit_lines(20 * GB, GB, None, true)[1],
            "Limited by free disk space: EffectCraft keeps 5% of the disk free (at least 2 GB, at most 10 GB)."
        );
        // No disk cache: no line.
        app.session.disk_cache = None;
        assert!(shown(&mut app).is_none());
        drop(dc);
        let _ = std::fs::remove_dir_all(&dir);
    }
}