From 0c438ebbcafbf97a9dccbc0937a6c42e46dbc1f8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 19 Nov 2026 08:38:50 +0100 Subject: [PATCH] Add inverse conversion from smart to edge crop --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/canvas-pilot/src/format_ui.rs | 13 ++++++ crates/canvas-pilot/src/gui.rs | 12 ++++++ crates/canvas-pilot/src/lib.rs | 22 ++++++++++ crates/canvas-pilot/tests/layer_fit.rs | 45 +++++++++++++++++++++ docs/canvas-pilot/changelog.md | 6 +++ docs/canvas-pilot/guide/formats-and-crop.md | 6 ++- 8 files changed, 104 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f928d6..8df8eea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "canvas-pilot" -version = "0.10.0-rc.14" +version = "0.10.0-rc.15" dependencies = [ "base64 0.22.1", "craft-dwg", diff --git a/Cargo.toml b/Cargo.toml index e5d5b9b..44fa72b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["crates/canvas-pilot"] resolver = "3" [workspace.package] -version = "0.10.0-rc.14" +version = "0.10.0-rc.15" edition = "2024" license = "MIT" repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot" diff --git a/crates/canvas-pilot/src/format_ui.rs b/crates/canvas-pilot/src/format_ui.rs index 17bc604..5686108 100644 --- a/crates/canvas-pilot/src/format_ui.rs +++ b/crates/canvas-pilot/src/format_ui.rs @@ -511,6 +511,19 @@ impl Editor { }); if !ui.input(|input| input.pointer.primary_down()) { d.crop_before = None; } ui.small(tr(ui.ctx(), "ui.crop-zoom-and-offsets-apply-as-you-edit-offsets-stop-at-the-content-edges-and-adjust")); + if ui.button("Convert to Edge crop") + .on_hover_text("Keep the current picture and crop, then edit its source edges independently. Undo returns to Smart crop.") + .clicked() { + match self.layout.convert_to_edge_crop(i) { + Ok(()) => { + self.sync(); + d.refresh_fit(&self.layout); + self.edit_mask = false; + self.edit_content = true; + } + Err(e) => d.error = e, + } + } } } if self.property_tab == PropertyTab::Preview { diff --git a/crates/canvas-pilot/src/gui.rs b/crates/canvas-pilot/src/gui.rs index eac633a..ccdd22d 100644 --- a/crates/canvas-pilot/src/gui.rs +++ b/crates/canvas-pilot/src/gui.rs @@ -2515,6 +2515,16 @@ impl Editor { } ui.close(); } + if self.layout.formats().layer_fit[i].is_none() + && ui.button("Convert to Edge crop") + .on_hover_text("Keep the current picture and crop, then edit its source edges independently. Undo returns to Smart crop.") + .clicked() { + match self.layout.convert_to_edge_crop(i) { + Ok(()) => { self.sync(); self.edit_mask = false; self.edit_content = true; }, + Err(e) => self.error = e, + } + ui.close(); + } if ui.button(tr(ui.ctx(), "ui.reset-crop")).clicked() { let result = if let Some(mode) = self.layout.formats().layer_fit[i] { self.layout @@ -9126,6 +9136,8 @@ mod tests { !labels.iter().any(|label| label.starts_with("crop.")), "raw key in {language:?} menu" ); + assert!(labels.contains(&"Convert to Edge crop")); + assert!(!labels.contains(&"Convert to Smart crop")); } } } diff --git a/crates/canvas-pilot/src/lib.rs b/crates/canvas-pilot/src/lib.rs index 2268f30..41f5823 100644 --- a/crates/canvas-pilot/src/lib.rs +++ b/crates/canvas-pilot/src/lib.rs @@ -1082,6 +1082,28 @@ impl Layout { Ok(()) } + /// Switch a normal crop to Centered edge crop without changing its picture. + pub fn convert_to_edge_crop(&mut self, i: usize) -> Result<(), String> { + if !self.selectable(i) { + return Err("Unlock and show this layer first.".into()); + } + if self.formats.layer_fit[i].is_some() { + return Ok(()); + } + self.finish_drag(); + let geometry = self.layers[i]; + let crop = self.formats.advanced[i]; + formats::mapping( + self.formats.input(i)?, + [geometry.width, geometry.height], + crop, + )?; + let before = self.snapshot(); + self.formats.layer_fit[i] = Some(formats::LayerFit::Centered); + self.remember(before); + Ok(()) + } + pub fn start_content_resize(&mut self, index: usize, handle: ResizeHandle, point: [f64; 2]) { self.finish_drag(); if !self.selectable(index) diff --git a/crates/canvas-pilot/tests/layer_fit.rs b/crates/canvas-pilot/tests/layer_fit.rs index 2cc134c..d5e3ff2 100644 --- a/crates/canvas-pilot/tests/layer_fit.rs +++ b/crates/canvas-pilot/tests/layer_fit.rs @@ -505,3 +505,48 @@ fn rounded_import_tolerance_is_bounded_and_does_not_change_native_or_stretch_mod } } } + +#[test] +fn smart_crop_converts_to_centered_edge_crop_losslessly_and_undoes() { + use canvas_pilot::formats::{Framing, Mask}; + let mut layout = Layout::default(); + layout.select(Some(0)); + layout + .set_framing( + 0, + Framing { + zoom: 1.75, + offset: [41.0, -23.0], + }, + ) + .unwrap(); + let mask = Mask { + left: 0.1, + right: 0.05, + ..Default::default() + }; + layout.set_mask(0, mask).unwrap(); + let before = Project::from_layout(&layout); + let geometry = layout.layers()[0]; + let crop = layout.formats().advanced[0]; + let input = layout.formats().input(0).unwrap(); + + layout.convert_to_edge_crop(0).unwrap(); + assert_eq!(layout.formats().layer_fit[0], Some(LayerFit::Centered)); + assert_eq!(layout.layers()[0], geometry); + assert_eq!(layout.formats().advanced[0], crop); + assert_eq!(layout.formats().masks[0], mask); + let image = LayerFit::Centered + .image(crop.remaining(input).unwrap(), geometry) + .unwrap(); + near(image.x, geometry.x); + near(image.y, geometry.y); + near(image.width, geometry.width); + near(image.height, geometry.height); + + layout.undo(); + assert_eq!(Project::from_layout(&layout), before); + layout.redo(); + layout.convert_to_smart_crop(0).unwrap(); + assert_eq!(Project::from_layout(&layout), before); +} diff --git a/docs/canvas-pilot/changelog.md b/docs/canvas-pilot/changelog.md index 527c99f..f0697b7 100644 --- a/docs/canvas-pilot/changelog.md +++ b/docs/canvas-pilot/changelog.md @@ -4,6 +4,12 @@ ## Unreleased +### v0.10.0-rc.15 + +- Add the inverse **Convert to Edge crop** action to the layer context menu and + Crop properties. Preserve the Smart crop's picture, crop, container and mask, + enter the green edge helper immediately, and return to Smart crop with Undo. + ### v0.10.0-rc.14 - Enabling an input preview from the right-click menu or Preview properties queues diff --git a/docs/canvas-pilot/guide/formats-and-crop.md b/docs/canvas-pilot/guide/formats-and-crop.md index e6afe29..4d9b7a9 100644 --- a/docs/canvas-pilot/guide/formats-and-crop.md +++ b/docs/canvas-pilot/guide/formats-and-crop.md @@ -192,8 +192,10 @@ Numeric crop controls remain available in Crop properties. **Convert to Smart crop**, in the right-click menu and Crop properties, switches to Canvas Pilot's normal zoom/position algorithm. It preserves an exactly matching crop; otherwise it resets to centered cover framing, so the picture can change. -Undo restores the previous fit and crop. Unchecking Advanced layer fit also resets -to centered cover framing. Pending Source edits still require Apply. +**Convert to Edge crop** appears in the same places for a Smart crop. It keeps the +current picture, crop, container and mask exactly as they are, selects Centered fit, +and opens the green edge helper. Undo returns to Smart crop. Unchecking Advanced +layer fit resets to centered cover framing. Pending Source edits still require Apply. Device loads use Smart crop when the retained picture exactly fills the container with uniform scaling and the crop is representable by the normal algorithm.