Skip to content

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

Merge main into fix/undo-in-open-text-editor

RiceDeec779488c79e2
diff --cc crates/ui-egui/src/content_ui.rs
index 77edee687,0da3b235c..23f9377a6
--- a/crates/ui-egui/src/content_ui.rs
+++ b/crates/ui-egui/src/content_ui.rs
@@@ -691,11 -697,6 +720,11 @@@ fn finish(cv: &mut ContentView, d: Text
}
}
+/// The added-text box's text field.
+pub(crate) fn input_id() -> egui::Id {
- egui::Id::new("added-text-input")
++ egui::Id::unique("added-text-input")
+}
+
/// How to use the tools, under the Add content list.
pub(crate) fn hint(ui: &mut egui::Ui, t: &Tokens) {
ui.label(
diff --cc crates/ui-egui/src/edit_text_ui.rs
index a0f73eaf4,4f5630c0b..da9c958d4
--- a/crates/ui-egui/src/edit_text_ui.rs
+++ b/crates/ui-egui/src/edit_text_ui.rs
@@@ -260,51 -260,6 +260,51 @@@ pub struct LineEditor
/// horizontal scale (%), and what they were.
pub extras: Extras,
extras0: Extras,
+ /// The text the box's own Undo history starts from; see [`forget_typing_before`].
+ typing_from: Option<String>,
+}
+
+/// The paragraph editor's text box.
+pub(crate) fn input_id() -> egui::Id {
- egui::Id::new("edit-text-line-input")
++ egui::Id::unique("edit-text-line-input")
+}
+
+/// The page text box `id` keeps its own Undo history, across every paragraph or box it has shown
+/// in every tab (they share the id). Once the page has `text` (the box opened on it, or formatting
+/// applied it), that history is cleared: Undo in the box takes back only what was typed since, and
+/// then reaches the document. Kept, it put back text from another paragraph, or the whole paragraph
+/// as it was before a format change, into a box showing the page as it is now. `from` is the box's
+/// own record of that; which document `doc` last used the history is kept beside it, since a box
+/// in another tab on the same text would otherwise take that document's typing with its Redo.
+/// When another document's box takes the text box, the box it leaves keeps its history (and
+/// caret) aside, and gets it back on its return, so its typing can still be undone there. Each
+/// box claims the text box when it is drawn, and Undo and Redo from the menu claim it first too,
+/// since they can run before the box is drawn again after a tab switch.
+pub(crate) fn forget_typing_before(ctx: &egui::Context, id: egui::Id, doc: pdfcraft_engine::DocId, from: &mut Option<String>, text: &str) {
+ let owner = id.with("typing-doc");
+ let last = ctx.data(|d| d.get_temp::<pdfcraft_engine::DocId>(owner));
+ if from.as_deref() == Some(text) && last == Some(doc) {
+ return;
+ }
+ let aside = |doc: pdfcraft_engine::DocId| id.with(("typing-aside", doc));
+ if let Some(left) = last.filter(|l| *l != doc)
+ && let Some(state) = egui::TextEdit::load_state(ctx, id)
+ {
+ // A copy of its own: a state's history is shared between its clones, and the next box
+ // clears it in place.
+ let mut kept = egui::text_edit::TextEditState::default();
+ kept.cursor = state.cursor;
+ kept.set_undoer(state.undoer());
+ ctx.data_mut(|d| d.insert_temp(aside(left), kept));
+ }
+ // Only the same box, still on the text it opened on, takes its history back.
+ let back = ctx.data_mut(|d| d.remove_temp::<egui::text_edit::TextEditState>(aside(doc))).filter(|_| from.as_deref() == Some(text));
+ // A box new to the text box starts with nothing of the last one's: no history, caret or
+ // selection.
+ let state = back.unwrap_or_default();
+ egui::TextEdit::store_state(ctx, id, state);
+ ctx.data_mut(|d| d.insert_temp(owner, doc));
+ *from = Some(text.to_owned());
}
#[derive(Clone, Copy, Debug, PartialEq)]