mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 07:17:00 +00:00
Focusing an open tab closes Home, checks read roots and waits for dialogs
Follow-ups to #5369 (#5420), from its last review: - Home shown over the document asked for again gives way to it. - Automation confined to some folders can't reach an open document outside them: open_file checks read access before looking for a tab. - While a dialog or a drag acts on the active document, a document asked for again comes to the front once it ends (opening::poll), as a file that finishes opening already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
committed by
bflatastic
co-authored by
Claude Opus 5.5
parent
65f55d65ea
commit
8f66e2b694
@@ -247,6 +247,8 @@ fn try_activate_open_document(app: &mut VectorcraftApp, path: &str) -> Result<Op
|
||||
});
|
||||
app.run("document.activate", json!({ "index": index }))?;
|
||||
app.sync_views();
|
||||
// Home, shown over the document that was already active, gives way to it too.
|
||||
app.ui.home = None;
|
||||
Ok(Some(r))
|
||||
}
|
||||
|
||||
@@ -273,8 +275,18 @@ pub fn open_path(app: &mut VectorcraftApp, path: &str) -> Result<Value, String>
|
||||
/// [`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<Value, String> {
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ pub struct Pending {
|
||||
#[derive(Default)]
|
||||
pub struct Opening {
|
||||
pub pending: Vec<Pending>,
|
||||
/// 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<String>,
|
||||
}
|
||||
|
||||
/// 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<u8>) -> Result<Val
|
||||
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;
|
||||
!(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<u8>) -> Result<Val
|
||||
/// waiting between its clicks (the Dimension tool after its first point): the file opens, and the
|
||||
/// tool starts afresh, as when the user switches documents.
|
||||
pub fn poll(app: &mut VectorcraftApp) {
|
||||
let s = &app.session;
|
||||
if app.ui.dialog.is_some() || s.in_interaction() || (s.tool_busy() && s.pointer_held()) {
|
||||
if !may_switch(app) {
|
||||
return;
|
||||
}
|
||||
for path in std::mem::take(&mut app.opening.activate) {
|
||||
// Closed meanwhile, it opens again; a failure is reported to the user there.
|
||||
let _ = crate::io::open_reporting(app, &path);
|
||||
}
|
||||
let mut i = 0;
|
||||
while let Some(p) = app.opening.pending.get(i) {
|
||||
let r = match p.done.try_recv() {
|
||||
|
||||
@@ -349,3 +349,63 @@ fn reopening_an_open_file_focuses_its_tab() {
|
||||
assert_eq!(focused(&app, "drop"), uid);
|
||||
let _ = std::fs::remove_dir_all(d);
|
||||
}
|
||||
|
||||
/// An app with `doc.vectorcraft` saved in a fresh folder `name`, open in the first tab, and an
|
||||
/// untitled document active in the second → (app, folder, document path).
|
||||
fn saved_and_another(name: &str) -> (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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user