Merge branch 'red5/g8' into area/app-1

This commit is contained in:
Dylan M. Taylor
2026-11-18 23:06:47 -05:00
11 changed files with 70 additions and 53 deletions
+4
View File
@@ -380,6 +380,9 @@ pub struct PageSession {
/// The caret and selection in the Fill & Sign text being typed.
pub fill_selection: craft::text::ui::editor::TextSelection,
pub(crate) signature_drag: crate::signature_drag::SignatureDrag,
/// The rectangle being dragged on a page by Snapshot, Crop or a signature area: the page tools are built each
/// pass, so the drag lives here from its press to its release.
pub(crate) box_drag: PageBoxDrag,
/// What the last secondary press on a page went to, and on which page (`None` off the pages): the page's menu
/// shows its entries when it has a menu and is the one selected, or else the page's own.
pub(crate) menu_for: Option<(usize, Owner)>,
@@ -434,6 +437,7 @@ impl PageSession {
fill_text: None,
fill_selection: Default::default(),
signature_drag: Default::default(),
box_drag: PageBoxDrag::default(),
menu_for: None,
}
}
+14 -7
View File
@@ -37,23 +37,30 @@ fn to_user(page: &pdfcraft_render::PageInfo, vx: f32, vy: f32) -> [f64; 2] {
}
/// The Crop tool, built per pass over the placed pages.
pub(crate) struct CropTool {
pub(crate) struct CropTool<'a> {
allowed: bool,
placed: Vec<PlacedPage>,
/// Per placed page: its unrotated view-space size.
sizes: Vec<(usize, (f32, f32))>,
rotation: u16,
drag: PageBoxDrag,
/// The session's drag, kept from the press to the release.
drag: &'a mut PageBoxDrag,
done: Option<CropResult>,
open_boxes: Option<usize>,
}
impl CropTool {
impl<'a> CropTool<'a> {
// PLAN: pdf-canvas-w9 asked for `CropTool::new(allowed)`; the tool needs the placed pages to know which page a press is on.
/// A tool over `placed`; `allowed` is false when page editing is forbidden.
pub(crate) fn new(allowed: bool, placed: &[PlacedPage], rotation: u16, page_size_of: impl Fn(usize) -> (f32, f32)) -> Self {
/// A tool over `placed`, with its rectangle in `drag`; `allowed` is false when page editing is forbidden.
pub(crate) fn new(
allowed: bool,
drag: &'a mut PageBoxDrag,
placed: &[PlacedPage],
rotation: u16,
page_size_of: impl Fn(usize) -> (f32, f32),
) -> Self {
let sizes = placed.iter().map(|p| (p.page, page_size_of(p.page))).collect();
Self { allowed, placed: placed.to_vec(), sizes, rotation, drag: PageBoxDrag::default(), done: None, open_boxes: None }
Self { allowed, placed: placed.to_vec(), sizes, rotation, drag, done: None, open_boxes: None }
}
/// The crop the last release made, once.
@@ -72,7 +79,7 @@ impl CropTool {
}
}
impl CanvasTool for CropTool {
impl CanvasTool for CropTool<'_> {
fn press(&mut self, pointer: &ToolPointer, _cx: &mut ToolContext<'_>) -> ToolAction {
if !self.allowed {
return ToolAction::Ignored;
+5 -5
View File
@@ -147,9 +147,9 @@ enum Arm<'a> {
/// The signature rectangle of Digitally sign.
SignArea(SignAreaTool<'a>),
/// Snapshot: the area to copy.
Snapshot(SnapshotTool),
Snapshot(SnapshotTool<'a>),
/// Crop: the rectangle to crop the page to, or a double click for Set Page Boxes.
Crop(CropTool),
Crop(CropTool<'a>),
}
/// The page tool of a pass, handed to the tool host: every [`CanvasTool`] method forwards to the arm.
@@ -332,7 +332,7 @@ pub(crate) fn page_tool<'a>(
QuickTool::Measure(t) => {
Arm::Measure(MeasureTool::new(&mut session.measure, cx.doc, map, cx.info, t, cx.units, cx.author, cx.host, cx.wake.clone()))
}
QuickTool::Crop => Arm::Crop(CropTool::new(cx.can_crop, cx.pages, cx.rotation, size)),
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)),
QuickTool::Fill(fill_tool) => {
let input = FillInput {
@@ -369,8 +369,8 @@ pub(crate) fn page_tool<'a>(
None => Arm::Hand(HandTool::new(cx.navigation)),
},
QuickTool::Link => Arm::Area(Box::new(LinkTool::new(session, cx.pages, cx.info, *press))),
QuickTool::SignArea { certify } => Arm::SignArea(SignAreaTool::new(certify, cx.pages, cx.rotation, cx.info)),
QuickTool::Snapshot => Arm::Snapshot(SnapshotTool::new(cx.pages, cx.rotation, size)),
QuickTool::SignArea { certify } => Arm::SignArea(SignAreaTool::new(certify, &mut session.box_drag, cx.pages, cx.rotation, cx.info)),
QuickTool::Snapshot => Arm::Snapshot(SnapshotTool::new(&mut session.box_drag, cx.pages, cx.rotation, size)),
};
PageTool { arm, tool, selects_text, released: false, presenting: cx.presenting, presenter_keys: Vec::new() }
}
+9 -7
View File
@@ -27,20 +27,22 @@ pub enum AreaChoice {
/// The Snapshot tool, built per pass over the placed pages. A drag reports the page and the
/// rectangle in page view points; a click with no drag, or under 2 points either way, reports nothing.
pub(crate) struct SnapshotTool {
pub(crate) struct SnapshotTool<'a> {
placed: Vec<PlacedPage>,
/// Per placed page: its unrotated view-space size.
sizes: Vec<(usize, (f32, f32))>,
rotation: u16,
drag: PageBoxDrag,
/// The session's drag, kept from the press to the release.
drag: &'a mut PageBoxDrag,
done: Option<(usize, [f32; 4])>,
}
impl SnapshotTool {
/// A tool over `placed`, in a view turned by `rotation`; `page_size_of` gives a page's size in view points.
pub(crate) fn new(placed: &[PlacedPage], rotation: u16, page_size_of: impl Fn(usize) -> (f32, f32)) -> Self {
impl<'a> SnapshotTool<'a> {
/// A tool over `placed`, in a view turned by `rotation`, with its rectangle in `drag`; `page_size_of` gives a
/// page's size in view points.
pub(crate) fn new(drag: &'a mut PageBoxDrag, placed: &[PlacedPage], rotation: u16, page_size_of: impl Fn(usize) -> (f32, f32)) -> Self {
let sizes = placed.iter().map(|p| (p.page, page_size_of(p.page))).collect();
Self { placed: placed.to_vec(), sizes, rotation, drag: PageBoxDrag::default(), done: None }
Self { placed: placed.to_vec(), sizes, rotation, drag, done: None }
}
/// The area the last release made, once.
@@ -54,7 +56,7 @@ impl SnapshotTool {
}
}
impl CanvasTool for SnapshotTool {
impl CanvasTool for SnapshotTool<'_> {
fn press(&mut self, pointer: &ToolPointer, _cx: &mut ToolContext<'_>) -> ToolAction {
match page_under(&self.placed, pointer.pos) {
Some(placed) if self.drag.press(placed, pointer.pos) => ToolAction::Claimed,
+8 -5
View File
@@ -361,8 +361,9 @@ pub(crate) fn parts(
let markers = marker_boxes(info, &anns, icon_side);
let Some(id) = pick_at(&anns, at, tolerance, icon_side, &markers) else { return Vec::new() };
let Some(found) = anns.iter().find(|a| a.id == id) else { return Vec::new() };
let mut shapes: Vec<Rect> =
if found.kind.is_markup() { found.points.chunks(4).filter_map(bounds).collect() } else { vec![found.extent(icon_side)] };
// A markup's lines, or its box when it has none (a highlight with no /QuadPoints is picked by its box too).
let lines: Vec<Rect> = if found.kind.is_markup() { found.points.chunks(4).filter_map(bounds).collect() } else { Vec::new() };
let mut shapes = if lines.is_empty() { vec![found.extent(icon_side)] } else { lines };
shapes.extend(markers.iter().filter(|(m, _)| *m == id).map(|(_, r)| *r));
let screen_shapes: Vec<egui::Rect> = shapes
.iter()
@@ -414,7 +415,7 @@ pub(super) fn edited_user_rect(info: &DocInfo, a: &Annotation, edit: &Annotation
pub(crate) fn edit(e: AnnotationEdit, map: &PageMap<'_>, view: &mut CommentView, info: &DocInfo, prefs: &CommentPrefs) -> Option<Edit> {
match e {
AnnotationEdit::Create(a) => create(&a, map, prefs),
AnnotationEdit::Move { ids, delta } => moved(&ids, delta, info),
AnnotationEdit::Move { ids, delta } => moved(&ids, delta, map, info),
AnnotationEdit::Resize { id, rect } => {
let (page, index) = key_of(id);
engine(info, page, index).filter(|a| resizable(a))?;
@@ -463,7 +464,7 @@ pub(crate) fn edit(e: AnnotationEdit, map: &PageMap<'_>, view: &mut CommentView,
}
}
fn moved(ids: &[AnnotationId], delta: Vec2, info: &DocInfo) -> Option<Edit> {
fn moved(ids: &[AnnotationId], delta: Vec2, map: &PageMap<'_>, info: &DocInfo) -> Option<Edit> {
if delta == Vec2::ZERO {
return None;
}
@@ -481,7 +482,9 @@ fn moved(ids: &[AnnotationId], delta: Vec2, info: &DocInfo) -> Option<Edit> {
// moved with its comment.
let a = engine(info, page, index).filter(|a| !a.is_reply() && !on_text(a) && !a.locked)?;
let p = info.pages.get(page)?;
let (dx, dy) = user_delta(p, delta);
// The delta is in the page as shown, the view's turn included: the map takes it back to user space.
let (from, to) = (map.to_user(page, Point::ZERO), map.to_user(page, Point::ZERO + delta));
let (dx, dy) = (to[0] - from[0], to[1] - from[1]);
if !(dx.is_finite() && dy.is_finite()) {
return None;
}
+6 -4
View File
@@ -28,9 +28,11 @@ fn page_map<'a>(placed: &'a [PlacedPage], info: &'a DocInfo, rotation: u16) -> P
PageMap::new(info, placed, rotation)
}
/// A kurbo rectangle as an egui one, for comparing a marker with a page's screen rectangle.
fn screen(r: craft::behavior::kurbo::Rect) -> Rect {
Rect::from_min_max(pos2(r.x0 as f32, r.y0 as f32), pos2(r.x1 as f32, r.y1 as f32))
/// A rectangle in page units of `page` on screen, through the page map, for comparing a marker with the page's
/// screen rectangle.
fn screen(map: &PageMap<'_>, page: usize, r: craft::behavior::kurbo::Rect) -> Rect {
let corner = |x, y| map.to_screen(page, Point::new(x, y)).expect("the page is placed");
Rect::from_two_pos(corner(r.x0, r.y0), corner(r.x1, r.y1))
}
/// A sticky note on a page turned a quarter is drawn upright beside its `/Rect`: an edit
@@ -412,7 +414,7 @@ fn markup_comment_markers_stay_inside_turned_and_zoomed_pages() {
let both = with(vec![top, a.clone()]);
let markers = bridge::markers(&both, &map, side);
let (_, marker) = *markers.last().expect("commented markup has a marker");
let inside = screen(marker);
let inside = screen(&map, 0, marker);
assert!(xf.rect.contains_rect(inside), "page/view rotation {page_rotation}/{rotation}, zoom {zoom}, anchor {rect:?}: {inside:?}");
let annotations = bridge::annotations(&both, &map, QuickTool::Select, true);
let picked = pick_at(&annotations, marker.center(), tolerance_of(&map), side, &markers);
+3 -4
View File
@@ -26,6 +26,7 @@ use craft::ui::layout_components::Row;
use craft::ui::primitives::textures::{CachedTexture, Revision, TextureStore, TextureStoreOptions};
use craft::ui::primitives::{ContentPainterCx, Draw};
use craft::ui::runtime::env::UnitKind;
use craft::ui::runtime::focus::FocusRequest;
use craft::ui::sections::Section;
use craft::ui::text::NumberField;
use egui::{Color32, CornerRadius, Pos2, Rect};
@@ -1032,10 +1033,8 @@ fn draft_session(
let mut selection = draft.selection.unwrap_or_else(|| TextSelection::caret_at(draft.text.chars().count()));
let mut host = AddedTextHost { draft, ctx: ctx.clone(), view: body.view, layout, laid_out: None };
let paper = Rect::from_min_size(at, egui::vec2(width, host.galley().rect.height() + INSET.y * 2.0));
let output = TextEditor::new(tl!("Added text"), &mut host, Bind::bound(&mut selection)).esc_commits(false).id(editor_id).show_at(ui, paper);
if fresh {
output.response.request_focus();
}
let editor = TextEditor::new(tl!("Added text"), &mut host, Bind::bound(&mut selection)).esc_commits(false).id(editor_id);
let output = if fresh { editor.request_focus(FocusRequest::Take) } else { editor }.show_at(ui, paper);
let ended = output.ended.filter(|end| !(fresh && *end == EditorEnd::Commit));
let galley = host.galley();
let paper = Rect::from_min_size(at, egui::vec2(width, galley.rect.height() + INSET.y * 2.0));
@@ -5,7 +5,8 @@ use std::ops::Range;
use craft::behavior::kurbo::Point;
use craft::pages::ui::PageBody;
use craft::text::ui::editor::{CaretLine, EditKind, EditorEnd, HostLine, HostRun, TextEditor, TextEditorHost, TextSelection};
use craft::ui::contract::{Bind, Show as _};
use craft::ui::contract::{Bind, Props as _, Show as _};
use craft::ui::runtime::focus::FocusRequest;
use egui::{Color32, FontId, Pos2, Rect, Stroke, vec2};
use pdfcraft_engine::{BorderShape, Edit};
use pdfcraft_render::DocInfo;
@@ -254,11 +255,9 @@ pub(crate) fn show(ui: &mut egui::Ui, body: &PageBody<'_>, session: &mut PageSes
// Escape cancels: the page's Escape belongs to the editor while a box is open.
// A box still waiting for its focus was started in this pass by the press the editor also sees as a press on the
// canvas background: that press is the one that opened it, not a click away from it.
let fresh = tb.focus;
let out = TextEditor::new(tl!("Text to place"), &mut host, Bind::bound(fill_selection)).esc_commits(false).show_at(ui, rect);
if std::mem::take(&mut tb.focus) {
out.response.request_focus();
}
let fresh = std::mem::take(&mut tb.focus);
let editor = TextEditor::new(tl!("Text to place"), &mut host, Bind::bound(fill_selection)).esc_commits(false);
let out = if fresh { editor.request_focus(FocusRequest::Take) } else { editor }.show_at(ui, rect);
match out.ended.filter(|end| !(fresh && *end == EditorEnd::Commit))? {
EditorEnd::Cancel => {
*fill_text = None;
+5 -4
View File
@@ -15,7 +15,8 @@ const LEAST_SIDE: f32 = 8.0;
/// Drags a signature rectangle on one page. The rectangle of a finished drag is taken with
/// [`SignAreaTool::finished`], in user space of its page.
pub(crate) struct SignAreaTool<'a> {
drag: PageBoxDrag,
/// The session's drag, kept from the press to the release.
drag: &'a mut PageBoxDrag,
/// Certify (DocMDP) rather than sign: names the command that takes the same numbers.
certify: bool,
placed: &'a [PlacedPage],
@@ -27,9 +28,9 @@ pub(crate) struct SignAreaTool<'a> {
impl<'a> SignAreaTool<'a> {
/// A tool over the placed pages that draws the area for a certification when `certify`, for a
/// signature otherwise.
pub(crate) fn new(certify: bool, placed: &'a [PlacedPage], rotation: u16, info: &'a DocInfo) -> Self {
Self { drag: PageBoxDrag::default(), certify, placed, rotation, info, drawn: None }
/// signature otherwise, with its rectangle in `drag`.
pub(crate) fn new(certify: bool, drag: &'a mut PageBoxDrag, placed: &'a [PlacedPage], rotation: u16, info: &'a DocInfo) -> Self {
Self { drag, certify, placed, rotation, info, drawn: None }
}
/// The (page, user-space rectangle) a drag finished with since the last call, once.
+9 -9
View File
@@ -2177,7 +2177,7 @@ fn photos_and_images_dropped_on_the_signature_pad_are_imported_not_opened() {
}
/// #2149: the first save after Fill & Sign places a signature asks whether to lock it into the
/// page. Lock and save flattens it; Keep editable keeps it a comment; Don't ask again becomes
/// page. Lock and save flattens it; Keep editable keeps it a comment; Don't show again becomes
/// the preference; the Fill & Sign panel has the switch too, once, and the Sign menu no longer
/// does (review on #2348).
#[test]
@@ -2197,21 +2197,21 @@ fn the_first_save_after_placing_a_signature_asks_to_lock_it() {
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().save_active(&ctx_h, SaveTarget::InPlace), "asks first");
h.run_steps(2);
h.get_by_label("Lock your signature into the page?");
h.get_by_role_and_label(egui::accesskit::Role::Dialog, "Lock your signature into the page?");
h.get_by_label("Keep editable").click();
h.run_steps(3);
assert!(!h.state().session.get(h.state().views[0].id).unwrap().dirty, "saved");
assert_eq!(signed(&h), 1, "still a removable comment");
let ctx_h = h.ctx.clone();
assert!(h.state_mut().save_active(&ctx_h, SaveTarget::InPlace), "no new signature: no question");
// A new signature asks again; Lock and save with Don't ask again flattens and remembers.
// A new signature asks again; Lock and save with Don't show again flattens and remembers.
h.execute("sign.fill.signature");
h.run_steps(2);
click(&mut h, 40.0, 200.0);
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().save_active(&ctx_h, SaveTarget::InPlace));
h.run_steps(2);
h.get_by_label("Don't ask again").click();
h.get_by_role_and_label(egui::accesskit::Role::CheckBox, "Don't show again").click();
h.run_steps(1);
h.get_by_label("Lock and save").click();
h.run_steps(3);
@@ -2407,7 +2407,7 @@ fn locking_a_signature_clears_the_comment_selection() {
}
/// #2348 review: when locking the signature fails (here the document's security allows comments
/// but not changes), nothing is lost: the next save asks again, and "Don't ask again" isn't
/// but not changes), nothing is lost: the next save asks again, and "Don't show again" isn't
/// remembered.
#[test]
fn a_lock_that_fails_keeps_the_question() {
@@ -2437,7 +2437,7 @@ fn a_lock_that_fails_keeps_the_question() {
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().save_active(&ctx_h, SaveTarget::InPlace), "asks first");
h.run_steps(2);
h.get_by_label("Don't ask again").click();
h.get_by_role_and_label(egui::accesskit::Role::CheckBox, "Don't show again").click();
h.run_steps(1);
h.get_by_label("Lock and save").click();
h.run_steps(3);
@@ -2446,7 +2446,7 @@ fn a_lock_that_fails_keeps_the_question() {
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().save_active(&ctx_h, SaveTarget::InPlace), "the next save asks again");
h.run_steps(2);
h.get_by_label("Lock your signature into the page?");
h.get_by_role_and_label(egui::accesskit::Role::Dialog, "Lock your signature into the page?");
let _ = std::fs::remove_file(&out);
}
@@ -2518,7 +2518,7 @@ fn the_lock_question_is_skipped_or_kept_where_it_must_be() {
let _ = std::fs::remove_file(&out);
}
/// #2149: Cancel saves nothing, and Keep editable with Don't ask again turns the question off.
/// #2149: Cancel saves nothing, and Keep editable with Don't show again turns the question off.
#[test]
fn cancelling_the_lock_question_saves_nothing() {
let out = std::env::temp_dir().join(format!("pdfcraft-lock-cancel-{}.pdf", std::process::id()));
@@ -2538,7 +2538,7 @@ fn cancelling_the_lock_question_saves_nothing() {
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().save_active(&ctx_h, SaveTarget::InPlace), "asked again");
h.run_steps(2);
h.get_by_label("Don't ask again").click();
h.get_by_role_and_label(egui::accesskit::Role::CheckBox, "Don't show again").click();
h.run_steps(1);
h.get_by_label("Keep editable").click();
h.run_steps(3);
+2 -2
View File
@@ -122,7 +122,7 @@ fn create_signing_id(h: &mut Harness<'static, PdfCraftApp>) {
assert_eq!(h.state().sign_draft.as_ref().map(|d| d.step), Some(SignStep::Configure), "no IDs yet: configure one");
// Create a new digital ID (P-256 keeps the test fast).
h.get_by_label("Configure a Digital ID for Signing");
h.get_by_role_and_label(egui::accesskit::Role::Dialog, "Configure a Digital ID for Signing");
field(h, "Name").click();
h.run_steps(1);
field(h, "Name").type_text("Grace Hopper");
@@ -870,7 +870,7 @@ fn an_imported_digital_id_can_be_removed() {
assert!(p12.exists(), "the file itself is not deleted");
// With no IDs left the dialog offers to configure one.
assert_eq!(h.state().sign_draft.as_ref().map(|d| d.step), Some(SignStep::Configure));
h.get_by_label("Configure a Digital ID for Signing");
h.get_by_role_and_label(egui::accesskit::Role::Dialog, "Configure a Digital ID for Signing");
}
/// Sign-and-save must use the same destination metadata policy as Save As.