diff --git a/crates/ui-egui/src/canvas/page_tools.rs b/crates/ui-egui/src/canvas/page_tools.rs index 1d16a600e1..afd78502c0 100644 --- a/crates/ui-egui/src/canvas/page_tools.rs +++ b/crates/ui-egui/src/canvas/page_tools.rs @@ -324,9 +324,14 @@ pub(crate) fn page_tool<'a>( // part took goes on to the text. Highlight shares Redact's area-box mechanism off text (L-262). _ => { let snap = caret_snap(tool, &cx, &session.texts, session.comments.tool.page); + let aspect = session + .comments + .primary() + .and_then(|(page, index)| session.signature_drag.aspect_ratio(page, index).map(|r| (page, index, r))); let view = &mut session.comments; 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) + .image_aspect(aspect) .settings(tool, cx.comment_prefs, cx.allowed, cx.doc.comments_hidden()) .outcome(&mut session.pending_edit, &mut view.notice); let annotate = match snap { @@ -365,6 +370,8 @@ pub(crate) fn page_tool<'a>( // 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 aspect = + session.comments.primary().and_then(|(page, index)| session.signature_drag.aspect_ratio(page, index).map(|r| (page, index, r))); let view = &mut session.comments; let input = FillInput { tool: fill_tool, @@ -381,6 +388,7 @@ pub(crate) fn page_tool<'a>( 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) + .image_aspect(aspect) .settings(tool, cx.comment_prefs, cx.allowed, cx.doc.comments_hidden()) .outcome(&mut session.pending_edit, &mut view.notice) .over(place); diff --git a/crates/ui-egui/src/tools/comments/bridge.rs b/crates/ui-egui/src/tools/comments/bridge.rs index 7c8892b921..454e1cb606 100644 --- a/crates/ui-egui/src/tools/comments/bridge.rs +++ b/crates/ui-egui/src/tools/comments/bridge.rs @@ -237,6 +237,10 @@ fn bounds(points: &[Point]) -> Option { /// stamp, typed text, a sound) the nearest that behaves alike. // PLAN: `DocInfo` carries no line endings, so an arrow reads as a Line here. pub(crate) fn kind_of(all: &[Annotation], a: &Annotation) -> AnnotationKind { + // A drawn signature is picked by its box, including the spaces between its strokes. + if a.fill_sign && a.subtype == "Ink" { + return AnnotationKind::Rectangle; + } tool_for_default(all, a, None).and_then(CommentTool::kind).unwrap_or(match a.subtype.as_str() { "FreeText" => AnnotationKind::TextBox, "Stamp" => AnnotationKind::Rectangle, diff --git a/crates/ui-egui/src/tools/comments/tool.rs b/crates/ui-egui/src/tools/comments/tool.rs index 06d09b1b2b..4062d6f74b 100644 --- a/crates/ui-egui/src/tools/comments/tool.rs +++ b/crates/ui-egui/src/tools/comments/tool.rs @@ -51,6 +51,7 @@ pub(crate) struct CommentsTool<'a> { allowed: bool, hidden: bool, caret: Option, + image_aspect: Option<(craft::review::AnnotationId, f64)>, } impl<'a> CommentsTool<'a> { @@ -78,6 +79,7 @@ impl<'a> CommentsTool<'a> { allowed: false, hidden: false, caret: None, + image_aspect: None, } } @@ -94,6 +96,30 @@ impl<'a> CommentsTool<'a> { self } + /// The embedded image's proportions, in the displayed view's axes. + pub(crate) fn image_aspect(mut self, aspect: Option<(usize, usize, f32)>) -> Self { + self.image_aspect = aspect.map(|(page, index, ratio)| { + let ratio = if self.map.rotation.is_multiple_of(180) { ratio } else { ratio.recip() }; + (bridge::id_of(page, index), f64::from(ratio)) + }); + self + } + + fn annotations(&self, page: usize) -> Vec { + bridge::annotations(self.map.info, &self.map, self.quick, self.allowed) + .into_iter() + .filter(|a| a.page == page) + .map(|mut a| { + if let Some((id, ratio)) = self.image_aspect + && a.id == id + { + a.aspect = Some(ratio); + } + a + }) + .collect() + } + /// Where a paste click's edit and notice go. pub(crate) fn outcome(mut self, placed: &'a mut Option, notice: &'a mut Option<&'static str>) -> Self { self.placed = Some(placed); @@ -171,11 +197,7 @@ impl CanvasTool for CommentsTool<'_> { } let page = self.annotate.page; self.annotate.tool = annotation_tool(self.quick); - self.annotate.annotations = if self.hidden { - Vec::new() - } else { - bridge::annotations(self.map.info, &self.map, self.quick, self.allowed).into_iter().filter(|a| a.page == page).collect() - }; + self.annotate.annotations = if self.hidden { Vec::new() } else { self.annotations(page) }; let mut eraser_width = 2.0; if let (Some(prefs), QuickTool::Comment(comment)) = (self.prefs, self.quick) { let style = prefs.style(comment); @@ -244,11 +266,7 @@ impl CanvasTool for CommentsTool<'_> { && page != self.annotate.page { self.annotate.page = page; - self.annotate.annotations = if self.hidden { - Vec::new() - } else { - bridge::annotations(self.map.info, &self.map, self.quick, self.allowed).into_iter().filter(|a| a.page == page).collect() - }; + self.annotate.annotations = if self.hidden { Vec::new() } else { self.annotations(page) }; } let action = self.annotate.press(pointer, cx); *self.pressed = action != ToolAction::Ignored; diff --git a/crates/ui-egui/tests/it/fill_sign.rs b/crates/ui-egui/tests/it/fill_sign.rs index 06b2d831c4..15a231250c 100644 --- a/crates/ui-egui/tests/it/fill_sign.rs +++ b/crates/ui-egui/tests/it/fill_sign.rs @@ -407,7 +407,7 @@ fn dragging_the_font_size_resizes_the_selected_text_to_where_the_drag_ends() { let dragged = h.state().fill_text_size; assert!(dragged > before, "the drag made the size larger: {before} -> {dragged}"); let placed = h.state().session.get(id).unwrap().comment_props(annotation.0, annotation.1).and_then(|p| p.font_size); - assert_eq!(placed, Some(dragged), "the selected text has the size the drag ended at"); + assert_eq!(placed.map(|size| format!("{size:.3}")), Some(format!("{dragged:.3}")), "the selected text has the size the drag ended at"); } /// The font sizes of the texts `text` drawn in the last frame. @@ -1254,10 +1254,6 @@ fn image_signature_live_resize_and_move_preserve_background_and_hide_moving_hand h.hover_at(end); // Wait only for the one-time background preparation. Later pointer frames are immediate. h.run_steps(1); - eprintln!( - "DIAG image_signature_live_resize_and_move_preserve_background_and_hide_moving_handles: in_flight={:?}", - h.state().views[0].session.comments.tool.in_flight() - ); rendered_ink(&mut h, 160.0, 242.0); let end = at(&h, 220.0, 236.0); h.hover_at(end); @@ -1386,10 +1382,6 @@ fn embedded_image_signature_live_gestures_follow_document_and_view_rotation() { h.hover_at(end); let [x, y] = u(160.0, 242.0); h.run_steps(1); - eprintln!( - "DIAG embedded_image_signature_live_gestures_follow_document_and_view_rotation: in_flight={:?}", - h.state().views[0].session.comments.tool.in_flight() - ); rendered_ink(&mut h, x, y); let pixels = h.render().unwrap(); let [x, y] = u(58.0, 254.0); @@ -1462,10 +1454,6 @@ fn image_signature_corners_restore_original_aspect_after_edge_resize_and_reopen( let end = at(&h, x + hx * 10.0, y + hy * 10.0); h.hover_at(end); h.run_steps(1); - eprintln!( - "DIAG image_signature_corners_restore_original_aspect_after_edge_resize_and_reopen: in_flight={:?}", - h.state().views[0].session.comments.tool.in_flight() - ); let expected = [ if hx < 0.0 { 16.0 } else { 70.0 }, if hy < 0.0 { 160.0 } else { 170.0 }, @@ -1747,11 +1735,6 @@ fn fill_and_sign_tools_pick_up_placed_marks_to_move_and_resize_them() { assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "{tool}: a placed mark can be picked up"); drag(&mut h, mid, (mid.0 + 40.0, mid.1 - 60.0)); - eprintln!( - "DIAG fill_and_sign_tools_pick_up_placed_marks_to_move_and_resize_them: tool={tool} selection={:?} rects={:?}", - h.state().views[0].session.comments.primary(), - rects(&h) - ); assert!(matches!(h.state().quick_tool, QuickTool::Fill(_)), "{tool}: the tool stays"); let moved = [placed[0] + 40.0, placed[1] - 60.0, placed[2] + 40.0, placed[3] - 60.0]; let [now] = rects(&h)[..] else { panic!("{tool}: still one mark: {:?}", rects(&h)) }; @@ -1897,10 +1880,6 @@ fn lines_grow_and_shrink_with_their_handles_before_release() { h.run_steps(1); h.hover_at(along(&h, 60.0)); h.run_steps(1); - eprintln!( - "DIAG lines_grow_and_shrink_with_their_handles_before_release: in_flight={:?}", - h.state().views[0].session.comments.tool.in_flight() - ); // Wait only for the one-time layers. Later pointer frames are immediate. let mut grown = h.render().unwrap(); for _ in 0..50 { @@ -2744,10 +2723,10 @@ fn fill_and_sign_bar_controls_pick_tools_and_restyle_the_selection() { let keeps_date: Row = |h| { fill_slot(h, "Fill & Sign: sign").click_secondary(); h.run_steps(2); - fill_flyout_row(h, "Fill & Sign: date").click(); + fill_flyout_row(h, "Fill Sign: date").click(); h.run_steps(2); assert_eq!(h.state().quick_tool, QuickTool::Fill(FillTool::Date)); - h.get_by_label("Select (V)").click(); + h.key_press(egui::Key::V); h.run_steps(2); assert_eq!(h.state().quick_tool, QuickTool::Select); let ctx_h = h.ctx.clone(); @@ -2758,7 +2737,7 @@ fn fill_and_sign_bar_controls_pick_tools_and_restyle_the_selection() { h.run_steps(2); assert_eq!(h.state().quick_tool, QuickTool::Fill(FillTool::Date), "a click picks the shown tool, not the default"); assert!(!pad_open(h), "the default tool's dialog stays closed"); - h.get_by_label("Select (V)").click(); + h.key_press(egui::Key::V); h.run_steps(2); h.state_mut().close_tab(1); h.state_mut().close_tab(0); @@ -2882,16 +2861,14 @@ fn fill_sign_enter_adds_lines_outside_click_commits_and_escape_cancels() { assert_eq!(h.state().views[0].session.fill_text.as_ref().unwrap().text, text); assert!(items(&h).is_empty(), "Enter never places an annotation"); // The page editor paints each line separately, including the empty lines' space. - let line = |text| { - h.output() - .shapes - .iter() - .find_map(|s| match &s.shape { - egui::Shape::Text(t) if t.galley.text() == text => Some(t), - _ => None, - }) - .unwrap() - }; + fn text_shape<'a>(shape: &'a egui::Shape, text: &str) -> Option<&'a egui::epaint::TextShape> { + match shape { + egui::Shape::Text(t) if t.galley.text() == text => Some(t), + egui::Shape::Vec(shapes) => shapes.iter().find_map(|s| text_shape(s, text)), + _ => None, + } + } + let line = |text| h.output().shapes.iter().find_map(|s| text_shape(&s.shape, text)).unwrap(); let (ada, lovelace) = (line("Ada"), line("Lovelace")); assert_eq!(ada.galley.rows.len(), 1, "short typed lines fit without accidental soft wraps"); assert_eq!(lovelace.galley.rows.len(), 1, "short typed lines fit without accidental soft wraps");