Allow moving 1:1 crops from the whole layer area
Beffroi/beffroi-tools/pipeline/head There was a failure building this commit Details

This commit is contained in:
Dandaedre 2026-11-16 09:26:55 +01:00
parent c9bb03c645
commit fb94c3c03f
8 changed files with 204 additions and 27 deletions

View File

@ -503,7 +503,7 @@ impl Editor {
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("Drag the canvas helper's edges to crop without zooming. The retained picture stays centered. Corners trim two edges independently. Double-click a layer to toggle the helper; use viewport zoom to reach offscreen handles.");
ui.small("Drag helper edges to trim without zooming; drag anywhere inside the layer, including empty margins, to reposition the crop without resizing it. Arrow keys move by 1 source pixel, Shift+arrows by 10. Movement stops at the usable source edges. Double-click toggles the helper.");
} else if matches!(current_fit, canvas_pilot::formats::LayerFit::Centered | canvas_pilot::formats::LayerFit::Cropped) {
ui.small("This mode fits automatically. The canvas crop helper is disabled; numeric crop edges remain available here.");
} else {

View File

@ -106,6 +106,27 @@ pub struct Crop {
pub bottom: f64,
}
impl Crop {
/// Move the image under a fixed crop window, in source pixels. Opposite
/// edges trade pixels so retained size is unchanged; stop at source bounds.
pub fn pan_image(self, delta: [f64; 2]) -> Result<Self, String> {
if delta.iter().any(|v| !v.is_finite())
|| [self.left, self.right, self.top, self.bottom]
.iter()
.any(|v| !v.is_finite() || *v < 0.0)
{
return Err(
"Crop edges and movement must be finite, with nonnegative crop edges.".into(),
);
}
let dx = delta[0].clamp(-self.right, self.left);
let dy = delta[1].clamp(-self.bottom, self.top);
Ok(Self {
left: self.left - dx,
right: self.right + dx,
top: self.top - dy,
bottom: self.bottom + dy,
})
}
pub fn remaining(self, size: [f64; 2]) -> Result<[f64; 2], String> {
if [self.left, self.right, self.top, self.bottom]
.iter()

View File

@ -1037,22 +1037,31 @@ impl Editor {
if self.layout.formats().layer_fit[i]
== Some(canvas_pilot::formats::LayerFit::OneToOne)
{
return; // 1:1 is centered: use crop handles/fields, never pan or zoom.
self.layout.formats().advanced[i]
.pan_image(d)
.and_then(|crop| {
self.layout.set_layer_fit(
i,
Some(canvas_pilot::formats::LayerFit::OneToOne),
crop,
)
})
} else {
let g = self.layout.layers()[i];
self.layout.formats().mapping(i, g).and_then(|m| {
let offset = std::array::from_fn(|axis| {
(m.framing.offset[axis] + d[axis])
.clamp(-m.offset_limits[axis], m.offset_limits[axis])
});
self.layout.set_framing(
i,
canvas_pilot::formats::Framing {
zoom: m.framing.zoom,
offset,
},
)
})
}
let g = self.layout.layers()[i];
self.layout.formats().mapping(i, g).and_then(|m| {
let offset = std::array::from_fn(|axis| {
(m.framing.offset[axis] + d[axis])
.clamp(-m.offset_limits[axis], m.offset_limits[axis])
});
self.layout.set_framing(
i,
canvas_pilot::formats::Framing {
zoom: m.framing.zoom,
offset,
},
)
})
} else {
self.layout.nudge(d[0], d[1])
};
@ -1586,7 +1595,7 @@ impl Editor {
if automatic_fit {
"Centered and Cropped fit automatically. Edit numeric crop edges in Crop properties if needed."
} else if self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne) {
"Drag helper edges to crop source pixels without zooming. The retained picture stays centered; the container stays fixed."
"Drag edges to trim; drag anywhere inside the layer to reposition the crop. Movement preserves crop size, pixel scale and the container."
} else if self.layout.formats().layer_fit[i].is_some() {
"Opens advanced crop edges and fit mode. The rectangle defines the container."
} else {
@ -2212,10 +2221,11 @@ impl Editor {
return None;
}
if self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne) {
return self
.crop_bounds(origin, scale)
.filter(|r| r.contains(pointer))
.map(|_| i);
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;
@ -3445,7 +3455,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.layout.selected().is_some_and(|i| self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne)) { "1:1 crop · Drag edges to trim source pixels · No zoom or pan · 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 · Drag crop handles to zoom content · Drag image to reposition · 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.layout.selected().is_some_and(|i| self.layout.formats().layer_fit[i] == Some(canvas_pilot::formats::LayerFit::OneToOne)) { "1:1 crop · Drag edges to trim · Drag picture or use arrows to reposition · 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 · Drag crop handles to zoom content · Drag image to reposition · 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",
@ -5906,6 +5916,53 @@ mod tests {
assert_eq!(*app.layout.formats(), before);
}
#[test]
fn one_to_one_crop_moves_from_empty_layer_margins_and_arrow_keys() {
use canvas_pilot::formats::{Crop, LayerFit};
let mut app = Editor::default();
let ctx = egui::Context::default();
let output = frame(&mut app, &ctx, vec![]);
let rect = layer_rects(&output)[1];
let crop = Crop {
left: 800.0,
right: 800.0,
top: 450.0,
bottom: 450.0,
};
app.layout
.set_layer_fit(1, Some(LayerFit::OneToOne), crop)
.unwrap();
app.edit_content = true;
frame(&mut app, &ctx, vec![]);
let boxes = app.layout.layers().clone();
// The retained image is 320×180 in a 640×360 layer. Start in its empty margin.
let start = Pos2::new(rect.left() + rect.width() * 0.1, rect.center().y);
let end = start + Vec2::new(20.0, 10.0);
frame(
&mut app,
&ctx,
vec![egui::Event::PointerMoved(start), mouse(start, true)],
);
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end)]);
frame(&mut app, &ctx, vec![mouse(end, false)]);
assert!(app.edit_content);
assert_eq!(app.layout.selected(), Some(1));
assert_eq!(*app.layout.layers(), boxes);
let moved = app.layout.formats().advanced[1];
assert!(moved.left < crop.left && moved.top < crop.top);
assert!((moved.left + moved.right - 1600.0).abs() < 1e-7);
assert!((moved.top + moved.bottom - 900.0).abs() < 1e-7);
frame(&mut app, &ctx, vec![key(egui::Key::ArrowRight)]);
let nudged = app.layout.formats().advanced[1];
assert!((nudged.left - (moved.left - 1.0)).abs() < 1e-7);
assert!((nudged.right - (moved.right + 1.0)).abs() < 1e-7);
assert_eq!(*app.layout.layers(), boxes);
app.layout.undo();
assert_eq!(app.layout.formats().advanced[1], moved);
app.layout.undo();
assert_eq!(app.layout.formats().advanced[1], crop);
}
#[test]
fn double_click_one_to_one_opens_crop_helper_and_handles_trim_without_zoom() {
use canvas_pilot::formats::{Crop, LayerFit};

View File

@ -877,11 +877,16 @@ impl Layout {
self.set_framing(i, framing)
}
pub fn start_content_drag(&mut self, i: usize, point: [f64; 2]) {
if self.formats.layer_fit.get(i).is_some_and(Option::is_some) {
if self
.formats
.layer_fit
.get(i)
.is_some_and(|fit| fit.is_some_and(|fit| fit != formats::LayerFit::OneToOne))
{
return;
}
self.finish_drag();
if self.selectable(i) && self.selected() == Some(i) {
if self.selectable(i) && self.selected() == Some(i) && point.iter().all(|v| v.is_finite()) {
self.content_drag = Some((i, self.snapshot(), point));
}
}
@ -1754,6 +1759,18 @@ impl Layout {
return;
}
if let Some((i, before, start)) = self.content_drag.clone() {
if before.formats.layer_fit[i] == Some(formats::LayerFit::OneToOne) {
match before.formats.advanced[i]
.pan_image([point[0] - start[0], point[1] - start[1]])
{
Ok(crop) => {
self.formats.advanced[i] = crop;
self.gesture_error = None;
}
Err(error) => self.gesture_error = Some(error),
}
return;
}
if let Ok(m) = before.formats.mapping(i, before.layers[i]) {
let offset = std::array::from_fn(|axis| {
(m.framing.offset[axis] + point[axis] - start[axis])

View File

@ -214,7 +214,7 @@ fn one_to_one_handles_trim_source_edges_without_zoom_and_undo_atomically() {
}
#[test]
fn one_to_one_crop_stops_at_source_limits_and_never_pans() {
fn one_to_one_crop_stops_at_source_limits_and_uncropped_image_cannot_move() {
use canvas_pilot::ResizeHandle;
let mut layout = Layout::default();
layout.select(Some(0));
@ -230,10 +230,79 @@ fn one_to_one_crop_stops_at_source_limits_and_never_pans() {
assert_eq!(layout.formats().advanced[0], Crop::default());
layout.finish_drag();
layout.start_content_drag(0, [0.0, 0.0]);
assert!(!layout.dragging());
layout.drag_to([50.0, -50.0]);
layout.finish_drag();
assert_eq!(layout.formats().advanced[0], Crop::default());
layout
.set_layer_fit(0, Some(LayerFit::Centered), Crop::default())
.unwrap();
layout.start_content_resize(0, ResizeHandle::Left, [0.0, 0.0]);
assert!(!layout.dragging());
}
#[test]
fn one_to_one_pan_preserves_crop_size_and_clamps_each_axis_with_undo_cancel() {
let mut layout = Layout::default();
layout.select(Some(0));
let crop = Crop {
left: 100.0,
right: 80.0,
top: 60.0,
bottom: 40.0,
};
layout
.set_layer_fit(0, Some(LayerFit::OneToOne), crop)
.unwrap();
let before = Project::from_layout(&layout);
let input = layout.formats().input(0).unwrap();
let g = layout.layers()[0];
layout.start_content_drag(0, [500.0, 400.0]);
layout.drag_to([510.0, 395.0]);
layout.drag_to([520.0, 390.0]);
layout.finish_drag();
assert_eq!(
layout.formats().advanced[0],
Crop {
left: 80.0,
right: 100.0,
top: 70.0,
bottom: 30.0
}
);
assert_eq!(
layout.formats().advanced[0].remaining(input).unwrap(),
crop.remaining(input).unwrap()
);
assert_eq!(layout.layers()[0], g);
assert_eq!(layout.formats().mapping(0, g).unwrap().scale, 1.0);
let after = Project::from_layout(&layout);
layout.undo();
assert_eq!(Project::from_layout(&layout), before);
layout.redo();
assert_eq!(Project::from_layout(&layout), after);
layout.start_content_drag(0, [0.0, 0.0]);
layout.drag_to([100000.0, -100000.0]);
assert_eq!(
layout.formats().advanced[0],
Crop {
left: 0.0,
right: 180.0,
top: 100.0,
bottom: 0.0
}
);
// Moving back uses the gesture's starting crop, so clamping cannot accumulate drift.
layout.drag_to([-100000.0, 100000.0]);
assert_eq!(
layout.formats().advanced[0],
Crop {
left: 180.0,
right: 0.0,
top: 0.0,
bottom: 100.0
}
);
layout.cancel_drag();
assert_eq!(Project::from_layout(&layout), after);
assert!(crop.pan_image([f64::NAN, 0.0]).is_err());
}

View File

@ -4,6 +4,12 @@
## Unreleased
- Allow moving the whole 1:1 crop by dragging anywhere inside the layer,
including empty margins, or using arrow keys (Shift for ten pixels). Opposite
crop edges move together, preserving crop size, pixel scale and the container;
stop at source bounds, with one-step Undo and Escape cancellation. No schema
or device-protocol changes.
- Disable the crop helper button, context-menu action and double-click shortcut
for Centered/Cropped: those modes fit automatically. Keep numeric crop fields
available in Crop properties; normal crop and the 1:1 helper are unchanged.

View File

@ -173,9 +173,14 @@ Resize changes the container and preserves these crop edges. For **1:1**, double
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, stretches or pans the image, and ignores aspect lock/pivot scaling.
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

View File

@ -27,6 +27,8 @@ to the [engineering stretch goal](midra-fit-precision.md).
same, with more/less clipping or background visible.
- [ ] In 1:1, double-click the layer; drag a helper side/corner. The picture
should crop without changing pixel scale; the retained region remains centered.
Drag inside the layer (including an empty margin) to reposition the whole crop;
its size and pixel scale must stay fixed. Check source-edge stops and arrow keys.
Verify the crop fields, Undo and Escape. Zoom out the viewport if handles lie
outside the screen.
- [ ] Apply a noticeable asymmetric crop; repeat modes and check that the crop