From 32ea37f06fee9af33f5fbccf8230d41ace1e2146 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 13 Nov 2026 14:11:50 +0100 Subject: [PATCH] Move alignment into a toolbar icon popup with layer targets --- crates/midra-layout/src/gui.rs | 287 +++++++++++++++++------ crates/midra-layout/src/lib.rs | 37 +-- crates/midra-layout/tests/positioning.rs | 21 +- docs/midra-layout.md | 26 +- 4 files changed, 274 insertions(+), 97 deletions(-) diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 4f97a9f..50ecaf9 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -829,6 +829,84 @@ impl Editor { MUTED, ); } + fn alignment_menu(&mut self, ui: &mut egui::Ui) { + ui.set_min_width(222.0); + ui.label("ALIGN TO"); + if self.align_target == AlignTarget::Screen { + self.align_target = AlignTarget::Selection; + } + let target_name = |target| match target { + AlignTarget::Layer(i) => format!( + "Layer {}: {}", + i + 1, + label(i).split(" / ").last().unwrap_or(label(i)) + ), + _ => "Selected layers".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, + "Selected layers", + ); + 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| { + use Alignment::{Bottom, HorizontalCenter, Left, Right, Top, VerticalCenter}; + for (col, row, title, horizontal, vertical) in [ + (0, 0, "Align top left", Some(Left), Some(Top)), + (1, 0, "Align top", None, Some(Top)), + (2, 0, "Align top right", Some(Right), Some(Top)), + (0, 1, "Align left", Some(Left), None), + ( + 1, + 1, + "Center both axes", + Some(HorizontalCenter), + Some(VerticalCenter), + ), + (2, 1, "Align right", Some(Right), None), + (0, 2, "Align bottom left", Some(Left), Some(Bottom)), + (1, 2, "Align bottom", None, Some(Bottom)), + (2, 2, "Align bottom right", Some(Right), Some(Bottom)), + (3, 0, "Center vertically", None, Some(VerticalCenter)), + (3, 2, "Center horizontally", Some(HorizontalCenter), None), + ] { + let rect = Rect::from_min_size( + grid.min + Vec2::new(col as f32 * 44.0, row as f32 * 44.0), + Vec2::splat(40.0), + ); + if alignment_icon(ui, rect, title, horizontal, vertical).clicked() { + let axes: Vec<_> = [horizontal, vertical].into_iter().flatten().collect(); + match self.layout.align_axes(&axes, self.align_target) { + Ok(()) => { + self.sync(); + ui.close_menu(); + } + Err(e) => self.error = e, + } + } + } + }); + ui.small("Moves selected layers; keeps their sizes."); + if self.layout.selected_indices().is_empty() { + ui.label("Select layers to align."); + } + if !self.error.is_empty() { + ui.colored_label(AMBER, &self.error); + } + } fn alignment_inspector(&mut self, ui: &mut egui::Ui, ink: &Ink, w: f32) { ink.text(0.0, 150.0, "2 layers selected", 25.0, TEXT); ink.text( @@ -838,51 +916,25 @@ impl Editor { 12.0, MUTED, ); - ink.text(0.0, 225.0, "ALIGN TO", 11.0, MUTED); - let half = (w - 12.0) / 2.0; - for (index, title, target) in [ - (0, "Selection bounds", AlignTarget::Selection), - (1, "Screen", AlignTarget::Screen), - (2, "Reference: Presenter", AlignTarget::Layer(0)), - (3, "Reference: Slides", AlignTarget::Layer(1)), - ] { - let x = if index % 2 == 0 { 0.0 } else { half + 12.0 }; - let y = 250.0 + (index / 2) as f32 * 42.0; - ink.button(x, y, half, title, self.align_target == target); - if click(ui, ink, [x, y, half, 34.0], title) { - self.align_target = target; - } - } - for (index, title, alignment) in [ - (0, "Align left", Alignment::Left), - (1, "Align top", Alignment::Top), - (2, "Align right", Alignment::Right), - (3, "Align bottom", Alignment::Bottom), - (4, "Center X", Alignment::HorizontalCenter), - (5, "Center Y", Alignment::VerticalCenter), - ] { - let x = if index % 2 == 0 { 0.0 } else { half + 12.0 }; - let y = 350.0 + (index / 2) as f32 * 46.0; - ink.button(x, y, half, title, false); - if click(ui, ink, [x, y, half, 34.0], title) { - match self.layout.align(alignment, self.align_target) { - Ok(()) => self.sync(), - Err(e) => self.error = e, - } - } - } ink.text( 0.0, - 503.0, + 225.0, + "Use the toolbar alignment menu to align layers.", + 12.0, + MUTED, + ); + ink.text( + 0.0, + 263.0, "Alignment changes position, never size.", 12.0, MUTED, ); - ink.text(0.0, 526.0, "A reference layer stays fixed.", 12.0, MUTED); - ink.line([0.0, 559.0], [w, 559.0], LINE); - ink.text(0.0, 581.0, "VISIBLE BOX / CENTER POSITIONS", 11.0, CYAN); + ink.text(0.0, 286.0, "A reference layer stays fixed.", 12.0, MUTED); + ink.line([0.0, 319.0], [w, 319.0], LINE); + ink.text(0.0, 341.0, "VISIBLE BOX / CENTER POSITIONS", 11.0, CYAN); for (row, i) in self.layout.selected_indices().into_iter().enumerate() { - let y = 615.0 + row as f32 * 80.0; + let y = 375.0 + row as f32 * 80.0; let [x, cy] = self.layout.layers()[i].center(); ink.text(0.0, y, label(i), 13.0, MUTED); ink.text( @@ -893,20 +945,20 @@ impl Editor { TEXT, ); } - ink.button(0.0, 792.0, w, "Copy selected positions", true); - if click(ui, ink, [0.0, 792.0, w, 34.0], "copy-selected") { + ink.button(0.0, 552.0, w, "Copy selected positions", true); + if click(ui, ink, [0.0, 552.0, w, 34.0], "copy-selected") { self.copy(ui.ctx()); } ink.text( 0.0, - 849.0, + 609.0, "Select one layer to edit its size; paste accepts the full selection.", 12.0, MUTED, ); if !self.error.is_empty() { ui.put( - Rect::from_min_size(ink.point(0.0, 880.0), Vec2::new(w, 60.0)), + Rect::from_min_size(ink.point(0.0, 640.0), Vec2::new(w, 60.0)), egui::Label::new(egui::RichText::new(&self.error).color(AMBER)).wrap(), ); } @@ -1014,7 +1066,45 @@ impl Editor { ui.scope_builder( egui::UiBuilder::new() .max_rect(Rect::from_min_size( - ink.point(190.0, 66.0), + ink.point(184.0, 66.0), + Vec2::new(36.0, 34.0), + )) + .layout(egui::Layout::left_to_right(egui::Align::Center)), + |ui| { + ui.spacing_mut().interact_size = Vec2::new(36.0, 34.0); + ui.spacing_mut().button_padding.x = 11.0; + let menu = ui.menu_button(" ", |ui| self.alignment_menu(ui)); + let response = menu.response.on_hover_text("Align selected layers"); + response.widget_info(|| { + egui::WidgetInfo::labeled( + egui::WidgetType::Button, + true, + "Align selected layers", + ) + }); + let c = response.rect.center(); + let p = ui.painter(); + let stroke = Stroke::new(1.6_f32, TEXT); + p.line_segment( + [c + Vec2::new(-10.0, -9.0), c + Vec2::new(-10.0, 9.0)], + stroke, + ); + p.rect_filled( + Rect::from_min_size(c + Vec2::new(-6.0, -7.0), Vec2::new(13.0, 5.0)), + 0.0, + TEXT, + ); + p.rect_filled( + Rect::from_min_size(c + Vec2::new(-6.0, 3.0), Vec2::new(9.0, 5.0)), + 0.0, + TEXT, + ); + }, + ); + ui.scope_builder( + egui::UiBuilder::new() + .max_rect(Rect::from_min_size( + ink.point(232.0, 66.0), Vec2::new(154.0, 34.0), )) .layout(egui::Layout::left_to_right(egui::Align::Center)), @@ -1585,6 +1675,68 @@ impl eframe::App for Editor { } } +fn alignment_icon( + ui: &mut egui::Ui, + rect: Rect, + title: &str, + horizontal: Option, + vertical: Option, +) -> egui::Response { + let response = ui.interact(rect, ui.id().with(title), egui::Sense::click()); + response.widget_info(|| { + egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), title) + }); + let p = ui.painter(); + p.rect( + rect, + 3.0, + if response.is_pointer_button_down_on() { + PURPLE + } else if response.hovered() { + PANEL + } else { + FIELD + }, + Stroke::new(1.0_f32, LINE), + StrokeKind::Inside, + ); + let c = rect.center(); + let stroke = Stroke::new(1.6_f32, if response.hovered() { CYAN } else { TEXT }); + let x = match horizontal { + Some(Alignment::Left) => -6.0, + Some(Alignment::Right) => 6.0, + _ => 0.0, + }; + let y = match vertical { + Some(Alignment::Top) => -6.0, + Some(Alignment::Bottom) => 6.0, + _ => 0.0, + }; + for (axis, value) in [(0, horizontal), (1, vertical)] { + if let Some(value) = value { + let offset = match value { + Alignment::Left | Alignment::Top => -12.0, + Alignment::Right | Alignment::Bottom => 12.0, + _ => 0.0, + }; + let ends = if axis == 0 { + [c + Vec2::new(offset, -12.0), c + Vec2::new(offset, 12.0)] + } else { + [c + Vec2::new(-12.0, offset), c + Vec2::new(12.0, offset)] + }; + p.line_segment(ends, stroke); + } + } + p.rect( + Rect::from_center_size(c + Vec2::new(x, y), Vec2::splat(8.0)), + 0.0, + FIELD, + stroke, + StrokeKind::Inside, + ); + response.on_hover_text(title) +} + #[derive(Clone, Copy)] enum ToolbarIcon { Screen, @@ -2519,11 +2671,32 @@ mod tests { assert_eq!(app.layout.selected(), Some(1)); assert!(app.marquee.is_none()); } + fn open_alignment_test_menu(app: &mut Editor, ctx: &egui::Context) -> egui::FullOutput { + frame(app, ctx, vec![]); + let pos = Pos2::new(200.0, 127.0); + frame( + app, + ctx, + vec![egui::Event::PointerMoved(pos), mouse(pos, true)], + ); + frame(app, ctx, vec![mouse(pos, false)]); + frame(app, ctx, vec![]) + } + fn alignment_test_position(output: &egui::FullOutput, title: &str) -> Pos2 { + let rects: Vec<_> = output + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::epaint::Shape::Rect(r) if r.rect.size() == Vec2::splat(40.0) => Some(r.rect), + _ => None, + }) + .collect(); + rects[if title == "Align top" { 1 } else { 7 }].center() + } #[test] fn top_and_bottom_buttons_align_rendered_edges_for_every_target() { for target in [ AlignTarget::Selection, - AlignTarget::Screen, AlignTarget::Layer(0), AlignTarget::Layer(1), ] { @@ -2561,17 +2734,8 @@ mod tests { app.align_target = target; let before = *app.layout.layers(); let ctx = egui::Context::default(); - let output = frame(&mut app, &ctx, vec![]); - let pos = output - .shapes - .iter() - .find_map(|s| match &s.shape { - egui::epaint::Shape::Text(t) if t.galley.job.text == title => { - Some(t.pos + Vec2::new(20.0, 6.0)) - } - _ => None, - }) - .unwrap(); + let output = open_alignment_test_menu(&mut app, &ctx); + let pos = alignment_test_position(&output, title); frame( &mut app, &ctx, @@ -2618,17 +2782,8 @@ mod tests { .unwrap(); app.layout.set_selection([true; 2]); let before = *app.layout.layers(); - let output = frame(&mut app, &ctx, vec![]); - let pos = output - .shapes - .iter() - .find_map(|s| match &s.shape { - egui::epaint::Shape::Text(t) if t.galley.job.text == "Align top" => { - Some(t.pos + Vec2::new(20.0, 6.0)) - } - _ => None, - }) - .unwrap(); + let output = open_alignment_test_menu(&mut app, &ctx); + let pos = alignment_test_position(&output, "Align top"); frame( &mut app, &ctx, diff --git a/crates/midra-layout/src/lib.rs b/crates/midra-layout/src/lib.rs index dc0f456..4e91590 100644 --- a/crates/midra-layout/src/lib.rs +++ b/crates/midra-layout/src/lib.rs @@ -466,6 +466,14 @@ impl Layout { } /// Align only selected rectangles; preserve size and the unrelated axis. pub fn align(&mut self, alignment: Alignment, target: AlignTarget) -> Result<(), String> { + self.align_axes(&[alignment], target) + } + /// Align one or both axes against one reference, with one undo record. + pub fn align_axes( + &mut self, + alignments: &[Alignment], + target: AlignTarget, + ) -> Result<(), String> { self.finish_drag(); let bounds = self.selection_bounds().ok_or("Select layers to align.")?; let reference = match target { @@ -476,12 +484,7 @@ impl Layout { width: self.formats.output[0] as f64, height: self.formats.output[1] as f64, }, - AlignTarget::Layer(i) => { - if !self.selected.get(i).copied().unwrap_or(false) { - return Err("Select the reference layer too.".into()); - } - self.layers[i] - } + AlignTarget::Layer(i) => *self.layers.get(i).ok_or("Unknown reference layer.")?, }; let mut next = self.layers; for i in self.selected_indices() { @@ -489,16 +492,18 @@ impl Layout { continue; } let g = &mut next[i]; - match alignment { - Alignment::Left => g.x = reference.x, - Alignment::Right => g.x = reference.x + reference.width - g.width, - Alignment::Top => g.y = reference.y, - Alignment::Bottom => g.y = reference.y + reference.height - g.height, - Alignment::HorizontalCenter => { - g.x = reference.x + (reference.width - g.width) / 2.0 - } - Alignment::VerticalCenter => { - g.y = reference.y + (reference.height - g.height) / 2.0 + for alignment in alignments { + match alignment { + Alignment::Left => g.x = reference.x, + Alignment::Right => g.x = reference.x + reference.width - g.width, + Alignment::Top => g.y = reference.y, + Alignment::Bottom => g.y = reference.y + reference.height - g.height, + Alignment::HorizontalCenter => { + g.x = reference.x + (reference.width - g.width) / 2.0 + } + Alignment::VerticalCenter => { + g.y = reference.y + (reference.height - g.height) / 2.0 + } } } } diff --git a/crates/midra-layout/tests/positioning.rs b/crates/midra-layout/tests/positioning.rs index 59b4575..47a4479 100644 --- a/crates/midra-layout/tests/positioning.rs +++ b/crates/midra-layout/tests/positioning.rs @@ -467,7 +467,7 @@ fn alignment_refuses_invalid_reference_and_failed_group_moves_are_atomic() { use midra_layout::{AlignTarget as T, Alignment as A}; let mut l = Layout::default(); let before = *l.layers(); - assert!(l.align(A::Top, T::Layer(0)).is_err()); + assert!(l.align(A::Top, T::Layer(99)).is_err()); assert_eq!(*l.layers(), before); l.select(None); assert!(l.align(A::Left, T::Screen).is_err()); @@ -700,3 +700,22 @@ fn midpoint_handles_fix_opposite_edge_and_preserve_undo_and_ratio() { } } } + +#[test] +fn corner_alignment_to_unselected_reference_is_one_undo_step() { + use midra_layout::{AlignTarget as T, Alignment as A}; + let mut l = Layout::default(); + let before = *l.layers(); + l.align_axes(&[A::Right, A::Bottom], T::Layer(0)).unwrap(); + assert_eq!(l.layers()[0], before[0]); + assert_eq!( + l.layers()[1].x + l.layers()[1].width, + before[0].x + before[0].width + ); + assert_eq!( + l.layers()[1].y + l.layers()[1].height, + before[0].y + before[0].height + ); + l.undo(); + assert_eq!(*l.layers(), before); +} diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 8b2fede..0bd24f5 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -136,21 +136,19 @@ clipboard testing against a running draw.io instance remains to be done. ## Align selected layers -Select both layers to reveal the alignment inspector. Choose an explicit target: +Open the **Align selected layers** icon menu beside the view/history toolbar. +The **Align to** dropdown defaults to **Selected layers** (their combined bounds). +It also lists **Layer 1: Presenter**, **Layer 2: Slides**, and any future layers. +A reference layer stays fixed and does not need to be selected. Screen alignment +is not offered in this menu. -- **Selection bounds** uses the selected rectangles' combined bounds at the time - of the action. Align left/top uses the minimum left/top; right/bottom uses the - maximum right/bottom. Selection order has no effect. -- **Screen** aligns to the screen edges or center lines. -- **Reference: Presenter / Slides** aligns to that selected layer's edges or - center lines. The named reference remains fixed. - -Use **Align left**, **Align right**, **Align top**, **Align bottom**, **Center X** -(horizontal centers) or **Center Y** (vertical centers). Each action changes only -the relevant position axis, never size, and is one undo step. The inspector lists -each layer's resulting center coordinates. Single-layer numeric fields are -replaced by alignment controls during multi-selection; no mixed values are shown -as if they belonged to one layer. Select exactly one layer to resize; paste/import accepts the full selection. +The compact icon grid offers four corners, top/bottom/left/right edges, center +on both axes, and separate horizontal/vertical centering. Edge buttons change +only their axis; corner buttons change both axes. Hover an icon for its label. +Every action preserves layer sizes and is one undo step, including corner moves. +The menu also works with one selected layer and a different reference layer. +The inspector lists each selected layer's resulting center coordinates; select +exactly one layer to resize. Paste/import accepts the full selection. Text inputs receive normal text paste and arrow navigation while focused; they never interpret these actions as geometry import or layer movement. Paste preview