diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 393352819a..b637d521cc 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -686,9 +686,12 @@ impl PdfCraftApp { self.poll_pending_destinations(); } self.check_restored_recent(); - // Showing a document hides the Combine files tab, and ends its preview. + // Showing a document hides the Combine files tab. if self.active.is_some() && self.combine_tab.focused { self.combine_tab.focused = false; + } + // The page shown large, while it shows: leaving the tab, whichever way, closes it. + if !self.combine_showing() { self.close_combine_preview(); } // The picks first: a file the user picked opens ahead of a startup file that arrived with it, which then waits diff --git a/crates/ui-egui/src/canvas/page_tools.rs b/crates/ui-egui/src/canvas/page_tools.rs index fdaa1562f1..a8a7d30d78 100644 --- a/crates/ui-egui/src/canvas/page_tools.rs +++ b/crates/ui-egui/src/canvas/page_tools.rs @@ -324,7 +324,7 @@ pub(crate) fn page_tool<'a>( Some(snap) => annotate.caret_snap(snap), None => annotate, }; - let annotate = if selects_text { annotate.text(select) } else { annotate }; + let annotate = if selects_text { annotate.over(select) } else { annotate }; Arm::Area(Box::new(annotate)) } } @@ -341,7 +341,10 @@ pub(crate) fn page_tool<'a>( } QuickTool::Crop => Arm::Crop(CropTool::new(cx.can_crop, &mut session.box_drag, cx.pages, cx.rotation, size)), QuickTool::EnhanceCamera => Arm::Camera(CameraTool::new(cx.can_modify, cx.pages, press)), + // A Fill & Sign tool runs the comments tool over its placing: its own marks already placed are picked up, + // moved and resized (`bridge::annotations` reaches only them), and a press anywhere else places the mark. QuickTool::Fill(fill_tool) => { + let view = &mut session.comments; let input = FillInput { tool: fill_tool, text_size: cx.fill.text_size, @@ -352,9 +355,15 @@ pub(crate) fn page_tool<'a>( author: cx.author, date_text: cx.date_text, fill_text: &mut session.fill_text, - comments: &mut session.comments, + member: &mut view.member, }; - Arm::Area(Box::new(FillSignTool::new(&cx, input, fill, *press))) + let place = FillSignTool::new(&cx, input, fill, *press); + let map = comments::PageMap::new(cx.info, cx.pages, cx.rotation, cx.view); + let annotate = CommentsTool::new(&mut view.tool, &mut view.paste, map, &mut view.tool_done, &mut view.tool_pressed) + .settings(tool, cx.comment_prefs, cx.allowed, cx.doc.comments_hidden()) + .outcome(&mut session.pending_edit, &mut view.notice) + .over(place); + Arm::Area(Box::new(annotate)) } QuickTool::Field(field_tool) => Arm::Area(Box::new(PrepareTool::new(Some(field_tool), cx.can_modify, cx.form, session, cx.info, xforms))), QuickTool::AddText => Arm::Area(Box::new(crate::tools::content_ui::AddTextTool::new(session, cx.doc, cx.pages, cx.text_style))), diff --git a/crates/ui-egui/src/canvas/xform.rs b/crates/ui-egui/src/canvas/xform.rs index b4643c77b5..01cb6a5919 100644 --- a/crates/ui-egui/src/canvas/xform.rs +++ b/crates/ui-egui/src/canvas/xform.rs @@ -128,20 +128,29 @@ impl PageXform { color: Color32, ) { let mut mesh = egui::Mesh::with_texture(tex); - let (w, h) = (rect[2] - rect[0], rect[3] - rect[1]); - let (shown_w, shown_h) = if turn % 180 == 0 { (w, h) } else { (h, w) }; - let [a, b, c, d, e, f] = pdfcraft_model::view_matrix_for(turn, rect); - for (u, v) in [(0.0_f32, 0.0_f32), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)] { - // The texture's (u, v), from its top-left, in the picture's upright frame, then in user space. - let (dx, dy) = (f64::from(u) * shown_w, f64::from(1.0 - v) * shown_h); - let (x, y) = (a * dx + c * dy + e, b * dx + d * dy + f); - let q = p.user_to_view(x as f32, y as f32); - mesh.vertices.push(egui::epaint::Vertex { pos: self.norm_to_screen(q[0] / self.pw, q[1] / self.ph), uv: pos2(u, v), color }); + let uvs = [pos2(0.0, 0.0), pos2(1.0, 0.0), pos2(1.0, 1.0), pos2(0.0, 1.0)]; + for (pos, uv) in self.user_image_corners(p, rect, turn).into_iter().zip(uvs) { + mesh.vertices.push(egui::epaint::Vertex { pos, uv, color }); } mesh.add_triangle(0, 1, 2); mesh.add_triangle(0, 2, 3); painter.add(egui::Shape::mesh(mesh)); } + + /// Where the top left, top right, bottom right and bottom left of an upright picture land on screen when it is + /// drawn in the user-space `rect` turned back by `turn`, as [`paint_user_image`](Self::paint_user_image) draws it. + pub(crate) fn user_image_corners(&self, p: &pdfcraft_render::PageInfo, rect: [f64; 4], turn: i64) -> [Pos2; 4] { + let (w, h) = (rect[2] - rect[0], rect[3] - rect[1]); + let (shown_w, shown_h) = if turn % 180 == 0 { (w, h) } else { (h, w) }; + let [a, b, c, d, e, f] = pdfcraft_model::view_matrix_for(turn, rect); + [(0.0_f64, 0.0_f64), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)].map(|(u, v)| { + // The texture's (u, v), from its top-left, in the picture's upright frame, then in user space. + let (dx, dy) = (u * shown_w, (1.0 - v) * shown_h); + let (x, y) = (a * dx + c * dy + e, b * dx + d * dy + f); + let q = p.user_to_view(x as f32, y as f32); + self.norm_to_screen(q[0] / self.pw, q[1] / self.ph) + }) + } pub fn norm_to_screen(&self, u: f32, v: f32) -> Pos2 { let (a, b) = match self.rot { 90 => (1.0 - v, u), diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index 72bafede89..4f8b7e26e2 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -88,9 +88,6 @@ fn title_bar(app: &mut PdfCraftApp, ui: &mut egui::Ui) { |ui, _| { let home = app.active.is_none() && !app.combine_showing(); if IconButton::new(IconId::new("house")).label(tl!("Home")).selected(home).show(ui).clicked() { - if app.combine_showing() { - app.close_combine_preview(); - } app.active = None; app.combine_tab.focused = false; } diff --git a/crates/ui-egui/src/chrome/doc_tabs.rs b/crates/ui-egui/src/chrome/doc_tabs.rs index 72e261db15..aa4f25c6e6 100644 --- a/crates/ui-egui/src/chrome/doc_tabs.rs +++ b/crates/ui-egui/src/chrome/doc_tabs.rs @@ -55,13 +55,8 @@ pub(crate) fn strip(app: &mut PdfCraftApp, ui: &mut egui::Ui, place: Place) { match event { DocumentTabsEvent::Selected(StripTab::View(key)) | DocumentTabsEvent::Picked(StripTab::View(key)) => { if let Some(i) = app.tab_with_key(key) { - let from_combine = app.combine_showing(); app.active = Some(i); app.combine_tab.focused = false; - if from_combine { - // PLAN: assumed `PdfCraftApp::close_combine_preview(&mut self)` (pdf-workflows-w7). - app.close_combine_preview(); - } } } DocumentTabsEvent::Selected(StripTab::Combine) | DocumentTabsEvent::Picked(StripTab::Combine) => { diff --git a/crates/ui-egui/src/tools/comments/tool.rs b/crates/ui-egui/src/tools/comments/tool.rs index 6ecfef2775..6c33797c81 100644 --- a/crates/ui-egui/src/tools/comments/tool.rs +++ b/crates/ui-egui/src/tools/comments/tool.rs @@ -14,7 +14,6 @@ use super::bridge::{self, PageMap}; use super::state::{Composer, ComposerKind, PageCx}; use super::{CommentPrefs, CommentTool, CommentView, DocInfo, Edit, MAX_LINE_WIDTH, QuickTool}; use crate::canvas::page_parts::{Owner, PagePress}; -use crate::canvas::page_select::PageSelectTool; use crate::canvas::{PageGeometry, PageSession}; /// Copied comments hanging on the pointer, waiting for a click to place them. @@ -33,13 +32,14 @@ pub struct PastePlacement { type CaretSnap = Box Option<(Point, f64)> + Send>; /// The comment tools on the pages as a canvas tool: the annotation tool for the comment gestures, the paste -/// placement, and the text selection beneath a markup tool. +/// placement, and the tool beneath them that takes what no comment takes: the text selection under Select or a +/// markup tool, Fill & Sign's placing under a Fill & Sign tool (whose own marks are picked up first, as Acrobat does). // PLAN: the sheet's constructor lists four arguments; the tool also needs the pass's tool, preferences and what the // document allows (`settings`), where a paste click's edit and notice go (`outcome`), and `paste` as the whole slot so a // click or Escape can drop it (pdf-canvas-w9's `view.paste.as_mut()` becomes `&mut view.paste`). pub(crate) struct CommentsTool<'a> { annotate: &'a mut AnnotateTool, - text: Option>, + beneath: Option>, paste: &'a mut Option, placed: Option<&'a mut Option>, notice: Option<&'a mut Option<&'static str>>, @@ -66,7 +66,7 @@ impl<'a> CommentsTool<'a> { ) -> Self { CommentsTool { annotate, - text: None, + beneath: None, paste, placed: None, notice: None, @@ -81,9 +81,9 @@ impl<'a> CommentsTool<'a> { } } - /// The text selection a press no comment takes goes on to. - pub(crate) fn text(mut self, text: PageSelectTool<'a>) -> Self { - self.text = Some(text); + /// The tool a press no comment takes goes on to: the text selection, or Fill & Sign's placing. + pub(crate) fn over(mut self, beneath: impl CanvasTool + 'a) -> Self { + self.beneath = Some(Box::new(beneath)); self } @@ -113,7 +113,7 @@ impl<'a> CommentsTool<'a> { } /// Whether the annotation tool holds the gesture, as it took the press or is clicking out a shape: the drag, - /// release and cancel are its, else the text's. + /// release and cancel are its, else the tool's beneath. fn annotating(&self) -> bool { *self.pressed || !self.annotate.drawing().is_empty() } @@ -210,8 +210,8 @@ impl CanvasTool for CommentsTool<'_> { } *self.annotate = built; self.annotate.each_pass(cx); - if let Some(text) = self.text.as_mut() { - text.each_pass(cx); + if let Some(beneath) = self.beneath.as_mut() { + beneath.each_pass(cx); } } @@ -232,7 +232,7 @@ impl CanvasTool for CommentsTool<'_> { // A markup tool or Replace Text marks the text its drag selects (`page_after_text`): the text takes the press // first. Under Select the comments come first, and a press no comment takes goes on to the text. if matches!(self.quick, QuickTool::Comment(_)) - && let Some(text) = self.text.as_mut() + && let Some(text) = self.beneath.as_mut() { let action = text.press(pointer, cx); if action != ToolAction::Ignored { @@ -241,7 +241,7 @@ impl CanvasTool for CommentsTool<'_> { } let action = self.annotate.press(pointer, cx); *self.pressed = action != ToolAction::Ignored; - // A press on no comment passes through to the text (`EmptyPress::PassThrough`): it leaves the comments + // A press on no comment passes through to the tool beneath (`EmptyPress::PassThrough`): it leaves the comments // selected only while Shift or Cmd adds to the selection. A press that began off the pages is no page's and // leaves them selected. if action == ToolAction::Ignored @@ -253,8 +253,8 @@ impl CanvasTool for CommentsTool<'_> { self.annotate.selected.clear(); } match action { - ToolAction::Ignored => match self.text.as_mut() { - Some(text) => text.press(pointer, cx), + ToolAction::Ignored => match self.beneath.as_mut() { + Some(beneath) => beneath.press(pointer, cx), None => ToolAction::Ignored, }, action => action, @@ -264,32 +264,32 @@ impl CanvasTool for CommentsTool<'_> { fn drag(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) { if self.annotating() { self.annotate.drag(pointer, cx); - } else if let Some(text) = self.text.as_mut() { - text.drag(pointer, cx); + } else if let Some(beneath) = self.beneath.as_mut() { + beneath.drag(pointer, cx); } } fn release(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) { if self.annotating() { self.annotate.release(pointer, cx); - } else if let Some(text) = self.text.as_mut() { - text.release(pointer, cx); + } else if let Some(beneath) = self.beneath.as_mut() { + beneath.release(pointer, cx); } *self.pressed = false; } fn hover(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) { self.annotate.hover(pointer, cx); - if let Some(text) = self.text.as_mut() { - text.hover(pointer, cx); + if let Some(beneath) = self.beneath.as_mut() { + beneath.hover(pointer, cx); } } fn cancel(&mut self, reason: CancelReason, cx: &mut ToolContext<'_>) { if self.annotating() { self.annotate.cancel(reason, cx); - } else if let Some(text) = self.text.as_mut() { - text.cancel(reason, cx); + } else if let Some(beneath) = self.beneath.as_mut() { + beneath.cancel(reason, cx); } *self.pressed = false; } @@ -300,9 +300,9 @@ impl CanvasTool for CommentsTool<'_> { fn key(&mut self, key: &ToolKey, cx: &mut ToolContext<'_>) -> bool { if matches!(key, ToolKey::Cancel) { - // One step back per press: drop a paste, cancel the drawing, deselect, then drop the tool (a comment - // tool, or a Fill & Sign one: its Text tool would otherwise open a box at every click with no way out - // by the keyboard). + // One step back per press: drop a paste, cancel the drawing, deselect, then Fill & Sign's own steps (its + // placing closes the type box, then leaves the tool), or drop a comment tool. The text selection beneath + // Select or a markup tool takes no part in them. if self.paste.take().is_some() { return true; } @@ -314,7 +314,10 @@ impl CanvasTool for CommentsTool<'_> { self.annotate.selected.clear(); return true; } - if matches!(self.quick, QuickTool::Comment(_) | QuickTool::Fill(_)) { + if matches!(self.quick, QuickTool::Fill(_)) { + return self.beneath.as_mut().is_some_and(|beneath| beneath.key(key, cx)); + } + if matches!(self.quick, QuickTool::Comment(_)) { *self.done = true; return true; } @@ -335,15 +338,15 @@ impl CanvasTool for CommentsTool<'_> { if self.annotating() { return own; } - // Where no comment takes the press, it goes on to the text (`press`): its cursor shows there. A markup tool's - // crosshair is the annotation tool's idle face; the drag selects text, so the text's cursor shows over it. + // Where no comment takes the press, it goes on to the tool beneath (`press`): its cursor shows there. A markup + // tool's crosshair is the annotation tool's idle face; the drag selects text, so the text's cursor shows over it. let idle = match own { Cursor::System(CursorIcon::Default) => true, Cursor::System(CursorIcon::Crosshair) => matches!(self.quick, QuickTool::Comment(_)), _ => false, }; - match self.text.as_ref().map(|text| text.cursor(cx)) { - Some(text) if idle && !matches!(text, Cursor::System(CursorIcon::Default)) => text, + match self.beneath.as_ref().map(|beneath| beneath.cursor(cx)) { + Some(beneath) if idle && !matches!(beneath, Cursor::System(CursorIcon::Default)) => beneath, _ => own, } } @@ -359,13 +362,17 @@ impl CanvasTool for CommentsTool<'_> { { super::overlay::paste_outline(draw, cx, paste, &self.map, page, pointer.pos); } - if let Some(text) = self.text.as_ref() { - text.paint(draw, cx); + if let Some(beneath) = self.beneath.as_ref() { + beneath.paint(draw, cx); } } + /// A Fill & Sign tool is reached through its own commands; the comment tools through the annotation tool's. fn command_form(&self) -> ToolPath { - self.annotate.command_form() + match self.beneath.as_ref() { + Some(beneath) if matches!(self.quick, QuickTool::Fill(_)) => beneath.command_form(), + _ => self.annotate.command_form(), + } } } diff --git a/crates/ui-egui/src/tools/fill_sign.rs b/crates/ui-egui/src/tools/fill_sign.rs index fb1612af64..97bc52038d 100644 --- a/crates/ui-egui/src/tools/fill_sign.rs +++ b/crates/ui-egui/src/tools/fill_sign.rs @@ -21,7 +21,6 @@ use pdfcraft_render::{DocInfo, PageInfo}; use crate::canvas::PageXform; use crate::canvas::page_tools::PageToolCx; -use crate::comments::CommentView; pub(crate) mod scan; pub mod type_box; @@ -321,7 +320,8 @@ pub enum FillOutcome { } /// Values that shape how Fill & Sign handles a pass's input, with the two session fields its text -/// click writes (the open type box, and the comment selection a new box clears). +/// click writes (the open type box, and the group member a new box lets go of; the comments tool +/// it runs beneath clears the selection on a press no mark takes). pub(crate) struct FillInput<'a> { pub tool: FillTool, pub text_size: f64, @@ -332,7 +332,7 @@ pub(crate) struct FillInput<'a> { pub author: &'a str, pub date_text: &'a Result, pub fill_text: &'a mut Option, - pub comments: &'a mut CommentView, + pub member: &'a mut Option<((usize, usize), (usize, usize))>, } /// The Fill & Sign tool over the placed pages: a click places what the tool makes and reports it @@ -359,6 +359,24 @@ impl<'a> FillSignTool<'a> { self.xforms.iter().find(|(_, xf)| xf.rect.contains(pos)).copied() } + /// The saved image the tool places and, for a pointer at `pos` on a page, the page's transform and info and the + /// image's user-space box there (`image_signature_box`, the box a click places it in). + fn ghost(&self, pos: Pos2) -> Option<(&'a SavedImage, PageXform, &'a PageInfo, [f64; 4])> { + let saved = match self.input.tool { + FillTool::Signature => self.input.signature, + FillTool::Initials => self.input.initials, + _ => None, + }; + let Some(SavedSignature::Image(image)) = saved else { return None }; + let (page, xf) = self.page_at(pos)?; + let info: &'a DocInfo = self.info; + let p = info.pages.get(page)?; + let at = to_user(&xf, info, page, pos); + let size = [image.width() as usize, image.height() as usize]; + let rect = pdfcraft_engine::signature_image::image_signature_box(size, p, at, self.input.tool == FillTool::Initials)?; + Some((image, xf, p, rect)) + } + /// What a click at `pointer` on `page` makes. Typed text starts above the click, as displayed. fn click(&mut self, page: usize, xf: PageXform, pointer: Pos2) -> Option { let info = self.info; @@ -375,7 +393,7 @@ impl<'a> FillSignTool<'a> { // sees its lost focus, so replacing the box first would drop what was typed. A new box // deselects, as a comment tool's note or text box does: Escape then closes the box and // leaves the tool in two presses. - self.input.comments.clear_selection(); + *self.input.member = None; let open = self.input.fill_text.replace(TypeBox { page, at: top, @@ -449,12 +467,25 @@ impl CanvasTool for FillSignTool<'_> { true } - // PLAN: framework gap: `ToolContext` and `Draw` give a tool no `egui::Context`, which `TextureCache::texture` - // needs for the saved image's ghost turned by page and view rotation (`image_signature_box` holds its box); the - // ghost, and the cursor that hides over it, wait for that accessor. - fn paint(&self, _draw: &Draw<'_>, _cx: &ToolContext<'_>) {} + /// A saved image signature or initials follows the pointer at the size a click places it, upright as displayed + /// at every page and view rotation. + fn paint(&self, draw: &Draw<'_>, cx: &ToolContext<'_>) { + let Some((image, xf, p, rect)) = cx.pointer.and_then(|pointer| self.ghost(pointer.pos)) else { return }; + let size = [image.width() as usize, image.height() as usize]; + // An image whose bytes don't fill its size paints nothing. + if image.rgba().len() != size[0] * size[1] * 4 { + return; + } + let tex = draw.cached_texture(("fill-sign-ghost", image.key()), || egui::ColorImage::from_rgba_unmultiplied(size, image.rgba())); + let whole = egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)); + draw.texture_quad(xf.user_image_corners(p, rect, i64::from(p.rotation)), tex, whole); + } - fn cursor(&self, _cx: &ToolContext<'_>) -> Cursor { + /// The image takes the pointer's place where it is shown. + fn cursor(&self, cx: &ToolContext<'_>) -> Cursor { + if cx.pointer.is_some_and(|pointer| self.ghost(pointer.pos).is_some()) { + return Cursor::System(CursorIcon::None); + } Cursor::System(match self.input.tool { FillTool::Text => CursorIcon::Text, _ => CursorIcon::Crosshair, diff --git a/crates/ui-egui/src/workflows/combine_grid.rs b/crates/ui-egui/src/workflows/combine_grid.rs index 2a0dd5a45b..5dfc9239f2 100644 --- a/crates/ui-egui/src/workflows/combine_grid.rs +++ b/crates/ui-egui/src/workflows/combine_grid.rs @@ -336,9 +336,7 @@ impl PdfCraftApp { } } - /// Closes the preview: leaving the tab ends it, and coming back does not reopen it. - // PLAN: called where the active tab changes away from Combine (owner: pdf-chrome-w12); closing the tab - // drops the preview with the tab's state. + /// Closes the preview: each frame the tab isn't showing ends it, so coming back does not reopen it. pub(crate) fn close_combine_preview(&mut self) { self.combine_tab.preview = None; }