diff --git a/crates/engine/src/cmd/fileio/load.rs b/crates/engine/src/cmd/fileio/load.rs index cedda4844a..85a221a838 100644 --- a/crates/engine/src/cmd/fileio/load.rs +++ b/crates/engine/src/cmd/fileio/load.rs @@ -845,6 +845,13 @@ impl ReadFile { r } + /// Is part of the read still to do ([`Self::finish_read`]): the policies and the color mode of a + /// file that stopped for its profile question, or the linked files and the type of an answered + /// one? + pub fn read_left(&self) -> bool { + self.rest.is_some() || self.links_left.is_some() + } + /// The rest of [`read_for_open_asking`], once: the color management policies with the profile /// answer (or as the policy says without one), the color mode `colorMode` asks for, the linked /// files and the type. The app runs it off the UI thread once the question is answered. diff --git a/crates/ui-egui/src/dialogs/profile_mismatch.rs b/crates/ui-egui/src/dialogs/profile_mismatch.rs index a0f447277e..47f97b77dd 100644 --- a/crates/ui-egui/src/dialogs/profile_mismatch.rs +++ b/crates/ui-egui/src/dialogs/profile_mismatch.rs @@ -87,28 +87,24 @@ fn paste_choices() -> [(&'static str, &'static str); 2] { /// Open `file`, read for File › Open from `path`, as `document.open` does, unless Edit › Color /// Settings asks about its profile ([`ReadFile::ask`]): then the dialog asks first, and the result -/// is `{dialog}`. A file read while another one's question is open waits for its own question -/// until that one is answered (`{dialog, queued: true}`). Opened, the views follow and the file +/// is `{dialog}`. A file read while another one's question, or another dialog, is open waits for +/// its own question until that one closes (`{dialog, queued: true}`). Opened, the views follow and the file /// goes on the recent files ([`crate::io::opened`]). `background`: the file was read off the UI /// thread, and the rest of its read after an answer is done there too. pub fn open_or_ask(app: &mut VectorcraftApp, file: ReadFile, path: Option, background: bool) -> Result { ask_or_open(app, PendingOpen { file, path, scope: ScopeHandle::capture(), background }) } -/// [`open_or_ask`] for a file that has its scope already. +/// [`open_or_ask`] for a file that has its scope already. While another dialog is open (a paste +/// waiting for its Swatch Conflict answer, say), the question waits for it to close too. fn ask_or_open(app: &mut VectorcraftApp, mut pending: PendingOpen) -> Result { let Some(q) = pending.file.ask() else { - let PendingOpen { file, path, scope, .. } = pending; - return scope.run(|| { - let r = fileio::open_read(&mut app.session, file).map_err(|e| e.to_string())?; - crate::io::opened(app, &r); - if let Some(p) = &path { - crate::io::note_recent(app, p); - } - Ok(r) - }); + // The question is gone (or there was none): a file read off the UI thread does the rest of + // its read there, as an answered one does. + let scope = pending.scope.clone(); + return open_now(app, pending, scope); }; - if app.pending_profile_open.is_some() { + if app.pending_profile_open.is_some() || app.ui.dialog.is_some() { app.queued_profile_opens.push_back(pending); return Ok(json!({ "dialog": OPEN_KIND, "queued": true })); } @@ -222,33 +218,36 @@ fn open_confirm(app: &mut VectorcraftApp, d: &Dialog) -> DialogResult { /// off the UI thread does the rest of its read (its linked files and type) there too /// ([`crate::opening::finish_answered`]). fn open_pending(app: &mut VectorcraftApp, pending: PendingOpen, scope: ScopeHandle) -> DialogResult { + let name = pending.file.name(); + let r = open_now(app, pending, scope); + if let Err(e) = &r { + crate::io::report_open_error(app, &name, e); + } + r +} + +/// Open `pending` under `scope` without reporting a failure. A file read off the UI thread with part +/// of its read left ([`ReadFile::read_left`]) does that part there ([`crate::opening::finish_answered`]) +/// → `{background: true, status}`; when no thread can start, it opens at once. +fn open_now(app: &mut VectorcraftApp, pending: PendingOpen, scope: ScopeHandle) -> Result { let PendingOpen { file, path, background, .. } = pending; - let name = file.name(); let file = match &path { - Some(p) if background && crate::opening::available() => match crate::opening::finish_answered(app, p, file, scope.clone()) { - Ok(r) => return Ok(r), - Err(Some(file)) => *file, - Err(None) => { - let e = "the file stopped being read (please report this bug)".to_string(); - crate::io::report_open_error(app, &name, &e); - return Err(e); + Some(p) if background && file.read_left() && crate::opening::available() => { + match crate::opening::finish_answered(app, p, file, scope.clone()) { + Ok(r) => return Ok(r), + Err(Some(file)) => *file, + Err(None) => return Err("the file stopped being read (please report this bug)".to_string()), } - }, + } _ => file, }; - scope.run(|| match fileio::open_read(&mut app.session, file) { - Ok(r) => { - crate::io::opened(app, &r); - if let Some(p) = &path { - crate::io::note_recent(app, p); - } - Ok(r) - } - Err(e) => { - let e = e.to_string(); - crate::io::report_open_error(app, &name, &e); - Err(e) + scope.run(|| { + let r = fileio::open_read(&mut app.session, file).map_err(|e| e.to_string())?; + crate::io::opened(app, &r); + if let Some(p) = &path { + crate::io::note_recent(app, p); } + Ok(r) }) } diff --git a/crates/ui-egui/src/dialogs/profile_mismatch/tests.rs b/crates/ui-egui/src/dialogs/profile_mismatch/tests.rs index 358c6f428e..0bd7cc82e2 100644 --- a/crates/ui-egui/src/dialogs/profile_mismatch/tests.rs +++ b/crates/ui-egui/src/dialogs/profile_mismatch/tests.rs @@ -132,6 +132,46 @@ fn files_opened_together_ask_in_turn(app: &mut VectorcraftApp, tagged: &[u8]) { assert!(app.ui.dialog.is_none() && app.pending_profile_open.is_none()); } +/// A file opened while another dialog is open (a message here) waits for it to close and then asks +/// its question; the other dialog stays. `tagged` asks with the settings in force. +fn a_question_waits_for_an_open_dialog(app: &mut VectorcraftApp, tagged: &[u8]) { + let docs = app.session.documents().len(); + crate::dialogs::confirm::tell(app, "Waiting", ""); + let r = crate::io::open_bytes(app, "tagged.vectorcraft", tagged, None).unwrap(); + assert_eq!(r, json!({ "dialog": OPEN_KIND, "queued": true })); + assert!(app.ui.dialog.as_ref().is_some_and(|d| d.kind != OPEN_KIND), "the message stays"); + assert!(app.pending_profile_open.is_none()); + super::super::cancel(app); + assert!(app.ui.dialog.as_ref().is_some_and(|d| d.kind == OPEN_KIND) && app.pending_profile_open.is_some(), "then it asks"); + super::super::confirm(app).unwrap(); + assert_eq!(app.session.documents().len(), docs + 1); +} + +/// A file read off the UI thread waits for another file's question, and Color Settings change so +/// that it has none any more: once the first question closes, the rest of its read is done off the +/// UI thread too, and it opens when that is done. `tagged` asks with the settings in force, which +/// Ask When Opening for a mismatch is one of. +fn a_background_open_whose_question_is_gone_finishes_off_the_ui_thread(app: &mut VectorcraftApp, tagged: &[u8]) { + let docs = app.session.documents().len(); + assert_eq!(crate::io::open_bytes(app, "first.vectorcraft", tagged, None).unwrap(), json!({ "dialog": OPEN_KIND })); + let path = "/tmp/background.vectorcraft".to_string(); + let file = fileio::read_for_open_asking("background.vectorcraft", tagged, Some(path.clone()), &json!({}), false, false, true).unwrap(); + assert!(file.read_left()); + let r = open_or_ask(app, file, Some(path.clone()), true).unwrap(); + assert_eq!(r, json!({ "dialog": OPEN_KIND, "queued": true })); + app.run("edit.colorSettings", json!({"askMismatchOpen": false})).unwrap(); + crate::opening::tests::with_threads(|| { + super::super::cancel(app); + assert!(app.queued_profile_opens.is_empty()); + assert_eq!(app.opening.pending.len(), 1, "read off the UI thread"); + assert_eq!(app.session.documents().len(), docs, "not open before its read is done"); + crate::opening::tests::open_ready(app); + }); + assert_eq!(app.session.documents().len(), docs + 1); + assert_eq!(app.ui.recent_files.first(), Some(&path)); + app.run("edit.colorSettings", json!({"askMismatchOpen": true})).unwrap(); +} + /// Color Settings change the open question while it is open: OK asks the question as it is now /// (another working space) instead of opening, and opens by the policy once there is no question /// (the working space is the file's profile). `tagged` (Wide Gamut RGB) asks with the settings in @@ -339,6 +379,8 @@ fn opening_and_pasting_ask_about_profiles() { open_confirmed_under_roots(&mut app, &tagged); links_read_under_the_roots_of_the_open(&mut app); files_opened_together_ask_in_turn(&mut app, &tagged); + a_question_waits_for_an_open_dialog(&mut app, &tagged); + a_background_open_whose_question_is_gone_finishes_off_the_ui_thread(&mut app, &tagged); // Missing Profiles: Ask When Opening, and a choice the file refuses. app.run("edit.colorSettings", json!({"askMismatchOpen": false, "askMissingOpen": true, "rgbPolicy": "preserveEmbedded"})).unwrap(); diff --git a/crates/ui-egui/src/opening.rs b/crates/ui-egui/src/opening.rs index 5b4c8619ee..5467de0aae 100644 --- a/crates/ui-egui/src/opening.rs +++ b/crates/ui-egui/src/opening.rs @@ -59,9 +59,16 @@ pub fn long_operation_busy(app: &VectorcraftApp) -> bool { } /// Does opening happen off the UI thread here? Not on wasm (no threads), in unit tests (they look -/// at the document right after asking for it), or with `VECTORCRAFT_SYNC_OPEN` set. +/// at the document right after asking for it, unless one turns it on: [`tests::with_threads`]), or +/// with `VECTORCRAFT_SYNC_OPEN` set. +#[cfg(not(test))] pub fn available() -> bool { - !(cfg!(target_arch = "wasm32") || cfg!(test) || std::env::var_os("VECTORCRAFT_SYNC_OPEN").is_some()) + !(cfg!(target_arch = "wasm32") || std::env::var_os("VECTORCRAFT_SYNC_OPEN").is_some()) +} + +#[cfg(test)] +pub fn available() -> bool { + tests::threads_on() } /// Decode `bytes`, the file at `path`, read its linked files and lay out its type on a thread of its @@ -278,4 +285,4 @@ fn finish(app: &mut VectorcraftApp, path: &str, r: Result) { } #[cfg(test)] -mod tests; +pub(crate) mod tests; diff --git a/crates/ui-egui/src/opening/tests.rs b/crates/ui-egui/src/opening/tests.rs index 43fe46e217..a1884da5b1 100644 --- a/crates/ui-egui/src/opening/tests.rs +++ b/crates/ui-egui/src/opening/tests.rs @@ -44,8 +44,26 @@ fn wait_read(app: &mut VectorcraftApp) { } } +std::thread_local! { + /// Opening happens off the UI thread in this test ([`with_threads`]). + static THREADS: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Is opening off the UI thread turned on for this test ([`available`])? +pub(crate) fn threads_on() -> bool { + THREADS.with(std::cell::Cell::get) +} + +/// Run `f` with opening off the UI thread, as on the desktop. +pub(crate) fn with_threads(f: impl FnOnce() -> R) -> R { + THREADS.with(|t| t.set(true)); + let r = f(); + THREADS.with(|t| t.set(false)); + r +} + /// Wait until every file being opened has been read, then open the files that may open now. -fn open_ready(app: &mut VectorcraftApp) { +pub(crate) fn open_ready(app: &mut VectorcraftApp) { wait_read(app); poll(app); }