Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

ui.window.open: an invalid document index is an error, not a side effect (#1133)

Elvis33LEf5080f977457
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index cf99a8827..96e8693bf 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -397,8 +397,10 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
None => err("no such dialog"),
},
"ui.window.open" => {
- if let Some(d) = u("document") {
- app.session.set_active(d as usize);
+ if let Some(d) = u("document")
+ && !app.session.set_active(d as usize)
+ {
+ return err(format!("no document {d}"));
}
wrap(crate::menus::invoke(app, ctx, "window.newWindowForDocument", json!({})))
}
@@ -805,6 +807,22 @@ mod tests {
}
}
+ #[test]
+ fn ui_window_open_rejects_an_unknown_document_index() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ app.run("file.new", json!({"width": 8, "height": 8})).unwrap();
+ // An out-of-range index must error instead of silently switching the active document
+ // and opening the window for whatever is active now.
+ let before = app.session.active_index();
+ let r = call(&mut app, &ctx, "ui.window.open", json!({"document": 99}));
+ assert_eq!(r["ok"], false, "{r}");
+ assert!(r["error"].as_str().unwrap().contains("no document 99"), "{r}");
+ assert_eq!(app.session.active_index(), before, "the active document is untouched");
+ // A valid index still opens the window.
+ assert_eq!(call(&mut app, &ctx, "ui.window.open", json!({"document": 0}))["ok"], true);
+ }
+
#[test]
fn ui_set_brush_size_dispatches_a_journaled_brush_command() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());