diff --git a/crates/ui-egui/src/io/open.rs b/crates/ui-egui/src/io/open.rs index ace4f2694e..370fc9c069 100644 --- a/crates/ui-egui/src/io/open.rs +++ b/crates/ui-egui/src/io/open.rs @@ -247,6 +247,8 @@ fn try_activate_open_document(app: &mut VectorcraftApp, path: &str) -> Result Result /// [`open_path`]; with `background`, a document (not a file that installs, imports or asks /// first) is decoded off the UI thread ([`crate::opening::start`]). pub(crate) fn open_file(app: &mut VectorcraftApp, path: &str, background: bool) -> Result { + // Automation confined to some folders reaches no file outside them, open in a tab or not. + vectorcraft_engine::file_access::check_read(path)?; // File › Open, Open Recent, the Home screen, a drop and the Finder all focus a document that - // is already open, or still being read, instead of opening a second copy. + // is already open, or still being read, instead of opening a second copy. While a dialog or + // a drag acts on the active document, its tab comes to the front once that ends + // ([`crate::opening::poll`]), as a file that finishes opening then does. + if document_index_for_path(app, path).is_some() && !crate::opening::may_switch(app) { + if !app.opening.activate.iter().any(|p| same_open_path(p, path)) { + app.opening.activate.push(path.to_string()); + } + return Ok(Value::Null); + } if let Some(r) = try_activate_open_document(app, path)? { return Ok(r); } diff --git a/crates/ui-egui/src/opening.rs b/crates/ui-egui/src/opening.rs index 6de2fe0f74..37eca5b627 100644 --- a/crates/ui-egui/src/opening.rs +++ b/crates/ui-egui/src/opening.rs @@ -29,6 +29,9 @@ pub struct Pending { #[derive(Default)] pub struct Opening { pub pending: Vec, + /// Open documents asked for again while the active one couldn't change: their tabs come to + /// the front once it can ([`may_switch`]). + pub activate: Vec, } /// Is a long operation running that should show the busy cursor and status spinner (background @@ -84,6 +87,12 @@ pub fn start(app: &mut VectorcraftApp, path: &str, bytes: Vec) -> Result bool { + let s = &app.session; + !(app.ui.dialog.is_some() || s.in_interaction() || (s.tool_busy() && s.pointer_held())) +} + /// Open the files that are ready (each frame). Not while a dialog is open: a document that opens /// becomes the active one, and the dialog's OK acts on the active document (#1694: Save As /// options confirmed after another file had finished opening saved that file's art under the @@ -94,10 +103,13 @@ pub fn start(app: &mut VectorcraftApp, path: &str, bytes: Vec) -> Result (VectorcraftApp, std::path::PathBuf, String) { + let services = crate::Services { + read: Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string()))), + write: Some(Box::new(|p: &str, b: &[u8]| std::fs::write(p, b).map_err(|e| e.to_string()))), + ..Default::default() + }; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), services); + let d = std::env::temp_dir().join(format!("vc-reopen-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + let path = d.join("doc.vectorcraft").to_string_lossy().to_string(); + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.run("file.saveAs", json!({"path": path})).unwrap(); + app.run("file.new", json!({})).unwrap(); + assert_eq!((app.session.documents().len(), app.session.active_index()), (2, Some(1))); + (app, d, path) +} + +/// Home shown over the document asked for gives way to it, as it does to any other tab (#5369). +#[test] +fn reopening_the_active_document_closes_home() { + let (mut app, d, path) = saved_and_another("home"); + app.run("document.activate", json!({"index": 0})).unwrap(); + app.run("app.home", json!({})).unwrap(); + assert!(crate::menus::home_showing(&app)); + assert_eq!(crate::io::open_reporting(&mut app, &path).unwrap()["activated"], true); + assert!(!crate::menus::home_showing(&app)); + let _ = std::fs::remove_dir_all(d); +} + +/// Automation confined to a folder can't reach an open document saved outside it (#5369). +#[test] +fn a_confined_open_does_not_reach_an_open_document_outside_its_roots() { + use vectorcraft_engine::file_access; + let (mut app, d, path) = saved_and_another("roots"); + let inside = d.join("inside"); + std::fs::create_dir_all(&inside).unwrap(); + let roots = file_access::AutomationRoots::new(&[inside.as_path()], &[]).unwrap(); + assert!(file_access::confine(roots.as_ref(), || app.run("file.open", json!({"path": path}))).is_err()); + assert_eq!((app.session.documents().len(), app.session.active_index()), (2, Some(1))); + let _ = std::fs::remove_dir_all(d); +} + +/// While a dialog acts on the active document, an open document asked for again comes to the +/// front only once the dialog closes, so the dialog's OK acts on its own document (#5369). +#[test] +fn reopening_while_a_dialog_is_open_waits_for_it_to_close() { + let (mut app, d, path) = saved_and_another("dialog"); + app.ui.dialog = Some(crate::state::Dialog::new(crate::dialogs::save_options::KIND, json!({}))); + assert_eq!(crate::io::open_reporting(&mut app, &path).unwrap(), serde_json::Value::Null); + crate::opening::poll(&mut app); + assert_eq!((app.session.documents().len(), app.session.active_index()), (2, Some(1)), "still the dialog's document"); + app.ui.dialog = None; + crate::opening::poll(&mut app); + assert_eq!((app.session.documents().len(), app.session.active_index()), (2, Some(0))); + let _ = std::fs::remove_dir_all(d); +}