Clamp crop property offsets to current zoom bounds

This commit is contained in:
Dandaedre 2026-11-14 14:25:45 +01:00
parent a0dd8dfb20
commit 88d6eee625
3 changed files with 90 additions and 5 deletions

View File

@ -35,7 +35,20 @@ impl FormatDraft {
) != self.baseline
|| self.ratio_edit.dirty
}
fn clamp_offsets(&mut self, layout: &Layout) -> Result<[f64; 2], String> {
let geometry = layout.layers()[self.layer];
let (_, bounds) = midra_layout::formats::limits(
layout.formats().input(self.layer)?,
[geometry.width, geometry.height],
self.framing.zoom,
)?;
for (offset, bound) in self.framing.offset.iter_mut().zip(bounds) {
*offset = offset.clamp(-bound, bound);
}
Ok(bounds)
}
fn apply_framing(&mut self, layout: &mut Layout, dragging: bool) -> Result<(), String> {
self.clamp_offsets(layout)?;
if dragging && self.crop_before.is_none() {
self.crop_before = Some(layout.clone());
}
@ -423,17 +436,21 @@ impl Editor {
let mut percent=d.framing.zoom*100.0;
ui.horizontal(|ui| {
if ui.add(egui::DragValue::new(&mut percent).range(100.0..=10000.0).custom_parser(nonnegative_number).speed(1.0).custom_formatter(|v,_|number(v)).suffix("% content zoom")).changed() {d.framing.zoom=percent/100.0;}
ui.add(egui::DragValue::new(&mut d.framing.offset[0]).speed(1.0).custom_formatter(|v,_|number(v)).prefix("X "));
ui.add(egui::DragValue::new(&mut d.framing.offset[1]).speed(1.0).custom_formatter(|v,_|number(v)).prefix("Y "));
match d.clamp_offsets(&self.layout) {
Ok(bounds) => {
for (axis, bound) in bounds.into_iter().enumerate() {
ui.add(egui::DragValue::new(&mut d.framing.offset[axis]).range(-bound..=bound).speed(1.0).custom_formatter(|v,_|number(v)).prefix(if axis == 0 { "X " } else { "Y " }));
}
}
Err(error) => d.error = error,
}
});
let current=self.layout.layers()[i];
if let Ok((_,limit))=midra_layout::formats::limits(self.layout.formats().input(i).unwrap(),[current.width,current.height],d.framing.zoom) {
ui.label(format!("Offsets in output px: X ±{:.1}, Y ±{:.1}. Positive moves content right/down.",limit[0],limit[1]));
}
ui.horizontal(|ui| {
let current_framing=self.layout.formats().mapping(i,current).unwrap().framing;
let apply=d.framing != before_framing;
let _ = current_framing;
let recenter=ui.button("Recenter").clicked();
let reset=ui.button("Reset crop").clicked();
if recenter { d.framing=self.layout.formats().mapping(i,current).unwrap().framing; d.framing.offset=[0.0;2]; }
@ -446,7 +463,7 @@ impl Editor {
let (_,rect)=ui.allocate_space(Vec2::new(ui.available_width(),170.0));
let preview_rect=fit_rect(rect,[current.width,current.height]);
paint_pattern(ui.painter(),preview_rect,self.layout.formats().sources[self.layout.formats().source_for[i]],self.layout.formats().advanced[i]);
ui.small("Crop zoom and offsets apply as you edit. Undo restores earlier values.");
ui.small("Crop zoom and offsets apply as you edit. Offsets stop at the content edges and adjust when zoom decreases. Undo restores earlier values.");
ui.label("Source test pattern only; the screen reference image is not source content.");
}
if !d.error.is_empty() {ui.colored_label(theme::palette(ui.ctx()).amber,&d.error);}
@ -797,6 +814,66 @@ mod tests {
);
}
#[test]
fn crop_offsets_clamp_to_bounds_and_reducing_zoom_keeps_content_inside() {
let mut app = Editor::default();
let geometry = app.layout.layers()[1];
let before = app.layout.formats().clone();
let mut draft = FormatDraft::new(&app.layout, 1);
draft.framing = Framing {
zoom: 2.0,
offset: [1e6, -1e6],
};
draft.apply_framing(&mut app.layout, false).unwrap();
let mapping = app.layout.formats().mapping(1, geometry).unwrap();
assert!((mapping.framing.offset[0] - mapping.offset_limits[0]).abs() < 1e-7);
assert!((mapping.framing.offset[1] + mapping.offset_limits[1]).abs() < 1e-7);
let zoomed = app.layout.formats().clone();
draft.framing.zoom = 1.0;
draft.apply_framing(&mut app.layout, false).unwrap();
assert_eq!(draft.framing.offset, [0.0, 0.0]);
assert_eq!(app.layout.layers()[1], geometry);
app.layout.undo();
assert_eq!(*app.layout.formats(), zoomed);
app.layout.undo();
assert_eq!(*app.layout.formats(), before);
}
#[test]
fn crop_tab_clamps_both_axes_without_an_offset_error() {
let ctx = egui::Context::default();
let mut app = Editor {
property_tab: PropertyTab::Crop,
..Default::default()
};
app.layout
.apply(
1,
midra_layout::Geometry {
width: 300.0,
height: 250.0,
..app.layout.layers()[1]
},
)
.unwrap();
let mut draft = FormatDraft::new(&app.layout, 1);
// A non-matching aspect still permits horizontal movement at 100% zoom.
draft.framing.offset = [-1e6, 1e6];
app.format_draft = Some(draft);
frame(&mut app, &ctx, vec![]);
let draft = app.format_draft.as_ref().unwrap();
assert!(draft.error.is_empty());
let mapping = app
.layout
.formats()
.mapping(1, app.layout.layers()[1])
.unwrap();
assert!(mapping.offset_limits[0] > 0.0);
assert!((mapping.framing.offset[0] + mapping.offset_limits[0]).abs() < 1e-7);
assert!(mapping.framing.offset[1].abs() < 1e-7);
assert!(!draft.pending(&app.layout));
}
#[test]
fn crop_control_drag_applies_without_a_button_and_keeps_pending_source_edits() {
let ctx = egui::Context::default();

View File

@ -867,3 +867,9 @@ Right-click a layer in the viewport for **Rename layer…** / **Delete layer** a
the existing tools. Layer-list rows have a compact menu with just those two actions.
Actions target the clicked layer; deletion supports Undo and requires a visible,
unlocked layer with at least one other layer remaining.
In **Layer properties → Crop**, X/Y offsets are constrained to the valid range
for the current zoom, input dimensions and visible box. Typing or dragging beyond
an edge stops at that edge; reducing zoom clamps existing offsets to the new
bounds. An axis with no spare content stays at zero. These adjustments apply
immediately and support Undo without an offset-range error.

View File

@ -4,6 +4,8 @@
## Unreleased
- Constrain Crop-tab X/Y offsets to the valid content bounds, including after zoom decreases; typed and dragged edits clamp to the edge instead of producing offset-range errors. Undo remains available.
- Remove redundant position/size and output Apply buttons and their close prompts; names/folders commit on Enter or focus loss with Undo. Rename staged source and clipboard/SVG actions to “Apply source changes” and “Import mapped values”; Midra sends remain explicit.
- Add ImageGen application artwork in PNG, Windows ICO and macOS ICNS formats. Embed native icons and support Linux/BSD desktop entries and macOS app launchers; native Windows/macOS visual checks remain required. Include artwork for the Python device probe.