Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge main into feat/align-selected-lines

RiceDee6901f6687666
diff --cc crates/ui-egui/src/edit_text_ui.rs
index 563dd4a29,f67398d84..dc8958f24
--- a/crates/ui-egui/src/edit_text_ui.rs
+++ b/crates/ui-egui/src/edit_text_ui.rs
@@@ -1628,10 -1560,8 +1635,9 @@@ fn editor_galley(ui: &egui::Ui, ed: &Li
/// The inline editor; returns the edit once the text is applied.
pub(crate) fn overlay(ctx: &egui::Context, view: &mut DocView, info: &DocInfo) -> Option<Edit> {
+ let view_id = view.id;
- // Clicks outside the document (the Format text panel) keep the paragraph open.
- // Where the click that took the focus was: the pointer itself may be gone by then (a touch, a
- // pen lifted away).
+ // Clicks outside the document (the Format text panel) keep the paragraph open. Where the
+ // click landed is `interact_pos`: `latest_pos` is gone on the frame a tap lifts.
let outside = ctx.input(|i| i.pointer.interact_pos()).is_some_and(|p| !view.viewport_rect().contains(p));
let page = view.line_editor.as_ref()?.page;
let xf = view.page_xform(page)?;
@@@ -1780,11 -1706,9 +1786,12 @@@
}
if ed.focus {
r.request_focus();
+ if let Some(range) = &ed.selection {
+ reselect(ui.ctx(), input, &ed.text, range);
+ }
ed.focus = false;
}
+ let lost = lost_focus_once(&r, &mut ed.had_focus);
let esc = ui.input(|i| i.key_pressed(egui::Key::Escape));
let apply = ui.input(|i| i.key_pressed(egui::Key::Enter) && i.modifiers.command);
if esc {
diff --cc crates/ui-egui/src/editing.rs
index 0cc261901,9a13d570d..55404a8dc
--- a/crates/ui-egui/src/editing.rs
+++ b/crates/ui-egui/src/editing.rs
@@@ -1863,6 -1701,56 +1870,74 @@@ mod revert_draft_tests
assert!(app.unsaved_flag.load(Ordering::Relaxed));
}
+ /// Text typed into a new text box is lost on close unless the save prompt appears; an empty
+ /// box has nothing to lose.
+ #[test]
+ fn text_typed_into_a_new_text_box_counts_as_unsaved_work() {
+ use crate::content_ui::{TextDraft, default_style};
+ let mut app = app();
- let draft = |text: &str| TextDraft { page: 0, index: None, rect: [0.0; 4], text: text.into(), style: default_style(), focus: true };
++ let draft = |text: &str| TextDraft {
++ page: 0,
++ index: None,
++ rect: [0.0; 4],
++ text: text.into(),
++ style: default_style(),
++ focus: true,
++ selection: None,
++ typing_from: None,
++ };
+ assert!(!app.has_unsaved_work(0));
+ app.views[0].content.draft = Some(draft(" \n"));
+ assert!(!app.has_unsaved_work(0));
+ app.views[0].content.draft = Some(draft("Typed"));
+ assert!(!app.session.get(app.views[0].id).unwrap().dirty);
+ assert!(app.has_unsaved_work(0));
+ }
+
+ /// Close ▸ Save, and Quit ▸ Save, write the text typed into a new text box before the tab
+ /// closes.
+ #[test]
+ fn saving_on_close_keeps_text_typed_into_a_new_text_box() {
+ use crate::content_ui::{TextDraft, default_style};
+ use pdfcraft_engine::AddedContent;
+ for quit in [false, true] {
+ let dir = std::env::temp_dir().join(format!("pdfcraft-ui-new-text-{}-{quit}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("source.pdf").to_string_lossy().into_owned();
+ let bytes = include_bytes!("../tests/data/form.pdf").to_vec();
+ std::fs::write(&path, &bytes).unwrap();
+ let mut app = PdfCraftApp::new();
+ app.open_bytes("source.pdf", Some(path.clone()), bytes).unwrap();
+ let rect = [72.0, 72.0, 272.0, 92.0];
- app.views[0].content.draft = Some(TextDraft { page: 0, index: None, rect, text: "Typed".into(), style: default_style(), focus: true });
++ app.views[0].content.draft = Some(TextDraft {
++ page: 0,
++ index: None,
++ rect,
++ text: "Typed".into(),
++ style: default_style(),
++ focus: true,
++ selection: None,
++ typing_from: None,
++ });
+ let ctx = egui::Context::default();
+ if quit {
+ assert!(!app.request_quit(&ctx));
+ } else {
+ app.request_close_tab(0);
+ }
+ assert!(app.close_request.is_some());
+ app.resolve_close(&ctx, Some(true));
+ assert!(app.views.is_empty(), "the tab closes once saved");
+
+ let mut reopened = PdfCraftApp::new();
+ reopened.open_bytes("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);
+ }
+ }
+
#[test]
fn successful_revert_clears_focused_or_queued_drafts_only_on_its_document() {
for queued in [false, true] {