Skip to content

Commit 535449a

Browse files
committed
M5.7: cache saved-signature menu eligibility (#1288)
1 parent a1e2848 commit 535449a

12 files changed

Lines changed: 377 additions & 20 deletions

File tree

‎ROADMAP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md
201201
## Progress log
202202

203203
- **2026-10-28 (M5, #1288):** Saved Fill & Sign signatures and initials can each retain a resized placement rectangle for future placements, with explicit commands and focused engine, annotation, automation and UI regressions.
204+
Menu availability reuses the source check until the document or selection changes; invoking the action checks the source again.
204205
Estimates unchanged.
205206

206207
- **2026-10-27 (M10, OCR models):** Model staging downloads and verifies the ONNX pair after the upstream RTEN URL brownout; runtime discovery prefers complete ONNX pairs per directory, retains complete RTEN fallback and preserves directory precedence.

‎crates/ui-egui/src/canvas.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,11 +3246,8 @@ fn apply_frame_requests(app: &mut PdfCraftApp, index: usize, ui: &mut egui::Ui,
32463246
if let Some((p, i)) = app.views[index].comments.default_request.take() {
32473247
app.make_comment_default(p, i);
32483248
}
3249-
if let Some((page, annotation, initials)) = app.views[index].comments.signature_size_request.take() {
3250-
let doc = app.views[index].id.0;
3251-
app.pending_signature_size_request = Some((doc, page, annotation, initials));
3252-
app.execute(if initials { "sign.fill.initials.size" } else { "sign.fill.signature.size" });
3253-
app.pending_signature_size_request = None;
3249+
if let Some(request) = app.views[index].comments.signature_size_request.take() {
3250+
app.dispatch_signature_size_request(request);
32543251
}
32553252
if let Some((p, i, open)) = app.views[index].comments.attachment_request.take() {
32563253
let doc = app.views[index].id;

‎crates/ui-egui/src/commands.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ impl PdfCraftApp {
362362
Some(true)
363363
}
364364

365+
/// Dispatch a menu request through the same fresh validation as a direct command.
366+
/// Clear the transient identity even when command enablement refuses execution.
367+
pub(crate) fn dispatch_signature_size_request(&mut self, request: crate::fill_sign::SignatureSizeRequest) -> bool {
368+
self.pending_signature_size_request = Some(request);
369+
let executed = self.execute(if request.initials { "sign.fill.initials.size" } else { "sign.fill.signature.size" });
370+
self.pending_signature_size_request = None;
371+
executed
372+
}
373+
365374
/// Validate the selected appearance and saved target for the explicit size-copy command.
366375
/// The same checks drive enablement, refusal text, and execution.
367376
fn signature_size_source(&self, id: &str) -> Result<pdfcraft_engine::SignaturePlacementSize, String> {
@@ -372,8 +381,9 @@ impl PdfCraftApp {
372381
let Some((view_index, doc_id)) = self.active_ids() else { return Err(tl!("Open a document first").to_string()) };
373382
let Some(view) = self.views.get(view_index) else { return Err(tl!("Open a document first").to_string()) };
374383
let Some((page, annotation)) = view.comments.selected else { return Err(tl!("Select a placed signature first").to_string()) };
375-
if let Some((requested_doc, requested_page, requested_annotation, requested_initials)) = self.pending_signature_size_request
376-
&& (requested_doc != doc_id.0 || requested_page != page || requested_annotation != annotation || requested_initials != initials)
384+
if self
385+
.pending_signature_size_request
386+
.is_some_and(|request| request != crate::fill_sign::SignatureSizeRequest { doc: doc_id, page, annotation, initials })
377387
{
378388
return Err(tl!("The selection changed. Select the signature again.").to_string());
379389
}
@@ -863,6 +873,9 @@ impl PdfCraftApp {
863873
}
864874
}
865875

876+
#[cfg(test)]
877+
mod signature_size_request_tests;
878+
866879
/// The tab after `current` of `count` (before it when `forward` is false), wrapping around. From
867880
/// no tab, forward is the first and back the last. `None` when there are no tabs.
868881
fn tab_after(current: Option<usize>, count: usize, forward: bool) -> Option<usize> {
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
use egui_kittest::Harness;
2+
use egui_kittest::kittest::Queryable;
3+
use pdfcraft_engine::{DocId, Edit, NewAnnotation, Shape, SignaturePlacementSize, Style};
4+
5+
use crate::PdfCraftApp;
6+
use crate::fill_sign::{SavedSig, SignatureSizeRequest};
7+
8+
fn add_document(app: &mut PdfCraftApp) -> DocId {
9+
let bytes = app.session.create_blank(300.0, 400.0, 1).unwrap();
10+
app.open_bytes("signature-request.pdf", None, bytes.as_ref().clone()).unwrap();
11+
let id = app.views[app.active.unwrap()].id;
12+
for signature in [true, true, false] {
13+
let shape = if signature {
14+
pdfcraft_engine::typed_signature_shape([30.0, 200.0], "Synthetic User", 32.0, 0).unwrap()
15+
} else {
16+
Shape::Rectangle { rect: [30.0, 180.0, 100.0, 220.0] }
17+
};
18+
app.session
19+
.apply(
20+
id,
21+
Edit::AddAnnotation(NewAnnotation {
22+
page: 0,
23+
style: Style::default_for(&shape),
24+
shape,
25+
contents: String::new(),
26+
author: String::new(),
27+
}),
28+
)
29+
.unwrap();
30+
}
31+
app.views[app.active.unwrap()].comments.selected = Some((0, 0));
32+
id
33+
}
34+
35+
fn app() -> (PdfCraftApp, DocId) {
36+
let mut app = PdfCraftApp::new();
37+
app.set_option("language", "en").unwrap();
38+
app.signature = Some(SavedSig::Typed("Synthetic User".into()));
39+
app.signature_placement_size = Some(SignaturePlacementSize::new(70.0, 16.0).unwrap());
40+
let id = add_document(&mut app);
41+
(app, id)
42+
}
43+
44+
fn paint_menu(ui: &mut egui::Ui, app: &mut PdfCraftApp) {
45+
let view = &mut app.views[app.active.unwrap()];
46+
let doc = app.session.get(view.id).unwrap();
47+
crate::comments::signature_size_items(ui, view, doc, app.signature.is_some(), app.initials.is_some());
48+
}
49+
50+
fn cache_menu(ctx: &egui::Context, app: &mut PdfCraftApp) {
51+
let mut output = ctx.run_ui(egui::RawInput::default(), |ui| paint_menu(ui, app));
52+
output.textures_delta.clear();
53+
}
54+
55+
#[derive(Debug, PartialEq)]
56+
struct DocumentState {
57+
id: DocId,
58+
bytes: Vec<u8>,
59+
dirty: bool,
60+
undo: Option<String>,
61+
redo: Option<String>,
62+
}
63+
64+
fn documents(app: &PdfCraftApp) -> Vec<DocumentState> {
65+
app.session
66+
.docs()
67+
.iter()
68+
.map(|doc| DocumentState {
69+
id: doc.id,
70+
bytes: app.session.save_bytes(doc.id).unwrap().as_ref().clone(),
71+
dirty: doc.dirty,
72+
undo: doc.can_undo().map(str::to_owned),
73+
redo: doc.can_redo().map(str::to_owned),
74+
})
75+
.collect()
76+
}
77+
78+
#[test]
79+
fn signature_size_request_rejects_stale_identity_and_removed_target_and_always_clears_pending() {
80+
for change in 0..5 {
81+
let (mut app, id) = app();
82+
let request = SignatureSizeRequest { doc: id, page: 0, annotation: 0, initials: false };
83+
let ctx = egui::Context::default();
84+
cache_menu(&ctx, &mut app);
85+
match change {
86+
0 => {
87+
add_document(&mut app);
88+
}
89+
1 => app.views[0].comments.selected = Some((0, 1)),
90+
2 => app.signature = None,
91+
3 => app.views[0].comments.selected = None,
92+
_ => app.active = None,
93+
}
94+
let before = documents(&app);
95+
let preferences = (app.signature_placement_size, app.initials_placement_size);
96+
assert!(!app.dispatch_signature_size_request(request), "stale request case {change} must be refused");
97+
assert_eq!((app.signature_placement_size, app.initials_placement_size), preferences);
98+
assert_eq!(documents(&app), before);
99+
assert!(app.pending_signature_size_request.is_none(), "early refusal must not leave a request that affects a later command");
100+
}
101+
}
102+
103+
#[test]
104+
fn signature_size_request_command_freshly_reads_resized_source_and_refuses_deleted_source_after_menu_cache() {
105+
let (mut app, id) = app();
106+
let ctx = egui::Context::default();
107+
cache_menu(&ctx, &mut app);
108+
app.session.apply(id, Edit::ResizeAnnotation { page: 0, index: 0, rect: [30.0, 180.0, 100.0, 197.0] }).unwrap();
109+
let size = app.session.get(id).unwrap().signature_placement_size(0, 0).unwrap().unwrap();
110+
let before = documents(&app);
111+
assert!(app.execute("sign.fill.signature.size"));
112+
assert_eq!(app.signature_placement_size, Some(size), "execution reads the current geometry rather than a cached placement size");
113+
assert_eq!(documents(&app), before);
114+
assert!(app.pending_signature_size_request.is_none());
115+
116+
cache_menu(&ctx, &mut app);
117+
// Remove both suitable sources so the ordinary rectangle reuses the cached source index.
118+
for _ in 0..2 {
119+
app.session.apply(id, Edit::DeleteAnnotation { page: 0, index: 0 }).unwrap();
120+
}
121+
let before = documents(&app);
122+
assert!(!app.execute("sign.fill.signature.size"));
123+
assert_eq!(app.signature_placement_size, Some(size));
124+
assert_eq!(documents(&app), before);
125+
assert!(app.pending_signature_size_request.is_none());
126+
}
127+
128+
#[test]
129+
fn signature_size_menu_cache_actual_items_update_across_frames_source_changes_and_target_changes() {
130+
let (app, id) = app();
131+
let mut h = Harness::builder().with_size(egui::vec2(600.0, 300.0)).build_ui_state(paint_menu, app);
132+
h.run_steps(4);
133+
assert!(h.query_by_label("Use this size for saved signature").is_some());
134+
assert!(h.query_by_label("Use this size for saved initials").is_none());
135+
h.state_mut().views[0].comments.selected = Some((0, 2));
136+
h.run_steps(3);
137+
assert!(h.query_by_label("Use this size for saved signature").is_none(), "changing selection invalidates the previous positive result");
138+
h.state_mut().views[0].comments.selected = Some((0, 0));
139+
h.run_steps(3);
140+
assert!(h.query_by_label("Use this size for saved signature").is_some());
141+
for _ in 0..2 {
142+
h.state_mut().session.apply(id, Edit::DeleteAnnotation { page: 0, index: 0 }).unwrap();
143+
}
144+
h.run_steps(3);
145+
assert!(h.query_by_label("Use this size for saved signature").is_none(), "an ordinary source at the same index is not eligible");
146+
h.state_mut().session.undo(id).unwrap();
147+
h.run_steps(3);
148+
assert!(h.query_by_label("Use this size for saved signature").is_some());
149+
h.state_mut().signature = None;
150+
h.run_steps(3);
151+
assert!(h.query_by_label("Use this size for saved signature").is_none());
152+
h.state_mut().initials = Some(SavedSig::Typed("SU".into()));
153+
h.run_steps(3);
154+
h.get_by_label("Use this size for saved initials").click();
155+
h.run_steps(2);
156+
let request = h.state_mut().views[0].comments.signature_size_request.take().unwrap();
157+
assert_eq!(request, SignatureSizeRequest { doc: id, page: 0, annotation: 0, initials: true });
158+
let before = documents(h.state());
159+
assert!(h.state_mut().dispatch_signature_size_request(request));
160+
assert!(h.state().initials_placement_size.is_some());
161+
assert_eq!(documents(h.state()), before);
162+
assert!(h.state().pending_signature_size_request.is_none());
163+
}

‎crates/ui-egui/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ pub struct PdfCraftApp {
573573
pub initials: Option<fill_sign::SavedSig>,
574574
pub signature_placement_size: Option<pdfcraft_engine::SignaturePlacementSize>,
575575
pub initials_placement_size: Option<pdfcraft_engine::SignaturePlacementSize>,
576-
pub(crate) pending_signature_size_request: Option<(u64, usize, usize, bool)>,
576+
pub(crate) pending_signature_size_request: Option<fill_sign::SignatureSizeRequest>,
577577
/// The Create signature / initials dialog, and its typed preview.
578578
pub signature_draft: fill_sign::SigDraft,
579579
pub(crate) signature_preview: Option<(fill_sign::SavedSig, egui::TextureHandle)>,

‎crates/ui-egui/src/panels/right.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,8 @@ pub fn right_panel(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
409409
if let Some((p, i)) = app.views.get_mut(index).and_then(|v| v.comments.default_request.take()) {
410410
app.make_comment_default(p, i);
411411
}
412-
if let Some((page, annotation, initials)) = app.views.get_mut(index).and_then(|v| v.comments.signature_size_request.take()) {
413-
let doc = app.views[index].id.0;
414-
app.pending_signature_size_request = Some((doc, page, annotation, initials));
415-
app.execute(if initials { "sign.fill.initials.size" } else { "sign.fill.signature.size" });
416-
app.pending_signature_size_request = None;
412+
if let Some(request) = app.views.get_mut(index).and_then(|v| v.comments.signature_size_request.take()) {
413+
app.dispatch_signature_size_request(request);
417414
}
418415
if let Some((p, i, open)) = app.views.get_mut(index).and_then(|v| v.comments.attachment_request.take()) {
419416
app.comment_attachment_action(id, p, i, open);

‎crates/ui-egui/src/tools/comments/menu.rs‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,60 @@ use super::{
55
icons, theme, tool_for, vec2,
66
};
77

8+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9+
struct SignatureSizeMenuKey {
10+
doc: pdfcraft_engine::DocId,
11+
page: usize,
12+
annotation: usize,
13+
generation: u64,
14+
}
15+
16+
impl SignatureSizeMenuKey {
17+
fn new(doc: &pdfcraft_engine::Document, page: usize, annotation: usize) -> Self {
18+
Self { doc: doc.id, page, annotation, generation: doc.edit_generation() }
19+
}
20+
}
21+
22+
#[derive(Clone, Copy)]
23+
struct SignatureSizeMenuCache {
24+
key: SignatureSizeMenuKey,
25+
eligible: bool,
26+
}
27+
28+
fn signature_size_menu_eligible(ctx: &egui::Context, key: SignatureSizeMenuKey, has_target: bool, query: impl FnOnce() -> bool) -> bool {
29+
if !has_target {
30+
return false;
31+
}
32+
// Replace one entry, rather than retaining a new egui data ID for every document,
33+
// selection and edit generation. Both comment menu surfaces share this slot.
34+
let id = egui::Id::unique("signature-size-menu-eligibility");
35+
if let Some(cached) = ctx.data(|data| data.get_temp::<SignatureSizeMenuCache>(id)).filter(|cached| cached.key == key) {
36+
return cached.eligible;
37+
}
38+
let eligible = query();
39+
ctx.data_mut(|data| data.insert_temp(id, SignatureSizeMenuCache { key, eligible }));
40+
eligible
41+
}
42+
843
/// Offer explicit size-copy actions only for an appearance the engine recognizes as a
944
/// Fill & Sign signature, and only when the corresponding saved target exists.
1045
pub(crate) fn signature_size_items(ui: &mut egui::Ui, view: &mut DocView, doc: &pdfcraft_engine::Document, has_signature: bool, has_initials: bool) {
1146
let Some((page, index)) = view.comments.selected else { return };
12-
let Ok(Some(_)) = doc.signature_placement_size(page, index) else { return };
47+
let key = SignatureSizeMenuKey::new(doc, page, index);
48+
if !signature_size_menu_eligible(ui.ctx(), key, has_signature || has_initials, || {
49+
matches!(doc.signature_placement_size(page, index), Ok(Some(_)))
50+
}) {
51+
return;
52+
}
1353
let mut offered = false;
1454
if has_signature {
1555
if ui
1656
.button(tl!("Use this size for saved signature"))
1757
.on_hover_text(tl!("Future placements use this size. Existing signatures are unchanged."))
1858
.clicked()
1959
{
20-
view.comments.signature_size_request = Some((page, index, false));
60+
view.comments.signature_size_request =
61+
Some(crate::fill_sign::SignatureSizeRequest { doc: view.id, page, annotation: index, initials: false });
2162
ui.close();
2263
}
2364
offered = true;
@@ -28,7 +69,8 @@ pub(crate) fn signature_size_items(ui: &mut egui::Ui, view: &mut DocView, doc: &
2869
.on_hover_text(tl!("Future placements use this size. Existing signatures are unchanged."))
2970
.clicked()
3071
{
31-
view.comments.signature_size_request = Some((page, index, true));
72+
view.comments.signature_size_request =
73+
Some(crate::fill_sign::SignatureSizeRequest { doc: view.id, page, annotation: index, initials: true });
3274
ui.close();
3375
}
3476
offered = true;
@@ -403,3 +445,6 @@ pub fn status_badge(state: &str) -> Option<(&'static str, &'static str, Color32)
403445
_ => None,
404446
}
405447
}
448+
449+
#[cfg(test)]
450+
mod signature_size_menu_cache;

0 commit comments

Comments
 (0)