diff --git a/Cargo.lock b/Cargo.lock index ff98cc0..721a7a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "canvas-pilot" -version = "0.10.0-rc.12" +version = "0.10.0-rc.13" dependencies = [ "base64 0.22.1", "craft-dwg", diff --git a/Cargo.toml b/Cargo.toml index c66c25a..7232d0f 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.12" +version = "0.10.0-rc.13" 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 1e1cd31..17bc604 100644 --- a/crates/canvas-pilot/src/format_ui.rs +++ b/crates/canvas-pilot/src/format_ui.rs @@ -469,14 +469,16 @@ impl Editor { let current = self.layout.layers()[i]; let (_, area) = ui.allocate_space(Vec2::new(ui.available_width(),170.0)); paint_layer_source(ui.painter(),fit_rect(area,[current.width,current.height]), &self.layout, i, self.layer_preview(i)); - if current_fit == canvas_pilot::formats::LayerFit::OneToOne { - ui.small(tr(ui.ctx(), "ui.drag-helper-edges-to-trim-without-zooming-drag-anywhere-inside-the-layer-including-em")); - } else if matches!(current_fit, canvas_pilot::formats::LayerFit::Centered | canvas_pilot::formats::LayerFit::Cropped) { - ui.small(tr(ui.ctx(), "ui.this-mode-fits-automatically-the-canvas-crop-helper-is-disabled-numeric-crop-edges-re")); - } else { - ui.small(tr(ui.ctx(), "ui.edit-crop-edges-here-visual-zoom-drag-controls-are-for-normal-crop-mode")); + ui.small("Drag green edge handles on the canvas to trim each side. The image stays still until release, then refits. Double-click or Enter opens/closes this helper."); + if ui.button("Convert to Smart crop") + .on_hover_text("Keep an exact matching crop; otherwise reset to centered cover framing. Undo restores the advanced fit.") + .clicked() { + match self.layout.convert_to_smart_crop(i) { + Ok(()) => { self.sync(); d.refresh_fit(&self.layout); self.edit_mask = false; self.edit_content = true; } + Err(e) => d.error = e, + } } - ui.small(tr(ui.ctx(), "ui.disabling-advanced-fit-restores-normal-cover-framing-device-subpixel-rendering-is-app")); + } else { ui.label(tr(ui.ctx(), "ui.zoom-and-position-change-the-crop-values-inside-this-layer-keeping-its-visible-box-fi")); @@ -582,9 +584,10 @@ pub(super) fn content_bounds(rect: Rect, source: Source, crop: Crop) -> Option String { ) } impl Editor { - fn automatic_layer_fit(&self, i: usize) -> bool { - matches!( - self.layout.formats().layer_fit[i], - Some( - canvas_pilot::formats::LayerFit::Centered - | canvas_pilot::formats::LayerFit::Cropped - ) - ) - } fn mask_editing(&self) -> bool { self.edit_mask || (self.edit_content && self.alt_mask) } @@ -603,18 +594,8 @@ impl Editor { fn toggle_crop_edit(&mut self) { self.edit_mask = false; - if let Some(i) = self.layout.selected() - && self.layout.formats().layer_fit[i] - .is_some_and(|fit| fit != canvas_pilot::formats::LayerFit::OneToOne) - { - self.edit_content = false; - self.property_tab = PropertyTab::Crop; - if self.format_draft.is_none() { - self.format_draft = Some(FormatDraft::new(&self.layout, i)); - } - } else { - self.edit_content = !self.edit_content; - } + self.layout.finish_drag(); + self.edit_content = !self.edit_content; } pub(super) fn with_saved_settings(ctx: &egui::Context) -> Self { @@ -1929,25 +1910,18 @@ impl Editor { "crop.adjust" }; let edit_width = if self.edit_content { w - 120.0 } else { w }; - let automatic_fit = self.automatic_layer_fit(i) && !self.mask_editing(); - if automatic_fit { - ink.card(0.0, 275.0, edit_width, 34.0, palette.field, palette.line); - ink.text( - 12.0, - 284.0, - &tr(ink.painter.ctx(), "ui.crop-helper-unavailable"), - 13.0, - palette.muted, - ); - } else { - ink.button( - 0.0, - 275.0, - edit_width, - &tr(ui.ctx(), edit_label), - self.edit_content, - ); - } + let edit_label_text = tr(ui.ctx(), edit_label); + ink.button( + 0.0, + 275.0, + edit_width, + if self.layout.formats().layer_fit[i].is_some() && !self.edit_content { + "Adjust edge crop" + } else { + &edit_label_text + }, + self.edit_content, + ); if self.edit_content { ink.button( w - 108.0, @@ -1987,14 +1961,12 @@ impl Editor { } } } - if !automatic_fit - && click( - ui, - &ink, - [0.0, 275.0, edit_width, 34.0], - "edit-layer-content", - ) - { + if click( + ui, + &ink, + [0.0, 275.0, edit_width, 34.0], + "edit-layer-content", + ) { if self.mask_editing() { self.edit_content = false; self.format_draft = None; @@ -2009,14 +1981,8 @@ impl Editor { ui.ctx(), if self.mask_editing() { "crop.help-mask" - } else if automatic_fit { - "crop.help-auto" - } else if self.layout.formats().layer_fit[i] - == Some(canvas_pilot::formats::LayerFit::OneToOne) - { - "crop.help-native" } else if self.layout.formats().layer_fit[i].is_some() { - "crop.help-advanced" + "Drag green edges to trim. Image refits on release." } else { "crop.help-normal" }, @@ -2526,17 +2492,29 @@ impl Editor { "crop.adjust" }; if ui - .add_enabled( - !self.automatic_layer_fit(i), - egui::Button::new(tr(ui.ctx(), crop_label)), - ) - .on_disabled_hover_text("Centered and Cropped fit automatically; use Crop properties for numeric crop edges.") - .on_hover_text(tr(ui.ctx(), "ui.change-crop-zoom-and-offset-inside-the-layer-keep-its-visible-box-fixed")) + .button(if self.layout.formats().layer_fit[i].is_some() { + if self.edit_content && !self.mask_editing() { "Finish edge crop" } else { "Adjust edge crop" }.into() + } else { + tr(ui.ctx(), crop_label) + }) + .on_hover_text(if self.layout.formats().layer_fit[i].is_some() { + "Trim green edges independently. The image refits on release; the container stays fixed.".into() + } else { tr(ui.ctx(), "ui.change-crop-zoom-and-offset-inside-the-layer-keep-its-visible-box-fixed") }) .clicked() { self.toggle_crop_edit(); ui.close(); } + if self.layout.formats().layer_fit[i].is_some() + && ui.button("Convert to Smart crop") + .on_hover_text("Keep an exact matching crop; otherwise reset to centered cover framing. Undo restores the advanced fit.") + .clicked() { + match self.layout.convert_to_smart_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 @@ -2734,16 +2712,13 @@ impl Editor { .is_some_and(|bounds| bounds.contains(pointer))) .then_some(i); } - if self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne) { + if self.layout.formats().layer_fit[i].is_some() { return (rect.contains(pointer) || self .crop_bounds(origin, scale) .is_some_and(|r| r.contains(pointer))) .then_some(i); } - if self.layout.formats().layer_fit[i].is_some() { - return None; - } format_ui::content_bounds( rect, self.layout.formats().sources[self.layout.formats().source_for[i]], @@ -2768,14 +2743,8 @@ impl Editor { Vec2::new(g.width as f32, g.height as f32) * scale, )); } - if let Some(fit) = self.layout.formats().layer_fit[i] { - if fit != canvas_pilot::formats::LayerFit::OneToOne { - return None; - } - let retained = self.layout.formats().advanced[i] - .remaining(self.layout.formats().input(i).ok()?) - .ok()?; - let g = fit.image(retained, self.layout.layers()[i]).ok()?; + if self.layout.formats().layer_fit[i].is_some() { + let g = self.layout.edge_crop_bounds(i).ok()?; return Some(Rect::from_min_size( origin + Vec2::new(g.x as f32, g.y as f32) * scale, Vec2::new(g.width as f32, g.height as f32) * scale, @@ -3779,17 +3748,21 @@ impl Editor { if self.edit_content && !self.mask_editing() && let Some(i) = self.layout.selected() - && self.layout.formats().layer_fit[i] - .is_none_or(|fit| fit == canvas_pilot::formats::LayerFit::OneToOne) { let g = self.layout.layers()[i]; let rect = Rect::from_min_size( origin + Vec2::new(g.x as f32, g.y as f32) * scale, Vec2::new(g.width as f32, g.height as f32) * scale, ); - if self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne) - { - if let Some(image) = self.crop_bounds(origin, scale) { + if let Some(fit) = self.layout.formats().layer_fit[i] { + if let Ok(image) = self.layout.formats().advanced[i] + .remaining(self.layout.formats().input(i).unwrap()) + .and_then(|retained| fit.image(retained, g)) + { + let image = Rect::from_min_size( + origin + Vec2::new(image.x as f32, image.y as f32) * scale, + Vec2::new(image.width as f32, image.height as f32) * scale, + ); // Do not stack the crop ghost over the masked-area ghost inside // the layer. Keep the usable-source guide outside it and in the reveal. let reveal = format_ui::layer_mask_rect(rect, &self.layout, i) @@ -3964,9 +3937,29 @@ impl Editor { let p = &screen.painter; let helper_color = if self.mask_editing() { Color32::from_rgb(222, 108, 220) + } else if self + .layout + .selected() + .is_some_and(|i| self.layout.formats().layer_fit[i].is_some()) + { + Color32::from_rgb(100, 220, 130) } else { palette.cyan }; + if !self.mask_editing() + && self.layout.dragging() + && let Some(i) = self.layout.selected() + && self.layout.formats().layer_fit[i].is_some() + { + let g = self.layout.layers()[i]; + let container = Rect::from_min_size( + origin + Vec2::new(g.x as f32, g.y as f32) * scale, + Vec2::new(g.width as f32, g.height as f32) * scale, + ); + for strip in format_ui::outside_strips(container, rect) { + p.rect_filled(strip, 0.0, Color32::from_black_alpha(110)); + } + } let covers = selection_outline::covers( &self.layout, self.layout.selected().unwrap(), @@ -4073,7 +4066,7 @@ impl Editor { ink.text( 24.0, r.height() - 40.0, - if move_pointer { "Move tool · Drag anywhere to move the current selection · V: Select · H: Pan" } else if self.navigation_tool == NavigationTool::Pan { "Pan tool · Drag to move the view · V: Select · Z: Zoom · Space/middle-drag pans from any tool" } else if self.navigation_tool == NavigationTool::Zoom { "Zoom tool · Click to zoom in · Alt-click to zoom out · Drag right/left to zoom · V: Select · H: Pan" } else if self.edit_content && self.mask_editing() { "Mask · Enter: finish · Tab: crop · Drag edges to blank · Drag inside layer or use arrows to move window · Shift+arrows: 10 px · Escape cancels" } else if self.edit_content && self.layout.selected().is_some_and(|i| self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne)) { "1:1 crop · Tab: mask · Drag edges to trim · Drag picture or use arrows to reposition · Enter, double-click or click outside to finish" } else if pivot_visible { "Pivot resize · Drag pivot to position · Double-click to center · Ctrl temporarily toggles pivot" } else if self.edit_content { "Crop mode · Tab: mask · Hold Alt/Option for temporary mask · Drag crop handles to zoom content · Drag image to reposition · Enter, double-click or click outside to finish" } else { match self.active_resize_mode() { + if move_pointer { "Move tool · Drag anywhere to move the current selection · V: Select · H: Pan" } else if self.navigation_tool == NavigationTool::Pan { "Pan tool · Drag to move the view · V: Select · Z: Zoom · Space/middle-drag pans from any tool" } else if self.navigation_tool == NavigationTool::Zoom { "Zoom tool · Click to zoom in · Alt-click to zoom out · Drag right/left to zoom · V: Select · H: Pan" } else if self.edit_content && self.mask_editing() { "Mask · Enter: finish · Tab: crop · Drag edges to blank · Drag inside layer or use arrows to move window · Shift+arrows: 10 px · Escape cancels" } else if self.edit_content && self.layout.selected().is_some_and(|i| self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne)) { "1:1 edge crop · Tab: mask · Green edges refit on release · Drag picture or use arrows to reposition · Enter, double-click or click outside to finish" } else if self.edit_content && self.layout.selected().is_some_and(|i| self.layout.formats().layer_fit[i].is_some()) { "Edge crop · Drag green edges to trim · Image refits on release · Enter/double-click: finish · Escape: cancel" } else if pivot_visible { "Pivot resize · Drag pivot to position · Double-click to center · Ctrl temporarily toggles pivot" } else if self.edit_content { "Crop mode · Tab: mask · Hold Alt/Option for temporary mask · Drag crop handles to zoom content · Drag image to reposition · Enter, double-click or click outside to finish" } else { match self.active_resize_mode() { ResizeMode::Individual => "Corners resize · Side handles keep opposite edge fixed · Shift-click selects", ResizeMode::Same => "Same handles · Matching handles scale selected layers proportionally · Escape cancels drag", ResizeMode::Mirror => "Mirror horizontally · Left/right mirrored handles · Escape cancels drag", @@ -6682,7 +6675,47 @@ mod tests { } #[test] - fn automatic_fit_shortcuts_open_crop_properties_without_changing_fit_or_geometry() { + fn advanced_crop_handles_stage_until_pointer_release() { + use canvas_pilot::formats::{Crop, LayerFit}; + for fit in [LayerFit::Centered, LayerFit::Cropped, LayerFit::Fullscreen] { + let mut app = Editor::default(); + let ctx = egui::Context::default(); + let rect = layer_rects(&frame(&mut app, &ctx, vec![]))[1]; + app.layout.select(Some(1)); + app.layout + .set_layer_fit(1, Some(fit), Crop::default()) + .unwrap(); + app.edit_content = true; + frame(&mut app, &ctx, vec![]); + let before = app.project(); + let g = app.layout.layers()[1]; + let image = app.layout.edge_crop_bounds(1).unwrap(); + let k = rect.width() / g.width as f32; + let start = rect.min + + Vec2::new( + (image.x - g.x) as f32, + (image.y + image.height / 2.0 - g.y) as f32, + ) * k; + let end = start + Vec2::new(15.0, 0.0); + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(start), mouse(start, true)], + ); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end)]); + assert!(app.layout.dragging(), "{fit:?}"); + assert_eq!(app.project(), before); + assert!(app.layout.edge_crop_bounds(1).unwrap().x > image.x); + frame(&mut app, &ctx, vec![mouse(end, false)]); + assert!(app.layout.formats().advanced[1].left > 0.0); + assert_eq!(app.layout.layers()[1], g); + app.layout.undo(); + assert_eq!(app.project(), before); + } + } + + #[test] + fn advanced_fit_shortcuts_open_edge_crop_without_changing_fit_or_geometry() { use canvas_pilot::formats::LayerFit; for fit in [LayerFit::Centered, LayerFit::Cropped, LayerFit::Fullscreen] { for keyboard in [false, true] { @@ -6708,14 +6741,8 @@ mod tests { frame(&mut app, &ctx, vec![mouse(pos, false)]); } } - assert!( - app.format_draft - .as_ref() - .is_some_and(|draft| draft.is_for(1)), - "{fit:?}, keyboard={keyboard}" - ); - assert!(matches!(app.property_tab, PropertyTab::Crop)); - assert!(!app.edit_content); + assert!(app.edit_content, "{fit:?}, keyboard={keyboard}"); + assert!(app.format_draft.is_none()); assert_eq!(app.project(), before); } } diff --git a/crates/canvas-pilot/src/lib.rs b/crates/canvas-pilot/src/lib.rs index 46b0220..24272b5 100644 --- a/crates/canvas-pilot/src/lib.rs +++ b/crates/canvas-pilot/src/lib.rs @@ -507,6 +507,7 @@ pub struct Layout { drag: Option<(Vec, Snapshot, [f64; 2])>, content_drag: Option<(usize, Snapshot, [f64; 2])>, content_resize: Option, + pending_edge_crop: Option, mask_drag: Option<(usize, Snapshot, [f64; 2], Option)>, pub gesture_error: Option, resize: Option, @@ -609,6 +610,7 @@ impl Default for Layout { formats: Formats::default(), content_drag: None, content_resize: None, + pending_edge_crop: None, mask_drag: None, gesture_error: None, undo: Vec::new(), @@ -1028,15 +1030,59 @@ impl Layout { } /// Resize content in normal crop mode, or trim source pixels in centered 1:1 mode. /// The layer container stays fixed; 1:1 ignores proportional scaling and pivots. - pub fn start_content_resize(&mut self, index: usize, handle: ResizeHandle, point: [f64; 2]) { - if self - .formats - .layer_fit - .get(index) - .is_some_and(|mode| mode.is_some_and(|fit| fit != formats::LayerFit::OneToOne)) - { - return; + /// Pending retained-image bounds under the frozen source transform. + pub fn edge_crop_bounds(&self, i: usize) -> Result { + let crop = self.formats.advanced[i]; + let input = self.formats.input(i)?; + let retained = crop.remaining(input)?; + let image = self.formats.layer_fit[i] + .ok_or("No advanced fit")? + .image(retained, self.layers[i])?; + let pending = self + .content_resize + .as_ref() + .filter(|r| r.index == i) + .and(self.pending_edge_crop) + .unwrap_or(crop); + let size = pending.remaining(input)?; + Ok(Geometry { + x: image.x + (pending.left - crop.left) * image.width / retained[0], + y: image.y + (pending.top - crop.top) * image.height / retained[1], + width: size[0] * image.width / retained[0], + height: size[1] * image.height / retained[1], + }) + } + + /// Switch to normal cover framing, retaining an exactly representable crop. + pub fn convert_to_smart_crop(&mut self, i: usize) -> Result<(), String> { + if !self.selectable(i) { + return Err("Unlock and show this layer first.".into()); } + self.finish_drag(); + let g = self.layers[i]; + let input = self.formats.input(i)?; + let crop = self.formats.advanced[i]; + let exact = self.formats.layer_fit[i].is_none_or(|fit| { + fit.image(crop.remaining(input).unwrap_or(input), g) + .is_ok_and(|image| { + (image.width - g.width).abs() < 1e-7 && (image.height - g.height).abs() < 1e-7 + }) + }) && crop + .remaining(input) + .is_ok_and(|r| (g.width / r[0] - g.height / r[1]).abs() < 1e-10) + && formats::mapping(input, [g.width, g.height], crop).is_ok(); + let before = self.snapshot(); + self.formats.advanced[i] = if exact { + crop + } else { + formats::crop_from_framing(input, [g.width, g.height], Framing::default())? + }; + self.formats.layer_fit[i] = None; + 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) || self.selected() != Some(index) @@ -1475,7 +1521,22 @@ impl Layout { } if let Some((source, crop)) = framing { if let Some(mode) = fit { - mode.image(crop.remaining(source.input()?)?, *geometry)?; + let image = mode.image(crop.remaining(source.input()?)?, *geometry)?; + // Normalize only an identical, uniformly scaled visible rectangle. + if (image.width - geometry.width).abs() < 1e-7 + && (image.height - geometry.height).abs() < 1e-7 + && crop.remaining(source.input()?).is_ok_and(|r| { + (geometry.width / r[0] - geometry.height / r[1]).abs() < 1e-10 + }) + && formats::mapping( + source.input()?, + [geometry.width, geometry.height], + *crop, + ) + .is_ok() + { + draft.formats.layer_fit[i] = None; + } } else { formats::mapping(source.input()?, [geometry.width, geometry.height], *crop)?; } @@ -1833,13 +1894,15 @@ impl Layout { let i = r.index; let g = r.before.layers[i]; let input = r.before.formats.input(i)?; - if r.before.formats.layer_fit[i] == Some(formats::LayerFit::OneToOne) { + if let Some(fit) = r.before.formats.layer_fit[i] { let mut crop = r.before.formats.advanced[i]; let remaining = crop.remaining(input)?; - // The retained image stays centered: moving a displayed edge - // by d changes retained size by 2d. Only the chosen source - // crop edge changes; pixel scale and container never change. - let delta = [2.0 * (point[0] - r.start[0]), 2.0 * (point[1] - r.start[1])]; + // Freeze the source transform until release; each edge follows the pointer. + let image = fit.image(remaining, g)?; + let delta = [ + (point[0] - r.start[0]) * remaining[0] / image.width, + (point[1] - r.start[1]) * remaining[1] / image.height, + ]; let left = matches!( r.handle, ResizeHandle::Left @@ -1962,7 +2025,11 @@ impl Layout { })(); match result { Ok(crop) => { - self.formats.advanced[r.index] = crop; + if r.before.formats.layer_fit[r.index].is_some() { + self.pending_edge_crop = Some(crop); + } else { + self.formats.advanced[r.index] = crop; + } self.gesture_error = None; } Err(e) => self.gesture_error = Some(e), @@ -2146,6 +2213,9 @@ impl Layout { self.remember(before); } if let Some(r) = self.content_resize.take() { + if let Some(crop) = self.pending_edge_crop.take() { + self.formats.advanced[r.index] = crop; + } self.remember(r.before); } if let Some((_, before, _)) = self.content_drag.take() { @@ -2159,6 +2229,7 @@ impl Layout { } } pub fn cancel_drag(&mut self) { + self.pending_edge_crop = None; if let Some((_, before, _, _)) = self.mask_drag.take() { self.restore(before); } diff --git a/crates/canvas-pilot/tests/layer_fit.rs b/crates/canvas-pilot/tests/layer_fit.rs index 0a65f20..a1c33ba 100644 --- a/crates/canvas-pilot/tests/layer_fit.rs +++ b/crates/canvas-pilot/tests/layer_fit.rs @@ -190,10 +190,10 @@ fn one_to_one_handles_trim_source_edges_without_zoom_and_undo_atomically() { assert_eq!( [crop.left, crop.right, crop.top, crop.bottom], [ - 100.0 + changes[0], - 80.0 + changes[1], - 60.0 + changes[2], - 40.0 + changes[3] + 100.0 + changes[0] / 2.0, + 80.0 + changes[1] / 2.0, + 60.0 + changes[2] / 2.0, + 40.0 + changes[3] / 2.0 ] ); assert_eq!(layout.layers()[0], g); @@ -221,11 +221,10 @@ fn one_to_one_crop_stops_at_source_limits_and_uncropped_image_cannot_move() { layout .set_layer_fit(0, Some(LayerFit::OneToOne), Crop::default()) .unwrap(); - let input = layout.formats().input(0).unwrap(); layout.start_content_resize(0, ResizeHandle::Left, [0.0, 0.0]); layout.drag_to([100000.0, 0.0]); - let crop = layout.formats().advanced[0]; - near(crop.remaining(input).unwrap()[0], 0.001); + near(layout.edge_crop_bounds(0).unwrap().width, 0.001); + assert_eq!(layout.formats().advanced[0], Crop::default()); layout.drag_to([-100000.0, 0.0]); assert_eq!(layout.formats().advanced[0], Crop::default()); layout.finish_drag(); @@ -237,7 +236,7 @@ fn one_to_one_crop_stops_at_source_limits_and_uncropped_image_cannot_move() { .set_layer_fit(0, Some(LayerFit::Centered), Crop::default()) .unwrap(); layout.start_content_resize(0, ResizeHandle::Left, [0.0, 0.0]); - assert!(!layout.dragging()); + assert!(layout.dragging()); } #[test] @@ -306,3 +305,115 @@ fn one_to_one_pan_preserves_crop_size_and_clamps_each_axis_with_undo_cancel() { assert_eq!(Project::from_layout(&layout), after); assert!(crop.pan_image([f64::NAN, 0.0]).is_err()); } + +#[test] +fn advanced_edges_freeze_image_until_release_and_cancel_atomically() { + use canvas_pilot::ResizeHandle; + for fit in LayerFit::ALL { + let mut layout = Layout::default(); + layout.select(Some(0)); + layout.set_layer_fit(0, Some(fit), Crop::default()).unwrap(); + let before = Project::from_layout(&layout); + let bounds = layout.edge_crop_bounds(0).unwrap(); + layout.start_content_resize(0, ResizeHandle::Left, [bounds.x, bounds.y]); + layout.drag_to([bounds.x + bounds.width / 4.0, bounds.y]); + assert_eq!(Project::from_layout(&layout), before); + let pending = layout.edge_crop_bounds(0).unwrap(); + near(pending.x, bounds.x + bounds.width / 4.0); + near(pending.width, bounds.width * 0.75); + layout.finish_drag(); + near( + layout.formats().advanced[0].left, + layout.formats().input(0).unwrap()[0] / 4.0, + ); + layout.undo(); + assert_eq!(Project::from_layout(&layout), before); + layout.start_content_resize(0, ResizeHandle::Right, [0.0, 0.0]); + layout.drag_to([-10.0, 0.0]); + layout.cancel_drag(); + assert_eq!(Project::from_layout(&layout), before); + } +} + +#[test] +fn device_exact_fit_uses_smart_crop_but_letterbox_and_stretch_stay_advanced() { + use canvas_pilot::formats::Source; + for (fit, width, height, normal) in [ + (LayerFit::Centered, 960.0, 540.0, true), + (LayerFit::Cropped, 960.0, 540.0, true), + (LayerFit::OneToOne, 1920.0, 1080.0, true), + (LayerFit::Fullscreen, 960.0, 540.0, true), + (LayerFit::Centered, 960.0, 600.0, false), + (LayerFit::Cropped, 960.0, 600.0, false), + (LayerFit::Fullscreen, 960.0, 540.01, false), + ] { + let mut layout = Layout::default(); + let g = Geometry { + x: 10.0, + y: 20.0, + width, + height, + }; + layout + .import_device_layers_with_fit( + &[(None, g, Some((Source::default(), Crop::default())))], + &[Some(fit)], + ) + .unwrap(); + let i = layout.layers().len() - 1; + assert_eq!( + layout.formats().layer_fit[i].is_none(), + normal, + "{fit:?} {height}" + ); + let before = Project::from_layout(&layout); + layout.convert_to_smart_crop(i).unwrap(); + assert!(layout.formats().layer_fit[i].is_none()); + assert_eq!(layout.layers()[i], g); + if !normal { + layout.undo(); + assert_eq!(Project::from_layout(&layout), before); + } + } +} + +#[test] +fn exact_import_keeps_nonzero_crop_and_mask() { + use canvas_pilot::formats::{LayerAppearance, Mask, Source}; + let mut layout = Layout::default(); + let crop = Crop { + left: 160.0, + right: 160.0, + top: 90.0, + bottom: 90.0, + }; + let mask = Mask { + left: 0.1, + ..Default::default() + }; + let g = Geometry { + x: 20.0, + y: 30.0, + width: 800.0, + height: 450.0, + }; + layout + .import_device_layers_with_appearance( + &[(None, g, Some((Source::default(), crop)))], + &[LayerAppearance { + fit: Some(LayerFit::Centered), + mask, + }], + ) + .unwrap(); + let i = layout.layers().len() - 1; + assert_eq!(layout.formats().layer_fit[i], None); + assert_eq!(layout.formats().advanced[i], crop); + assert_eq!(layout.formats().masks[i], mask); + layout + .set_layer_fit(i, Some(LayerFit::Centered), crop) + .unwrap(); + layout.convert_to_smart_crop(i).unwrap(); + assert_eq!(layout.formats().advanced[i], crop); + assert_eq!(layout.formats().masks[i], mask); +} diff --git a/docs/canvas-pilot/changelog.md b/docs/canvas-pilot/changelog.md index a5118ad..24fd00e 100644 --- a/docs/canvas-pilot/changelog.md +++ b/docs/canvas-pilot/changelog.md @@ -4,6 +4,15 @@ ## Unreleased +### v0.10.0-rc.13 + +- Import exact device fits as normal Smart crop. Preserve advanced modes for + letterboxing, overflow and stretching, and preserve saved project choices. +- Add green advanced edge-crop handles with a frozen image during dragging and + refitting on release. Add explicit, undoable Convert to Smart crop actions in + Crop properties and the context menu. Geometry/interaction regression tests + cover exact-fit selection, staged edits, source limits and undo/cancel. + ### v0.10.0-rc.12 - Double-click and Enter open Crop properties for Centered/Cropped layers instead diff --git a/docs/canvas-pilot/guide/formats-and-crop.md b/docs/canvas-pilot/guide/formats-and-crop.md index c61fc6b..89002a0 100644 --- a/docs/canvas-pilot/guide/formats-and-crop.md +++ b/docs/canvas-pilot/guide/formats-and-crop.md @@ -176,29 +176,29 @@ pixels, apply first; the retained image is centered and clipped to that containe 1:1 preserves pixel scale, Centered preserves proportions with transparent margins, Fullscreen stretches, and Cropped fills while clipping overflow. -Resize changes the container and preserves these crop edges. For **1:1**, double-click -the layer or choose **Adjust layer crop** to show the crop helper. Drag a side to -trim/restore that source edge; corners adjust two edges independently. The cyan -outline shows the retained image, with the usable source faded behind it. It -never zooms or stretches the image, and ignores aspect lock/pivot scaling. -The retained picture stays centered, so its outline contracts/expands symmetrically -even though only the chosen source crop edge changes. The container stays fixed. -Drag anywhere inside the layer, including empty margins outside the retained -picture, to reposition the crop as a whole. The picture follows the pointer; -opposite crop edges trade pixels, preserving the retained width/height and 1:1 -scale. Movement stops at the usable source boundaries. Arrow keys move by one -source pixel; Shift+arrows move by ten. The layer container stays in place. -Use viewport zoom/pan to reach handles outside the screen. Each drag is one Undo -step; Escape cancels an active drag and exits the helper. Numeric crop fields stay -in sync. **Centered** and **Cropped** fit automatically, so their helper button -and context-menu helper action are disabled. Double-click or Enter opens their -**Crop properties** with editable numeric crop edges instead of the canvas helper. -Fullscreen uses those same numeric crop controls. These shortcuts preserve the -selected fit mode and do not change the document. Unchecking -resets to normal centered cover framing. Input/global inheritance is intentionally -omitted. Pending Source edits remain staged and require Apply as usual. +Resize changes the container and preserves these crop edges. Double-click, Enter, +or **Adjust edge crop** opens the advanced helper. Its **green** outline shows the +retained image; the full usable source appears faded behind it. Drag a side to +trim/restore that edge, or a corner to change two edges independently. The image +stays still during the drag, with discarded areas shaded. On release, the chosen +fit recenters/refits the retained image inside the unchanged container. Aspect +lock and pivot scaling do not affect this edge operation. -Midra loads preserve all four explicit modes and open the checkbox automatically. +Use viewport zoom/pan to reach handles outside the screen. Each drag is one Undo +step; Escape cancels it. In 1:1 mode, dragging the picture or using arrow keys also +repositions the crop without changing its retained size (Shift moves ten pixels). +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. + +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. +Letterboxing, overflow and stretching retain their advanced fit. Existing saved +projects keep their explicitly chosen modes. Sends use container dimensions regardless of sizing strategy, and never change input routing. Aquilon sends reject advanced fit until its adapter supports it. Projects/recovery preserve the mode. Pixel rounding is approximate pending the diff --git a/docs/interfaces/device-adapters.md b/docs/interfaces/device-adapters.md index 0dc3313..b0dff2c 100644 --- a/docs/interfaces/device-adapters.md +++ b/docs/interfaces/device-adapters.md @@ -214,3 +214,14 @@ Only available inputs with valid signals, compatible native dimensions, and supp uncorrected input processing are accepted. Preview following is read-only, clears stale captures, and uses the existing pooled capture scheduler. Neither option changes the project or exchange schema. + +### Editor normalization of exact imported fits + +Adapter snapshots retain the device fit. On import into the editor, an explicit +fit becomes normal (`null`) only if its retained-image width/height equal the +container within 1e-7 output pixels, horizontal/vertical scales differ by less than +1e-10, and the normal crop mapping validates. Source, crop, container and mask are +unchanged. This applies to linked and unlinked loads; project deserialization does +not normalize saved modes. Advanced edge-helper drags stage crop changes until +release, keeping the source transform fixed; cancellation discards the staged crop +and release creates one undo entry. This is editor behavior, not device acceptance.