PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M10: review of #1609: no automatic recognition in Read mode or beneath a dialog
Philip0b3fa7afb5a9
diff --git a/crates/ui-egui/src/ocr_ui.rs b/crates/ui-egui/src/ocr_ui.rsindex 34f023d6d..dcd355375 100644--- a/crates/ui-egui/src/ocr_ui.rs+++ b/crates/ui-egui/src/ocr_ui.rs@@ -184,7 +184,10 @@ impl PdfCraftApp {// the same test the page view uses for editing content.let editing = (self.left_open && self.left == crate::LeftPanel::Tool("edit"))|| matches!(self.quick_tool, crate::QuickTool::EditText | crate::QuickTool::AddText);- if !editing || self.ocr_run.is_some() || self.ocr_batch.is_some() {+ // Not in Read mode (which leaves the Edit tool picked but is not editing) and not beneath a+ // dialog or prompt: nothing may change the document under those. It is tried again once+ // they are gone.+ if !editing || self.mode == crate::Mode::Read || self.modal_open() || self.ocr_run.is_some() || self.ocr_batch.is_some() {return;}let Some((vi, id)) = self.active_ids() else { return };diff --git a/crates/ui-egui/tests/ocr.rs b/crates/ui-egui/tests/ocr.rsindex 86740c451..872768c54 100644--- a/crates/ui-egui/tests/ocr.rs+++ b/crates/ui-egui/tests/ocr.rs@@ -309,6 +309,33 @@ fn a_page_that_becomes_a_scan_while_edit_is_on_is_recognized() {assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Recognize text"));}+/// Edit changes the document only while the user is editing: not in Read mode (which leaves the Edit+/// tool picked), and not beneath a dialog. The page is recognized once those are gone.+#[test]+fn edit_recognizes_a_scan_only_outside_read_mode_and_beneath_no_dialog() {+ if !pdfcraft_engine::ocr::available() {+ eprintln!("skipped: OCR models not installed");+ return;+ }+ let mut h = edit_harness(scan());+ let id = h.state().active_ids().unwrap().1;+ let undo = |h: &Harness<'static, PdfCraftApp>| -> Option<String> { h.state().session.get(id).unwrap().can_undo().map(String::from) };+ // Read mode with the Edit tool still picked.+ assert!(h.state_mut().execute("edit.edit_text"));+ h.state_mut().set_option("mode", "read").unwrap();+ h.run_steps(5);+ assert_eq!(undo(&h), None, "not in Read mode");+ // A dialog over the window.+ h.state_mut().set_option("mode", "edit").unwrap();+ h.state_mut().dialog = Some(pdfcraft_ui_egui::Dialog::JsConsole);+ h.run_steps(5);+ assert_eq!(undo(&h), None, "not beneath a dialog");+ // Both gone: it is read now.+ h.state_mut().dialog = None;+ h.run_steps(5);+ assert_eq!(undo(&h).as_deref(), Some("Recognize text"));+}+#[test]fn editing_a_page_that_has_text_does_not_recognize_anything() {let mut h = edit_harness(text_page());