Add Reset crop to the visual crop editor

This commit is contained in:
Dandaedre 2026-11-13 15:06:17 +01:00
parent e6ebc114b2
commit 229412af22
2 changed files with 70 additions and 2 deletions

View File

@ -782,8 +782,31 @@ impl Editor {
} else {
"Adjust layer crop"
};
ink.button(0.0, 275.0, w, edit_label, self.edit_content);
if click(ui, &ink, [0.0, 275.0, w, 34.0], "edit-layer-content") {
let edit_width = if self.edit_content { w - 120.0 } else { w };
ink.button(0.0, 275.0, edit_width, edit_label, self.edit_content);
if self.edit_content {
ink.button(w - 108.0, 275.0, 108.0, "Reset crop", false);
if click(
ui,
&ink,
[w - 108.0, 275.0, 108.0, 34.0],
"reset-visual-crop",
) {
match self
.layout
.set_framing(i, midra_layout::formats::Framing::default())
{
Ok(()) => self.sync(),
Err(e) => self.error = e,
}
}
}
if click(
ui,
&ink,
[0.0, 275.0, edit_width, 34.0],
"edit-layer-content",
) {
self.edit_content = !self.edit_content;
}
ui.put(
@ -2330,6 +2353,46 @@ mod tests {
frame(&mut app, &ctx, vec![key(egui::Key::Escape)]);
assert!(!app.edit_content);
}
#[test]
fn visual_reset_crop_centers_at_minimum_zoom_and_is_undoable() {
let mut app = Editor {
edit_content: true,
..Default::default()
};
app.layout
.set_framing(
1,
midra_layout::formats::Framing {
zoom: 2.0,
offset: [30.0, -20.0],
},
)
.unwrap();
let before = *app.layout.formats();
let boxes = *app.layout.layers();
let ctx = egui::Context::default();
frame(&mut app, &ctx, vec![]);
let pos = ctx
.read_response(egui::Id::new("reset-visual-crop"))
.unwrap()
.rect
.center();
frame(
&mut app,
&ctx,
vec![egui::Event::PointerMoved(pos), mouse(pos, true)],
);
frame(&mut app, &ctx, vec![mouse(pos, false)]);
let framing = app.layout.formats().mapping(1, boxes[1]).unwrap().framing;
assert!((framing.zoom - 1.0).abs() < 1e-8);
assert!(framing.offset.iter().all(|v| v.abs() < 1e-8));
assert_eq!(*app.layout.layers(), boxes);
assert_eq!(app.layout.formats().sources, before.sources);
assert!(app.edit_content);
app.layout.undo();
assert_eq!(*app.layout.formats(), before);
}
#[test]
fn crop_corner_gesture_zooms_content_without_resizing_layer() {
for zoom in [0.6, 1.0] {

View File

@ -399,3 +399,8 @@ and the reference image.
The [save/export interface specification](interfaces/midra-layout.md) documents
both formats, their limitations, validation, and compatibility policy, with a
[complete JSON example](interfaces/midra-layout-v1.example.json).
Visual crop mode also provides **Reset crop** beside **Done adjusting crop** in
layer properties. It restores 100% content zoom and centered offsets, keeps the
visible box and source/input crop unchanged, and leaves visual editing active.
The reset is undoable.