mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 20:01:07 +01:00
Merge pull request #8927 from dotCooCoo/fix/profile-mismatch-background-queue
Color Settings: a background open whose profile question is gone finishes off the UI thread
This commit is contained in:
5 files changed
+109
-31
No files matched your search
@@ -849,6 +849,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.
|
||||
|
||||
@@ -83,28 +83,24 @@ const PASTE_CHOICES: [(&str, &str); 2] = [("preserveNumbers", "Preserve the colo
|
||||
|
||||
/// 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<String>, background: bool) -> Result<Value, String> {
|
||||
ask_or_open(app, PendingOpen { file, path, scope: ScopeHandle::capture(), background, asked: Value::Null })
|
||||
}
|
||||
|
||||
/// [`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<Value, String> {
|
||||
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.any_dialog_open() {
|
||||
app.queued_profile_opens.push_back(pending);
|
||||
return Ok(json!({ "dialog": OPEN_KIND, "queued": true }));
|
||||
}
|
||||
@@ -220,24 +216,29 @@ fn finish_open(app: &mut VectorcraftApp, mut pending: PendingOpen, choice: Profi
|
||||
/// 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) -> Result<Value, String> {
|
||||
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 on a job of its own
|
||||
/// ([`crate::opening::finish_answered`]) → `{background: true, status}`.
|
||||
fn open_now(app: &mut VectorcraftApp, pending: PendingOpen, scope: ScopeHandle) -> Result<Value, String> {
|
||||
let PendingOpen { file, path, background, .. } = pending;
|
||||
let name = file.name();
|
||||
if let Some(p) = path.as_deref().filter(|_| background && crate::opening::available()) {
|
||||
if let Some(p) = path.as_deref().filter(|_| background && file.read_left() && crate::opening::available()) {
|
||||
return Ok(crate::opening::finish_answered(app, p, file, scope));
|
||||
}
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -198,6 +198,49 @@ fn files_opened_together_ask_in_turn(app: &mut VectorcraftApp, tagged: &[u8]) {
|
||||
assert!(!is_open::<ProfileMismatchDialog>(app) && 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!(is_open::<crate::dialogs::confirm::Message>(app), "the message stays");
|
||||
assert!(!is_open::<ProfileMismatchDialog>(app) && app.pending_profile_open.is_none());
|
||||
let id = open_id::<crate::dialogs::confirm::Message>(app);
|
||||
answer_cancel(app, id);
|
||||
assert!(is_open::<ProfileMismatchDialog>(app) && app.pending_profile_open.is_some(), "then it asks");
|
||||
let id = open_id::<ProfileMismatchDialog>(app);
|
||||
answer_ok(app, id);
|
||||
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();
|
||||
let id = open_id::<ProfileMismatchDialog>(app);
|
||||
crate::opening::tests::with_threads(|| {
|
||||
answer_cancel(app, id);
|
||||
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
|
||||
@@ -391,6 +434,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 profile the list doesn't offer.
|
||||
app.run("edit.colorSettings", json!({"askMismatchOpen": false, "askMissingOpen": true, "rgbPolicy": "preserveEmbedded"})).unwrap();
|
||||
|
||||
@@ -56,9 +56,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 job of its
|
||||
@@ -247,4 +254,4 @@ fn finish(app: &mut VectorcraftApp, path: &str, r: Result<ReadFile, String>) {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub(crate) mod tests;
|
||||
@@ -45,6 +45,24 @@ 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<bool> = 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<R>(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.
|
||||
/// Whether the missing-fonts question is open once a frame has drawn the dialogs.
|
||||
fn asks_fonts(app: &mut VectorcraftApp) -> bool {
|
||||
@@ -53,7 +71,7 @@ fn asks_fonts(app: &mut VectorcraftApp) -> bool {
|
||||
open_dialogs(app).iter().any(|d| d.key.as_deref() == Some(crate::dialogs::missing_fonts::KIND))
|
||||
}
|
||||
|
||||
fn open_ready(app: &mut VectorcraftApp) {
|
||||
pub(crate) fn open_ready(app: &mut VectorcraftApp) {
|
||||
wait_read(app);
|
||||
poll(app);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user