From 63ff373b46331bc3f4264cd25560fce88a14a488 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 18:11:47 -0500 Subject: [PATCH] lead: L-219 cascade rounds 9-12 (open path callers), harness contexts hoisted into statements, test functions make their own Context --- crates/ui-egui/src/app.rs | 2 +- crates/ui-egui/src/app/files.rs | 22 ++++++++-------- crates/ui-egui/src/app/input.rs | 12 ++++----- crates/ui-egui/src/canvas.rs | 2 +- crates/ui-egui/src/canvas/image_pick.rs | 19 +++++++++----- crates/ui-egui/src/canvas/pictures.rs | 2 +- crates/ui-egui/src/canvas/sign_tab_tests.rs | 10 ++++--- crates/ui-egui/src/canvas/signature_drag.rs | 2 +- crates/ui-egui/src/canvas/tests.rs | 11 ++++---- crates/ui-egui/src/commands.rs | 4 +-- crates/ui-egui/src/dialogs/word_count_ui.rs | 5 ++-- crates/ui-egui/src/editing.rs | 21 ++++++++------- crates/ui-egui/src/editing/close.rs | 2 +- crates/ui-egui/src/editing/tests.rs | 10 +++---- crates/ui-egui/src/frame_guard.rs | 7 ++--- crates/ui-egui/src/home.rs | 2 +- crates/ui-egui/src/menus.rs | 2 +- crates/ui-egui/src/menus/tab_context.rs | 8 +++--- crates/ui-egui/src/panels/format_text_ui.rs | 2 +- crates/ui-egui/src/panels/right.rs | 4 +-- crates/ui-egui/src/persist/recovery.rs | 6 +++-- .../ui-egui/src/platform/open_worker/tests.rs | 20 +++++++++----- crates/ui-egui/src/properties_session.rs | 4 +-- crates/ui-egui/src/script_dialog.rs | 25 +++++++++++------- crates/ui-egui/src/tools/content_ui.rs | 2 +- crates/ui-egui/src/tools/edit_text_ui.rs | 6 ++--- crates/ui-egui/src/tools/sign_ui.rs | 4 +-- .../ui-egui/src/workflows/combine_ui/tests.rs | 4 +-- crates/ui-egui/src/workflows/compare_ui.rs | 2 +- crates/ui-egui/src/workflows/create_ui.rs | 26 ++++++++++++------- crates/ui-egui/src/workflows/js_ui.rs | 6 ++--- crates/ui-egui/src/workflows/ocr_ui.rs | 6 ++--- crates/ui-egui/src/workflows/ocr_verify.rs | 8 +++--- crates/ui-egui/src/workflows/optimize_ui.rs | 2 +- 34 files changed, 153 insertions(+), 117 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 874fd888a6..f89877c1b2 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -655,7 +655,7 @@ impl PdfCraftApp { } // PLAN: assumed PdfCraftApp::open_intake(&mut self, &egui::Context), which opens what the intake holds (pdf-shell-w13). self.open_intake(ctx); - self.present_portfolios(); + self.present_portfolios(ctx); if let Some((_, id)) = self.active_ids() { let out = self.session.take_js_output(id); self.handle_js(ctx, id, out); diff --git a/crates/ui-egui/src/app/files.rs b/crates/ui-egui/src/app/files.rs index 9c4e60aa9e..49c460763e 100644 --- a/crates/ui-egui/src/app/files.rs +++ b/crates/ui-egui/src/app/files.rs @@ -491,7 +491,7 @@ impl PdfCraftApp { /// Save an attachment to disk, or open a PDF attachment in a new tab (or go to the tab it is /// already open in). Only bytes that are a PDF open, whatever the file's name says; anything /// else asked to open is offered to save. - pub fn attachment_action(&mut self, doc: DocId, index: usize, open: bool) { + pub fn attachment_action(&mut self, ctx: &egui::Context, doc: DocId, index: usize, open: bool) { let Some(d) = self.session.get(doc) else { return }; let Some(att) = d.info.attachments.get(index).cloned() else { return }; let data = pdfcraft_render::attachment_data(&d.bytes, d.password.as_deref(), &att); @@ -511,7 +511,7 @@ impl PdfCraftApp { return; } let before = self.views.len(); - match self.open_bytes(&name, None, bytes) { + match self.open_bytes(ctx, &name, None, bytes) { // Opening bytes adds their tab at once (unless a password is asked for first). Ok(()) if self.views.len() > before => { if let Some(v) = self.views.last_mut() { @@ -547,7 +547,7 @@ impl PdfCraftApp { /// with the right panel closed, unless the portfolio keeps the list hidden (`/View /H`). /// Leaving the tab gives back the panel the list replaced, unless the reader changed panels /// meanwhile. Called every frame. - pub(crate) fn present_portfolios(&mut self) { + pub(crate) fn present_portfolios(&mut self, ctx: &egui::Context) { // A prompt or picker over the window belongs to the document it was raised for (the // password of a file opened together with the portfolio, say): the initial document // waits for its answer, and opening it then would close the prompt. A file still @@ -563,7 +563,7 @@ impl PdfCraftApp { && self.active_ids().is_some_and(|(_, id)| id == doc) && let Some(index) = self.portfolio_attachment(doc, &key) { - self.open_portfolio_initial(doc, index); + self.open_portfolio_initial(ctx, doc, index); // A portfolio inside the portfolio waits until the reader opens it, so a nested // file can't open tab after tab on its own. self.portfolio_initial = None; @@ -610,14 +610,14 @@ impl PdfCraftApp { /// Open portfolio `doc`'s attachment `index` in a tab of its own when its bytes are a PDF, /// whatever its name. A file of another kind, or one that can't be read, stays in the list. - fn open_portfolio_initial(&mut self, doc: DocId, index: usize) { + fn open_portfolio_initial(&mut self, ctx: &egui::Context, doc: DocId, index: usize) { let Some(d) = self.session.get(doc) else { return }; let Some(att) = d.info.attachments.get(index).cloned() else { return }; let Ok(bytes) = pdfcraft_render::attachment_data(&d.bytes, d.password.as_deref(), &att) else { return }; if pdfcraft_engine::source_kind(&att.name, &bytes) != Some(pdfcraft_engine::SourceKind::Pdf) { return; } - if let Err(e) = self.open_bytes(&safe_file_name(&att.name, "attachment"), None, bytes) { + if let Err(e) = self.open_bytes(ctx, &safe_file_name(&att.name, "attachment"), None, bytes) { self.notify_fmt("Couldn't open {name}: {e}", &[("name", &att.name), ("e", &e.to_string())]); } } @@ -639,11 +639,11 @@ impl PdfCraftApp { } /// [`Self::attachment_action`] for the file of the attachment comment at (page, index). - pub fn comment_attachment_action(&mut self, doc: DocId, page: usize, index: usize, open: bool) { + pub fn comment_attachment_action(&mut self, ctx: &egui::Context, doc: DocId, page: usize, index: usize, open: bool) { let source = pdfcraft_render::AttachmentSource::Annotation { page, index }; // The menus offer the items only for a listed file, so a miss is a comment gone since. if let Some(i) = self.session.get(doc).and_then(|d| d.info.attachments.iter().position(|a| a.source == source)) { - self.attachment_action(doc, i, open); + self.attachment_action(ctx, doc, i, open); } } @@ -952,7 +952,7 @@ impl PdfCraftApp { }) } - pub fn open_dialog(&mut self) { + pub fn open_dialog(&mut self, ctx: &egui::Context) { self.startup_superseded = true; // PLAN: `pick_file(request, target, continuation)` assumed from pdf-shell-w14; the continuation gets the `Picked`. // The web answers `Picked::Contents`, the desktop `Picked::Paths`. @@ -965,7 +965,7 @@ impl PdfCraftApp { } Picked::Contents(files) => { for (name, bytes) in files { - if let Err(error) = app.open_bytes(&name, None, bytes) { + if let Err(error) = app.open_bytes(ctx, &name, None, bytes) { app.notify_fmt("Couldn't open {name}: {e}", &[("name", &name), ("e", &error)]); } } @@ -987,7 +987,7 @@ impl PdfCraftApp { } Picked::Contents(files) => { for (name, bytes) in files { - match self.open_bytes(&name, None, bytes) { + match self.open_bytes(ctx, &name, None, bytes) { Ok(()) => { self.execute(ctx, then); } diff --git a/crates/ui-egui/src/app/input.rs b/crates/ui-egui/src/app/input.rs index f66eb4bde5..cf3e7379e2 100644 --- a/crates/ui-egui/src/app/input.rs +++ b/crates/ui-egui/src/app/input.rs @@ -50,13 +50,13 @@ impl PdfCraftApp { } Intake::Bytes { name, bytes } => { let name = if name.is_empty() { DROPPED_NAME.to_string() } else { name }; - if let Err(error) = self.open_bytes(&name, None, bytes.to_vec()) { + if let Err(error) = self.open_bytes(ctx, &name, None, bytes.to_vec()) { self.notify_fmt("Couldn't open {name}: {e}", &[("name", &name), ("e", &error)]); } } Intake::Dropped { files, .. } => { for file in files { - self.open_dropped_file(file.name, file.path, file.bytes); + self.open_dropped_file(ctx, file.name, file.path, file.bytes); } } } @@ -65,15 +65,15 @@ impl PdfCraftApp { /// Opens the files dropped on the window: a file with a path like File ▸ Open, one that /// arrived as bytes (the web) as a new tab. - pub(crate) fn open_dropped(&mut self, files: Vec) { + pub(crate) fn open_dropped(&mut self, ctx: &egui::Context, files: Vec) { for file in files { - self.open_dropped_file(file.name, file.path, file.bytes); + self.open_dropped_file(ctx, file.name, file.path, file.bytes); } } /// Opens one dropped file, from the window or from the OS's file intake: by its path when it has one, else from /// its bytes under `name`. - fn open_dropped_file(&mut self, name: String, path: Option, bytes: Option>) { + fn open_dropped_file(&mut self, ctx: &egui::Context, name: String, path: Option, bytes: Option>) { #[cfg(not(target_arch = "wasm32"))] if let Some(path) = &path { self.open_path(&path.to_string_lossy()); @@ -86,7 +86,7 @@ impl PdfCraftApp { self.notify_fmt("Couldn't read {name}: {e}", &[("name", &name), ("e", "the file has no contents")]); return; }; - if let Err(error) = self.open_bytes(&name, None, bytes.to_vec()) { + if let Err(error) = self.open_bytes(ctx, &name, None, bytes.to_vec()) { self.notify_fmt("Couldn't open {name}: {e}", &[("name", &name), ("e", &error)]); } } diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 8ed00fba88..3f78f5e3a8 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1602,7 +1602,7 @@ fn apply_frame_requests(app: &mut PdfCraftApp, ctx: &egui::Context, index: usize } if let Some((p, i, open)) = app.views[index].session.comments.attachment_request.take() { let doc = app.views[index].id; - app.comment_attachment_action(doc, p, i, open); + app.comment_attachment_action(ctx, doc, p, i, open); } if let Some((name, w)) = field_props { crate::prepare::dialog::open(app, &name, w, crate::prepare::FieldTab::General); diff --git a/crates/ui-egui/src/canvas/image_pick.rs b/crates/ui-egui/src/canvas/image_pick.rs index 09e4809dd3..d802a556fd 100644 --- a/crates/ui-egui/src/canvas/image_pick.rs +++ b/crates/ui-egui/src/canvas/image_pick.rs @@ -166,7 +166,7 @@ mod picture_tests { /// The page open with the Select tool on an app whose jobs wait in `jobs`, once it is on screen, and where a point /// on it is. The jobs are run only while the page settles. - fn open_page() -> (Harness<'static, PdfCraftApp>, Arc, impl Fn(f32, f32) -> egui::Pos2) { + fn open_page(ctx: &egui::Context) -> (Harness<'static, PdfCraftApp>, Arc, impl Fn(f32, f32) -> egui::Pos2) { let jobs = Arc::new(ManualJobs::new()); let host_jobs = jobs.clone(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| { @@ -181,7 +181,7 @@ mod picture_tests { ); let mut app = PdfCraftApp::with_host(Arc::new(host), services); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("marks.pdf", None, page_with_a_comment_and_a_link()).unwrap(); + app.open_bytes(ctx, "marks.pdf", None, page_with_a_comment_and_a_link()).unwrap(); app }); let mut on_screen = None; @@ -219,7 +219,8 @@ mod picture_tests { /// taken in the pass after the job has. #[test] fn a_slow_page_does_not_hold_up_the_window() { - let (mut h, jobs, at) = open_page(); + let ctx = &egui::Context::default(); + let (mut h, jobs, at) = open_page(ctx); click_still_being_looked_into(&mut h, &at); assert!(jobs.waiting() > 0, "the search is queued as a job"); assert_eq!(h.state().views[0].session.picked_image, None); @@ -231,7 +232,8 @@ mod picture_tests { /// Escape takes back a click whose picture isn't known yet: it isn't selected when it is. #[test] fn escape_cancels_a_click_on_a_page_still_being_read() { - let (mut h, _jobs, at) = open_page(); + let ctx = &egui::Context::default(); + let (mut h, _jobs, at) = open_page(ctx); click_still_being_looked_into(&mut h, &at); h.key_press(egui::Key::Escape); h.run_steps(2); @@ -241,7 +243,8 @@ mod picture_tests { /// Another tool lets go of the click, even while its page isn't drawn (Organize pages). #[test] fn another_tool_takes_back_a_click_on_a_page_not_drawn() { - let (mut h, _jobs, at) = open_page(); + let ctx = &egui::Context::default(); + let (mut h, _jobs, at) = open_page(ctx); click_still_being_looked_into(&mut h, &at); let ctx = h.ctx.clone(); assert!(h.state_mut().execute(&ctx, "page.organize")); @@ -258,7 +261,8 @@ mod picture_tests { /// comment is selected. #[test] fn a_click_on_a_comment_takes_back_the_earlier_click() { - let (mut h, _jobs, at) = open_page(); + let ctx = &egui::Context::default(); + let (mut h, _jobs, at) = open_page(ctx); click_still_being_looked_into(&mut h, &at); h.run_steps(30); click(&mut h, at(50.0, 50.0)); @@ -269,7 +273,8 @@ mod picture_tests { /// A click on a link follows it and lets go of the earlier click too. #[test] fn a_click_on_a_link_takes_back_the_earlier_click() { - let (mut h, _jobs, at) = open_page(); + let ctx = &egui::Context::default(); + let (mut h, _jobs, at) = open_page(ctx); click_still_being_looked_into(&mut h, &at); h.run_steps(30); click(&mut h, at(150.0, 250.0)); diff --git a/crates/ui-egui/src/canvas/pictures.rs b/crates/ui-egui/src/canvas/pictures.rs index 8cf920e76b..236d8b3eee 100644 --- a/crates/ui-egui/src/canvas/pictures.rs +++ b/crates/ui-egui/src/canvas/pictures.rs @@ -305,7 +305,7 @@ mod tests { ); let mut app = PdfCraftApp::with_host(Arc::new(host), services); let blank = app.session.create_blank(200.0, 300.0, 3).expect("a blank document"); - app.open_bytes("three.pdf", None, blank.as_ref().clone()).expect("opens"); + app.open_bytes(&egui::Context::default(), "three.pdf", None, blank.as_ref().clone()).expect("opens"); app }); settle(&mut h, &jobs); diff --git a/crates/ui-egui/src/canvas/sign_tab_tests.rs b/crates/ui-egui/src/canvas/sign_tab_tests.rs index 504b24cad1..c7055566a4 100644 --- a/crates/ui-egui/src/canvas/sign_tab_tests.rs +++ b/crates/ui-egui/src/canvas/sign_tab_tests.rs @@ -228,11 +228,11 @@ trailer << /Root 1 0 R >> %%EOF"; /// The real shell with `bytes` open, the side panes closed, after the first frames. -fn shell(bytes: &'static [u8]) -> egui_kittest::Harness<'static, PdfCraftApp> { +fn shell(ctx: &egui::Context, bytes: &'static [u8]) -> egui_kittest::Harness<'static, PdfCraftApp> { let mut h = egui_kittest::Harness::builder().with_size(vec2(1400.0, 900.0)).build_eframe(move |cc| { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("sign-fields.pdf", None, bytes.to_vec()).unwrap(); + app.open_bytes(ctx, "sign-fields.pdf", None, bytes.to_vec()).unwrap(); app.set_option(&cc.egui_ctx, "left", "closed").unwrap(); app.set_option(&cc.egui_ctx, "panel", "none").unwrap(); app @@ -262,7 +262,8 @@ fn click_at(h: &mut egui_kittest::Harness<'static, PdfCraftApp>, at: Pos2) { /// have no tab: a click where theirs would be starts no signing. #[test] fn empty_signature_fields_show_a_sign_tab_that_signs_them() { - let mut h = shell(SIGN_FIELDS); + let ctx = &egui::Context::default(); + let mut h = shell(ctx, SIGN_FIELDS); let (witness, locked) = (tab_on_screen(&h, "Witness"), tab_on_screen(&h, "Locked")); for (what, spot) in [("a signed field", witness), ("a read-only field", locked)] { click_at(&mut h, spot.center()); @@ -282,7 +283,8 @@ fn empty_signature_fields_show_a_sign_tab_that_signs_them() { /// tab, the overlapping field still takes clicks as before. #[test] fn a_sign_tab_signs_its_field_where_an_earlier_field_overlaps_it() { - let mut h = shell(SIGN_UNDER_TEXT); + let ctx = &egui::Context::default(); + let mut h = shell(ctx, SIGN_UNDER_TEXT); let (name, approver) = (field_on_screen(&h, "Name"), field_on_screen(&h, "Approver")); let tab = tab_on_screen(&h, "Approver"); assert!(name.contains_rect(tab), "the text field covers the tab: {tab:?} in {name:?}"); diff --git a/crates/ui-egui/src/canvas/signature_drag.rs b/crates/ui-egui/src/canvas/signature_drag.rs index b0cc7da027..bd8fa9cee3 100644 --- a/crates/ui-egui/src/canvas/signature_drag.rs +++ b/crates/ui-egui/src/canvas/signature_drag.rs @@ -552,7 +552,7 @@ mod tests { let mut h = egui_kittest::Harness::builder().with_size(vec2(1400.0, 900.0)).build_eframe(move |cc| { let mut app = crate::PdfCraftApp::for_unit_tests(); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("silent.pdf", None, bytes.clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "silent.pdf", None, bytes.clone()).unwrap(); app.set_option(&cc.egui_ctx, "left", "closed").unwrap(); app.set_option(&cc.egui_ctx, "panel", "none").unwrap(); app diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index ce76ef50d3..e716e77b12 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -243,7 +243,7 @@ fn edit_text_boxes_stay_drawn_while_page_input_is_blocked() { let mut h = egui_kittest::Harness::builder().with_size(vec2(1400.0, 900.0)).build_eframe(|_cc| { let mut app = PdfCraftApp::for_unit_tests(); let bytes = app.session.create_blank(300.0, 400.0, 1).unwrap(); - app.open_bytes("blank.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "blank.pdf", None, bytes.as_ref().clone()).unwrap(); app.quick_tool = QuickTool::EditText; app }); @@ -251,7 +251,8 @@ fn edit_text_boxes_stay_drawn_while_page_input_is_blocked() { // Block page input the way the window losing focus does. h.state_mut().palette_open = true; h.run_steps(2); - assert!(h.state_mut().apply_edit(&h.ctx.clone(), Edit::RotatePages { pages: vec![0], degrees: 90 })); + let ctx_h = h.ctx.clone(); + assert!(h.state_mut().apply_edit(&ctx_h, Edit::RotatePages { pages: vec![0], degrees: 90 })); h.run_steps(3); let app = h.state(); let generation = app.session.get(app.views[0].id).unwrap().edit_generation(); @@ -268,7 +269,7 @@ fn a_cut_page_is_named_beside_an_xfa_form_notice() { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); let bytes = pdfcraft_xfa::fixtures::static_shell("Ada"); - app.open_bytes("form.pdf", None, bytes).unwrap(); + app.open_bytes(&egui::Context::default(), "form.pdf", None, bytes).unwrap(); app }); h.run_steps(4); @@ -289,7 +290,7 @@ fn a_page_found_cut_after_a_dismissal_is_still_named() { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); let bytes = app.session.create_blank(300.0, 400.0, 2).unwrap(); - app.open_bytes("blank.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "blank.pdf", None, bytes.as_ref().clone()).unwrap(); app }); h.run_steps(3); @@ -613,7 +614,7 @@ fn replace_all_is_one_undo_step_and_counts_pages_whose_matches_had_no_paragraph( let mut h = egui_kittest::Harness::builder().with_size(vec2(1400.0, 900.0)).build_eframe(|cc| { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("fox.pdf", None, fox_pages_pdf()).unwrap(); + app.open_bytes(&egui::Context::default(), "fox.pdf", None, fox_pages_pdf()).unwrap(); app }); h.run_steps(3); diff --git a/crates/ui-egui/src/commands.rs b/crates/ui-egui/src/commands.rs index d86ef1c7b6..2b7357cf8f 100644 --- a/crates/ui-egui/src/commands.rs +++ b/crates/ui-egui/src/commands.rs @@ -324,7 +324,7 @@ impl PdfCraftApp { fn run_file_command(&mut self, ctx: &egui::Context, id: &str, active: Option) -> Option { match id { - "file.open" => self.open_dialog(), + "file.open" => self.open_dialog(ctx), "file.open_recent" => match self.recent.first().map(|r| r.path.clone()) { // The palette runs commands without a submenu: open the most recent file. Some(p) => self.open_recent(&p), @@ -379,7 +379,7 @@ impl PdfCraftApp { "actions.distribution" => crate::actions_ui::open_wizard(self, ctx, Some("Prepare for Distribution")), "actions.optimize_scans" => crate::actions_ui::open_wizard(self, ctx, Some("Optimize Scanned Documents")), "create.blank" => self.create_blank(), - "create.file" => self.open_dialog(), + "create.file" => self.open_dialog(ctx), "create.multiple" => self.create_multiple_dialog(ctx), "create.multiple_separate" => self.create_multiple_separate_dialog(ctx), "create.images" => self.create_from_images_dialog(ctx), diff --git a/crates/ui-egui/src/dialogs/word_count_ui.rs b/crates/ui-egui/src/dialogs/word_count_ui.rs index d8272114ac..3256553a2a 100644 --- a/crates/ui-egui/src/dialogs/word_count_ui.rs +++ b/crates/ui-egui/src/dialogs/word_count_ui.rs @@ -234,7 +234,7 @@ mod tests { let mut h = Harness::builder().with_size(egui::vec2(1200.0, 900.0)).build_eframe(move |cc| { let mut app = app_on(build_jobs); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("words.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "words.pdf", None, bytes.as_ref().clone()).unwrap(); app.run_command(&cc.egui_ctx, "file.word_count"); app }); @@ -250,7 +250,8 @@ mod tests { h.get_by_label("Current page only").click(); settle(&mut h); assert_eq!(words_shown(&h), "2"); - h.state_mut().apply_edit(&h.ctx.clone(), pdfcraft_engine::Edit::DeletePages { pages: vec![0] }); + let ctx_h = h.ctx.clone(); + h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::DeletePages { pages: vec![0] }); settle(&mut h); assert_eq!(words_shown(&h), "3"); h.get_by_label("Close").click(); diff --git a/crates/ui-egui/src/editing.rs b/crates/ui-egui/src/editing.rs index 1651c1a055..5788211b68 100644 --- a/crates/ui-egui/src/editing.rs +++ b/crates/ui-egui/src/editing.rs @@ -1408,7 +1408,7 @@ mod revert_draft_tests { fn app() -> PdfCraftApp { let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("source.pdf", None, include_bytes!("../tests/data/form.pdf").to_vec()).unwrap(); + app.open_bytes(&egui::Context::default(), "source.pdf", None, include_bytes!("../tests/data/form.pdf").to_vec()).unwrap(); app } @@ -1444,7 +1444,7 @@ mod revert_draft_tests { for queued in [false, true] { let mut app = app(); let id = app.views[0].id; - app.open_bytes("other.pdf", None, include_bytes!("../tests/data/form.pdf").to_vec()).unwrap(); + app.open_bytes(&egui::Context::default(), "other.pdf", None, include_bytes!("../tests/data/form.pdf").to_vec()).unwrap(); if queued { queue_draft(&mut app, 0, "Unsaved typing"); } else { @@ -1534,7 +1534,7 @@ mod revert_draft_tests { let bytes = include_bytes!("../tests/data/form.pdf").to_vec(); std::fs::write(&path, &bytes).unwrap(); let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("source.pdf", Some(path.clone()), bytes).unwrap(); + app.open_bytes(&egui::Context::default(), "source.pdf", Some(path.clone()), bytes).unwrap(); let rect = [72.0, 72.0, 272.0, 92.0]; let mut draft = TextDraft::new(0, None, rect, "Typed".into(), default_style()); draft.focus = true; @@ -1544,7 +1544,8 @@ mod revert_draft_tests { if quit { assert!(!h.state_mut().request_quit(&ctx)); } else { - h.state_mut().request_close_tab(&h.ctx.clone(), 0); + let ctx_h = h.ctx.clone(); + h.state_mut().request_close_tab(&ctx_h, 0); } assert!(h.state().close_guard.is_some(), "the typed box holds the close"); h.run_steps(2); @@ -1552,7 +1553,7 @@ mod revert_draft_tests { assert!(h.state().views.is_empty(), "the tab closes once saved"); let mut reopened = PdfCraftApp::for_unit_tests(); - reopened.open_bytes("source.pdf", Some(path.clone()), std::fs::read(&path).unwrap()).unwrap(); + reopened.open_bytes(&egui::Context::default(), "source.pdf", Some(path.clone()), std::fs::read(&path).unwrap()).unwrap(); let doc = reopened.session.get(reopened.views[0].id).unwrap(); assert!(doc.added().iter().any(|a| matches!(&a.content, AddedContent::Text(t) if t.text == "Typed")), "quit: {quit}"); let _ = std::fs::remove_dir_all(&dir); @@ -1564,7 +1565,8 @@ mod revert_draft_tests { #[test] fn revert_runs_no_exit_for_the_field_that_had_the_focus() { let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("focus.pdf", None, pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::focus_template())).unwrap(); + app.open_bytes(&egui::Context::default(), "focus.pdf", None, pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::focus_template())) + .unwrap(); let id = app.views[0].id; let count = |app: &PdfCraftApp, name: &str| { app.session.get(id).unwrap().form.iter().find(|f| f.name == name).and_then(|f| f.value.first().cloned()).unwrap_or_default() @@ -1585,7 +1587,8 @@ mod revert_draft_tests { #[test] fn committing_the_form_runs_the_focus_scripts_of_a_move_not_yet_run() { let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("focus.pdf", None, pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::focus_template())).unwrap(); + app.open_bytes(&egui::Context::default(), "focus.pdf", None, pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::focus_template())) + .unwrap(); let hint = |app: &PdfCraftApp| { app.session .get(app.views[0].id) @@ -1630,7 +1633,7 @@ mod revert_draft_tests { pdf.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes()); for (typed, shown) in [(false, "new"), (true, "typed")] { let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("hot.pdf", None, pdf.clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "hot.pdf", None, pdf.clone()).unwrap(); let hot = app.session.get(app.views[0].id).unwrap().form.iter().find(|f| f.name == "hot").unwrap().obj; let text = if typed { "typed" } else { "old" }; app.views[0].session.forms.focus = Some(Focus { text: text.into(), picked: vec!["old".into()], ..focus("") }); @@ -1655,7 +1658,7 @@ mod revert_draft_tests { } else { app.views[0].session.forms.focus = Some(focus("Focused source draft")); } - app.open_bytes("other.pdf", None, include_bytes!("../tests/data/form.pdf").to_vec()).unwrap(); + app.open_bytes(&egui::Context::default(), "other.pdf", None, include_bytes!("../tests/data/form.pdf").to_vec()).unwrap(); let other = app.views[1].id; assert!(app.apply_edit( &egui::Context::default(), diff --git a/crates/ui-egui/src/editing/close.rs b/crates/ui-egui/src/editing/close.rs index aeeaade957..6e85b9db8f 100644 --- a/crates/ui-egui/src/editing/close.rs +++ b/crates/ui-egui/src/editing/close.rs @@ -376,7 +376,7 @@ mod tests { fn the_window_close_holds_while_work_is_at_risk_and_closes_when_clean() { let mut app = app(); let bytes = app.session.create_blank(200.0, 300.0, 1).unwrap(); - app.open_bytes("a.pdf", None, bytes.to_vec()).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, bytes.to_vec()).unwrap(); assert_eq!(app.close_requested(&egui::Context::default()), CloseDecision::Close, "nothing to lose"); assert!(!app.dialogs.is_open::()); assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 })); diff --git a/crates/ui-egui/src/editing/tests.rs b/crates/ui-egui/src/editing/tests.rs index a57d5f623a..a83f1c4050 100644 --- a/crates/ui-egui/src/editing/tests.rs +++ b/crates/ui-egui/src/editing/tests.rs @@ -88,7 +88,7 @@ fn no_changes_pdf() -> Vec { fn a_refused_text_box_is_put_back_whole() { let mut app = crate::PdfCraftApp::for_unit_tests(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); - app.open_bytes("a.pdf", None, no_changes_pdf()).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, no_changes_pdf()).unwrap(); let style = crate::content_ui::default_style(); app.views[0].session.content.draft = Some(crate::content_ui::TextDraft::new(0, None, [20.0, 20.0, 120.0, 40.0], "Typed".into(), style)); app.undo(&egui::Context::default()); @@ -104,7 +104,7 @@ fn save_commits_the_open_text_box() { let dest = dir.join("out.pdf"); let (mut h, files) = shown_with_picks(); let app = h.state_mut(); - app.open_bytes("a.pdf", None, one_page_pdf()).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, one_page_pdf()).unwrap(); let style = crate::content_ui::default_style(); let mut draft = crate::content_ui::TextDraft::new(0, None, [20.0, 20.0, 120.0, 40.0], "Typed text".into(), style); draft.focus = true; @@ -164,7 +164,7 @@ fn moving_pages_cancels_an_in_progress_drawing() { let mut app = crate::PdfCraftApp::for_unit_tests(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); - app.open_bytes("a.pdf", None, pdf).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, pdf).unwrap(); app.views[0].session.comments.tool = tool; assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 })); assert_eq!(app.views[0].session.comments.tool.drawing().len(), 2, "rotating the page keeps the drawing on it"); @@ -184,7 +184,7 @@ fn save_asks_where_to_keep_an_email_attachment_and_leaves_the_temporary_copy_alo let (mut h, files) = shown_with_picks(); let app = h.state_mut(); - app.open_bytes("Quote.pdf", Some(attachment.to_string_lossy().into_owned()), original.clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "Quote.pdf", Some(attachment.to_string_lossy().into_owned()), original.clone()).unwrap(); assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 })); assert!(!app.save_active(&egui::Context::default(), SaveTarget::InPlace), "Save asks instead of writing the temporary copy"); @@ -209,7 +209,7 @@ fn recolouring_print_ink_text_in_its_box_drops_the_ink_without_an_empty_step() { let open = || { let mut app = crate::PdfCraftApp::for_unit_tests(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); - app.open_bytes("a.pdf", None, one_page_pdf()).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, one_page_pdf()).unwrap(); let mut text = pdfcraft_engine::AddedText { rect: [20.0, 200.0, 180.0, 220.0], text: "Proof".into(), ..Default::default() }; text.set_ink(Some(pdfcraft_engine::TextInk::Cmyk([0.0, 0.0, 0.0, 1.0]))); assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::AddText { page: 0, text })); diff --git a/crates/ui-egui/src/frame_guard.rs b/crates/ui-egui/src/frame_guard.rs index fc3e3ea9f4..13b2505465 100644 --- a/crates/ui-egui/src/frame_guard.rs +++ b/crates/ui-egui/src/frame_guard.rs @@ -160,12 +160,13 @@ mod tests { let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(move |cc| { let mut app = app(Some(&dir)); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("a.pdf", None, pdf("First")).unwrap(); - app.open_bytes("b.pdf", None, pdf("Second")).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, pdf("First")).unwrap(); + app.open_bytes(&egui::Context::default(), "b.pdf", None, pdf("Second")).unwrap(); app }); h.run_steps(4); - assert!(h.state_mut().apply_edit(&h.ctx.clone(), Edit::RotatePages { pages: vec![0], degrees: 90 })); + let ctx_h = h.ctx.clone(); + assert!(h.state_mut().apply_edit(&ctx_h, Edit::RotatePages { pages: vec![0], degrees: 90 })); if about { let ctx = h.ctx.clone(); h.state_mut().set_option(&ctx, "dialog", "about").unwrap(); diff --git a/crates/ui-egui/src/home.rs b/crates/ui-egui/src/home.rs index e2dad9798c..515d2ec1df 100644 --- a/crates/ui-egui/src/home.rs +++ b/crates/ui-egui/src/home.rs @@ -159,7 +159,7 @@ pub fn show(app: &mut PdfCraftApp, ui: &mut egui::Ui) { for action in actions { match action { - StartAction::Open => app.open_dialog(), + StartAction::Open => app.open_dialog(ui.ctx()), StartAction::OpenRecent(path) | StartAction::OpenPinnedFile(path) => app.open_recent(&path.to_string_lossy()), StartAction::RemoveRecent(path) => { let path = path.to_string_lossy(); diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 9aa81e6ae9..85fbf4f709 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -531,7 +531,7 @@ mod tests { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); for name in docs { - app.open_bytes(name, None, PDF.to_vec()).unwrap(); + app.open_bytes(&egui::Context::default(), name, None, PDF.to_vec()).unwrap(); } (app, egui::Context::default()) } diff --git a/crates/ui-egui/src/menus/tab_context.rs b/crates/ui-egui/src/menus/tab_context.rs index 67d0bb2472..09d38ac9ee 100644 --- a/crates/ui-egui/src/menus/tab_context.rs +++ b/crates/ui-egui/src/menus/tab_context.rs @@ -121,10 +121,11 @@ mod tests { #[test] fn confirmed_close_uses_id_and_cancel_keeps_history_empty() { + let ctx = &egui::Context::default(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| { let mut app = app(); - app.open_bytes("first.pdf", Some("/tmp/first.pdf".into()), PDF.to_vec()).unwrap(); - app.open_bytes("second.pdf", None, PDF.to_vec()).unwrap(); + app.open_bytes(ctx, "first.pdf", Some("/tmp/first.pdf".into()), PDF.to_vec()).unwrap(); + app.open_bytes(ctx, "second.pdf", None, PDF.to_vec()).unwrap(); app }); h.run_steps(4); @@ -219,10 +220,11 @@ mod tests { #[test] fn inactive_tab_menu_closes_target_and_disables_unavailable_actions() { + let ctx = &egui::Context::default(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| { let mut app = app(); for name in ["first.pdf", "second.pdf"] { - app.open_bytes(name, None, PDF.to_vec()).unwrap(); + app.open_bytes(ctx, name, None, PDF.to_vec()).unwrap(); } app }); diff --git a/crates/ui-egui/src/panels/format_text_ui.rs b/crates/ui-egui/src/panels/format_text_ui.rs index 61d27073ce..40435d92cd 100644 --- a/crates/ui-egui/src/panels/format_text_ui.rs +++ b/crates/ui-egui/src/panels/format_text_ui.rs @@ -770,7 +770,7 @@ mod tests { fn app_with_text() -> PdfCraftApp { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&egui::Context::default(), "language", "en").unwrap(); - app.open_bytes("a.pdf", None, one_page_pdf()).unwrap(); + app.open_bytes(&egui::Context::default(), "a.pdf", None, one_page_pdf()).unwrap(); let text = AddedText { rect: [20.0, 200.0, 180.0, 220.0], text: "Proof".into(), size: 12.0, ..Default::default() }; assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::AddText { page: 0, text })); app diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs index 5bdaf5b73f..09eae73c9a 100644 --- a/crates/ui-egui/src/panels/right.rs +++ b/crates/ui-egui/src/panels/right.rs @@ -106,7 +106,7 @@ pub fn panel_body(app: &mut PdfCraftApp, ui: &mut egui::Ui, cx: &mut BodyCx<'_>, app.make_comment_default(p, i); } if let Some((p, i, open)) = app.views.get_mut(index).and_then(|v| v.session.comments.attachment_request.take()) { - app.comment_attachment_action(id, p, i, open); + app.comment_attachment_action(ui.ctx(), id, p, i, open); } if let Some((p, i)) = app.views.get_mut(index).and_then(|v| v.session.comments.props_request.take()) { app.open_comment_props(&ctx, p, i); @@ -121,7 +121,7 @@ pub fn panel_body(app: &mut PdfCraftApp, ui: &mut egui::Ui, cx: &mut BodyCx<'_>, app.bookmark_action(ui.ctx(), a); } if let Some((ai, open)) = result.attachment { - app.attachment_action(id, ai, open); + app.attachment_action(ui.ctx(), id, ai, open); } if let Some((layer, visible)) = result.layer && app.session.set_layer_visible(id, layer, visible) diff --git a/crates/ui-egui/src/persist/recovery.rs b/crates/ui-egui/src/persist/recovery.rs index 6a09aca8bc..e7e2d7ac18 100644 --- a/crates/ui-egui/src/persist/recovery.rs +++ b/crates/ui-egui/src/persist/recovery.rs @@ -486,6 +486,7 @@ mod sidecar_tests { /// the document's file from then on. #[test] fn the_first_save_after_recovering_asks_instead_of_writing_the_sidecars_path() { + let ctx = &egui::Context::default(); let ctx = &egui::Context::default(); let dir = TestDir::new(); let folder = dir.path().join("recovery"); @@ -494,7 +495,7 @@ mod sidecar_tests { { let (mut crashed, ..) = app_in(Some(&folder)); let bytes = crashed.session.create_blank(200.0, 300.0, 1).unwrap(); - crashed.open_bytes("work.pdf", Some(named.to_string_lossy().into_owned()), bytes.to_vec()).unwrap(); + crashed.open_bytes(ctx, "work.pdf", Some(named.to_string_lossy().into_owned()), bytes.to_vec()).unwrap(); assert!(crashed.apply_edit(ctx, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 })); crashed.autosave_now(); } @@ -526,7 +527,8 @@ mod sidecar_tests { assert!(std::fs::read(&chosen).unwrap().starts_with(b"%PDF"), "the document went where the user chose"); // The chosen file is the document's file now: the next Save writes it without asking. - assert!(h.state_mut().apply_edit(&h.ctx.clone(), pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 })); + let ctx_h = h.ctx.clone(); + assert!(h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 })); assert!(h.state_mut().save_active(ctx, SaveTarget::InPlace)); } } diff --git a/crates/ui-egui/src/platform/open_worker/tests.rs b/crates/ui-egui/src/platform/open_worker/tests.rs index 39077dfc37..151f78595d 100644 --- a/crates/ui-egui/src/platform/open_worker/tests.rs +++ b/crates/ui-egui/src/platform/open_worker/tests.rs @@ -107,7 +107,7 @@ impl Rig { let mut app = PdfCraftApp::with_host(Arc::new(host), services); app.set_option(&cc.egui_ctx, "language", "en").expect("language preference"); app.attach_control(ControlChannel::new(rx), None); - app.open_bytes("existing.pdf", None, fixture(3)).expect("existing document"); + app.open_bytes(&egui::Context::default(), "existing.pdf", None, fixture(3)).expect("existing document"); app }); h.run_steps(3); @@ -220,7 +220,8 @@ fn chosen_tool_runs_after_preparation(extension: &str, bytes: Vec) { rig.choose("export.text", &file); assert_eq!(rig.tabs(), 0); assert!(!rig.export_open(), "the tool cannot run before its document exists"); - rig.h.state_mut().open_bytes("other.pdf", None, fixture(3)).expect("another document opens while preparation is held"); + let ctx_h = h.ctx.clone(); + rig.h.state_mut().open_bytes(&ctx_h, "other.pdf", None, fixture(3)).expect("another document opens while preparation is held"); let other = rig.h.state().active_ids().expect("the other document is active").1; rig.release(); let app = rig.h.state(); @@ -293,7 +294,8 @@ fn cancelling_a_chosen_tools_picker_or_password_never_runs_it_on_another_documen rig.release(); assert!(rig.password_dialog()); rig.press("Cancel"); - rig.h.state_mut().open_bytes("other.pdf", None, fixture(3)).expect("a different document opens after cancellation"); + let ctx_h = h.ctx.clone(); + rig.h.state_mut().open_bytes(&ctx_h, "other.pdf", None, fixture(3)).expect("a different document opens after cancellation"); rig.h.run_steps(3); assert!(!rig.password_dialog()); assert_eq!(rig.tabs(), 1); @@ -316,7 +318,8 @@ fn a_chosen_tool_does_not_run_when_native_preparation_rejects_the_file() { #[test] fn startup_intake_waits_for_local_authentication_and_keeps_its_own_background_password_prompt() { let mut rig = Rig::new(); - rig.h.state_mut().open_bytes("local-secret.pdf", None, encrypted_fixture()).expect("the local PDF asks for its password"); + let ctx_h = h.ctx.clone(); + rig.h.state_mut().open_bytes(&ctx_h, "local-secret.pdf", None, encrypted_fixture()).expect("the local PDF asks for its password"); rig.h.run_steps(2); rig.intake.open_bytes("startup-secret.pdf".into(), Arc::from(encrypted_fixture())); rig.h.run_steps(2); @@ -338,7 +341,8 @@ fn startup_intake_waits_for_local_authentication_and_keeps_its_own_background_pa #[test] fn startup_intake_waits_through_wrong_password_retry_and_cancel_preserves_the_existing_document() { let mut rig = Rig::new(); - rig.h.state_mut().open_bytes("local-secret.pdf", None, encrypted_fixture()).expect("the local PDF asks for its password"); + let ctx_h = h.ctx.clone(); + rig.h.state_mut().open_bytes(&ctx_h, "local-secret.pdf", None, encrypted_fixture()).expect("the local PDF asks for its password"); rig.h.run_steps(2); rig.enter_password("wrong-password"); rig.intake.open_bytes("startup.pdf".into(), Arc::from(fixture(1))); @@ -455,7 +459,8 @@ fn tool_picker_keeps_picked_recipient_when_other_open_is_ready() { let inserted = TempFile::new("picker-insert", "pdf", &fixture(1)); let other = TempFile::new("picker-other", "pdf", &fixture(1)); // The tool's second picker is out while another file finishes opening. - rig.h.state_mut().pick_files(&h.ctx.clone(), crate::FilePurpose::InsertPages, true); + let ctx_h = h.ctx.clone(); + rig.h.state_mut().pick_files(&ctx_h, crate::FilePurpose::InsertPages, true); rig.h.run_steps(2); rig.h.state_mut().open_path(&other.path()); rig.release(); @@ -500,7 +505,8 @@ fn startup_files_wait_for_absolute_link_consent_and_preparation() { target.path() ); let mut rig = Rig::empty(); - rig.h.state_mut().open_bytes("source.pdf", None, source.into_bytes()).expect("the source document"); + let ctx_h = h.ctx.clone(); + rig.h.state_mut().open_bytes(&ctx_h, "source.pdf", None, source.into_bytes()).expect("the source document"); rig.h.run_steps(3); let page = rig.h.state().views[0].page_screen_rect(0).expect("the page is on screen"); rig.h.event(egui::Event::PointerMoved(page.center())); diff --git a/crates/ui-egui/src/properties_session.rs b/crates/ui-egui/src/properties_session.rs index 28ae798059..6ea32f5cd3 100644 --- a/crates/ui-egui/src/properties_session.rs +++ b/crates/ui-egui/src/properties_session.rs @@ -331,7 +331,7 @@ mod tests { let mut app = PdfCraftApp::for_unit_tests(); let ctx = egui::Context::default(); let bytes = app.session.create_blank(200.0, 300.0, 1).unwrap(); - app.open_bytes("properties.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "properties.pdf", None, bytes.as_ref().clone()).unwrap(); app.open_properties(&ctx, PropsTab::Description); assert!(app.prepare_properties(&ctx)); set_field(&mut app, &ctx, "title", json!("Draft title")); @@ -397,7 +397,7 @@ mod tests { let (mut app, ctx) = app(); let (_, id) = app.active_ids().unwrap(); let bytes = app.session.create_blank(200.0, 300.0, 1).unwrap(); - app.open_bytes("other.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "other.pdf", None, bytes.as_ref().clone()).unwrap(); let (_, other) = app.active_ids().unwrap(); assert_ne!(id, other); assert!(app.finish_properties(&ctx)); diff --git a/crates/ui-egui/src/script_dialog.rs b/crates/ui-egui/src/script_dialog.rs index 909a5ab315..ca2d8a5ecb 100644 --- a/crates/ui-egui/src/script_dialog.rs +++ b/crates/ui-egui/src/script_dialog.rs @@ -464,9 +464,10 @@ mod review_tests { /// The central shortcut/page-input gate must see a real hosted script prompt. #[test] fn a_script_prompt_blocks_document_input_until_answered() { + let ctx = &egui::Context::default(); let mut app = PdfCraftApp::for_unit_tests(); let bytes = app.session.create_blank(200.0, 300.0, 1).unwrap(); - app.open_bytes("synthetic.pdf", None, bytes.to_vec()).unwrap(); + app.open_bytes(ctx, "synthetic.pdf", None, bytes.to_vec()).unwrap(); let id = app.active_ids().unwrap().1; app.start_ui_script(id, "app.response({cQuestion:'Wait'});", None, None); let ctx = egui::Context::default(); @@ -505,7 +506,7 @@ mod review13_ui_tests { use std::sync::Arc; /// Create a native app over `files` with a visible synthetic blank document and no renderer. - fn app_over(files: Arc) -> (PdfCraftApp, DocId) { + fn app_over(ctx: &egui::Context, files: Arc) -> (PdfCraftApp, DocId) { let host = Host::builder(AppInfo { id: "pdfcraft", name: "PdfCraft", version: "0" }).files(files).build(); let (intake, _) = FileIntake::new(Wake::none()); let clipboard = Clipboard::new(EventClipboard::new(|| 0, |_| Ok(0))); @@ -513,19 +514,20 @@ mod review13_ui_tests { let mut app = PdfCraftApp::with_host(Arc::new(host), services); app.set_option(&egui::Context::default(), "language", "en").unwrap(); let bytes = app.session.create_blank(200.0, 300.0, 2).unwrap(); - app.open_bytes("synthetic.pdf", None, bytes.to_vec()).unwrap(); + app.open_bytes(ctx, "synthetic.pdf", None, bytes.to_vec()).unwrap(); let id = app.active_ids().unwrap().1; (app, id) } - fn app() -> (PdfCraftApp, DocId) { - app_over(Arc::new(ScriptedFiles::new())) + fn app(ctx: &egui::Context) -> (PdfCraftApp, DocId) { + app_over(ctx, Arc::new(ScriptedFiles::new())) } /// Refused clicks and execution/prompt timeouts are visible, while stale runs stay silent. #[test] fn busy_and_timeout_notices_do_not_claim_worker_termination() { + let ctx = &egui::Context::default(); let ctx = egui::Context::default(); for (timeout, prompt) in [(true, false), (true, true), (false, false)] { - let (mut app, id) = app(); + let (mut app, id) = app(ctx); app.start_ui_script(id, "app.response({cQuestion:'Wait'});", None, None); app.start_ui_script(id, "var shouldNotRun=1;", None, None); assert!(app.posted_notices.contains(&PostedNotice::new("Another form script is active; this click was not run"))); @@ -555,14 +557,15 @@ mod review13_ui_tests { /// Comparison completion preserves globals and sends Save As/Print to its source document. #[test] fn non_active_comparison_document_completes_native_script() { + let ctx = &egui::Context::default(); let ctx = &egui::Context::default(); for request in ["", "app.execMenuItem('SaveAs');", "print();"] { let saved = PathBuf::from("/scripted/source.pdf"); let files = Arc::new(ScriptedFiles::answering([Picked::Paths(vec![saved.clone()])])); - let (mut app, old) = app_over(files.clone()); + let (mut app, old) = app_over(ctx, files.clone()); app.views.iter_mut().find(|v| v.id == old).unwrap().view.current = 1; let bytes = app.session.create_blank(200.0, 300.0, 1).unwrap(); - app.open_bytes("new.pdf", None, bytes.to_vec()).unwrap(); + app.open_bytes(ctx, "new.pdf", None, bytes.to_vec()).unwrap(); let new = app.active_ids().unwrap().1; assert_ne!(new, old); let new_before = app.session.save_bytes(new).unwrap(); @@ -604,7 +607,8 @@ mod review13_ui_tests { #[test] fn script_focus_reveals_an_offscreen_widget() { let ctx = &egui::Context::default(); - let (mut app, id) = app(); + let ctx = &egui::Context::default(); + let (mut app, id) = app(ctx); app.session .apply( id, @@ -656,13 +660,14 @@ mod link_script_tests { /// window's prompt, the answer resumes it, and its field change and openDoc consent apply. #[test] fn a_javascript_link_asks_its_menu_and_applies_its_result() { + let ctx = &egui::Context::default(); let ctx = &egui::Context::default(); // An absolute path on this system (the document has no folder): on Windows, Acrobat's // `/C/…` form, which reads as `C:/…`. let path = if cfg!(windows) { "/C/abs/other.pdf" } else { "/abs/other.pdf" }; let script = format!("getField\\('log'\\).value = app.popUpMenuEx\\({{cName:'A', cReturn:'a'}}\\) + event.type; app.openDoc\\('{path}'\\);"); let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("link.pdf", None, linked(&script)).unwrap(); + app.open_bytes(ctx, "link.pdf", None, linked(&script)).unwrap(); let id = app.active_ids().unwrap().1; app.run_link_script(ctx, id, 0, 0); // The worker's question is read from its reply channel, before any pass hosts it. diff --git a/crates/ui-egui/src/tools/content_ui.rs b/crates/ui-egui/src/tools/content_ui.rs index 3684702264..e581a6f17c 100644 --- a/crates/ui-egui/src/tools/content_ui.rs +++ b/crates/ui-egui/src/tools/content_ui.rs @@ -1767,7 +1767,7 @@ mod tests { let id = app.active_ids().unwrap().1; let bytes = app.session.save_bytes(id).unwrap(); let mut locked = crate::PdfCraftApp::for_unit_tests(); - locked.open_bytes("locked.pdf", None, bytes.as_ref().clone()).unwrap(); + locked.open_bytes(&egui::Context::default(), "locked.pdf", None, bytes.as_ref().clone()).unwrap(); assert!(!locked.session.get(locked.active_ids().unwrap().1).unwrap().allows_modification()); locked.last_copied_image = Some((1, 1, vec![0, 255, 0, 255])); let before = locked.last_copied_image.clone(); diff --git a/crates/ui-egui/src/tools/edit_text_ui.rs b/crates/ui-egui/src/tools/edit_text_ui.rs index 068f1d32f2..e2a918f2ad 100644 --- a/crates/ui-egui/src/tools/edit_text_ui.rs +++ b/crates/ui-egui/src/tools/edit_text_ui.rs @@ -1372,7 +1372,7 @@ mod tests { #[test] fn an_edge_goes_to_the_box_the_pointer_is_level_with() { let mut app = crate::PdfCraftApp::for_unit_tests(); - app.open_bytes("two.pdf", None, two_paragraphs()).unwrap(); + app.open_bytes(&egui::Context::default(), "two.pdf", None, two_paragraphs()).unwrap(); let mut h = egui_kittest::Harness::builder().with_size(vec2(1400.0, 900.0)).build_eframe(move |_cc| app); h.run_steps(4); let ctx = h.ctx.clone(); @@ -1491,7 +1491,7 @@ mod tests { fn preview_app() -> (crate::PdfCraftApp, LineEditor) { let mut app = crate::PdfCraftApp::for_unit_tests(); let bytes = app.session.create_from_text("Preview", "Preview text").unwrap(); - app.open_bytes("preview.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "preview.pdf", None, bytes.as_ref().clone()).unwrap(); let doc = app.session.get(app.views[0].id).unwrap(); let block = doc.text_blocks(0).remove(0); let ed = LineEditor { @@ -1623,7 +1623,7 @@ mod tests { let mut app = crate::PdfCraftApp::for_unit_tests(); let png = pdfcraft_engine::export::encode_png(200, 100, &[235u8; 200 * 100 * 4]).unwrap(); let bytes = app.session.create_from_images(&[("scan.png".into(), png)]).unwrap(); - app.open_bytes("scan.pdf", None, bytes.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "scan.pdf", None, bytes.as_ref().clone()).unwrap(); let id = app.views[0].id; let words = vec![word("JOHN", 20.0, 45.0, 15.0), word("SMITH", 49.0, 78.0, 9.0), word("EXAMPLE", 82.0, 124.0, 11.0)]; app.session.apply(id, Edit::AddOcrText { page: 0, words }).unwrap(); diff --git a/crates/ui-egui/src/tools/sign_ui.rs b/crates/ui-egui/src/tools/sign_ui.rs index 9997930f25..137c692337 100644 --- a/crates/ui-egui/src/tools/sign_ui.rs +++ b/crates/ui-egui/src/tools/sign_ui.rs @@ -799,7 +799,7 @@ mod tests { } let mut app = PdfCraftApp::for_unit_tests(); let plain = app.session.create_from_text("t", "No signatures here.").unwrap(); - app.open_bytes("plain.pdf", None, plain.as_ref().clone()).unwrap(); + app.open_bytes(&egui::Context::default(), "plain.pdf", None, plain.as_ref().clone()).unwrap(); app.views[0].session.texts.insert(0, std::sync::Arc::new(pdfcraft_render::PageText::default())); app.apply_status_captions(crate::i18n::Lang::from_code("de").unwrap()); assert!(!app.views[0].session.texts.is_empty(), "its text, selection and search results stay"); @@ -1081,7 +1081,7 @@ mod tests { assert_eq!(import_app.digital_ids[0].name, "Grace Hopper"); let bytes = pdfcraft_engine::Session::new().create_blank(200.0, 200.0, 1).unwrap(); - import_app.open_bytes("unsigned.pdf", None, bytes.as_ref().clone()).unwrap(); + import_app.open_bytes(&egui::Context::default(), "unsigned.pdf", None, bytes.as_ref().clone()).unwrap(); let mut sign_draft = SignDraft::new(0, None, None, None, &import_app.digital_ids); sign_draft.step = SignStep::SignAs; sign_draft.password = "secret12".into(); diff --git a/crates/ui-egui/src/workflows/combine_ui/tests.rs b/crates/ui-egui/src/workflows/combine_ui/tests.rs index af04859f03..eaa87c759c 100644 --- a/crates/ui-egui/src/workflows/combine_ui/tests.rs +++ b/crates/ui-egui/src/workflows/combine_ui/tests.rs @@ -226,7 +226,7 @@ fn removing_pages_drops_emptied_parts_and_joins_what_meets() { #[cfg(not(target_arch = "wasm32"))] fn a_password_answered_after_combine_opens_behind_the_combine_tab() { let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("locked.pdf", None, locked()).unwrap(); + app.open_bytes(&egui::Context::default(), "locked.pdf", None, locked()).unwrap(); assert!(app.password_prompt.is_some() && app.views.is_empty()); app.combine_paths(&egui::Context::default(), &[]); assert!(app.combine_showing()); @@ -236,7 +236,7 @@ fn a_password_answered_after_combine_opens_behind_the_combine_tab() { assert!(app.combine_showing(), "and the Combine files tab still shows"); let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("locked.pdf", None, locked()).unwrap(); + app.open_bytes(&egui::Context::default(), "locked.pdf", None, locked()).unwrap(); app.password_prompt.as_mut().unwrap().then = Some("export.text"); app.combine_paths(&egui::Context::default(), &[]); assert!(app.password_prompt.as_ref().unwrap().activate, "a tool's open keeps its focus"); diff --git a/crates/ui-egui/src/workflows/compare_ui.rs b/crates/ui-egui/src/workflows/compare_ui.rs index 84366680d3..444387a91d 100644 --- a/crates/ui-egui/src/workflows/compare_ui.rs +++ b/crates/ui-egui/src/workflows/compare_ui.rs @@ -308,7 +308,7 @@ impl PdfCraftApp { }, PanelAction::Report => match self.session.compare_report_of(old, new, &pages) { Ok(bytes) => { - if let Err(e) = self.open_bytes("Compare Report.pdf", None, bytes.to_vec()) { + if let Err(e) = self.open_bytes(ctx, "Compare Report.pdf", None, bytes.to_vec()) { self.notify_error(e); } } diff --git a/crates/ui-egui/src/workflows/create_ui.rs b/crates/ui-egui/src/workflows/create_ui.rs index b98e18bb53..558e729f88 100644 --- a/crates/ui-egui/src/workflows/create_ui.rs +++ b/crates/ui-egui/src/workflows/create_ui.rs @@ -419,7 +419,7 @@ mod tests { let mut app = PdfCraftApp::for_unit_tests_on(host); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); app.session = std::mem::take(&mut app.session).with_clock(|| 1_700_000_000); - app.open_bytes("notes.pdf", None, source).unwrap(); + app.open_bytes(&egui::Context::default(), "notes.pdf", None, source).unwrap(); app }); h.run_steps(2); @@ -474,7 +474,8 @@ mod tests { let files = Arc::new(ScriptedFiles::held()); let jobs = Arc::new(ManualJobs::new()); let mut h = reducing(files.clone(), jobs.clone(), text_pdf("Minutes of the harbour board")); - h.state_mut().reduce_file_size(&h.ctx.clone()); + let ctx_h = h.ctx.clone(); + h.state_mut().reduce_file_size(&ctx_h); h.run_steps(2); assert!(h.state().optimize_run.is_none() && jobs.waiting() == 0, "nothing runs until the answer is in"); assert_eq!(files.open(), 1, "the question is out"); @@ -496,9 +497,11 @@ mod tests { let files = Arc::new(ScriptedFiles::held()); let jobs = Arc::new(ManualJobs::new()); let mut h = reducing(files.clone(), jobs.clone(), text_pdf("Minutes of the harbour board")); - h.state_mut().reduce_file_size(&h.ctx.clone()); + let ctx_h = h.ctx.clone(); + h.state_mut().reduce_file_size(&ctx_h); h.run_steps(2); - h.state_mut().open_bytes("other.pdf", None, text_pdf("Another document")).unwrap(); + let ctx_h = h.ctx.clone(); + h.state_mut().open_bytes(&ctx_h, "other.pdf", None, text_pdf("Another document")).unwrap(); files.answer(Picked::Paths(vec![out.clone()])); h.run_steps(3); jobs.run_all(); @@ -518,7 +521,8 @@ mod tests { let files = Arc::new(FailingWrites { inner: inner.clone(), fails: Mutex::new(2) }); let jobs = Arc::new(ManualJobs::new()); let mut h = reducing(files, jobs.clone(), text_pdf("Minutes of the harbour board")); - h.state_mut().reduce_file_size(&h.ctx.clone()); + let ctx_h = h.ctx.clone(); + h.state_mut().reduce_file_size(&ctx_h); h.run_steps(2); jobs.run_all(); h.run_steps(8); @@ -534,9 +538,11 @@ mod tests { let jobs = Arc::new(ManualJobs::new()); let mut h = reducing(files.clone(), jobs.clone(), text_pdf("Minutes")); // A copy of a plain text file is no smaller. - h.state_mut().open_bytes("plain.txt", None, "lorem ipsum ".repeat(500).into_bytes()).unwrap(); + let ctx_h = h.ctx.clone(); + h.state_mut().open_bytes(&ctx_h, "plain.txt", None, "lorem ipsum ".repeat(500).into_bytes()).unwrap(); h.run_steps(2); - h.state_mut().reduce_file_size(&h.ctx.clone()); + let ctx_h = h.ctx.clone(); + h.state_mut().reduce_file_size(&ctx_h); h.run_steps(2); jobs.run_all(); h.run_steps(3); @@ -550,10 +556,12 @@ mod tests { let files = Arc::new(ScriptedFiles::answering([Picked::Paths(vec![out])])); let jobs = Arc::new(ManualJobs::new()); let mut h = reducing(files.clone(), jobs.clone(), text_pdf("Minutes of the harbour board")); - h.state_mut().reduce_file_size(&h.ctx.clone()); + let ctx_h = h.ctx.clone(); + h.state_mut().reduce_file_size(&ctx_h); h.run_steps(2); assert!(h.state().optimize_run.is_some(), "the first run is waiting for its job"); - h.state_mut().reduce_file_size(&h.ctx.clone()); + let ctx_h = h.ctx.clone(); + h.state_mut().reduce_file_size(&ctx_h); h.run_steps(2); assert_eq!(files.requests().len(), 1, "no second question"); h.get_by_label_contains("Optimization is already running"); diff --git a/crates/ui-egui/src/workflows/js_ui.rs b/crates/ui-egui/src/workflows/js_ui.rs index e78a51051f..040f6b4d31 100644 --- a/crates/ui-egui/src/workflows/js_ui.rs +++ b/crates/ui-egui/src/workflows/js_ui.rs @@ -106,7 +106,7 @@ impl PdfCraftApp { self.save_view(ctx, i, crate::SaveTarget::As); } } - Request::OpenAttachment { name, open } => self.open_script_attachment(id, &name, open), + Request::OpenAttachment { name, open } => self.open_script_attachment(ctx, id, &name, open), Request::ShowPanel(p) => { let panel = match p { Panel::Bookmarks => crate::RightPanel::Bookmarks, @@ -161,7 +161,7 @@ impl PdfCraftApp { /// A form button's `exportDataObject`: the attached file `name` (its key in the document's /// embedded files, else its file name), opened in a new tab when the script asks to open a /// PDF, and otherwise offered to save. Other files aren't launched in other programs. - fn open_script_attachment(&mut self, id: DocId, name: &str, open: bool) { + fn open_script_attachment(&mut self, ctx: &egui::Context, id: DocId, name: &str, open: bool) { let Some(doc) = self.session.get(id) else { return }; let keyed = |a: &pdfcraft_render::Attachment| matches!(&a.source, pdfcraft_render::AttachmentSource::Document { key } if key == name); let index = doc.info.attachments.iter().position(keyed).or_else(|| doc.info.attachments.iter().position(|a| a.name == name)); @@ -172,7 +172,7 @@ impl PdfCraftApp { return; }; // Whether it is a PDF is decided by its bytes, not its name (`attachment_action`). - self.attachment_action(id, i, open); + self.attachment_action(ctx, id, i, open); } /// A form's script turned field highlighting on or off (`app.runtimeHighlight`): as the diff --git a/crates/ui-egui/src/workflows/ocr_ui.rs b/crates/ui-egui/src/workflows/ocr_ui.rs index 3acb914d01..6c1d380441 100644 --- a/crates/ui-egui/src/workflows/ocr_ui.rs +++ b/crates/ui-egui/src/workflows/ocr_ui.rs @@ -1212,7 +1212,7 @@ mod tests { fn app_with(pdf: Vec) -> PdfCraftApp { let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("doc.pdf", None, pdf).unwrap(); + app.open_bytes(&egui::Context::default(), "doc.pdf", None, pdf).unwrap(); app.quick_tool = QuickTool::EditText; app } @@ -1226,7 +1226,7 @@ mod tests { let jobs = Arc::new(ManualJobs::new()); let host = craft::host::Host::builder(AppInfo { id: "pdfcraft-test", name: "PdfCraft", version: "0" }).jobs(jobs.clone()).build(); let mut app = PdfCraftApp::for_unit_tests_on(Arc::new(host)); - app.open_bytes("doc.pdf", None, scan()).unwrap(); + app.open_bytes(&egui::Context::default(), "doc.pdf", None, scan()).unwrap(); app.quick_tool = QuickTool::EditText; (app, jobs) } @@ -1588,7 +1588,7 @@ mod tests { fn a_result_that_arrives_after_the_document_changed_is_refused() { let mut app = PdfCraftApp::for_unit_tests(); let pdf = pdfcraft_engine::Session::new().create_from_text("t", "Typed text").expect("fixture"); - app.open_bytes("a.pdf", None, pdf.to_vec()).expect("opens"); + app.open_bytes(&egui::Context::default(), "a.pdf", None, pdf.to_vec()).expect("opens"); let id = app.active_ids().expect("a document").1; let guard = app.session.ocr_guard(id).expect("a guard"); app.session.apply(id, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 }).expect("an edit while the worker runs"); diff --git a/crates/ui-egui/src/workflows/ocr_verify.rs b/crates/ui-egui/src/workflows/ocr_verify.rs index aae0ec0295..f74b6b7709 100644 --- a/crates/ui-egui/src/workflows/ocr_verify.rs +++ b/crates/ui-egui/src/workflows/ocr_verify.rs @@ -1728,7 +1728,7 @@ mod tests { fn app_review(words: Vec>) -> (PdfCraftApp, DocId) { let blank = Session::new().create_blank(612.0, 792.0, words.len()).expect("fixture").to_vec(); let mut app = PdfCraftApp::for_unit_tests(); - app.open_bytes("blank.pdf", None, blank).expect("fixture"); + app.open_bytes(&egui::Context::default(), "blank.pdf", None, blank).expect("fixture"); let id = app.active_ids().expect("open").1; let job = app.session.ocr_job(id, &[], OcrSettings::default()).expect("fixture"); let pages = words @@ -2253,7 +2253,7 @@ mod tests { let mut app = PdfCraftApp::for_unit_tests(); let bytes = Session::new().create_from_text("t", "Recognize these scanned words").expect("fixture").to_vec(); for name in ["a.pdf", "b.pdf", "c.pdf"] { - app.open_bytes(name, None, bytes.clone()).expect("fixture"); + app.open_bytes(&egui::Context::default(), name, None, bytes.clone()).expect("fixture"); } let a = app.views.first().expect("a is open").id; let job = app.session.ocr_job(a, &[0], OcrSettings::default()).expect("fixture"); @@ -2285,7 +2285,7 @@ mod tests { let mut app = PdfCraftApp::for_unit_tests(); let bytes = Session::new().create_from_text("t", "Recognize these scanned words").expect("fixture").to_vec(); for name in ["a.pdf", "b.pdf", "c.pdf"] { - app.open_bytes(name, None, bytes.clone()).expect("fixture"); + app.open_bytes(&egui::Context::default(), name, None, bytes.clone()).expect("fixture"); } let a = app.views.first().expect("a is open").id; let job = app.session.ocr_job(a, &[0], OcrSettings::default()).expect("fixture"); @@ -2365,7 +2365,7 @@ mod tests { let mut app = PdfCraftApp::for_unit_tests(); let bytes = Session::new().create_from_text("t", "Recognize these scanned words").expect("fixture").to_vec(); for name in ["a.pdf", "b.pdf"] { - app.open_bytes(name, None, bytes.clone()).expect("fixture"); + app.open_bytes(&egui::Context::default(), name, None, bytes.clone()).expect("fixture"); } let a = app.views[0].id; let job = app.session.ocr_job(a, &[0], OcrSettings::default()).expect("fixture"); diff --git a/crates/ui-egui/src/workflows/optimize_ui.rs b/crates/ui-egui/src/workflows/optimize_ui.rs index b88da17f4b..687ab6f807 100644 --- a/crates/ui-egui/src/workflows/optimize_ui.rs +++ b/crates/ui-egui/src/workflows/optimize_ui.rs @@ -785,7 +785,7 @@ mod tests { let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(move |cc| { let mut app = PdfCraftApp::for_unit_tests(); app.set_option(&cc.egui_ctx, "language", "en").unwrap(); - app.open_bytes("notes.txt", None, b"hello".to_vec()).unwrap(); + app.open_bytes(&egui::Context::default(), "notes.txt", None, b"hello".to_vec()).unwrap(); let (_, doc) = app.active_ids().unwrap(); app.optimize_run = Some(OptimizeRun { doc, kind: OptimizeKind::Advanced, destination: None, progress: p, stage: s }); app