From 20dd6c69ebbacb0210e491ffd88e744304bbbe5f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 14 Nov 2026 12:01:37 +0100 Subject: [PATCH] Align zoomed content inside the layer in crop mode --- crates/midra-layout/src/gui.rs | 150 ++++++++++++++++++++++----- crates/midra-layout/src/lib.rs | 19 ++++ crates/midra-layout/tests/formats.rs | 46 ++++++++ docs/midra-layout.md | 6 ++ docs/midralayout/changelog.md | 2 + 5 files changed, 197 insertions(+), 26 deletions(-) diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 6387094..2dae66b 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -1273,35 +1273,46 @@ impl Editor { fn alignment_menu(&mut self, ui: &mut egui::Ui) { let palette = theme::palette(ui.ctx()); ui.set_min_width(222.0); - ui.label("ALIGN TO"); - if self.align_target == AlignTarget::Screen - || matches!(self.align_target, AlignTarget::Layer(i) if i >= self.layout.layers().len()) - { - self.align_target = AlignTarget::Selection; - } let single_layer = self.layout.selected_indices().len() == 1; - let default_name = if single_layer { - "Screen (one selected layer)" + let crop_mode = self.edit_content && single_layer; + if crop_mode { + ui.label("ALIGN CONTENT TO LAYER BOX"); } else { - "Selected layers" - }; - let target_name = |target| match target { - AlignTarget::Layer(i) => format!("Layer {}: {}", i + 1, self.layout.layer_info(i).name), - _ => default_name.to_owned(), - }; - egui::ComboBox::from_id_salt("alignment-target") - .width(210.0) - .selected_text(target_name(self.align_target)) - .show_ui(ui, |ui| { - ui.selectable_value(&mut self.align_target, AlignTarget::Selection, default_name); - for i in 0..self.layout.layers().len() { + ui.label("ALIGN TO"); + if self.align_target == AlignTarget::Screen + || matches!(self.align_target, AlignTarget::Layer(i) if i >= self.layout.layers().len()) + { + self.align_target = AlignTarget::Selection; + } + let default_name = if single_layer { + "Screen (one selected layer)" + } else { + "Selected layers" + }; + let target_name = |target| match target { + AlignTarget::Layer(i) => { + format!("Layer {}: {}", i + 1, self.layout.layer_info(i).name) + } + _ => default_name.to_owned(), + }; + egui::ComboBox::from_id_salt("alignment-target") + .width(210.0) + .selected_text(target_name(self.align_target)) + .show_ui(ui, |ui| { ui.selectable_value( &mut self.align_target, - AlignTarget::Layer(i), - target_name(AlignTarget::Layer(i)), + AlignTarget::Selection, + default_name, ); - } - }); + for i in 0..self.layout.layers().len() { + ui.selectable_value( + &mut self.align_target, + AlignTarget::Layer(i), + target_name(AlignTarget::Layer(i)), + ); + } + }); + } ui.add_space(8.0); let (grid, _) = ui.allocate_exact_size(Vec2::new(172.0, 128.0), egui::Sense::hover()); ui.add_enabled_ui(!self.layout.selected_indices().is_empty(), |ui| { @@ -1336,7 +1347,13 @@ impl Editor { } else { self.align_target }; - match self.layout.align_axes(&axes, target) { + let result = if crop_mode { + self.layout + .align_content(self.layout.selected().unwrap(), &axes) + } else { + self.layout.align_axes(&axes, target) + }; + match result { Ok(()) => { self.sync(); ui.close_menu(); @@ -1346,7 +1363,11 @@ impl Editor { } } }); - ui.small("Moves selected layers; keeps their sizes."); + ui.small(if crop_mode { + "Moves zoomed content; keeps the layer box and zoom fixed." + } else { + "Moves selected layers; keeps their sizes." + }); if self.layout.selected_indices().is_empty() { ui.label("Select layers to align."); } @@ -5010,6 +5031,83 @@ mod tests { .collect(); rects[if title == "Align top" { 1 } else { 7 }].center() } + #[test] + fn crop_alignment_grid_moves_content_without_moving_box_or_changing_zoom() { + for (index, x, y) in [ + (0, Some(1.0), Some(1.0)), + (1, None, Some(1.0)), + (2, Some(-1.0), Some(1.0)), + (3, Some(1.0), None), + (4, Some(0.0), Some(0.0)), + (5, Some(-1.0), None), + (6, Some(1.0), Some(-1.0)), + (7, None, Some(-1.0)), + (8, Some(-1.0), Some(-1.0)), + (9, Some(0.0), None), + (10, None, Some(0.0)), + ] { + let mut app = Editor { + edit_content: true, + align_target: AlignTarget::Layer(0), + ..Default::default() + }; + app.layout + .set_framing( + 1, + midra_layout::formats::Framing { + zoom: 2.5, + offset: [30.0, -20.0], + }, + ) + .unwrap(); + let boxes = app.layout.layers().clone(); + let formats = app.layout.formats().clone(); + let mapping = formats.mapping(1, boxes[1]).unwrap(); + let ctx = egui::Context::default(); + let output = open_alignment_test_menu(&mut app, &ctx); + assert!(output.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Text(t) if t.galley.text() == "ALIGN CONTENT TO LAYER BOX"))); + let pos = output + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Rect(r) if r.rect.size() == Vec2::splat(40.0) => { + Some(r.rect.center()) + } + _ => None, + }) + .nth(index) + .unwrap(); + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(pos), mouse(pos, true)], + ); + frame(&mut app, &ctx, vec![mouse(pos, false)]); + let actual = app.layout.formats().mapping(1, boxes[1]).unwrap().framing; + let expected = [ + x.map_or(30.0, |v| v * mapping.offset_limits[0]), + y.map_or(-20.0, |v| v * mapping.offset_limits[1]), + ]; + for axis in 0..2 { + assert!( + (actual.offset[axis] - expected[axis]).abs() < 1e-7, + "button {index}" + ); + } + assert!((actual.zoom - 2.5).abs() < 1e-7); + assert_eq!(app.layout.layers(), &boxes); + assert_eq!(app.layout.formats().sources, formats.sources); + assert_eq!(app.layout.formats().advanced[0], formats.advanced[0]); + assert!(app.edit_content); + assert_eq!(app.align_target, AlignTarget::Layer(0)); + let aligned = app.layout.formats().clone(); + app.layout.undo(); + assert_eq!(app.layout.formats(), &formats); + app.layout.redo(); + assert_eq!(app.layout.formats(), &aligned); + } + } + #[test] fn single_layer_alignment_grid_uses_current_screen_bounds() { for (index, x, y) in [ diff --git a/crates/midra-layout/src/lib.rs b/crates/midra-layout/src/lib.rs index fe4cdd9..bf4337e 100644 --- a/crates/midra-layout/src/lib.rs +++ b/crates/midra-layout/src/lib.rs @@ -665,6 +665,25 @@ impl Layout { self.remember(before); Ok(()) } + /// Align the zoomed source content to its visible box without moving/resizing the box. + pub fn align_content(&mut self, i: usize, axes: &[Alignment]) -> Result<(), String> { + if !self.selectable(i) { + return Err("Unlock and show this layer first.".into()); + } + let mapping = self.formats.mapping(i, self.layers[i])?; + let mut framing = mapping.framing; + for axis in axes { + match axis { + Alignment::Left => framing.offset[0] = mapping.offset_limits[0], + Alignment::Right => framing.offset[0] = -mapping.offset_limits[0], + Alignment::Top => framing.offset[1] = mapping.offset_limits[1], + Alignment::Bottom => framing.offset[1] = -mapping.offset_limits[1], + Alignment::HorizontalCenter => framing.offset[0] = 0.0, + Alignment::VerticalCenter => framing.offset[1] = 0.0, + } + } + self.set_framing(i, framing) + } pub fn start_content_drag(&mut self, i: usize, point: [f64; 2]) { self.finish_drag(); if self.selectable(i) && self.selected() == Some(i) { diff --git a/crates/midra-layout/tests/formats.rs b/crates/midra-layout/tests/formats.rs index 18fb570..7ecc08b 100644 --- a/crates/midra-layout/tests/formats.rs +++ b/crates/midra-layout/tests/formats.rs @@ -372,3 +372,49 @@ fn view_and_export_rounding_never_changes_crop_geometry() { assert_eq!(layout.layers()[1], g); assert_eq!(*layout.formats(), before); } + +#[test] +fn crop_alignment_handles_cover_cropping_and_protected_layers() { + for zoom in [1.0, 3.0] { + let mut layout = Layout::default(); + layout.apply(1, box_size(300.0, 300.0)).unwrap(); + layout + .set_framing( + 1, + Framing { + zoom, + offset: [0.0; 2], + }, + ) + .unwrap(); + let boxes = layout.layers().clone(); + layout + .align_content(1, &[Alignment::Left, Alignment::Top]) + .unwrap(); + let crop = layout.formats().advanced[1]; + close(crop.left, 0.0); + close(crop.top, 0.0); + layout + .align_content(1, &[Alignment::Right, Alignment::Bottom]) + .unwrap(); + let crop = layout.formats().advanced[1]; + close(crop.right, 0.0); + close(crop.bottom, 0.0); + layout + .align_content(1, &[Alignment::HorizontalCenter, Alignment::VerticalCenter]) + .unwrap(); + let actual = layout.formats().mapping(1, boxes[1]).unwrap().framing; + close(actual.zoom, zoom); + close(actual.offset[0], 0.0); + close(actual.offset[1], 0.0); + assert_eq!(layout.layers(), &boxes); + let before = layout.formats().clone(); + layout.set_locked(1, true); + assert!(layout.align_content(1, &[Alignment::Left]).is_err()); + layout.set_locked(1, false); + layout.set_visible(1, false); + assert!(layout.align_content(1, &[Alignment::Left]).is_err()); + assert!(layout.align_content(1000, &[Alignment::Left]).is_err()); + assert_eq!(layout.formats(), &before); + } +} diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 92ff248..c888129 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -192,6 +192,12 @@ It also lists **Layer 1: Presenter**, **Layer 2: Slides**, and any future layers An explicitly chosen reference layer takes precedence, stays fixed and does not need to be selected. There is no separate Screen option for multi-selection. +In **Adjust layer crop** mode, alignment positions the zoomed content inside the +selected layer's visible box. Left/top/right/bottom align the corresponding +content edge to the box edge; center resets that axis's offset. Corners align both +axes in one Undo step. Zoom, source proportions and layer geometry stay fixed; +leaving crop mode restores normal layer alignment and its previous target. + The compact icon grid offers four corners, top/bottom/left/right edges, center on both axes, and separate horizontal/vertical centering. The top-right button centers horizontally (X); the bottom-right button centers vertically (Y). Edge buttons change diff --git a/docs/midralayout/changelog.md b/docs/midralayout/changelog.md index 553008f..f8ffe1c 100644 --- a/docs/midralayout/changelog.md +++ b/docs/midralayout/changelog.md @@ -4,6 +4,8 @@ ## Unreleased +- In Adjust layer crop mode, alignment buttons move zoomed content within the fixed layer box; preserve zoom and support Undo. + - Snap mirrored/opposite resizing to exact edge contact between both moving layers, including unequal sizes; preserve anchors/ratios, Alt bypass and Undo. - Distinguish completely out-of-view layers from partial/complete output-screen overflow; give navigation versus positioning advice. Rename Fit selected to Zoom to selection.