Add midpoint handles to crop zoom bounding box

This commit is contained in:
Dandaedre 2026-11-13 15:45:32 +01:00
parent d10530d80c
commit 56fdbd310f
4 changed files with 54 additions and 40 deletions

View File

@ -1349,18 +1349,14 @@ impl Editor {
self.layout.formats().advanced[i],
)
}
fn crop_handle(&self, pointer: Pos2, origin: Pos2, scale: f32) -> Option<Corner> {
fn crop_handle(&self, pointer: Pos2, origin: Pos2, scale: f32) -> Option<ResizeHandle> {
let rect = self.crop_bounds(origin, scale)?;
[
(Corner::TopLeft, rect.left_top()),
(Corner::TopRight, rect.right_top()),
(Corner::BottomLeft, rect.left_bottom()),
(Corner::BottomRight, rect.right_bottom()),
]
.into_iter()
.find(|(_, p)| Rect::from_center_size(*p, Vec2::splat(14.0)).contains(pointer))
.map(|(corner, _)| corner)
crop_handle_positions(rect)
.into_iter()
.find(|(_, p)| Rect::from_center_size(*p, Vec2::splat(14.0)).contains(pointer))
.map(|(handle, _)| handle)
}
fn resize_handle(
&self,
pointer: Pos2,
@ -1682,8 +1678,12 @@ impl Editor {
&& let Some(corner) = pointer.and_then(|p| self.crop_handle(p, origin, scale))
{
ui.ctx().set_cursor_icon(match corner {
Corner::TopLeft | Corner::BottomRight => egui::CursorIcon::ResizeNwSe,
_ => egui::CursorIcon::ResizeNeSw,
ResizeHandle::Corner(Corner::TopLeft | Corner::BottomRight) => {
egui::CursorIcon::ResizeNwSe
}
ResizeHandle::Corner(_) => egui::CursorIcon::ResizeNeSw,
ResizeHandle::Left | ResizeHandle::Right => egui::CursorIcon::ResizeHorizontal,
ResizeHandle::Top | ResizeHandle::Bottom => egui::CursorIcon::ResizeVertical,
});
}
if !self.edit_content
@ -1984,12 +1984,7 @@ impl Editor {
if let Some(rect) = self.crop_bounds(origin, scale) {
let p = &screen.painter;
p.rect_stroke(rect, 0.0, Stroke::new(1.5_f32, CYAN), StrokeKind::Inside);
for corner in [
rect.left_top(),
rect.right_top(),
rect.left_bottom(),
rect.right_bottom(),
] {
for (_, corner) in crop_handle_positions(rect) {
p.rect(
Rect::from_center_size(corner, Vec2::splat(9.0)),
1.0,
@ -2055,7 +2050,7 @@ impl Editor {
ink.text(
24.0,
r.height() - 57.0,
if self.edit_content { "Crop mode · Drag cyan corners to zoom content · Drag image to reposition · Click outside to finish" } else { match self.resize_mode {
if self.edit_content { "Crop mode · Drag cyan handles to zoom content · Drag image to reposition · Click outside to finish" } else { match self.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 exits",
ResizeMode::Mirror => "Mirror horizontally · Left/right mirrored handles · Escape exits",
@ -2073,6 +2068,16 @@ impl Editor {
);
}
}
fn crop_handle_positions(rect: Rect) -> [(ResizeHandle, Pos2); 8] {
ResizeHandle::positions(Geometry {
x: rect.min.x as f64,
y: rect.min.y as f64,
width: rect.width() as f64,
height: rect.height() as f64,
})
.map(|(handle, [x, y])| (handle, Pos2::new(x as f32, y as f32)))
}
// Use the modifiers attached to the click, even if Shift was released later
// in the same input frame.
fn click_shift_modifier(ui: &egui::Ui) -> bool {
@ -2660,8 +2665,11 @@ mod tests {
}
#[test]
fn crop_corner_gesture_zooms_content_without_resizing_layer() {
for zoom in [0.6, 1.0] {
fn crop_handle_gesture_zooms_content_without_resizing_layer() {
for (zoom, handle_index) in [0.6, 1.0]
.into_iter()
.flat_map(|zoom| (0..8).map(move |i| (zoom, i)))
{
let mut app = Editor {
edit_content: true,
keep_aspect_ratio: false,
@ -2672,8 +2680,10 @@ mod tests {
let before = *app.layout.layers();
let formats = *app.layout.formats();
let output = frame(&mut app, &ctx, vec![]);
let start = layer_rects(&output)[1].right_bottom();
let end = start + Vec2::new(20.0, 15.0);
let rect = layer_rects(&output)[1];
let (_, start) = crop_handle_positions(rect)[handle_index];
let direction = start - rect.center();
let end = start + Vec2::new(direction.x.signum() * 20.0, direction.y.signum() * 15.0);
frame(
&mut app,
&ctx,

View File

@ -393,8 +393,8 @@ impl Layout {
self.content_drag = Some((i, self.snapshot(), point));
}
}
/// Zoom the content from a corner, leaving the visible layer box unchanged.
pub fn start_content_resize(&mut self, index: usize, corner: Corner, point: [f64; 2]) {
/// Zoom content from a corner or side, fixing the opposite anchor and visible layer box.
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)
@ -406,7 +406,7 @@ impl Layout {
mode: ResizeMode::Individual,
targets: self.selected,
index,
handle: ResizeHandle::Corner(corner),
handle,
before: self.snapshot(),
start: point,
keep_ratio: true,

View File

@ -914,11 +914,15 @@ fn selected_layers_are_foreground_for_viewport_and_hit_testing() {
#[test]
fn crop_corner_zoom_keeps_layer_fixed_and_supports_limits_history_and_cancel() {
use midra_layout::ResizeHandle;
for corner in [
Corner::TopLeft,
Corner::TopRight,
Corner::BottomLeft,
Corner::BottomRight,
for handle in [
ResizeHandle::Corner(Corner::TopLeft),
ResizeHandle::Corner(Corner::TopRight),
ResizeHandle::Corner(Corner::BottomLeft),
ResizeHandle::Corner(Corner::BottomRight),
ResizeHandle::Top,
ResizeHandle::Bottom,
ResizeHandle::Left,
ResizeHandle::Right,
] {
let mut l = Layout::default();
let i = 1;
@ -927,16 +931,16 @@ fn crop_corner_zoom_keeps_layer_fixed_and_supports_limits_history_and_cancel() {
let g = before[i];
let start = ResizeHandle::positions(g)
.into_iter()
.find(|(h, _)| *h == ResizeHandle::Corner(corner))
.find(|(h, _)| *h == handle)
.unwrap()
.1;
let opposite = ResizeHandle::positions(g)
.into_iter()
.find(|(h, _)| *h == ResizeHandle::Corner(corner).opposite())
.find(|(h, _)| *h == handle.opposite())
.unwrap()
.1;
let end = std::array::from_fn(|axis| opposite[axis] + 2.0 * (start[axis] - opposite[axis]));
l.start_content_resize(i, corner, start);
l.start_content_resize(i, handle, start);
l.drag_to(end);
l.finish_drag();
assert_eq!(*l.layers(), before);
@ -953,7 +957,7 @@ fn crop_corner_zoom_keeps_layer_fixed_and_supports_limits_history_and_cancel() {
};
let fixed = ResizeHandle::positions(full)
.into_iter()
.find(|(h, _)| *h == ResizeHandle::Corner(corner).opposite())
.find(|(h, _)| *h == handle.opposite())
.unwrap()
.1;
for axis in 0..2 {
@ -963,20 +967,20 @@ fn crop_corner_zoom_keeps_layer_fixed_and_supports_limits_history_and_cancel() {
assert_eq!(*l.formats(), formats);
l.redo();
assert_eq!(*l.formats(), after);
l.start_content_resize(i, corner, end);
l.start_content_resize(i, handle, end);
l.drag_to(opposite);
let m = l.formats().mapping(i, g).unwrap();
assert!((m.framing.zoom - 1.0).abs() < 1e-8);
l.cancel_drag();
assert_eq!(*l.formats(), after);
l.start_content_resize(i, corner, end);
l.start_content_resize(i, handle, end);
l.drag_to(std::array::from_fn(|axis| {
opposite[axis] + 1000.0 * (start[axis] - opposite[axis])
}));
assert!((l.formats().mapping(i, g).unwrap().framing.zoom - 100.0).abs() < 1e-6);
l.set_locked(i, true);
assert_eq!(*l.formats(), after);
l.start_content_resize(i, corner, start);
l.start_content_resize(i, handle, start);
assert!(!l.dragging());
}
}

View File

@ -372,10 +372,10 @@ outside preview keeps crop mode active. Property controls remain usable while
editing the crop.
In visual crop mode, a cyan bounding box surrounds the full usable input image,
including the faded preview. Drag any of its four corners to zoom the content
including the faded preview. Drag any corner or side midpoint to zoom the content
through cropping; the gold visible layer box stays fixed. Content always keeps
its proportions, independently of the layer resize ratio toggle. The opposite
content corner stays anchored where possible; offsets clamp to keep the visible
content corner or edge stays anchored where possible; offsets clamp to keep the visible
box filled when zooming out. Zoom is limited to 100–10000%. Drag the image to pan
its content. Each zoom gesture is one undo step; Escape cancels an active gesture.
Use viewport zoom/pan to reach content handles beyond the current view.