diff --git a/crates/ui-egui/src/dialogs/graph_data.rs b/crates/ui-egui/src/dialogs/graph_data.rs index b42f240922..9cd2da74a4 100644 --- a/crates/ui-egui/src/dialogs/graph_data.rs +++ b/crates/ui-egui/src/dialogs/graph_data.rs @@ -400,7 +400,7 @@ fn graph_grid(app: &mut VectorcraftApp, graph: Option) -> Result bool { file_asks::can(app, craft::host::PickKind::Open) - && (app.host.files().browser_saves() || app.services.read_head.is_some() || app.services.read.is_some()) + && (app.files_host().files().browser_saves() || app.services.read_head.is_some() || app.services.read.is_some()) } /// Import Data's answer: a tab-separated text file's rows into the sheet that asked for it. @@ -1148,8 +1148,8 @@ mod tests { app.services.read = None; app.services.read_head = None; let files = Arc::new(ScriptedFiles::held().in_browser()); + // The web shell installs its browser file service in the window's context only. app.host.with_files(files.clone()).install(app.ui_ctx.as_ref().unwrap()); - app.host = Arc::new(app.host.with_files(files.clone())); open(&mut app).unwrap(); let mut h = harness(app); h.run_steps(4); diff --git a/crates/ui-egui/src/dialogs/proof_setup.rs b/crates/ui-egui/src/dialogs/proof_setup.rs index 5bd069d011..3338d9b7bb 100644 --- a/crates/ui-egui/src/dialogs/proof_setup.rs +++ b/crates/ui-egui/src/dialogs/proof_setup.rs @@ -298,7 +298,7 @@ fn save(app: &mut VectorcraftApp, fields: &DialogFields) { if let Some(o) = p.as_object_mut() { o.remove("proof"); } - if app.host.files().browser_saves() { + if app.files_host().files().browser_saves() { match proof_file::draft_bytes(&p) { Ok(bytes) => { crate::file_asks::save(app, request, bytes, Owner::Free, |app, picked| { @@ -871,7 +871,9 @@ mod tests { fn saving_a_browser_proof_setup_gives_the_file_service_the_draft_bytes() { let _view = DefaultProofView::new(); let (mut app, ctx) = app(); - let files = scripted(&mut app, ScriptedFiles::held().in_browser()); + let files = Arc::new(ScriptedFiles::held().in_browser()); + // The web shell installs its browser file service in the window's context only. + crate::tests_picks::install_window_files(&mut app, files.clone()); app.run("ui.proofSetupDialog", json!({})).unwrap(); for (k, v) in [("intent", json!("saturation")), ("preview", json!(false))] { set(&mut app, &ctx, k, v); diff --git a/crates/ui-egui/src/dialogs/relink.rs b/crates/ui-egui/src/dialogs/relink.rs index 2ee70056be..ecf3c9d4ec 100644 --- a/crates/ui-egui/src/dialogs/relink.rs +++ b/crates/ui-egui/src/dialogs/relink.rs @@ -598,6 +598,8 @@ mod tests { let (mut app, dir) = setup("all", &["photo.png", "other.png"]); let (a, b, c) = (place(&mut app, &dir.join("photo.png")), place(&mut app, &dir.join("photo.png")), place(&mut app, &dir.join("other.png"))); app.run("select.set", json!({"ids": [a]})).unwrap(); + assert!(!crate::menus::enabled(&app, "ui.relinkDialog"), "Relink… is dimmed without a picker"); + crate::tests::picks::install_files(&mut app, std::sync::Arc::new(craft::host::ScriptedFiles::held())); assert!(crate::menus::enabled(&app, "ui.relinkDialog")); app.run("ui.relinkDialog", json!({})).unwrap(); let f = fields(&app); diff --git a/crates/ui-egui/src/file_asks.rs b/crates/ui-egui/src/file_asks.rs index f1e90aea30..d06eb41921 100644 --- a/crates/ui-egui/src/file_asks.rs +++ b/crates/ui-egui/src/file_asks.rs @@ -111,9 +111,12 @@ pub(crate) fn as_automated(app: &mut VectorcraftApp, automated: bool, f: impl r } -/// Whether a dialog for `kind` can open: every kind on the desktop, only the opening ones on the web. +/// Whether a dialog for `kind` can open: every kind on the desktop, only the opening ones on the web, +/// and none when the file service ([`VectorcraftApp::files_host`]) has no picker (every pick answers Cancelled). pub(crate) fn can(app: &VectorcraftApp, kind: PickKind) -> bool { - matches!(kind, PickKind::Open | PickKind::OpenMany) || !crate::io::is_web(app) + let host = app.files_host(); + let files = host.files(); + (files.can_pick() || files.browser_saves()) && (matches!(kind, PickKind::Open | PickKind::OpenMany) || !crate::io::is_web(app)) } /// Show `request` and run `then` with the answer. Returns `{pending: "fileDialog"}`, or `Null` when @@ -384,9 +387,50 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { #[cfg(test)] mod tests { + use std::sync::Arc; + use super::*; use vectorcraft_engine::Session; + #[test] + fn no_dialog_can_open_where_the_file_service_has_no_picker() { + const KINDS: [PickKind; 4] = [PickKind::Open, PickKind::OpenMany, PickKind::Save, PickKind::Folder]; + let can_each = |app: &VectorcraftApp| KINDS.map(|kind| can(app, kind)); + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + assert!(!app.host.files().can_pick() && !app.host.files().browser_saves(), "the default service has no picker"); + assert_eq!(can_each(&app), [false; 4], "every pick would answer Cancelled"); + crate::tests::picks::scripted(&mut app); + assert_eq!(can_each(&app), [true; 4], "a desktop picker opens every kind"); + let mut web = VectorcraftApp::new(Session::new(), Default::default()); + web.services.download = Some(Box::new(|_, _| Ok(()))); + crate::tests::picks::scripted_browser(&mut web); + assert_eq!(can_each(&web), [true, true, false, false], "a browser picker only opens files"); + } + + #[test] + fn the_web_shells_browser_picker_in_the_window_opens_files() { + let mut web = VectorcraftApp::new(Session::new(), Default::default()); + web.services.download = Some(Box::new(|_, _| Ok(()))); + crate::tests::picks::scripted_web(&mut web); + assert!(!web.host.files().can_pick() && !web.host.files().browser_saves(), "the app's own host keeps the default service"); + assert_eq!([can(&web, PickKind::Open), can(&web, PickKind::OpenMany)], [true; 2], "the window's browser picker opens files"); + assert_eq!([can(&web, PickKind::Save), can(&web, PickKind::Folder)], [false; 2], "the web opens no save or folder dialog"); + } + + #[test] + fn the_desktop_picker_opens_every_kind_before_and_after_the_window_installs_it() { + const KINDS: [PickKind; 4] = [PickKind::Open, PickKind::OpenMany, PickKind::Save, PickKind::Folder]; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + let files: Arc = Arc::new(craft::host::ScriptedFiles::held()); + app.host = Arc::new(app.host.with_files(files)); + let ctx = crate::testing::ui_context(); + app.ui_ctx = Some(ctx.clone()); + assert!(Host::try_of_ctx(&ctx).is_none(), "the window has no host yet"); + assert_eq!(KINDS.map(|kind| can(&app, kind)), [true; 4], "the app's own host answers until the window has one"); + Host::clone(&app.host).install(&ctx); + assert_eq!(KINDS.map(|kind| can(&app, kind)), [true; 4], "the window's host is the app's own on the desktop"); + } + #[test] fn an_svg_picker_returns_to_its_opening_tab_only_if_that_drawing_was_not_reverted() { const SVG: &[u8] = br#""#; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 011ff5475f..a51817a7f1 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -404,4 +404,10 @@ impl VectorcraftApp { (None, None) => craft::host::default_jobs(), } } + + /// The host whose file service picks and saves: the one the window's context installed (the + /// web shell installs its browser file service there only), else the app's own host. + pub(crate) fn files_host(&self) -> Arc { + self.ui_ctx.as_ref().and_then(Host::try_of_ctx).unwrap_or_else(|| self.host.clone()) + } } diff --git a/crates/ui-egui/src/panels/actions/tests_menu.rs b/crates/ui-egui/src/panels/actions/tests_menu.rs index bbda7d1991..d036d98c99 100644 --- a/crates/ui-egui/src/panels/actions/tests_menu.rs +++ b/crates/ui-egui/src/panels/actions/tests_menu.rs @@ -56,6 +56,8 @@ fn the_panel_menu_states_follow_selection_recording_and_playback() { } let mut app = with_sets(); + // Replace Actions… asks for a file: the app has a picker. + crate::tests::picks::install_files(&mut app, std::sync::Arc::new(craft::host::ScriptedFiles::held())); for (command, want) in [(C::Batch, true), (C::Play, true), (C::InsertStop, true), (C::Duplicate, true), (C::Replace, true), (C::ClearAll, true)] { assert_eq!(enabled(&app, command), want, "an action selected: {command:?}"); } diff --git a/crates/ui-egui/src/panels/library_panel.rs b/crates/ui-egui/src/panels/library_panel.rs index 02db69a70a..b3534b75bd 100644 --- a/crates/ui-egui/src/panels/library_panel.rs +++ b/crates/ui-egui/src/panels/library_panel.rs @@ -22,7 +22,7 @@ use std::collections::HashMap; use std::rc::Rc; use craft::behavior::{IconId, Selection, SelectionModel}; -use craft::host::PickRequest; +use craft::host::{PickKind, PickRequest}; use craft::theme::variant::Tone; use craft::ui::contract::{Bind, Entry, MemoKey, MemoProps, SelectMode, Show as _, ShowContainer as _, Trigger}; use craft::ui::menu::{Menu, MenuItem, PanelRow}; @@ -805,12 +805,12 @@ fn footer(app: &mut VectorcraftApp, ui: &mut Ui, id: &str, names } /// The body of the panel of library `id` when the library is missing ([`missing_file`]): its file, -/// and Locate Library…. +/// and Locate Library…, dimmed where the app has no open dialog. fn missing(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) { let Some(file) = missing_file(app, kind_of::(), id) else { return }; let text = crate::i18n::fmt(tl!("Missing: {path}"), &[("path", &file)]); - // PLAN: `NoticeAction` has no disabled state; where the app has no picker, Locate reports it in the status bar. - let out = Notice::new(Tone::Neutral, text).layout(NoticeLayout::Centred).action(NoticeAction::new(tl!("Locate Library…"))).show(ui); + let locate = NoticeAction::new(tl!("Locate Library…")).enabled(file_asks::can(app, PickKind::Open)); + let out = Notice::new(Tone::Neutral, text).layout(NoticeLayout::Centred).action(locate).show(ui); if out.action == Some(0) && let Err(e) = app.run("ui.libraryPanel.locate", json!({ "panel": panel_id(K::KIND, id) })) { diff --git a/crates/ui-egui/src/panels/library_panel/tests.rs b/crates/ui-egui/src/panels/library_panel/tests.rs index 284b4a559e..06c48036b2 100644 --- a/crates/ui-egui/src/panels/library_panel/tests.rs +++ b/crates/ui-egui/src/panels/library_panel/tests.rs @@ -6,7 +6,7 @@ use vectorcraft_engine::Session; use super::*; use crate::i18n::LangExt as _; use crate::panels::swatches::SwatchLibraries; -use crate::tests::picks::{Picker, answer_dialog, open_dialogs, scripted}; +use crate::tests::picks::{Picker, answer_dialog, open_dialogs, scripted, scripted_browser}; fn app() -> VectorcraftApp { let mut app = VectorcraftApp::new(Session::new(), Default::default()); @@ -577,11 +577,13 @@ fn a_persistent_panel_whose_file_is_gone_is_listed_as_missing_until_located() { assert!(text.contains(&shown), "{text}"); let ctx = context(); let body = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, &brand); - // Without a picker, Locate Library… reports it and the panel stays missing. - let (_, at) = control(&mut next, &ctx, &body, "Locate Library…"); + // Without a picker, Locate Library… is dimmed, a click on it does nothing and the panel stays missing. + next.ui.status.clear(); + let (enabled, at) = control(&mut next, &ctx, &body, "Locate Library…"); + assert!(!enabled, "Locate Library… is dimmed without a picker"); click_at(&mut next, &ctx, &body, at); draw_frame(&mut next, &ctx, &body, vec![]); - assert!(!next.ui.status.is_empty() && is_open(&next.ui, &brand), "{}", next.ui.status); + assert!(next.ui.status.is_empty() && is_open(&next.ui, &brand), "{}", next.ui.status); assert!(neighbour::(&next, gone, 1).is_none(), "no library to step from"); let mut next = restarted(&next); assert!(is_open(&next.ui, &brand), "still listed after another restart"); @@ -591,8 +593,10 @@ fn a_persistent_panel_whose_file_is_gone_is_listed_as_missing_until_located() { std::fs::write(&moved, palette).unwrap(); let picked = moved.to_string_lossy().into_owned(); let picker = scripted(&mut next); - let (_, at) = control(&mut next, &ctx, &body, "Locate Library…"); + let (enabled, at) = control(&mut next, &ctx, &body, "Locate Library…"); + assert!(enabled, "Locate Library… is available with a picker"); click_at(&mut next, &ctx, &body, at); + assert_eq!(picker.requests().len(), 1, "the click opens the picker"); // The picker's answer arrives on a later pass. picker.answer(&[&picked]); let window = next.ui_ctx.clone().expect("a window context"); @@ -623,7 +627,10 @@ fn locate_library_loads_browser_contents_into_the_missing_panel() { let mut app = restarted(&app); tidy(&mut app); app.services.download = Some(Box::new(|_, _| Ok(()))); - let picker = scripted(&mut app); + let picker = scripted_browser(&mut app); + let body = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, &missing); + let (enabled, _) = control(&mut app, &context(), &body, "Locate Library…"); + assert!(enabled, "a browser picker reads the library's contents"); assert_eq!(app.run("ui.libraryPanel.locate", json!({"panel": missing})), Ok(json!({"pending": "fileDialog"}))); picker.answer_contents(&[("Replacement.gpl", palette)]); let ctx = app.ui_ctx.clone().unwrap(); @@ -636,6 +643,64 @@ fn locate_library_loads_browser_contents_into_the_missing_panel() { assert!(SwatchLibraries::get(&app, &panel.id).is_some_and(|(_, lib)| lib.swatch("New Red").is_some())); } +/// The node screen readers name `label` in the frame `out`, and whether that frame focused it. +fn focused(out: &egui::FullOutput, label: &str) -> (bool, bool) { + let update = out.platform_output.accesskit_update.as_ref().expect("an accessibility tree"); + let node = update.nodes.iter().find(|(_, n)| n.label().map(str::trim) == Some(label)); + (node.is_some(), node.is_some_and(|(id, _)| *id == update.focus)) +} + +#[test] +fn locate_library_follows_the_web_shells_browser_picker_for_every_missing_library() { + let dir = std::env::temp_dir().join(format!("vc-web-locate-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let mut app = app(); + let mut panels = Vec::new(); + for name in ["Brand", "Spring"] { + let gpl = dir.join(format!("{name}.gpl")); + std::fs::write(&gpl, format!("GIMP Palette\nName: {name}\n#\n255 0 0\tRed\n")).unwrap(); + let panel = panel_of(app.run("window.swatchLibrary.other", json!({"path": gpl.to_string_lossy()})).unwrap()); + app.run("ui.libraryPanel", json!({"panel": panel, "persistent": true})).unwrap(); + panels.push(panel); + } + std::fs::remove_dir_all(&dir).unwrap(); + let mut app = restarted(&app); + tidy(&mut app); + assert!(panels.iter().all(|p| is_open(&app.ui, p)), "{}", app.ui.status); + let tab = Event::Key { key: egui::Key::Tab, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }; + for panel in &panels { + let body = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, panel); + let ctx = context(); + assert!(!control(&mut app, &ctx, &body, "Locate Library…").0, "{panel}: dimmed without a picker"); + for _ in 0..4 { + let out = draw_frame(&mut app, &ctx, &body, vec![tab.clone()]); + assert_eq!(focused(&out, "Locate Library…"), (true, false), "{panel}: Tab does not focus the dimmed button"); + } + } + // The web shell installs its browser file service in the window's context only. + app.services.download = Some(Box::new(|_, _| Ok(()))); + let picker = crate::tests::picks::scripted_web(&mut app); + for panel in &panels { + let body = |app: &mut VectorcraftApp, ui: &mut Ui| show(app, ui, panel); + let ctx = context(); + let (enabled, at) = control(&mut app, &ctx, &body, "Locate Library…"); + assert!(enabled, "{panel}: the window's browser picker reads the library's contents"); + let mut reached = false; + for _ in 0..4 { + reached |= focused(&draw_frame(&mut app, &ctx, &body, vec![tab.clone()]), "Locate Library…").1; + } + assert!(reached, "{panel}: Tab reaches the available button"); + click_at(&mut app, &ctx, &body, at); + let kind = picker.requests().last().map(|r| r.kind); + assert!(matches!(kind, Some(craft::host::PickKind::Open)), "{panel}: the click opens the picker"); + picker.cancel(); + let window = app.ui_ctx.clone().unwrap(); + crate::tests::picks::poll(&mut app, &window); + } + assert_eq!(picker.requests().len(), panels.len(), "one pick for each missing library"); +} + #[test] fn locating_a_library_open_in_its_own_panel_brings_that_panel_forward() { let dir = std::env::temp_dir().join(format!("vc-locate-open-library-{}", std::process::id())); diff --git a/crates/ui-egui/src/panels/tests_links.rs b/crates/ui-egui/src/panels/tests_links.rs index d6be853ad4..71c9033b8d 100644 --- a/crates/ui-egui/src/panels/tests_links.rs +++ b/crates/ui-egui/src/panels/tests_links.rs @@ -1076,6 +1076,7 @@ fn switching_documents_dismisses_a_links_row_menu() { fn right_click_context_menu_in_links_panel() { let dir = Folder::new("context-menu"); let (mut app, linked, embedded) = app(&dir); + crate::tests::picks::install_files(&mut app, std::sync::Arc::new(craft::host::ScriptedFiles::held())); let revealed: Rc>> = Rc::default(); let rev = revealed.clone(); diff --git a/crates/ui-egui/src/panels/variables.rs b/crates/ui-egui/src/panels/variables.rs index 30a840d7b9..ae54a5aac5 100644 --- a/crates/ui-egui/src/panels/variables.rs +++ b/crates/ui-egui/src/panels/variables.rs @@ -710,7 +710,7 @@ fn ask_open(app: &mut VectorcraftApp, filters: &[(&str, &[&str])], command: &'st /// command and give the bytes to the file service for a save or download. fn ask_save(app: &mut VectorcraftApp, name: &str, filters: &[(&str, &[&str])], command: &'static str, field: &str) -> Value { let request = filters.iter().fold(PickRequest::save("variables").file_name(name), |r, (name, exts)| r.filter(*name, exts)); - if !app.host.files().browser_saves() { + if !app.files_host().files().browser_saves() { return file_asks::ask(app, request, Owner::Document, move |app, picked| match picked.first_path() { Some(path) => app.run(command, json!({"path": path.to_string_lossy()})), None => Ok(Value::Null), @@ -810,7 +810,8 @@ mod tests { let mut app = app_with_data(); let revision = app.session.active().unwrap().revision; let files = Arc::new(craft::host::ScriptedFiles::held().in_browser()); - crate::tests::picks::install_files(&mut app, files.clone()); + // The web shell installs its browser file service in the window's context only. + crate::tests::picks::install_window_files(&mut app, files.clone()); run(&mut app, command, &json!({})).unwrap().unwrap(); files.answer(craft::host::Picked::Saved { path: path.into(), file_type: None }); crate::file_asks::poll(&mut app); diff --git a/crates/ui-egui/src/tests/picks.rs b/crates/ui-egui/src/tests/picks.rs index 1d521af1c0..50f14a1ee8 100644 --- a/crates/ui-egui/src/tests/picks.rs +++ b/crates/ui-egui/src/tests/picks.rs @@ -74,11 +74,36 @@ pub(crate) fn scripted_sheets(app: &mut VectorcraftApp, on: bool) -> Picker { scripted_files(app, ScriptedFiles::held().sheets(on)) } +/// [`scripted`] with a browser's file service: picks answer names and contents, not paths. +pub(crate) fn scripted_browser(app: &mut VectorcraftApp) -> Picker { + scripted_files(app, ScriptedFiles::held().in_browser()) +} + /// [`scripted`] with `files` as the app's file service: the app's host carries it, and so does the /// window's context, as a frame installs the app's host there. fn scripted_files(app: &mut VectorcraftApp, files: ScriptedFiles) -> Picker { let files = Arc::new(files); install_files(app, files.clone()); + picker(app, files) +} + +/// [`scripted_browser`] installed as the web shell installs its file service: in the window's context only, while +/// the app's own host keeps its default service, which has no picker. +pub(crate) fn scripted_web(app: &mut VectorcraftApp) -> Picker { + let files = Arc::new(ScriptedFiles::held().in_browser()); + install_window_files(app, files.clone()); + picker(app, files) +} + +/// Makes `files` the file service of `app`'s window's context only, as the web shell installs its browser service; +/// the app's own host keeps its service. An app without a window gets one. +pub(crate) fn install_window_files(app: &mut VectorcraftApp, files: Arc) { + let ctx = app.ui_ctx.get_or_insert_with(crate::testing::ui_context).clone(); + app.host.with_files(files).install(&ctx); +} + +/// The [`Picker`] of `files`, with the reader and writer [`scripted`] describes. +fn picker(app: &mut VectorcraftApp, files: Arc) -> Picker { let written = Rc::new(RefCell::new(Vec::new())); let w = written.clone(); app.services.read.get_or_insert_with(|| Box::new(|_: &str| Ok(SVG.to_vec())));