diff --git a/crates/midra-layout/src/format_ui.rs b/crates/midra-layout/src/format_ui.rs index c8ec4e4..8e29612 100644 --- a/crates/midra-layout/src/format_ui.rs +++ b/crates/midra-layout/src/format_ui.rs @@ -60,8 +60,18 @@ fn aspect_combo(ui: &mut egui::Ui, id: &str, aspect: &mut Aspect) -> bool { fn dimensions(ui: &mut egui::Ui, size: &mut [f64; 2], prefix: &str) { ui.horizontal(|ui| { ui.label(prefix); - ui.add(egui::DragValue::new(&mut size[0]).speed(1.0).prefix("W ")); - ui.add(egui::DragValue::new(&mut size[1]).speed(1.0).prefix("H ")); + ui.add( + egui::DragValue::new(&mut size[0]) + .speed(1.0) + .custom_formatter(|v, _| number(v)) + .prefix("W "), + ); + ui.add( + egui::DragValue::new(&mut size[1]) + .speed(1.0) + .custom_formatter(|v, _| number(v)) + .prefix("H "), + ); }); } fn resolution(ui: &mut egui::Ui, size: &mut [u32; 2], prefix: &str, ratio: Option) { @@ -119,7 +129,7 @@ impl Editor { if before[0]!=d.size[0] {d.size[1]=(d.size[0]/ratio).round();} else if before[1]!=d.size[1] {d.size[0]=(d.size[1]*ratio).round();} } - ui.label(format!("Proposed output: {} × {} px. Linked dimensions round the other side to whole pixels.",d.size[0],d.size[1])); + ui.label(format!("Proposed output: {:.1} × {:.1} px. Linked dimensions round the other side to whole pixels.",d.size[0],d.size[1])); ui.label("Changing output preserves layer positions and sizes. Custom dimensions are planning sizes, not verified device modes."); if ui.button("Apply output resolution").clicked() { match midra_layout::formats::resolution_from_values(d.size).and_then(|size|self.layout.set_output(size)) { Ok(())=>{ self.zoom=1.0;self.pan=Vec2::ZERO;self.sync(); d.error.clear(); },Err(e)=>d.error=e } @@ -182,7 +192,7 @@ impl Editor { }); let old_crop=d.source.input_crop; ui.horizontal_wrapped(|ui| { - for (title,value) in [("L ",&mut d.source.input_crop.left),("R ",&mut d.source.input_crop.right),("T ",&mut d.source.input_crop.top),("B ",&mut d.source.input_crop.bottom)] { ui.add(egui::DragValue::new(value).speed(1.0).prefix(title)); } + for (title,value) in [("L ",&mut d.source.input_crop.left),("R ",&mut d.source.input_crop.right),("T ",&mut d.source.input_crop.top),("B ",&mut d.source.input_crop.bottom)] { ui.add(egui::DragValue::new(value).speed(1.0).custom_formatter(|v,_|number(v)).prefix(title)); } }); if old_crop!=d.source.input_crop && let Ok(size)=d.source.input() { d.input_size=size; } ui.label("Input crop edges: native source pixels. Native-size changes reset the draft input crop before review."); @@ -217,13 +227,13 @@ impl Editor { ui.label("These controls operate on the applied formats above. 100% is the minimum crop that fills the box."); let mut percent=d.framing.zoom*100.0; ui.horizontal(|ui| { - if ui.add(egui::DragValue::new(&mut percent).speed(1.0).suffix("% content zoom")).changed() {d.framing.zoom=percent/100.0;} - ui.add(egui::DragValue::new(&mut d.framing.offset[0]).speed(1.0).prefix("X ")); - ui.add(egui::DragValue::new(&mut d.framing.offset[1]).speed(1.0).prefix("Y ")); + if ui.add(egui::DragValue::new(&mut percent).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 ")); }); 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 ±{:.4}, Y ±{:.4}. Positive moves content right/down.",limit[0],limit[1])); + ui.label(format!("Offsets in output px: X ±{:.1}, Y ±{:.1}. Positive moves content right/down.",limit[0],limit[1])); } ui.horizontal(|ui| { let apply=ui.button("Apply framing").clicked(); @@ -245,7 +255,7 @@ impl Editor { ui.collapsing("Calculated crops / provisional WebRCS mapping",|ui| { if let Ok(report)=self.layout.calculation_report(i) { ui.label(&report); - if ui.button("Copy provisional calculation").clicked() {ctx.copy_text(report);} + if ui.button("Copy provisional calculation").clicked() && let Ok(export)=self.layout.calculation_export(i) {ctx.copy_text(export);} } }); }); @@ -358,7 +368,7 @@ mod tests { assert_eq!(*app.layout.layers(), before); assert_eq!( app.fields[0], - (2560.0 - before[1].x - before[1].width).to_string() + number(2560.0 - before[1].x - before[1].width) ); app.layout.undo(); assert_eq!(app.layout.formats().output, [1920, 1080]); diff --git a/crates/midra-layout/src/formats.rs b/crates/midra-layout/src/formats.rs index 93c649d..0a5c6c1 100644 --- a/crates/midra-layout/src/formats.rs +++ b/crates/midra-layout/src/formats.rs @@ -168,7 +168,7 @@ pub fn crop_from_framing( for (i, bound) in limit.iter().enumerate() { if !framing.offset[i].is_finite() || framing.offset[i].abs() > bound + 1e-7 { return Err(format!( - "Content offset {} must be between {:.6} and {:.6} output px.", + "Content offset {} must be between {:.1} and {:.1} output px.", if i == 0 { "X" } else { "Y" }, -bound, bound diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 40cd28f..4651ad0 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -2,7 +2,9 @@ use super::*; #[path = "format_ui.rs"] mod format_ui; use format_ui::{FormatDraft, OutputDraft}; -use midra_layout::{AlignTarget, Alignment, Corner, DrawioShape, Geometry, Layout, parse_drawio}; +use midra_layout::{ + AlignTarget, Alignment, Corner, DrawioShape, Geometry, Layout, ResizeHandle, parse_drawio, +}; pub struct Editor { layout: Layout, @@ -84,7 +86,7 @@ impl Default for Editor { } } fn number(n: f64) -> String { - n.to_string() + midra_layout::view_pixels(n) } fn label(i: usize) -> &'static str { if i == 0 { @@ -208,9 +210,43 @@ impl Editor { .collect(); let result = values .map_err(|_| "Enter a number in each field.".to_string()) - .and_then(|v| { + .and_then(|mut v| { + if let Some(i) = self.layout.selected() { + let g = self.layout.layers()[i]; + let [x, y] = self.corner.offsets_on(g, self.layout.formats().output); + let original = [x, y, g.width, g.height]; + for j in 0..4 { + if self.fields[j] == number(original[j]) { + v[j] = original[j]; + } + } + // Linked numeric fields are displayed rounded, but keep the + // original aspect when only one dimension drives the edit. + if self.keep_aspect_ratio { + if v[2] != g.width && self.fields[3] == number(v[2] * g.height / g.width) { + v[3] = v[2] * g.height / g.width; + } else if v[3] != g.height + && self.fields[2] == number(v[3] * g.width / g.height) + { + v[2] = v[3] * g.width / g.height; + } + } + } self.corner .geometry_on(v[0], v[1], v[2], v[3], self.layout.formats().output) + .map(|mut geometry| { + if let Some(i) = self.layout.selected() { + let old = self.layout.layers()[i]; + let offsets = self.corner.offsets_on(old, self.layout.formats().output); + if v[0] == offsets[0] && geometry.width == old.width { + geometry.x = old.x; + } + if v[1] == offsets[1] && geometry.height == old.height { + geometry.y = old.y; + } + } + geometry + }) }); match result { Ok(g) => { @@ -235,10 +271,10 @@ impl Editor { format!( "{} — visible box (device container unverified)\nHorizontal: {} px\nVertical: {} px\nWidth: {} px\nHeight: {} px", label(i), - number(h), - number(v), - number(g.width), - number(g.height) + midra_layout::export_pixels(h), + midra_layout::export_pixels(v), + midra_layout::export_pixels(g.width), + midra_layout::export_pixels(g.height) ) }) .collect(); @@ -566,7 +602,7 @@ impl Editor { 12.0, 223.0, &format!( - "{} × {} px / {:.4}:1", + "{} × {} px / {:.1}:1", number(g.width), number(g.height), g.width / g.height @@ -788,15 +824,15 @@ impl Editor { ); } } - fn resize_handle(&self, pointer: Pos2, origin: Pos2, scale: f32) -> Option<(usize, Corner)> { + fn resize_handle( + &self, + pointer: Pos2, + origin: Pos2, + scale: f32, + ) -> Option<(usize, ResizeHandle)> { for i in self.layout.selected_indices().into_iter().rev() { let g = self.layout.layers()[i]; - for (corner, x, y) in [ - (Corner::TopLeft, g.x, g.y), - (Corner::TopRight, g.x + g.width, g.y), - (Corner::BottomLeft, g.x, g.y + g.height), - (Corner::BottomRight, g.x + g.width, g.y + g.height), - ] { + for (corner, [x, y]) in ResizeHandle::positions(g) { let center = origin + Vec2::new(x as f32, y as f32) * scale; if Rect::from_center_size(center, Vec2::splat(14.0)).contains(pointer) { return Some((i, corner)); @@ -934,12 +970,14 @@ impl Editor { && let Some(p) = pointer && let Some((_, corner)) = self.resize_handle(p, origin, scale) { - ui.ctx() - .set_cursor_icon(if matches!(corner, Corner::TopLeft | Corner::BottomRight) { + ui.ctx().set_cursor_icon(match corner { + ResizeHandle::Corner(Corner::TopLeft | Corner::BottomRight) => { egui::CursorIcon::ResizeNwSe - } else { - egui::CursorIcon::ResizeNeSw - }); + } + ResizeHandle::Corner(_) => egui::CursorIcon::ResizeNeSw, + ResizeHandle::Left | ResizeHandle::Right => egui::CursorIcon::ResizeHorizontal, + ResizeHandle::Top | ResizeHandle::Bottom => egui::CursorIcon::ResizeVertical, + }); } if response.drag_started_by(egui::PointerButton::Primary) || response.drag_started_by(egui::PointerButton::Middle) @@ -967,7 +1005,7 @@ impl Editor { } } else if let Some((i, corner)) = handle.filter(|_| !shift) { self.layout - .start_resize(i, corner, point, self.keep_aspect_ratio); + .start_handle_resize(i, corner, point, self.keep_aspect_ratio); } else if shift || self.layout.hit(point).is_none() { let previous = self.layout.selection_mask(); let base = if shift { previous } else { [false; 2] }; @@ -1135,6 +1173,10 @@ impl Editor { rect.right_top(), rect.left_bottom(), rect.right_bottom(), + rect.center_top(), + rect.center_bottom(), + rect.left_center(), + rect.right_center(), ] { p.rect( Rect::from_center_size(corner, Vec2::splat(7.0)), @@ -1200,14 +1242,14 @@ impl Editor { guide.text( Pos2::new((edge.x + boundary.x) / 2.0, edge.y + 6.0), Align2::CENTER_TOP, - format!("{dx:.2} px"), + format!("{dx:.1} px"), FontId::proportional(11.0), AMBER, ); guide.text( Pos2::new(edge.x + 6.0, (edge.y + boundary.y) / 2.0), Align2::LEFT_CENTER, - format!("{dy:.2} px"), + format!("{dy:.1} px"), FontId::proportional(11.0), AMBER, ); @@ -1216,7 +1258,7 @@ impl Editor { ink.text( 24.0, r.height() - 57.0, - "Drag corners to resize · Drag to move · Shift-click selects", + "Corners resize · Side handles resize symmetrically · Shift-click selects", 12.0, MUTED, ); @@ -1434,6 +1476,70 @@ mod tests { assert!(!app.edit_content); } #[test] + fn rounded_fields_do_not_quantize_unchanged_values_and_copy_uses_pixels() { + let mut app = Editor::default(); + let ctx = egui::Context::default(); + let g = Geometry { + x: 12.36, + y: 20.51, + width: 300.33, + height: 250.58, + }; + app.layout.apply(1, g).unwrap(); + app.sync(); + assert_eq!(app.fields[2], "300.3"); + assert_eq!(app.fields[3], "250.6"); + app.apply_fields(); + assert_eq!(app.layout.layers()[1], g); + let output = ctx.run(egui::RawInput::default(), |ctx| app.copy(ctx)); + assert!(output.platform_output.commands.iter().any(|c|matches!(c,egui::OutputCommand::CopyText(text) if text.contains("Width: 300 px") && text.contains("Height: 251 px")))); + assert_eq!(app.layout.layers()[1], g); + } + #[test] + fn side_handle_hit_regions_resize_at_multiple_zoom_levels() { + for zoom in [0.6, 1.0] { + for side in 0..4 { + let mut app = Editor { + zoom, + keep_aspect_ratio: false, + ..Default::default() + }; + let ctx = egui::Context::default(); + let output = frame(&mut app, &ctx, vec![]); + let rect = layer_rects(&output)[1]; + let before = app.layout.layers()[1]; + let (start, delta) = match side { + 0 => (rect.center_top(), Vec2::new(0.0, -20.0)), + 1 => (rect.center_bottom(), Vec2::new(0.0, 20.0)), + 2 => (rect.left_center(), Vec2::new(-20.0, 0.0)), + _ => (rect.right_center(), Vec2::new(20.0, 0.0)), + }; + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(start), mouse(start, true)], + ); + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(start + delta)], + ); + frame(&mut app, &ctx, vec![mouse(start + delta, false)]); + let after = app.layout.layers()[1]; + assert_eq!(after.center(), before.center()); + if side < 2 { + assert!(after.height > before.height); + assert_eq!(after.width, before.width); + } else { + assert!(after.width > before.width); + assert_eq!(after.height, before.height); + } + app.layout.undo(); + assert_eq!(app.layout.layers()[1], before); + } + } + } + #[test] fn corner_handles_resize_at_different_zoom_levels_and_escape_restores() { for zoom in [0.6, 1.0] { for keep_aspect_ratio in [true, false] { diff --git a/crates/midra-layout/src/lib.rs b/crates/midra-layout/src/lib.rs index 6581766..0a5f4a3 100644 --- a/crates/midra-layout/src/lib.rs +++ b/crates/midra-layout/src/lib.rs @@ -3,6 +3,41 @@ use std::collections::HashMap; pub mod formats; use formats::{Formats, Framing, Source}; +/// Presentation only: retain full precision in the document. +pub fn view_pixels(value: f64) -> String { + let rounded = (value * 10.0).round() / 10.0; + format!("{:.1}", if rounded == 0.0 { 0.0 } else { rounded }) +} +pub fn export_pixels(value: f64) -> String { + let rounded = value.round(); + format!("{:.0}", if rounded == 0.0 { 0.0 } else { rounded }) +} +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ResizeHandle { + Corner(Corner), + Top, + Bottom, + Left, + Right, +} +impl ResizeHandle { + pub fn positions(g: Geometry) -> [(Self, [f64; 2]); 8] { + let [cx, cy] = g.center(); + [ + (Self::Corner(Corner::TopLeft), [g.x, g.y]), + (Self::Corner(Corner::TopRight), [g.x + g.width, g.y]), + (Self::Corner(Corner::BottomLeft), [g.x, g.y + g.height]), + ( + Self::Corner(Corner::BottomRight), + [g.x + g.width, g.y + g.height], + ), + (Self::Top, [cx, g.y]), + (Self::Bottom, [cx, g.y + g.height]), + (Self::Left, [g.x, cy]), + (Self::Right, [g.x + g.width, cy]), + ] + } +} /// Visible layer bounds, in output screen pixels. No crop or camera transform. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Geometry { @@ -135,7 +170,7 @@ pub struct Layout { #[derive(Clone)] struct Resize { index: usize, - corner: Corner, + handle: ResizeHandle, before: Snapshot, start: [f64; 2], keep_ratio: bool, @@ -264,7 +299,7 @@ impl Layout { if adjusted { let m = next.formats.mapping(j, next.layers[j])?; adjustments.push(format!( - "Layer {}: content offsets adjusted to {:.4}, {:.4} px", + "Layer {}: content offsets adjusted to {:.1}, {:.1} px", j + 1, m.framing.offset[0], m.framing.offset[1] @@ -281,6 +316,13 @@ impl Layout { } /// Provisional geometry report; deliberately not a verified WebRCS command recipe. pub fn calculation_report(&self, i: usize) -> Result { + self.formatted_report(i, false) + } + pub fn calculation_export(&self, i: usize) -> Result { + self.formatted_report(i, true) + } + fn formatted_report(&self, i: usize, export: bool) -> Result { + let p = if export { export_pixels } else { view_pixels }; let f = &self.formats; let g = self.layers[i]; let source = f.sources[f.source_for[i]]; @@ -295,36 +337,36 @@ impl Layout { height: m.scale * input[1], }; Ok(format!( - "PROVISIONAL GEOMETRY — not a verified device recipe\nSource {} / {} / {} x {}\nInput crop L/R/T/B (native px): {:.6} / {:.6} / {:.6} / {:.6}\nInput resolution: {:.6} x {:.6}\nLayer resolution: {:.6} x {:.6}; visible center: {:.6}, {:.6}\nAdvanced crop L/R/T/B (usable-input px): {:.6} / {:.6} / {:.6} / {:.6}\nCombined native retained region: x {:.6}, y {:.6}, w {:.6}, h {:.6}\nContent zoom: {:.6}%; offsets (output px): {:.6}, {:.6}\nProjected full usable input: {:.6} x {:.6}; center {:.6}, {:.6}\nIntended aspect mode: Centered. Device container, crop units and quantization remain unverified.", + "PROVISIONAL GEOMETRY — not a verified device recipe\nSource {} / {} / {} x {}\nInput crop L/R/T/B (native px): {} / {} / {} / {}\nInput resolution: {} x {}\nLayer resolution: {} x {}; visible center: {}, {}\nAdvanced crop L/R/T/B (usable-input px): {} / {} / {} / {}\nCombined native retained region: x {}, y {}, w {}, h {}\nContent zoom: {}%; offsets (output px): {}, {}\nProjected full usable input: {} x {}; center {}, {}\nIntended aspect mode: Centered. Device container, crop units and quantization remain unverified.", f.source_for[i] + 1, source.aspect.label(), source.native[0], source.native[1], - c.left, - c.right, - c.top, - c.bottom, - input[0], - input[1], - g.width, - g.height, - g.center()[0], - g.center()[1], - a.left, - a.right, - a.top, - a.bottom, - c.left + a.left, - c.top + a.top, - m.retained[0], - m.retained[1], - m.framing.zoom * 100.0, - m.framing.offset[0], - m.framing.offset[1], - projected.width, - projected.height, - projected.center()[0], - projected.center()[1] + p(c.left), + p(c.right), + p(c.top), + p(c.bottom), + p(input[0]), + p(input[1]), + p(g.width), + p(g.height), + p(g.center()[0]), + p(g.center()[1]), + p(a.left), + p(a.right), + p(a.top), + p(a.bottom), + p(c.left + a.left), + p(c.top + a.top), + p(m.retained[0]), + p(m.retained[1]), + view_pixels(m.framing.zoom * 100.0), + p(m.framing.offset[0]), + p(m.framing.offset[1]), + p(projected.width), + p(projected.height), + p(projected.center()[0]), + p(projected.center()[1]) )) } pub fn layers(&self) -> &[Geometry; 2] { @@ -529,6 +571,15 @@ impl Layout { corner: Corner, point: [f64; 2], keep_ratio: bool, + ) { + self.start_handle_resize(index, ResizeHandle::Corner(corner), point, keep_ratio); + } + pub fn start_handle_resize( + &mut self, + index: usize, + handle: ResizeHandle, + point: [f64; 2], + keep_ratio: bool, ) { self.finish_drag(); if !self.selectable(index) || !self.selected[index] { @@ -537,7 +588,7 @@ impl Layout { self.select(Some(index)); self.resize = Some(Resize { index, - corner, + handle, before: self.snapshot(), start: point, keep_ratio, @@ -568,11 +619,49 @@ impl Layout { } if let Some(r) = &self.resize { let original = r.before.layers[r.index]; - let left = matches!(r.corner, Corner::TopLeft | Corner::BottomLeft); - let top = matches!(r.corner, Corner::TopLeft | Corner::TopRight); + let left = matches!( + r.handle, + ResizeHandle::Corner(Corner::TopLeft | Corner::BottomLeft) | ResizeHandle::Left + ); + let top = matches!( + r.handle, + ResizeHandle::Corner(Corner::TopLeft | Corner::TopRight) | ResizeHandle::Top + ); let dx = (point[0] - r.start[0]).round() * if left { -1.0 } else { 1.0 }; let dy = (point[1] - r.start[1]).round() * if top { -1.0 } else { 1.0 }; - let (width, height) = if r.keep_ratio { + let side = !matches!(r.handle, ResizeHandle::Corner(_)); + let horizontal = matches!(r.handle, ResizeHandle::Left | ResizeHandle::Right); + let (width, height) = if side { + if horizontal { + let width = (original.width + 2.0 * dx).max(if r.keep_ratio { + (original.width / original.height).max(1.0) + } else { + 1.0 + }); + ( + width, + if r.keep_ratio { + width * original.height / original.width + } else { + original.height + }, + ) + } else { + let height = (original.height + 2.0 * dy).max(if r.keep_ratio { + (original.height / original.width).max(1.0) + } else { + 1.0 + }); + ( + if r.keep_ratio { + height * original.width / original.height + } else { + original.width + }, + height, + ) + } + } else if r.keep_ratio { let factor = ((original.width * (original.width + dx) + original.height * (original.height + dy)) / (original.width * original.width + original.height * original.height)) @@ -585,12 +674,16 @@ impl Layout { ) }; let next = Geometry { - x: if left { + x: if side { + original.center()[0] - width / 2.0 + } else if left { original.x + original.width - width } else { original.x }, - y: if top { + y: if side { + original.center()[1] - height / 2.0 + } else if top { original.y + original.height - height } else { original.y diff --git a/crates/midra-layout/tests/formats.rs b/crates/midra-layout/tests/formats.rs index 3e4dfff..d27c35b 100644 --- a/crates/midra-layout/tests/formats.rs +++ b/crates/midra-layout/tests/formats.rs @@ -186,7 +186,7 @@ fn input_crop_is_composed_once_and_keeps_original_source_format() { close(source.input_crop.left + c.left, 660.0); let report = layout.calculation_report(1).unwrap(); assert!(report.contains("not a verified device recipe")); - assert!(report.contains("x 660.000000, y 290.000000, w 600.000000, h 500.000000")); + assert!(report.contains("x 660.0, y 290.0, w 600.0, h 500.0")); } #[test] fn shared_sources_update_atomically_and_layer_framing_stays_independent() { @@ -343,3 +343,32 @@ fn content_drag_clamps_crops_and_undo_and_escape_restore_full_state() { assert_eq!(*layout.formats(), before); assert_eq!(*layout.layers(), boxes); } + +#[test] +fn view_and_export_rounding_never_changes_crop_geometry() { + use midra_layout::{export_pixels, view_pixels}; + assert_eq!(view_pixels(12.36), "12.4"); + assert_eq!(export_pixels(12.36), "12"); + assert_eq!(export_pixels(12.5), "13"); + assert_eq!(export_pixels(-12.5), "-13"); + assert_eq!(view_pixels(-0.01), "0.0"); + assert_eq!(export_pixels(-0.1), "0"); + let mut layout = Layout::default(); + let g = Geometry { + x: 12.36, + y: 20.51, + width: 300.33, + height: 250.58, + }; + layout.apply(1, g).unwrap(); + let before = *layout.formats(); + assert!( + layout + .calculation_report(1) + .unwrap() + .contains("300.3 x 250.6") + ); + assert!(layout.calculation_export(1).unwrap().contains("300 x 251")); + assert_eq!(layout.layers()[1], g); + assert_eq!(*layout.formats(), before); +} diff --git a/crates/midra-layout/tests/positioning.rs b/crates/midra-layout/tests/positioning.rs index 2c0ee7f..f5a80be 100644 --- a/crates/midra-layout/tests/positioning.rs +++ b/crates/midra-layout/tests/positioning.rs @@ -641,3 +641,54 @@ fn free_resize_changes_ratio_and_cancels_without_flipping_or_moving_locked_layer layout.start_resize(1, Corner::BottomRight, [0.0, 0.0], false); assert!(!layout.dragging()); } + +#[test] +fn midpoint_handles_resize_symmetrically_and_preserve_undo_and_ratio() { + use midra_layout::ResizeHandle as H; + for handle in [H::Left, H::Right, H::Top, H::Bottom] { + for keep_ratio in [true, false] { + let mut layout = Layout::default(); + let before = *layout.layers(); + let center = before[1].center(); + let (handle, start) = H::positions(before[1]) + .into_iter() + .find(|(h, _)| *h == handle) + .unwrap(); + let end = match handle { + H::Left => [start[0] - 20.0, start[1]], + H::Right => [start[0] + 20.0, start[1]], + H::Top => [start[0], start[1] - 20.0], + H::Bottom => [start[0], start[1] + 20.0], + _ => unreachable!(), + }; + layout.start_handle_resize(1, handle, start, keep_ratio); + layout.drag_to(end); + layout.finish_drag(); + let after = layout.layers()[1]; + assert_eq!(after.center(), center); + assert_eq!(layout.layers()[0], before[0]); + if matches!(handle, H::Left | H::Right) { + assert_eq!(after.width, 680.0); + if !keep_ratio { + assert_eq!(after.height, 360.0); + } + } else { + assert_eq!(after.height, 400.0); + if !keep_ratio { + assert_eq!(after.width, 640.0); + } + } + if keep_ratio { + assert!((after.width / after.height - 16.0 / 9.0).abs() < 1e-10); + } + layout.undo(); + assert_eq!(*layout.layers(), before); + layout.redo(); + assert_eq!(layout.layers()[1], after); + layout.start_handle_resize(1, handle, start, false); + layout.drag_to(end); + layout.cancel_drag(); + assert_eq!(layout.layers()[1], after); + } + } +} diff --git a/docs/midra-layout.md b/docs/midra-layout.md index fdf9470..e268555 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -246,3 +246,18 @@ verified device recipe: Centered-mode container behavior, crop units/limits and quantization still require offline firmware verification. No hardware commands are generated or sent. See [the resolution model](midra-layout-resolutions.md) for equations and worked examples. + +## Display/export precision and midpoint handles + +Geometry in the viewport, inspector and calculation view is shown to the nearest +0.1 pixel. Copied positions and provisional calculation exports use whole pixels +(round-half-away-from-zero). Internal geometry and crops retain full precision; +viewing/copying or applying unchanged rounded fields does not quantize the document. +This presentation rule does not validate the hardware's crop/container mapping. + +Each selected box also has a handle at the middle of every side. Dragging top or +bottom resizes height equally about the center; left or right resizes width the +same way. With the aspect lock enabled, the other dimension scales around the +same center. Disable it to change just one axis. Corner handles still keep the +opposite corner fixed. Crop preservation, lock protection, Escape and Undo apply +to both kinds of resize handles.