PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge PR #3862: M7: open the paragraph editor without a fade-in
aiongg2a3482c95173
diff --cc crates/ui-egui/src/edit_text_ui.rsindex 3efaecacc,75cbd03e1..031d9b3fb--- a/crates/ui-egui/src/edit_text_ui.rs+++ b/crates/ui-egui/src/edit_text_ui.rs@@@ -1527,13 -1336,13 +1527,16 @@@ pub(crate) fn overlay(ctx: &egui::Conteed.source.as_ref().and_then(|(block, lines)| block.lines.iter().filter_map(|i| lines.get(*i)).map(|l| l.size as f32).reduce(f32::min));let font = editor_font(&ed.look, smallest.map_or(ed.size, |s| (s * scale * 0.5).clamp(1.0, 300.0).min(ed.size)));let text_color = color32(ed.look.color);+ let block = ed.block;let mut done = None;+ let mut reveal = false;+ let mut look = None;- egui::Area::new(egui::Id::new("edit-text-line")).order(egui::Order::Foreground).fixed_pos(Pos2::new(ed.rect.left(), ed.rect.top())).show(- ctx,- |ui| {+ // No fade-in: while it faded, the page's own copy of the text showed through the cover.+ egui::Area::new(egui::Id::new("edit-text-line"))+ .order(egui::Order::Foreground)+ .fade_in(false)+ .fixed_pos(Pos2::new(ed.rect.left(), ed.rect.top()))+ .show(ctx, |ui| {// The editor opens at the source size. Once text changes, show the real draft// extent: single rows grow toward the page edge, paragraphs rewrap at their// source width, and the visible frame grows/shrinks vertically with their rows.@@@ -1626,14 -1420,7 +1629,13 @@@done = Some(true);}});- },- );+ });+ if reveal {+ view.text_background.reveal();+ }+ if let Some((galley, pos)) = look {+ view.text_background.remember(&xf, page, block, galley, pos, text_color);+ }match done {Some(apply) => {let ed = view.line_editor.take()?;diff --cc crates/ui-egui/tests/editing.rsindex 78ceeb5ce,568667619..942d789d1--- a/crates/ui-egui/tests/editing.rs+++ b/crates/ui-egui/tests/editing.rs@@@ -2895,107 -2735,62 +2895,163 @@@ fn switching_straight_to_another_box_apassert_eq!(ed.text, "Bottom");}+/// Dark pixels in `r`: glyph ink on a white page.+fn ink(pixels: &image::RgbaImage, r: egui::Rect) -> usize {+ let (x0, y0, x1, y1) = (r.left().max(0.0) as u32, r.top().max(0.0) as u32, r.right() as u32, r.bottom() as u32);+ (y0..y1.min(pixels.height()))+ .flat_map(|y| (x0..x1.min(pixels.width())).map(move |x| (x, y)))+ .filter(|(x, y)| pixels.get_pixel(*x, *y).0[..3].iter().all(|v| *v < 90))+ .count()+}++/// Applying a paragraph changes the page, whose raster still has the old text until it is+/// rendered again. Until then the paragraph keeps the look it had in the editor; the old text+/// used to come back under the new outline for those frames and then jump to the new one.+#[test]+fn an_applied_paragraph_keeps_its_edited_look_until_the_page_is_redrawn() {+ let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Topmost) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {+ let mut app = PdfCraftApp::new();+ app.set_option("language", "en").unwrap();+ app.open_bytes("boxes.pdf", None, bytes).expect("opens");+ app+ });+ h.run_steps(4);+ assert!(h.state_mut().execute("edit.edit_text"));+ h.run_steps(2);+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");+ let k = r.width() / 300.0;+ let top = {+ let s = h.state();+ let blocks = s.session.get(s.views[0].id).unwrap().text_blocks(0);+ let b = blocks.iter().find(|b| b.text == "Topmost").expect("the top box").rect;+ egui::Rect::from_min_max(+ egui::pos2(r.left() + b[0] as f32 * k, r.top() + (300.0 - b[3] as f32) * k),+ egui::pos2(r.left() + b[2] as f32 * k, r.top() + (300.0 - b[1] as f32) * k),+ )+ };+ // "T" stays; the rest of the word goes.+ let head = egui::Rect::from_min_max(top.min, egui::pos2(top.left() + top.width() * 0.12, top.bottom()));+ let tail = egui::Rect::from_min_max(egui::pos2(top.left() + top.width() * 0.3, top.top()), top.max).shrink(2.0);+ click_at(&mut h, top.center());+ support::wait_until(&mut h, "the editor's textless page", |app| !app.render_pending());+ h.state_mut().views[0].line_editor.as_mut().expect("the top box is open").text = "T".into();+ h.run_steps(2);+ h.state_mut().views[0].hold_rasters = true;+ h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);+ for frame in 0..4 {+ h.step();+ assert!(h.state().views[0].line_editor.is_none(), "applied");+ let pixels = h.render().unwrap();+ assert!(ink(&pixels, head) > 20, "frame {frame} after applying shows no text");+ assert_eq!(ink(&pixels, tail), 0, "frame {frame} after applying shows the old text again");+ }+ h.state_mut().views[0].hold_rasters = false;+ support::wait_until(&mut h, "the page drawn with the edit", |app| !app.render_pending());+ h.run_steps(2);+ let pixels = h.render().unwrap();+ assert!(ink(&pixels, head) > 20, "the page draws the applied text");+ assert_eq!(ink(&pixels, tail), 0, "and not the old one");+ let s = h.state();+ assert_eq!(s.session.get(s.views[0].id).unwrap().text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>(), ["T", "Bottom"]);+}++/// A paragraph whose edit fails (here a letter no font can draw) stays open with what was typed,+/// as a form field does; the editor used to close with the error and the typing was lost.+#[test]+fn a_paragraph_whose_edit_fails_stays_open_with_its_typing() {+ let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {+ let mut app = PdfCraftApp::new();+ app.set_option("language", "en").unwrap();+ app.open_bytes("boxes.pdf", None, bytes).expect("opens");+ app+ });+ h.run_steps(4);+ assert!(h.state_mut().execute("edit.edit_text"));+ h.run_steps(2);+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");+ let k = r.width() / 300.0;+ let top = egui::pos2(r.left() + 30.0 * k, r.top() + (300.0 - 228.0) * k);+ click_at(&mut h, top);+ assert_eq!(h.state().views[0].line_editor.as_ref().map(|e| e.text.as_str()), Some("Top"));+ // U+0E01 (Thai): neither the page's font nor a fallback face draws it.+ h.state_mut().views[0].line_editor.as_mut().unwrap().text = "Top ก".into();+ h.run_steps(1);+ h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);+ h.run_steps(3);+ let s = h.state();+ assert!(s.toast.as_ref().is_some_and(|(m, _)| m.starts_with("Edit text failed")), "the failure is said: {:?}", s.toast);+ assert_eq!(s.session.get(s.views[0].id).unwrap().can_undo(), None, "nothing was applied");+ let ed = s.views[0].line_editor.as_ref().expect("the paragraph is still open");+ assert_eq!((ed.block, ed.text.as_str()), (0, "Top ก"), "with what was typed");+ // Taking the letter out applies it.+ h.state_mut().views[0].line_editor.as_mut().unwrap().text = "Top edited".into();+ h.run_steps(1);+ h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);+ h.run_steps(3);+ let s = h.state();+ assert!(s.views[0].line_editor.is_none());+ let doc = s.session.get(s.views[0].id).unwrap();+ assert_eq!(doc.text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>(), ["Top edited", "Bottom"]);+}++ /// Two boxes, "Top" and "Bottom", in Edit text, with egui's animations on: the window animates+ /// unless the system asks for less motion, and the test harness turns them off.+ fn two_boxes_animated() -> (Harness<'static, PdfCraftApp>, [egui::Rect; 2]) {+ let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(move |_cc| {+ let mut app = PdfCraftApp::new();+ app.set_option("language", "en").unwrap();+ app.open_bytes("boxes.pdf", None, bytes).expect("opens");+ app+ });+ h.ctx.all_styles_mut(|s| s.animation_time = 0.2);+ h.run_steps(4);+ assert!(h.state_mut().execute("edit.edit_text"));+ h.run_steps(2);+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");+ let k = r.width() / 300.0;+ let mut blocks = {+ let s = h.state();+ s.session.get(s.views[0].id).unwrap().text_blocks(0)+ };+ blocks.sort_by(|a, b| b.rect[1].partial_cmp(&a.rect[1]).unwrap_or(std::cmp::Ordering::Equal));+ let on_screen = |b: &pdfcraft_engine::TextBlock| {+ egui::Rect::from_min_max(+ egui::pos2(r.left() + b.rect[0] as f32 * k, r.top() + (300.0 - b.rect[3] as f32) * k),+ egui::pos2(r.left() + b.rect[2] as f32 * k, r.top() + (300.0 - b.rect[1] as f32) * k),+ )+ };+ (h, [on_screen(&blocks[0]), on_screen(&blocks[1])])+ }++ /// The colour of the editor's frame (the accent stroke around `r`) this frame, if it is painted.+ fn editor_frame_color(h: &Harness<'static, PdfCraftApp>, r: egui::Rect) -> Option<egui::Color32> {+ h.output().shapes.iter().find_map(|shape| match &shape.shape {+ egui::Shape::Rect(rect) if rect.stroke.width == 1.5 && rect.rect.contains(r.center()) => Some(rect.stroke.color),+ _ => None,+ })+ }++ /// A paragraph opens at once: it used to fade in over a fifth of a second, its frame dimmed and+ /// the page's own copy of the text showing through the editor's cover.+ #[test]+ fn a_paragraph_editor_opens_at_full_opacity() {+ let (mut h, [top, _]) = two_boxes_animated();+ h.hover_at(top.center());+ h.run_steps(1);+ h.drag_at(top.center());+ h.run_steps(1);+ h.drop_at(top.center());+ let accent = egui::Color32::from_rgb(0x14, 0x73, 0xE6);+ for frame in 0..8 {+ h.step();+ // The click's own frame opens it: the editor's frame is there from then on, at full colour.+ assert_eq!(editor_frame_color(&h, top), Some(accent), "frame {frame} after the click");+ }+ }+#[test]fn editing_existing_text_in_place() {let mut h = harness(1, |_| {});