lead: L-219 cascade rounds 9-12 (open path callers), harness contexts hoisted into statements, test functions make their own Context

This commit is contained in:
Dylan M. Taylor committed 2026-11-18 18:11:47 -05:00
1 parent e084526985
commit 63ff373b46
34 files changed
+153 -117

No files matched your search

+1 -1
View File
@@ -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);
+11 -11
View File
@@ -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);
}
+6 -6
View File
@@ -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<OsFile>) {
pub(crate) fn open_dropped(&mut self, ctx: &egui::Context, files: Vec<OsFile>) {
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<std::path::PathBuf>, bytes: Option<std::sync::Arc<[u8]>>) {
fn open_dropped_file(&mut self, ctx: &egui::Context, name: String, path: Option<std::path::PathBuf>, bytes: Option<std::sync::Arc<[u8]>>) {
#[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)]);
}
}
+1 -1
View File
@@ -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);
+12 -7
View File
@@ -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<ManualJobs>, impl Fn(f32, f32) -> egui::Pos2) {
fn open_page(ctx: &egui::Context) -> (Harness<'static, PdfCraftApp>, Arc<ManualJobs>, 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));
+1 -1
View File
@@ -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);
+6 -4
View File
@@ -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:?}");
+1 -1
View File
@@ -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
+6 -5
View File
@@ -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("<form1><page1><name>Ada</name></page1></form1>");
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);
+2 -2
View File
@@ -324,7 +324,7 @@ impl PdfCraftApp {
fn run_file_command(&mut self, ctx: &egui::Context, id: &str, active: Option<usize>) -> Option<bool> {
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),
+3 -2
View File
@@ -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();
+12 -9
View File
@@ -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(),
+1 -1
View File
@@ -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::<UnsavedChangesDialog>());
assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 }));
+5 -5
View File
@@ -88,7 +88,7 @@ fn no_changes_pdf() -> Vec<u8> {
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 }));
+4 -3
View File
@@ -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();
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -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())
}
+5 -3
View File
@@ -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
});
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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)
+4 -2
View File
@@ -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));
}
}
@@ -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<u8>) {
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()));
+2 -2
View File
@@ -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));
+15 -10
View File
@@ -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<ScriptedFiles>) -> (PdfCraftApp, DocId) {
fn app_over(ctx: &egui::Context, files: Arc<ScriptedFiles>) -> (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.
+1 -1
View File
@@ -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();
+3 -3
View File
@@ -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();
+2 -2
View File
@@ -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();
@@ -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");
+1 -1
View File
@@ -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);
}
}
+17 -9
View File
@@ -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");
+3 -3
View File
@@ -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
+3 -3
View File
@@ -1212,7 +1212,7 @@ mod tests {
fn app_with(pdf: Vec<u8>) -> 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");
+4 -4
View File
@@ -1728,7 +1728,7 @@ mod tests {
fn app_review(words: Vec<Vec<PlacedWord>>) -> (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");
+1 -1
View File
@@ -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