mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-22 02:15:24 +01:00
Color Settings: keep script gates and background reads after a profile answer
- A scope narrowed to the confirming caller keeps the script gates of both, and reads are exempt from them only where both scopes exempt them. - A file read off the UI thread does the rest of its read after its profile answer (linked files and type) off the UI thread too; an open a caller asked for by path still finishes before OK returns.
This commit is contained in:
1 parent
95ed13dd80
commit
6c5e10fd64
6 files changed
+136
-28
No files matched your search
@@ -698,6 +698,9 @@ pub struct ReadFile {
|
||||
/// The profile question as it was last asked ([`ReadFile::ask`]): an answer is taken only while
|
||||
/// the question is still the same.
|
||||
asked: Option<crate::cmd::colormgmt::ProfileQuestion>,
|
||||
/// Update Links and the low-resolution EPS proxy setting, while the linked files and the type
|
||||
/// are left to do after the answer ([`ReadFile::finish_read`]).
|
||||
links_left: Option<(bool, bool)>,
|
||||
}
|
||||
|
||||
/// The part of [`read_for_open`] a file waiting for a profile answer has left to do.
|
||||
@@ -772,6 +775,7 @@ pub fn read_for_open_asking(
|
||||
rest: Some(Rest { update_links, low_res_proxy_eps, color_mode }),
|
||||
answered: None,
|
||||
asked: None,
|
||||
links_left: None,
|
||||
};
|
||||
if ask && file.choice.is_none() && file.ask().is_some() {
|
||||
return Ok(file);
|
||||
@@ -802,11 +806,12 @@ impl ReadFile {
|
||||
self.asked.clone()
|
||||
}
|
||||
|
||||
/// Answer the profile question with `choice` and do the rest of the read: the policies (the
|
||||
/// answer in place of the one of the document's color mode), the color mode, the linked files
|
||||
/// and the type. An error leaves the file waiting: a profile to assign that is not one of that
|
||||
/// model, or Color Settings changed since the question was asked ([`Self::ask`]) so that the
|
||||
/// question, its model, the profile it is about or the working space differ.
|
||||
/// Answer the profile question with `choice` and apply it: the policies (the answer in place of
|
||||
/// the one of the document's color mode) and the color mode. The linked files and the type are
|
||||
/// left for [`Self::finish_read`] or [`open_read`]. An error leaves the file waiting: a profile
|
||||
/// to assign that is not one of that model, or Color Settings changed since the question was
|
||||
/// asked ([`Self::ask`]) so that the question, its model, the profile it is about or the working
|
||||
/// space differ.
|
||||
pub fn answer(&mut self, choice: crate::cmd::colormgmt::ProfileChoice) -> Result<()> {
|
||||
if let Some(asked) = &self.asked
|
||||
&& self.rest.is_some()
|
||||
@@ -818,7 +823,7 @@ impl ReadFile {
|
||||
}
|
||||
}
|
||||
let before = self.choice.replace(choice);
|
||||
let r = self.finish_read();
|
||||
let r = self.apply_answer();
|
||||
if r.is_err() {
|
||||
self.choice = before;
|
||||
}
|
||||
@@ -826,8 +831,26 @@ impl ReadFile {
|
||||
}
|
||||
|
||||
/// The rest of [`read_for_open_asking`], once: the color management policies with the profile
|
||||
/// answer, the color mode `colorMode` asks for, the linked files and the type.
|
||||
fn finish_read(&mut self) -> Result<()> {
|
||||
/// 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.
|
||||
pub fn finish_read(&mut self) -> Result<()> {
|
||||
self.apply_answer()?;
|
||||
self.finish_links();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Read the linked files and lay out the type of a file whose profile answer is applied
|
||||
/// ([`Self::answer`]), once. A file still waiting for its answer is left as it is.
|
||||
fn finish_links(&mut self) {
|
||||
let Some((update_links, low_res_proxy_eps)) = self.links_left.take() else { return };
|
||||
let base = crate::cmd::links::LinkBase::file(self.path.as_deref());
|
||||
self.links = crate::cmd::links::resolve(&mut self.loaded.doc, base, update_links, low_res_proxy_eps);
|
||||
Session::refresh_text_bounds(&mut self.loaded.doc);
|
||||
}
|
||||
|
||||
/// The policies with the profile answer and the color mode `colorMode` asks for, once; the
|
||||
/// linked files and the type are left for [`Self::finish_read`].
|
||||
fn apply_answer(&mut self) -> Result<()> {
|
||||
if self.rest.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -850,9 +873,7 @@ impl ReadFile {
|
||||
super::super::colormgmt::set_color_mode(doc, mode, true, None, grays);
|
||||
});
|
||||
}
|
||||
let base = crate::cmd::links::LinkBase::file(self.path.as_deref());
|
||||
self.links = crate::cmd::links::resolve(&mut self.loaded.doc, base, rest.update_links, rest.low_res_proxy_eps);
|
||||
Session::refresh_text_bounds(&mut self.loaded.doc);
|
||||
self.links_left = Some((rest.update_links, rest.low_res_proxy_eps));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,17 +253,24 @@ impl ScopeHandle {
|
||||
}
|
||||
|
||||
/// The captured scope narrowed to what the calling thread may reach as well: the roots of both
|
||||
/// are in force ([`AutomationRoots::intersect`]), and without roots in either, none are.
|
||||
/// are in force ([`AutomationRoots::intersect`]), and without roots in either, none are. The
|
||||
/// script gates of both are in force too, and reads are exempt from them only where both are.
|
||||
pub fn within_current(self) -> Self {
|
||||
let here = Self::capture();
|
||||
let roots = match (self.roots(), here.roots()) {
|
||||
(None, None) => return self,
|
||||
(Some(r), None) | (None, Some(r)) => r,
|
||||
(Some(a), Some(b)) if Arc::ptr_eq(&a, &b) => a,
|
||||
(Some(a), Some(b)) => a.intersect(&b),
|
||||
let gate = match (self.1.clone(), here.1.clone()) {
|
||||
(None, None) => None,
|
||||
(Some(g), None) | (None, Some(g)) => Some(g),
|
||||
(Some(a), Some(b)) if Arc::ptr_eq(&a, &b) => Some(a),
|
||||
(Some(a), Some(b)) => Some(Arc::new(ScriptGate { also: Some(b), ..(*a).clone() })),
|
||||
};
|
||||
let Self(_, gate, exempt) = self;
|
||||
Self(Some(Scope::Confined(roots)), gate, exempt)
|
||||
let exempt = self.2 && here.2;
|
||||
let scope = match (self.roots(), here.roots()) {
|
||||
(None, None) => self.0,
|
||||
(Some(r), None) | (None, Some(r)) => Some(Scope::Confined(r)),
|
||||
(Some(a), Some(b)) if Arc::ptr_eq(&a, &b) => Some(Scope::Confined(a)),
|
||||
(Some(a), Some(b)) => Some(Scope::Confined(a.intersect(&b))),
|
||||
};
|
||||
Self(scope, gate, exempt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,10 +284,15 @@ pub(crate) struct ScriptGate {
|
||||
pub(crate) write: ScriptScope,
|
||||
/// The paths of the open documents' files, as the documents hold them.
|
||||
pub(crate) documents: Vec<String>,
|
||||
/// Another gate a path must pass as well ([`ScopeHandle::within_current`]).
|
||||
pub(crate) also: Option<Arc<ScriptGate>>,
|
||||
}
|
||||
|
||||
impl ScriptGate {
|
||||
fn check(&self, path: &str, access: Access) -> Result<(), String> {
|
||||
if let Some(also) = &self.also {
|
||||
also.check(path, access)?;
|
||||
}
|
||||
let real = if ScriptScope::refused_by_name(Path::new(path)) { None } else { resolve(path).ok() };
|
||||
let allowed = real.is_some_and(|real| {
|
||||
let scoped = match access {
|
||||
@@ -889,6 +901,31 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A scope narrowed to the calling thread keeps the script gate of both: a script command that
|
||||
/// carries on work saved without a gate reads only what its gate allows, and with a gate saved
|
||||
/// too, only what both allow; reads are exempt only where both scopes exempt them.
|
||||
#[test]
|
||||
fn a_narrowed_scope_keeps_the_script_gates_of_both() {
|
||||
let (base, _) = folders("narrowed-gate");
|
||||
let gate =
|
||||
|dir: &str| ScriptGate { read: ScriptScope::of_folder(&base.join(dir)), write: ScriptScope::default(), documents: vec![], also: None };
|
||||
let (inside, other) = (s(base.join("in/a.svg")), s(base.join("outside/secret.svg")));
|
||||
let reads = |saved: ScopeHandle, gate_here: Option<ScriptGate>| {
|
||||
let narrowed = match gate_here {
|
||||
Some(g) => with_script_gate(g, || saved.within_current()),
|
||||
None => saved.within_current(),
|
||||
};
|
||||
narrowed.run(|| (check_read(&inside).is_ok(), check_read(&other).is_ok()))
|
||||
};
|
||||
let plain = ScopeHandle(None, None, false);
|
||||
assert_eq!(reads(plain.clone(), Some(gate("in"))), (true, false), "the confirming command's gate");
|
||||
assert_eq!(reads(ScopeHandle(None, Some(Arc::new(gate("in"))), false), None), (true, false), "the saved gate");
|
||||
assert_eq!(reads(ScopeHandle(None, Some(Arc::new(gate("in"))), false), Some(gate("outside"))), (false, false), "both gates");
|
||||
assert_eq!(reads(ScopeHandle(None, None, true), Some(gate("in"))), (true, false), "exempt only where both are");
|
||||
assert_eq!(reads(plain, None), (true, true));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_paths_are_told_by_name() {
|
||||
for p in [r"\\server\share\x.svg", "//server/share/x.svg", r"\\?\UNC\server\share\x.svg", r"\\?\unc\server\share"] {
|
||||
|
||||
@@ -457,7 +457,7 @@ impl Disk {
|
||||
/// The gate for a command the script runs: the places it reads and writes now, and the files of
|
||||
/// the open documents, `documents`.
|
||||
pub(crate) fn gate(&self, documents: Vec<String>) -> crate::file_access::ScriptGate {
|
||||
crate::file_access::ScriptGate { read: self.scope.clone(), write: self.writes.clone(), documents }
|
||||
crate::file_access::ScriptGate { read: self.scope.clone(), write: self.writes.clone(), documents, also: None }
|
||||
}
|
||||
|
||||
/// The files written since this was last asked, to name in the report.
|
||||
|
||||
@@ -61,6 +61,10 @@ pub(crate) struct PendingOpen {
|
||||
/// The automation roots in force where the open was asked for: its linked files are read, and
|
||||
/// the document is installed, under them and those of the caller that confirms.
|
||||
scope: ScopeHandle,
|
||||
/// It was read off the UI thread ([`crate::opening`]): the rest of its read is done there too
|
||||
/// once the question is answered. An open that answers at once (a path a caller asked for)
|
||||
/// finishes before OK returns.
|
||||
background: bool,
|
||||
}
|
||||
|
||||
/// The choices of a mismatch (`missing`: of a missing profile), as (`choice`, label).
|
||||
@@ -85,15 +89,16 @@ fn paste_choices() -> [(&'static str, &'static str); 2] {
|
||||
/// 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
|
||||
/// goes on the recent files ([`crate::io::opened`]).
|
||||
pub fn open_or_ask(app: &mut VectorcraftApp, file: ReadFile, path: Option<String>) -> Result<Value, String> {
|
||||
ask_or_open(app, PendingOpen { file, path, scope: ScopeHandle::capture() })
|
||||
/// 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 })
|
||||
}
|
||||
|
||||
/// [`open_or_ask`] for a file that has its scope already.
|
||||
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;
|
||||
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);
|
||||
@@ -213,10 +218,24 @@ fn open_confirm(app: &mut VectorcraftApp, d: &Dialog) -> DialogResult {
|
||||
open_pending(app, pending, scope)
|
||||
}
|
||||
|
||||
/// Open `pending`, answered or without a question, under `scope`; a failure is reported.
|
||||
/// Open `pending`, answered or without a question, under `scope`; a failure is reported. A file read
|
||||
/// 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 PendingOpen { file, path, .. } = pending;
|
||||
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);
|
||||
}
|
||||
},
|
||||
_ => file,
|
||||
};
|
||||
scope.run(|| match fileio::open_read(&mut app.session, file) {
|
||||
Ok(r) => {
|
||||
crate::io::opened(app, &r);
|
||||
|
||||
@@ -138,7 +138,7 @@ pub fn open_document(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: O
|
||||
let update_links = app.session.prefs.update_links == "automatically";
|
||||
let low_res_proxy_eps = app.session.prefs.low_res_proxy_eps;
|
||||
let file = fileio::read_for_open_asking(name, bytes, path.clone(), p, update_links, low_res_proxy_eps, true).map_err(|e| e.to_string())?;
|
||||
dialogs::profile_mismatch::open_or_ask(app, file, path)
|
||||
dialogs::profile_mismatch::open_or_ask(app, file, path, false)
|
||||
}
|
||||
|
||||
/// Did an open answer that it asks a question first (`{dialog}`), so the file isn't open yet?
|
||||
|
||||
@@ -113,6 +113,37 @@ pub fn start_with(app: &mut VectorcraftApp, path: &str, bytes: Vec<u8>, params:
|
||||
Ok(json!({ "background": true, "status": status }))
|
||||
}
|
||||
|
||||
/// Do the rest of the read of `file`, read for File › Open from `path` once its profile question
|
||||
/// is answered or gone (its linked files and type, [`ReadFile::finish_read`]), on a thread of its
|
||||
/// own under `scope`; it then opens as a file read off the UI thread does → `{background: true,
|
||||
/// status}`, or the file back when no thread could start (the caller opens it at once).
|
||||
pub(crate) fn finish_answered(app: &mut VectorcraftApp, path: &str, file: ReadFile, scope: ScopeHandle) -> Result<Value, Option<Box<ReadFile>>> {
|
||||
let (tx, done) = channel();
|
||||
let slot = Arc::new(std::sync::Mutex::new(Some(file)));
|
||||
let taken = slot.clone();
|
||||
let run = scope.clone();
|
||||
let spawned = std::thread::Builder::new().name("vectorcraft-open".into()).spawn(move || {
|
||||
let Some(mut file) = taken.lock().ok().and_then(|mut f| f.take()) else { return };
|
||||
let r = run
|
||||
.run(|| vectorcraft_engine::guard::catch_panic(|| file.finish_read().map_err(|e| e.to_string())))
|
||||
.unwrap_or_else(|msg| Err(format!("internal error: {msg} (please report this bug)")));
|
||||
let r = r.map(|()| file);
|
||||
// The app may have quit meanwhile: nothing to hand it to.
|
||||
let _ = tx.send(r);
|
||||
});
|
||||
if spawned.is_err() {
|
||||
return Err(slot.lock().ok().and_then(|mut f| f.take()).map(Box::new));
|
||||
}
|
||||
let label = format!("Opening {}", fileio::file_name(path));
|
||||
app.opening.next_id += 1;
|
||||
let id = app.opening.next_id;
|
||||
let token = CancelToken::new();
|
||||
app.opening.pending.push(Pending { id, path: path.to_string(), label: label.clone(), done, scope, token, hidden: false });
|
||||
let status = format!("{label}…");
|
||||
app.status(status.clone());
|
||||
Ok(json!({ "background": true, "status": status }))
|
||||
}
|
||||
|
||||
/// May the active document change now ([`poll`])?
|
||||
pub(crate) fn may_switch(app: &VectorcraftApp) -> bool {
|
||||
let s = &app.session;
|
||||
@@ -240,7 +271,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
/// after the profile question Edit › Color Settings asks about it, if any
|
||||
/// ([`crate::dialogs::profile_mismatch::open_or_ask`]).
|
||||
fn finish(app: &mut VectorcraftApp, path: &str, r: Result<ReadFile, String>) {
|
||||
let opened = r.and_then(|file| crate::dialogs::profile_mismatch::open_or_ask(app, file, Some(path.to_string())));
|
||||
let opened = r.and_then(|file| crate::dialogs::profile_mismatch::open_or_ask(app, file, Some(path.to_string()), true));
|
||||
if let Err(e) = opened {
|
||||
crate::io::report_open_error(app, &fileio::file_name(path), &e);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user