DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #599 update: dropped files land on the spread under the pointer (#931)
aionggc6c03ccb90fc
diff --cc crates/ui-egui/src/lib.rsindex fec7d92e3,928ff210e..fb1943bb2--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@@ -1111,12 -933,12 +1111,13 @@@ impl DesignAppif self.fonts_ready {menus::shortcuts(self, ctx);}+ self.flush_window_commands(ctx);#[cfg(not(target_arch = "wasm32"))]- for f in ctx.input(|i| i.raw.dropped_files.clone()) {+ let (dropped, pointer) = ctx.input(|i| (i.raw.dropped_files.clone(), i.pointer.hover_pos()));+ for f in dropped {let p = f.path().to_string_lossy().to_string();if !p.is_empty() {- let _ = self.open_dropped(&p);+ let _ = self.open_dropped_at(&p, pointer);}}self.handle_quit(ctx);@@@ -1266,34 -1088,31 +1267,53 @@@}}- /// A file dropped on the window: documents open, anything else is placed on the spread in- /// view (a parent spread while parents are being edited).+ /// A file dropped on the window: documents open, the swatches in a swatch exchange (.ase) file- /// are loaded, and anything else is placed. A file without an extension opens when its content- /// is a DesignCraft or IDML document.++ /// are loaded, and anything else is placed on the spread in view (a parent spread while parents++ /// are being edited). A file without an extension opens when its content is a DesignCraft or++ /// IDML document.pub fn open_dropped(&mut self, path: &str) -> Result<Value, String> {+ self.open_dropped_at(path, None)+ }++ /// [`Self::open_dropped`] at the pointer: a graphic's top left lands where the file was- /// dropped on the canvas, text goes to a new frame on the page there.++ /// dropped on the canvas, text goes to a new frame on the document page there.+ pub fn open_dropped_at(&mut self, path: &str, pointer: Option<egui::Pos2>) -> Result<Value, String> {- if opens_as_document(path) {+ if path.to_ascii_lowercase().ends_with(".ase") {+ return self.run("swatch.load", json!({"path": path}));+ }+ let document = opens_as_document(path)+ || (std::path::Path::new(path).extension().is_none() && std::fs::read(path).is_ok_and(|b| designcraft_engine::cmd::is_document(&b)));- let cmd = if document { "file.open" } else { "file.place" };- self.open_file(cmd, json!({"path": path}))++ if document {+ return self.open_file("file.open", json!({"path": path}));+ }+ let mut params = json!({"path": path});+ if let (Some((sr, at)), Some(o)) = (canvas::drop_point(self, pointer), params.as_object_mut()) {+ o.insert("spread".into(), serde_json::to_value(sr).unwrap_or(Value::Null));+ o.insert("x".into(), json!(at.x));+ o.insert("y".into(), json!(at.y));+ if let (designcraft_doc::SpreadRef::Doc(si), Some(st)) = (sr, self.session.active()) {+ let pi = st.doc.spreads.get(si).and_then(|s| s.page_at_x(at.x)).unwrap_or(0);+ o.insert("page".into(), json!(st.doc.first_page_of_spread(si) + pi + 1));+ }+ }+ self.open_file("file.place", params)}+ /// Queue files the operating system asked the app to open. They can arrive before the window+ /// exists (the event that launched the app); the first ready frame opens them, each as a new+ /// document through `file.open`, as File › Open does.+ pub fn open_documents_later(&mut self, paths: impl IntoIterator<Item = String>) {+ self.os_documents.extend(paths);+ }++ fn open_os_documents(&mut self) {+ for path in std::mem::take(&mut self.os_documents) {+ if let Err(e) = self.open_file("file.open", json!({"path": path})) {+ log::warn!("{path}: {e}");+ }+ }+ }+/// File › Place: the file fills the selected empty or graphic frame; with none, an image loads/// into the place cursor (click to place at its natural size, drag to draw the frame).pub(crate) fn place_file(&mut self, mut params: Value) -> Result<Value, String> {@@@ -2377,40 -2055,23 +2397,57 @@@ mod testsassert_eq!(alert(&app), ("Can't Place the File".to_string(), e));}+ #[test]+ fn a_dropped_indd_file_the_converter_rejects_alerts() {+ let path = unreadable_file("brochure.indd");+ let mut app = DesignApp::new(Session::new(), Services::default());+ let r = app.open_dropped(&path.to_string_lossy());+ let _ = std::fs::remove_file(&path);+ let e = r.unwrap_err();+ assert!(e.contains("InDesign file"), "{e}");+ assert_eq!(alert(&app), ("Can't Open the File".to_string(), e));+ assert!(app.session.documents().is_empty());+ }++ #[test]+ fn dropping_an_ase_file_loads_its_swatches() {+ let mut source = Session::new();+ source.execute("file.new", &json!({})).unwrap();+ source.execute("swatch.create", &json!({"name": "Dropped Teal", "color": "#108080"})).unwrap();+ let data = source.execute("swatch.save", &json!({"names": ["Dropped Teal"]})).unwrap();+ let bytes = designcraft_engine::cmd::base64_decode(data["base64"].as_str().unwrap());+ for name in ["palette.ase", "PALETTE.ASE"] {+ let path = std::env::temp_dir().join(format!("designcraft-ui-{}-{name}", std::process::id()));+ std::fs::write(&path, &bytes).unwrap();+ let mut app = DesignApp::new(Session::new(), Services::default());+ app.run("file.new", json!({})).unwrap();+ let r = app.open_dropped(&path.to_string_lossy());+ let _ = std::fs::remove_file(&path);+ assert!(app.ui.dialog.is_none(), "{name}: {:?}", app.ui.dialog.as_ref().map(|d| &d.fields));+ assert_eq!(r, Ok(json!({"added": 1, "replaced": 0})), "{name}");+ let doc = &app.session.doc().unwrap().doc;+ assert!(doc.swatches.iter().any(|s| s.name == "Dropped Teal"), "{name}");+ assert!(doc.assets.is_empty(), "{name} isn't placed");+ }+ }++ #[test]+ fn a_file_dropped_while_a_parent_is_open_lands_on_the_parent() {+ let mut app = DesignApp::new(Session::new(), Services::default());+ app.run("file.new", json!({"pages": 3})).unwrap();+ app.canvas_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0)));+ app.run("layout.parents.edit", json!({"on": true})).unwrap();+ let _ = app.view_mut();+ let path = std::env::temp_dir().join(format!("designcraft-ui-{}-drop.png", std::process::id()));+ std::fs::write(&path, designcraft_render::Rendered { width: 10, height: 10, pixels: vec![200; 400] }.to_png()).unwrap();+ let r = app.open_dropped(&path.to_string_lossy());+ let _ = std::fs::remove_file(&path);+ r.unwrap();+ let d = &app.session.doc().unwrap().doc;+ assert_eq!(d.parents[0].items.len(), 1, "the graphic is on the parent spread");+ assert!(d.spreads.iter().all(|s| s.items.is_empty()), "no document page gets it");+ }+#[test]fn file_open_failures_show_an_alert() {let path = unreadable_file("open.designcraft");