Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge place-loads-cursor: Place: with no frame selected, the graphic loads into the place cursor

aionggc0c4973f349b
diff --cc crates/engine/src/cmd/file.rs
index bf40799d5,aa2356493..2bf163b8e
--- a/crates/engine/src/cmd/file.rs
+++ b/crates/engine/src/cmd/file.rs
@@@ -122,7 -100,7 +122,7 @@@ pub fn specs() -> Vec<CommandSpec>
"Place…",
["File"],
Some("Cmd+D"),
- "{path?|base64?, name?, frame?: id (place into), spread?, x?, y?, width?, pdfPage?: n (1-based, Image Import Options), pdfCrop?: crop|art|trim|bleed|media|contentVisible|contentAll (the box the graphic shows; `width` is the whole page's), layoutPage?: n (IDML / .designcraft: that page's objects as a group)} — places an image (into the selected empty frame if any); text files (.txt, .docx, .rtf, .md) and Excel workbooks (.xlsx, as a table) go into the insertion point, the selected frame or a new frame on `page`/`rect` — {autoflow?: adds pages with threaded frames until the text fits, removeStyles?, styleMap?: {imported name: document style}, styleConflicts?: useExisting|redefine|autoRename}",
- "{path?|base64?, name?, frame?: id (place into), spread?, x?, y?, width?, pdfPage?: n (1-based, Image Import Options), pdfCrop?: crop|trim|bleed|art|media, loadCursor?: with no frame to fill and no x/y, an image loads into the place cursor (File › Place), layoutPage?: n (IDML / .designcraft: that page's objects as a group)} — places an image (into the selected empty frame if any); text files (.txt, .docx, .rtf, .md) and Excel workbooks (.xlsx, as a table) go into the insertion point, the selected frame or a new frame on `page`/`rect` — {autoflow?: adds pages with threaded frames until the text fits, removeStyles?, styleMap?: {imported name: document style}, styleConflicts?: useExisting|redefine|autoRename}",
++ "{path?|base64?, name?, frame?: id (place into), spread?, x?, y?, width?, pdfPage?: n (1-based, Image Import Options), pdfCrop?: crop|art|trim|bleed|media|contentVisible|contentAll (the box the graphic shows; `width` is the whole page's), loadCursor?: with no frame to fill and no x/y, an image loads into the place cursor (File › Place), layoutPage?: n (IDML / .designcraft: that page's objects as a group)} — places an image (into the selected empty frame if any); text files (.txt, .docx, .rtf, .md) and Excel workbooks (.xlsx, as a table) go into the insertion point, the selected frame or a new frame on `page`/`rect` — {autoflow?: adds pages with threaded frames until the text fits, removeStyles?, styleMap?: {imported name: document style}, styleConflicts?: useExisting|redefine|autoRename}",
has_doc,
file_place
),
@@@ -453,9 -336,15 +453,12 @@@ fn file_place(s: &mut Session, p: &Valu
let px = p.get("x").and_then(Value::as_f64);
let py = p.get("y").and_then(Value::as_f64);
let want_w = p.get("width").and_then(Value::as_f64);
+ if target_frame.is_none() && px.is_none() && py.is_none() && p.get("loadCursor").and_then(Value::as_bool) == Some(true) {
- let mime = designcraft_render::image_mime(&bytes).to_string();
- let asset = Asset { page: pdf_page, id: AssetId(0), name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)) };
- return load_cursor(s, asset, (nw, nh), crop_box);
++ return load_cursor(s, asset, (bw, bh));
+ }
s.edit(|d, sel| {
let fresh = AssetId(d.alloc());
- let mime = designcraft_render::image_mime(&bytes).to_string();
- let aid = d.add_asset(Asset { page: pdf_page, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)) });
+ let aid = d.add_asset(Asset { id: fresh, ..asset });
// The width the whole page (or image) gets; the height follows the proportions.
let w = match want_w {
Some(w) => w,
@@@ -574,26 -447,32 +577,32 @@@ fn place_load(s: &mut Session, p: &Valu
let (bytes, name, link) = read_source(p)?;
let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad("place.load", "unsupported or corrupt image"))?;
let natural = designcraft_render::natural_size(&bytes, 0).unwrap_or((pw as f64, ph as f64));
+ let mime = designcraft_render::image_mime(&bytes).to_string();
- let asset = Asset { page: 0, id: AssetId(0), name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)) };
- load_cursor(s, asset, natural, None)
++ let asset = Asset { name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)), ..Default::default() };
++ load_cursor(s, asset, natural)
+ }
+
+ /// Adds `asset` to the document (with a fresh id) and loads it into the place cursor.
-fn load_cursor(s: &mut Session, mut asset: Asset, size: (f64, f64), crop: Option<(f64, f64, f64, f64)>) -> Result<Value> {
++fn load_cursor(s: &mut Session, mut asset: Asset, size: (f64, f64)) -> Result<Value> {
+ let name = asset.name.clone();
+ let (pw, ph) = asset.pixels.unwrap_or_default();
let aid = s.edit(|d, _| {
- let fresh = AssetId(d.alloc());
- let mime = designcraft_render::image_mime(&bytes).to_string();
- Ok(d.add_asset(Asset {
- id: fresh,
- name: name.clone(),
- mime,
- link: link.clone(),
- data: Arc::new(bytes),
- pixels: Some((pw, ph)),
- ..Default::default()
- }))
+ asset.id = AssetId(d.alloc());
+ Ok(d.add_asset(asset))
})?;
- s.loaded = Some((aid, natural));
- s.loaded = Some(crate::LoadedGraphic { asset: aid, size, crop });
++ s.loaded = Some(crate::LoadedGraphic { asset: aid, size });
s.set_tool("placeGun");
- Ok(json!({"asset": aid.0, "name": name, "width": pw, "height": ph}))
+ Ok(json!({"asset": aid.0, "name": name, "width": pw, "height": ph, "loaded": true}))
}
fn place_drop(s: &mut Session, p: &Value) -> Result<Value> {
- let (aid, (nw, nh)) = s.loaded.ok_or_else(|| bad("place.drop", "nothing loaded in the place cursor"))?;
- let crate::LoadedGraphic { asset: aid, size: (nw, nh), crop } =
++ let crate::LoadedGraphic { asset: aid, size: (nw, nh) } =
+ s.loaded.ok_or_else(|| bad("place.drop", "nothing loaded in the place cursor"))?;
+ // Undo can remove the loaded graphic's asset.
+ if !s.doc()?.doc.assets.contains_key(&aid) {
+ s.loaded = None;
+ return Err(bad("place.drop", "the loaded graphic is no longer in the document"));
+ }
let sr = super::spread_param(p, "spread");
let lid = s.doc()?.active_layer;
let frame = super::id_param(p, "frame");
diff --cc crates/engine/src/lib.rs
index ab381b309,29a396e39..a8ee6a166
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@@ -111,6 -105,16 +111,15 @@@ pub struct DocState
#[derive(Debug)]
pub struct FontScope(pub u32);
+ /// A graphic loaded in the place cursor (`place.load`, File › Place with no frame to fill).
+ #[derive(Clone, Copy, Debug)]
+ pub struct LoadedGraphic {
+ pub asset: designcraft_doc::AssetId,
- /// Natural size in points.
++ /// Natural size in points: the Image Import Options › Crop to box for a PDF page (the asset's
++ /// `pdf_box`).
+ pub size: (f64, f64),
- /// Image Import Options › Crop to: the box of the graphic a new frame shows (x, y, w, h).
- pub crop: Option<(f64, f64, f64, f64)>,
+ }
+
impl Drop for FontScope {
fn drop(&mut self) {
designcraft_fonts::FontDb::global().close_scope(self.0);
diff --cc crates/engine/src/tooling.rs
index b7ad229a8,e562ec144..70bba77b8
--- a/crates/engine/src/tooling.rs
+++ b/crates/engine/src/tooling.rs
@@@ -58,11 -58,11 +58,15 @@@ impl Session
if self.tool.id() == id {
return;
}
+ // A composition ends with the tool that shows it: its marked text stays as typed.
+ if let Err(e) = self.end_composition() {
+ log::warn!("ending the IME composition: {e}");
+ }
self.tool = designcraft_tools::create(id);
+ // Any other tool unloads the place cursor.
+ if id != "placeGun" {
+ self.loaded = None;
+ }
// Leaving the Type tool keeps the text frame selected.
if id != "type"
&& let Some(st) = self.active_mut()
diff --cc crates/ui-egui/src/lib.rs
index 784ebdff8,ac25e6c23..0070a8fd2
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -1112,21 -810,15 +1112,30 @@@ impl DesignApp
self.open_file(cmd, json!({"path": path}))
}
+ /// 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> {
+ if let Some(o) = params.as_object_mut() {
+ o.insert("loadCursor".into(), json!(true));
+ }
+ self.open_file("file.place", params)
+ }
+
/// Open (`file.open`, `file.openBytes`) or place (`file.place`) a file the user chose. A file
/// that can't be opened or placed gets an alert with the reason, as well as the status line.
/// A successful import with warnings keeps the document and shows the warnings in an alert.