diff --git a/crates/ui-egui/src/app/clipboard.rs b/crates/ui-egui/src/app/clipboard.rs index 5d7fcd9bec..e4b72538bc 100644 --- a/crates/ui-egui/src/app/clipboard.rs +++ b/crates/ui-egui/src/app/clipboard.rs @@ -158,10 +158,25 @@ impl PdfCraftApp { /// [`Self::paste_from_clipboard`], preferring content delivered with the paste event (`pasted`, F-89) /// over a fresh system-clipboard read so desktop `SystemClipboard` hosts still receive event payloads. pub(crate) fn paste_from_clipboard_with(&mut self, ctx: &egui::Context, i: usize, pasted: Option<&craft::ui::runtime::commands::ClipContent>) { + self.paste_from_clipboard_at(ctx, i, pasted, None); + } + + pub(crate) fn paste_from_clipboard_at( + &mut self, + ctx: &egui::Context, + i: usize, + pasted: Option<&craft::ui::runtime::commands::ClipContent>, + at: Option, + ) { if self.views.get(i).is_some_and(|v| v.session.forms.focus.is_some() || v.organize) { return; } - let page = self.views.get(i).map_or(0, |v| v.current()); + let target = at.and_then(|at| { + let view = self.views.get(i)?; + let doc = self.session.get(view.id)?; + view.session.user_point_at(view.geometry(), &doc.info, at) + }); + let page = target.map_or_else(|| self.views.get(i).map_or(0, |v| v.current()), |(page, _)| page); let delivered_text = pasted.and_then(|c| c.text.as_deref()).filter(|t| !t.is_empty()).map(str::to_owned); let delivered_image = pasted.and_then(|c| c.image.as_ref()).and_then(|img| { let (width, height) = (u32::try_from(img.size[0]).ok()?, u32::try_from(img.size[1]).ok()?); @@ -180,7 +195,7 @@ impl PdfCraftApp { match color_image(&image) { // PLAN: assumed `paste_received_image(page, &egui::ColorImage) -> bool` (pdf-edit-w7) Some(pixels) => { - self.paste_received_image(ctx, page, &pixels); + self.paste_received_image_at(ctx, page, &pixels, target.map(|(_, at)| at)); } None => self.notify_error("the clipboard image is empty, malformed or exceeds size limits"), } diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 79d8618994..96850f3db4 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1169,6 +1169,15 @@ fn route_press( } } +/// Request the clipboard at a page-space menu point. +fn request_paste_at_page(app: &PdfCraftApp, ctx: &egui::Context, index: usize, page: usize, at: [f64; 2]) { + let Some(view) = app.views.get(index) else { return }; + let Some(info) = app.session.get(view.id).and_then(|doc| doc.info.pages.get(page)) else { return }; + let Some(xf) = view.page_xform(page) else { return }; + let point = info.user_to_view(at[0] as f32, at[1] as f32); + craft::ui::runtime::commands::request_paste_at(ctx, xf.norm_to_screen(point[0] / xf.pw, point[1] / xf.ph)); +} + /// A right-click no part took, or the Menu key, at `at`. The page's menu is anchored on the page under it, the first /// where two pages touch, and on none between the pages. With every tool but Add text and the field tools it is the /// comments': the comment under it is selected for its menu, or the selection cleared, between the pages too. With @@ -2061,7 +2070,7 @@ fn apply_frame_requests(app: &mut PdfCraftApp, ctx: &egui::Context, index: usize } Some(crate::edit_text_ui::ImageAction::Paste(page, at)) => { if let Some(pt) = at { - app.paste_image_at(ctx, page, pt); + request_paste_at_page(app, ctx, index, page, pt); } else { app.paste_image(ctx, page, None); } @@ -2105,7 +2114,7 @@ fn apply_frame_requests(app: &mut PdfCraftApp, ctx: &egui::Context, index: usize } } Some(PageMenuAction::Paste(page, at)) => { - app.paste_image_at(ctx, page, at); + request_paste_at_page(app, ctx, index, page, at); } Some(PageMenuAction::PasteComments(page, at)) => { app.paste_comments(ctx, index, None, Some((page, at))); diff --git a/crates/ui-egui/src/commands.rs b/crates/ui-egui/src/commands.rs index 0c7e60f1f1..569d346b5a 100644 --- a/crates/ui-egui/src/commands.rs +++ b/crates/ui-egui/src/commands.rs @@ -88,7 +88,7 @@ impl PdfCraftApp { }; if !organize && !self.is_preparing() && id == crate::menus::PASTE && fired.pasted.is_some() { if let Some(i) = self.active.filter(|&i| i < self.views.len()) { - self.paste_from_clipboard_with(ctx, i, fired.pasted.as_ref()); + self.paste_from_clipboard_at(ctx, i, fired.pasted.as_ref(), fired.paste_at); } return; } diff --git a/crates/ui-egui/src/tools/content_ui.rs b/crates/ui-egui/src/tools/content_ui.rs index 15ff807a5c..92f2b3364a 100644 --- a/crates/ui-egui/src/tools/content_ui.rs +++ b/crates/ui-egui/src/tools/content_ui.rs @@ -1441,6 +1441,10 @@ impl crate::PdfCraftApp { /// Paste the integration's delivered pixels without rereading a clipboard that may have changed. pub(crate) fn paste_received_image(&mut self, ctx: &egui::Context, page: usize, image: &egui::ColorImage) -> bool { + self.paste_received_image_at(ctx, page, image, None) + } + + pub(crate) fn paste_received_image_at(&mut self, ctx: &egui::Context, page: usize, image: &egui::ColorImage, at: Option<[f64; 2]>) -> bool { let [width, height] = image.size; let pixels = width.checked_mul(height); let bytes = pixels.and_then(|n| n.checked_mul(4)); @@ -1456,7 +1460,7 @@ impl crate::PdfCraftApp { } let rgba = image.pixels.iter().flat_map(egui::Color32::to_srgba_unmultiplied).collect(); let delivered = crate::create_ui::Clip::Image { width, height, rgba }; - self.paste_image_with_system(ctx, page, None, None, None, false, Some(delivered)) + self.paste_image_with_system(ctx, page, None, at, None, false, Some(delivered)) } /// Paste an image from the clipboard onto `page`.