PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge PR #3863: M7: keep an applied paragraph's look until its page is drawn again
aiongg626529768a59
diff --cc crates/ui-egui/src/edit_text_ui.rsindex eb1edce62,197f80c89..404ae4d53--- a/crates/ui-egui/src/edit_text_ui.rs+++ b/crates/ui-egui/src/edit_text_ui.rs@@@ -32,9 -32,23 +32,26 @@@ pub(crate) struct TextBackgroundreceived: Option<u64>,draft_key: Option<(String, pdfcraft_engine::BlockStyle)>,draft_rect: Option<[f64; 4]>,+ /// This frame's place for the textless page under the open paragraph's editor. It stays+ /// empty, so the page shows its own text, until the paragraph is changed ([`Self::reveal`]).+ slot: Option<(egui::Painter, egui::layers::ShapeIdx, egui::Shape)>,+ /// How the open paragraph looked in its editor last frame.+ look: Option<EditorLook>,+ /// An applied paragraph's last look. The page raster still has the old text until the page+ /// is rendered again, so the textless page and this look stay up until then.+ settling: Option<EditorLook>,+ }++ /// A paragraph editor's text as last drawn: its laid-out rows, where they were on the page (in+ /// the page's 0..1 coordinates), and the page's width and turn on screen then.+ struct EditorLook {+ page: usize,+ block: usize,+ galley: Arc<egui::Galley>,+ at: (f32, f32),+ color: Color32,+ page_width: f32,+ rot: u16,}impl TextBackground {@@@ -53,7 -95,9 +98,10 @@@let key = editor.map(|ed| (doc.edit_generation(), ed.page, TextTarget::Block(ed.block))).or_else(|| draft.and_then(|d| d.index.map(|index| (doc.edit_generation(), d.page, TextTarget::Added(index)))));+ self.slot = None;+ if key.is_none() && self.settling.is_some() {+ return;+ }if self.key != key {*self = Self { key, ..Default::default() };if let Some((_, page, target)) = key {@@@ -126,8 -165,13 +174,14 @@@editor: Option<&LineEditor>,draft: Option<&crate::content_ui::TextDraft>,) {+ let for_editor = editor.is_some_and(|ed| ed.page == page && self.ready(page, ed.block));- let ready = for_editor || draft.is_some_and(|d| d.page == page && d.index.is_some_and(|index| self.ready_added(page, index)));+ // A zoom or a turn of the view since the paragraph was applied would misplace its rows:+ // the page raster (with the old text) shows until the new one arrives instead.+ let settled =+ self.settling.as_ref().filter(|look| look.page == page && look.rot == xf.rot && (look.page_width - xf.rect.width()).abs() < 0.5);- let ready = editor.is_some_and(|ed| ed.page == page && self.ready(page, ed.block))++ let ready = for_editor+ || draft.is_some_and(|d| d.page == page && d.index.is_some_and(|index| self.ready_added(page, index)))+ || settled.is_some();if !ready {return;}@@@ -135,26 -179,9 +189,29 @@@// Font metrics can underestimate the actual glyph ink. Paint the complete textless// page so overhanging glyphs cannot remain outside the estimated source rectangle.let painter = painter.with_clip_rect(painter.clip_rect().intersect(xf.rect));- xf.paint_image(&painter, tex.id(), 0.0, 0.0, 1.0, 1.0);- if let Some(look) = settled {- painter.galley(xf.norm_to_screen(look.at.0, look.at.1), Arc::clone(&look.galley), look.color);+ if for_editor {+ // Its place under everything drawn on the page later; the editor fills it once the+ // paragraph changes (an unchanged paragraph looks as the page draws it).+ let mut mesh = egui::Mesh::with_texture(tex.id());+ for (u, v) in [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)] {+ mesh.vertices.push(egui::epaint::Vertex { pos: xf.norm_to_screen(u, v), uv: Pos2::new(u, v), color: Color32::WHITE });+ }+ mesh.add_triangle(0, 1, 2);+ mesh.add_triangle(0, 2, 3);+ let idx = painter.add(egui::Shape::Noop);+ self.slot = Some((painter, idx, egui::Shape::mesh(mesh)));+ } else {+ xf.paint_image(&painter, tex.id(), 0.0, 0.0, 1.0, 1.0);++ if let Some(look) = settled {++ painter.galley(xf.norm_to_screen(look.at.0, look.at.1), Arc::clone(&look.galley), look.color);++ }+ }+ }++ /// The open paragraph has changed: show the textless page under its editor this frame.+ pub fn reveal(&mut self) {+ if let Some((painter, idx, shape)) = self.slot.take() {+ painter.set(idx, shape);}}}@@@ -1468,8 -1392,9 +1525,10 @@@ 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| {@@@ -1535,13 -1453,7 +1594,14 @@@.desired_width(width).desired_rows(rows).show(ui);+ if ed.has_changes() {+ if background_ready {+ reveal = true;+ } else {+ ui.painter().set(cover, egui::Shape::rect_filled(ed.rect.expand(1.0).union(output.response.rect), CornerRadius::ZERO, fill));+ }+ }+ look = Some((Arc::clone(&output.galley), output.galley_pos));if let Some(cursor) = output.cursor_range {let selected = cursor.as_sorted_char_range();let byte = |index| ed.text.char_indices().nth(index).map_or(ed.text.len(), |(at, _)| at);@@@ -1566,9 -1478,9 +1626,12 @@@});},);+ 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()?;