PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
refactor(ui): finish shared image selection and clipboard state cleanup
FredAppsd4810fd00f93
diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rsindex b24e91c52..2f7fb15ce 100644--- a/crates/ui-egui/src/app.rs+++ b/crates/ui-egui/src/app.rs@@ -228,12 +228,10 @@ impl PdfCraftApp {last_snapshot: None,last_copied_image: None,last_copied_file: None,- last_copied_image_rect: None,system_clipboard: true,clipboard_override: None,v_pressed: false,- frame_count: 0,- last_command_frame: None,+ last_command_time: None,attach_override: None,duplicate_draft: None,page_props_draft: None,diff --git a/crates/ui-egui/src/app/clipboard.rs b/crates/ui-egui/src/app/clipboard.rsindex 576be4b10..25266562f 100644--- a/crates/ui-egui/src/app/clipboard.rs+++ b/crates/ui-egui/src/app/clipboard.rs@@ -71,7 +71,6 @@ impl PdfCraftApp {if !has_override_image && (paste_event_text.is_some() || system_has_text) {self.last_copied_image = None;self.last_copied_file = None;- self.last_copied_image_rect = None;return;}diff --git a/crates/ui-egui/src/canvas/image_pick.rs b/crates/ui-egui/src/canvas/image_pick.rsindex cede9b536..cc938170d 100644--- a/crates/ui-egui/src/canvas/image_pick.rs+++ b/crates/ui-egui/src/canvas/image_pick.rs@@ -206,7 +206,6 @@ impl PdfCraftApp {self.put_file_on_clipboard(&name, &bytes)?;self.last_copied_image = None;self.last_copied_file = Some((name, bytes));- self.last_copied_image_rect = None;Ok(ext)});match copied {diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 90076fea1..606862fec 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -152,8 +152,8 @@ pub use tools::bulk_fields;use tools::calc_order_ui;use tools::camera_ui;pub use tools::comments;-pub use tools::content_ui;-pub use tools::edit_text_ui;+use tools::content_ui;+use tools::edit_text_ui;pub use tools::field_table_ui;pub use tools::fill_sign;pub use tools::forms_ui;@@ -551,14 +551,12 @@ pub struct PdfCraftApp {/// The last image copied in its original format (file name, bytes), also put on the/// system clipboard as a file when `system_clipboard` is on.pub last_copied_file: Option<(String, Vec<u8>)>,- pub last_copied_image_rect: Option<[f64; 4]>,pub system_clipboard: bool,/// What ⌘V reads instead of the system clipboard (tests).pub clipboard_override: Option<create_ui::Clip>,/// V is down as a plain key press (see [`Self::paste_key_released`]).v_pressed: bool,- frame_count: u64,- last_command_frame: Option<(u64, f64)>,+ last_command_time: Option<f64>,/// Attach file: the file to attach instead of asking (tests, automation).pub attach_override: Option<(String, Vec<u8>)>,/// Duplicate Field: which field and onto which pages.diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rsindex a41680be9..165651e60 100644--- a/crates/ui-egui/src/tools.rs+++ b/crates/ui-egui/src/tools.rs@@ -2,8 +2,8 @@ pub mod bulk_fields;pub(super) mod calc_order_ui;pub(super) mod camera_ui;pub mod comments;-pub mod content_ui;-pub mod edit_text_ui;+pub(super) mod content_ui;+pub(super) mod edit_text_ui;pub mod field_table_ui;pub mod fill_sign;pub mod forms_ui;diff --git a/crates/ui-egui/src/tools/content_ui.rs b/crates/ui-egui/src/tools/content_ui.rsindex af97ba3ed..c10a4b8bb 100644--- a/crates/ui-egui/src/tools/content_ui.rs+++ b/crates/ui-egui/src/tools/content_ui.rs@@ -924,7 +924,6 @@ impl crate::PdfCraftApp {}self.last_copied_file = None;self.last_copied_image = Some((w, h, rgba));- self.last_copied_image_rect = None;Ok((w, h))});match copied {@@ -1014,16 +1013,7 @@ impl crate::PdfCraftApp {if self.apply_edit(Edit::AddImage { page, rect: None, name, bytes: std::sync::Arc::new(bytes) }) {self.views[i].content.selected = Some((page, before_added));self.views[i].content.select_added = Some((page, before_added));- self.views[i].image_selection = Some(crate::edit_text_ui::ImageSelection {- page,- index: before_img,- crop_mode: false,- crop_rect: None,- drag: None,- crop: None,- turning: false,- settled: None,- });+ self.views[i].image_selection = Some(crate::edit_text_ui::ImageSelection::at(page, before_img));self.quick_tool = crate::QuickTool::EditText;self.left = crate::LeftPanel::Tool("edit");self.left_open = true;@@ -1035,18 +1025,17 @@ impl crate::PdfCraftApp {/// release of V with no press of V since the last one is the shortcut, even when ⌘ or Ctrl/// was let go first. Read every frame, dialogs or not, so a press is never missed.pub(crate) fn paste_key_released(&mut self, ctx: &egui::Context) -> bool {- self.frame_count = self.frame_count.wrapping_add(1);let mut released = false;ctx.input(|inp| {if inp.modifiers.command {- self.last_command_frame = Some((self.frame_count, inp.time));+ self.last_command_time = Some(inp.time);}for e in &inp.events {if let egui::Event::Key { key: egui::Key::V, pressed, modifiers, .. } = e {if modifiers.command {- self.last_command_frame = Some((self.frame_count, inp.time));+ self.last_command_time = Some(inp.time);}- let command_recent = self.last_command_frame.is_some_and(|(_, t)| inp.time >= t && inp.time - t < 0.35);+ let command_recent = self.last_command_time.is_some_and(|t| inp.time >= t && inp.time - t < 0.35);let command_active = modifiers.command || command_recent;if !*pressed && !self.v_pressed && command_active {released = true;@@ -1117,7 +1106,6 @@ impl crate::PdfCraftApp {if !is_same {self.last_copied_file = None;self.last_copied_image = Some((width as u32, height as u32, rgba.clone()));- self.last_copied_image_rect = None;}crate::create_ui::png_from_rgba(width, height, &rgba).ok().map(|b| ("pasted_image.png".to_string(), b))}@@ -1126,7 +1114,6 @@ impl crate::PdfCraftApp {if matches!(system, Some(crate::create_ui::Clip::Text(_))) {self.last_copied_image = None;self.last_copied_file = None;- self.last_copied_image_rect = None;self.notify_tr("No image on clipboard to paste");return false;}@@ -1148,7 +1135,6 @@ impl crate::PdfCraftApp {// System clipboard has a newer / external image!self.last_copied_file = None;self.last_copied_image = Some((img_width as u32, img_height as u32, img_bytes.clone()));- self.last_copied_image_rect = None;crate::create_ui::png_from_rgba(img_width, img_height, &img_bytes).ok().map(|b| ("clipboard_image.png".to_string(), b))}} else if let Some((name, bytes)) = self.last_copied_file.clone() {@@ -1187,7 +1173,6 @@ impl crate::PdfCraftApp {};if self.apply_edit(pdfcraft_engine::Edit::AddImage { page, rect: target_rect, name, bytes: std::sync::Arc::new(bytes) }) {- self.last_copied_image_rect = None;let added_count = self.session.get(id).map_or(0, |d| d.added().iter().filter(|a| a.page == page).count());let added_idx = added_count.saturating_sub(1);let pasted_idx = self.session.get(id).map_or(0, |d| {@@ -1210,16 +1195,7 @@ impl crate::PdfCraftApp {v.current = page;v.content.selected = Some((page, added_idx));v.content.select_added = Some((page, added_idx));- v.image_selection = Some(crate::edit_text_ui::ImageSelection {- page,- index: pasted_idx,- crop_mode: false,- crop_rect: None,- drag: None,- crop: None,- turning: false,- settled: None,- });+ v.image_selection = Some(crate::edit_text_ui::ImageSelection::at(page, pasted_idx));}self.quick_tool = crate::QuickTool::EditText;self.notify_tr("Image pasted");@@ -1339,8 +1315,7 @@ mod tests {fn a_plain_v_release_long_after_command_is_not_a_paste() {let mut app = crate::PdfCraftApp::new();let ctx = egui::Context::default();- app.frame_count = 1;- app.last_command_frame = Some((1, 1.0));+ app.last_command_time = Some(1.0);ctx.run_ui(egui::RawInput {time: Some(10.0),diff --git a/crates/ui-egui/src/tools/edit_text_ui/image.rs b/crates/ui-egui/src/tools/edit_text_ui/image.rsindex ccb22b617..e95e5dbbc 100644--- a/crates/ui-egui/src/tools/edit_text_ui/image.rs+++ b/crates/ui-egui/src/tools/edit_text_ui/image.rs@@ -78,12 +78,6 @@ pub struct ImageSelection {pub drag: Option<ImageDrag>,/// Crop mode: the box kept so far.pub crop: Option<CropMode>,- /// Unused (crop mode is `crop.is_some()`); goes once no literal sets it.- #[doc(hidden)]- pub crop_mode: bool,- /// Unused; goes once no literal sets it.- #[doc(hidden)]- pub crop_rect: Option<Rect>,/// The drag is turning the picture by its rotate handle (the `drag` start is then unused).pub turning: bool,/// A move or resize applied on release: the frame it had and the one it got, in the page's@@ -94,7 +88,7 @@ pub struct ImageSelection {impl ImageSelection {/// Select a page image without an active gesture.pub fn at(page: usize, index: usize) -> Self {- Self { page, index, drag: None, crop: None, crop_mode: false, crop_rect: None, turning: false, settled: None }+ Self { page, index, drag: None, crop: None, turning: false, settled: None }}/// Cancel an in-progress text, rotation or crop drag and restore the crop it was previewing.diff --git a/crates/ui-egui/tests/it/editing.rs b/crates/ui-egui/tests/it/editing.rsindex f197550ad..75ad3f7db 100644--- a/crates/ui-egui/tests/it/editing.rs+++ b/crates/ui-egui/tests/it/editing.rs@@ -4805,8 +4805,6 @@ fn tiny_image_crop_handle_drag_does_not_panic() {index: 0,drag: None,crop: Some(pdfcraft_ui_egui::CropMode::new([100.0, 100.0, 105.0, 105.0])),- crop_mode: true,- crop_rect: None,turning: false,settled: None,});@@ -7652,7 +7650,6 @@ fn newer_clipboard_override_beats_stale_in_app_copy() {// Stale in-app copy: 10x10 red imageh.state_mut().last_copied_image = Some((10, 10, [255u8, 0, 0, 255].repeat(100)));- h.state_mut().last_copied_image_rect = Some([10.0, 10.0, 20.0, 20.0]);// Newer clipboard override: 50x50 green imageh.state_mut().clipboard_override = Some(pdfcraft_ui_egui::Clip::Image { width: 50, height: 50, rgba: [0u8, 255, 0, 255].repeat(2500) });@@ -8061,16 +8058,8 @@ fn arrange_duplicate_image_clears_selection_when_ambiguous() {assert_eq!(images[0].name, images[1].name);// Select the second occurrence (index 1)- h.state_mut().views[0].image_selection = Some(pdfcraft_ui_egui::edit_text_ui::ImageSelection {- page: 0,- index: 1,- drag: None,- crop: None,- crop_mode: false,- crop_rect: None,- turning: false,- settled: None,- });+ h.state_mut().views[0].image_selection =+ Some(pdfcraft_ui_egui::ImageSelection { page: 0, index: 1, drag: None, crop: None, turning: false, settled: None });h.run_steps(2);// Arrange send to back@@ -8127,16 +8116,8 @@ fn arrange_unique_image_preserves_selection() {assert_eq!(images[1].name, "FigB");// Select FigA at index 0 and bring it to front (should move after FigB, becoming index 1)- h.state_mut().views[0].image_selection = Some(pdfcraft_ui_egui::edit_text_ui::ImageSelection {- page: 0,- index: 0,- drag: None,- crop: None,- crop_mode: false,- crop_rect: None,- turning: false,- settled: None,- });+ h.state_mut().views[0].image_selection =+ Some(pdfcraft_ui_egui::ImageSelection { page: 0, index: 0, drag: None, crop: None, turning: false, settled: None });h.run_steps(2);let applied = h.state_mut().apply_edit(pdfcraft_engine::Edit::EditPageImage {@@ -8191,16 +8172,8 @@ fn undo_arrange_re_resolves_image_selection_or_clears() {assert_eq!(images[1].name, "FigB");// Select FigA (back image, index 0)- h.state_mut().views[0].image_selection = Some(pdfcraft_ui_egui::edit_text_ui::ImageSelection {- page: 0,- index: 0,- drag: None,- crop: None,- crop_mode: false,- crop_rect: None,- turning: false,- settled: None,- });+ h.state_mut().views[0].image_selection =+ Some(pdfcraft_ui_egui::ImageSelection { page: 0, index: 0, drag: None, crop: None, turning: false, settled: None });h.run_steps(2);// Bring to front@@ -8482,17 +8455,12 @@ fn copy_pasted_image_discards_coordinates_and_moves_without_offset() {// 2. Copy the image from page 0assert!(h.state_mut().copy_page_image(0, 0));- // Verify coordinates data was discarded during copy!- assert!(h.state().last_copied_image_rect.is_none(), "coordinates data must be discarded during copy");assert!(h.state().last_copied_image.is_some(), "image pixel data is kept");// 3. Paste the image via Ctrl+V (paste_image with rect: None)assert!(h.state_mut().paste_image(0, None));h.run_steps(3);- // Verify after paste that coordinates data remains None- assert!(h.state().last_copied_image_rect.is_none());-let images_after_paste = h.state().session.get(id).unwrap().page_images(0);assert_eq!(images_after_paste.len(), 2, "second image pasted");