feat(open): open drawings in the running editor
Double-clicking a drawing started a whole second copy of the app. Now it lands as a tab in the editor already open, and that window comes forward. The election is the bind itself: every GUI launch tries a deterministic per-user loopback port, and the OS grants it to exactly one process and releases it on any death, SIGKILL included. Nothing to reap, no PID to probe, no stale lock after a crash. The winner serves the port from an iced subscription; the loser checks it is talking to a matching editor, hands the paths over and exits. Every surprise — no answer, a stranger on the port, a timeout — boots a normal window, i.e. the old behaviour: the feature can degrade, but it cannot lose the file. The gate in main is positional, after every headless mode has returned. The plugin runner is this same binary re-spawning itself, so a flag list would rot the first time a mode is added; a position cannot. Handing a drawing over that is already open switches to its tab instead of loading a second copy, comparing resolved paths so a symlink or a `..` hop is still recognised as the same file. `opening` is a single slot that a second open would overwrite, and on_file_opened drops any result arriving once it is clear — so drawings handed over at once would silently vanish. They queue in pending_opens, drained from every path that clears the slot, error and cancel included. Exec becomes %F so one launch takes the whole selection; the CLI takes a list and boot fans it out through the same door. --new-instance forces a separate process; --read-only / --script / --new always get their own. Not reused: the automation server. It dispatches arbitrary commands and writes arbitrary paths, and its open replaces the active tab's document in place — publishing that on a port every local process can reach would be unforced privilege escalation. Only its JSON-lines framing is shared. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
1490fe4ea9
commit
b3bd93d8c5
11 changed files with 665 additions and 19 deletions
|
|
@ -541,6 +541,94 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handing_over_an_already_open_drawing_switches_to_its_tab() {
|
||||
// Double-clicking a drawing that is already open should land on the tab
|
||||
// showing it, not load a second copy of the same file.
|
||||
use crate::app::Message;
|
||||
let mut app = OpenCADStudio::new_for_test();
|
||||
app.automation_op(r#"{"op":"new"}"#);
|
||||
|
||||
let path = std::env::temp_dir().join("ocs_already_open.dwg");
|
||||
std::fs::write(&path, b"x").unwrap();
|
||||
let canon = std::fs::canonicalize(&path).unwrap();
|
||||
|
||||
// Two tabs, the second holding the drawing; leave the first active.
|
||||
app.tabs
|
||||
.push(crate::app::document::DocumentTab::new_drawing(99));
|
||||
let target = app.tabs.len() - 1;
|
||||
app.tabs[target].current_path = Some(canon.clone());
|
||||
app.active_tab = 0;
|
||||
|
||||
let _ = app.update(Message::OpenExternal(canon.clone()));
|
||||
assert_eq!(app.active_tab, target, "should have switched to the tab");
|
||||
assert!(
|
||||
app.opening.is_none(),
|
||||
"an already-open drawing must not start a load"
|
||||
);
|
||||
assert!(app.pending_opens.is_empty(), "and must not queue one either");
|
||||
|
||||
// The same file spelled differently (a `..` hop) is still the same file.
|
||||
let indirect = canon.parent().unwrap().join("..").join(
|
||||
canon
|
||||
.strip_prefix(canon.parent().unwrap().parent().unwrap())
|
||||
.unwrap(),
|
||||
);
|
||||
app.active_tab = 0;
|
||||
let _ = app.update(Message::OpenExternal(indirect));
|
||||
assert_eq!(
|
||||
app.active_tab, target,
|
||||
"an unresolved spelling of the same path must still match the tab"
|
||||
);
|
||||
assert!(app.opening.is_none(), "still no second load");
|
||||
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_handoff_queues_instead_of_displacing_the_first() {
|
||||
// `opening` is one slot, and `on_file_opened` drops any result that
|
||||
// arrives once it is clear — so without the queue, two drawings handed
|
||||
// over at the same moment (select several files in a file manager: one
|
||||
// process each, all arriving together) would leave one tab and silently
|
||||
// lose the rest.
|
||||
use crate::app::Message;
|
||||
let mut app = OpenCADStudio::new_for_test();
|
||||
app.automation_op(r#"{"op":"new"}"#);
|
||||
|
||||
// Any existing file will do: OpenRecent only stats it, and the actual
|
||||
// load is an async Task this test drops.
|
||||
let dir = std::env::temp_dir();
|
||||
let (a, b) = (dir.join("ocs_si_a.dwg"), dir.join("ocs_si_b.dwg"));
|
||||
std::fs::write(&a, b"x").unwrap();
|
||||
std::fs::write(&b, b"x").unwrap();
|
||||
|
||||
let _ = app.update(Message::OpenExternal(a.clone()));
|
||||
assert!(
|
||||
app.opening.is_some(),
|
||||
"first handoff should start an open, not queue"
|
||||
);
|
||||
assert!(app.pending_opens.is_empty(), "nothing to queue yet");
|
||||
|
||||
let _ = app.update(Message::OpenExternal(b.clone()));
|
||||
assert_eq!(
|
||||
app.pending_opens.len(),
|
||||
1,
|
||||
"second handoff arriving mid-open must queue, not be dropped"
|
||||
);
|
||||
assert_eq!(app.pending_opens.front(), Some(&b));
|
||||
|
||||
// A drawing that fails to parse must still release the queue behind it.
|
||||
let _ = app.update(Message::FileOpened(Err("boom".into())));
|
||||
assert!(
|
||||
app.pending_opens.is_empty(),
|
||||
"a failed open must drain the queue, not strand it"
|
||||
);
|
||||
|
||||
let _ = std::fs::remove_file(&a);
|
||||
let _ = std::fs::remove_file(&b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_then_open_round_trips() {
|
||||
let mut app = OpenCADStudio::new_for_test();
|
||||
|
|
|
|||
Loading…
Reference in a new issue