From 4ec925d1fa2068b04921a6faab5d763210242499 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 14 Nov 2026 15:35:12 +0100 Subject: [PATCH] Group and reorder viewport toolbar controls --- crates/midra-layout/src/gui.rs | 265 +++++++++++++++++++-------------- docs/midralayout/changelog.md | 2 + 2 files changed, 152 insertions(+), 115 deletions(-) diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 1159a49..f5c296f 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -2055,62 +2055,27 @@ impl Editor { let ink = Ink::new(ui, r); let output = self.layout.formats().output; let [sw, sh] = output.map(|v| v as f32); - ink.card(24.0, 20.0, 36.0, 36.0, palette.field, palette.line); - if toolbar_icon( - ui, - Rect::from_min_size(ink.point(26.0, 22.0), Vec2::splat(32.0)), - "Output format", - ToolbarIcon::Output, - ) - .clicked() - { - self.output_draft = Some(OutputDraft::new(output)); - } - // Keep captions separate from the buttons, with room for their full names. let names = self.show_toolbar_icon_names; - let mut extra_height = if names { 22.0 } else { 0.0 }; - let (strip_x, strip_width, separator, positions, align_x, import_x, project_x, options_x) = - if names { - ( - 88.0, - 240.0, - 226.0, - [92.0, 164.0, 236.0, 292.0], - 356.0, - 420.0, - 476.0, - 536.0, - ) - } else { - ( - 72.0, - 150.0, - 147.0, - [76.0, 110.0, 152.0, 186.0], - 232.0, - 280.0, - 328.0, - 390.0, - ) - }; - let tools_wrapped = r.width() < options_x + 52.0 + 284.0 + 16.0; - let tools_x = if tools_wrapped { - 24.0 - } else { - options_x + 52.0 - }; - let tools_y = if tools_wrapped { - 62.0 + extra_height - } else { - 22.0 - }; - if tools_wrapped { - extra_height += if names { 56.0 } else { 40.0 }; + let (buttons, groups, toolbar_bottom) = toolbar_groups(r.width(), names); + for group in groups { + ink.card( + group.min.x, + group.min.y, + group.width(), + group.height(), + palette.field, + palette.line, + ); + } + let button = |index: usize| buttons[index].translate(r.min.to_vec2()); + let extra_height = toolbar_bottom - 56.0; + if toolbar_icon(ui, button(13), "Output format", ToolbarIcon::Output).clicked() { + self.output_draft = Some(OutputDraft::new(output)); } let modifiers = ui.input(|i| i.modifiers); ui.scope_builder( egui::UiBuilder::new() - .max_rect(Rect::from_min_size(ink.point(tools_x, tools_y), Vec2::new(64.0, 32.0))) + .max_rect(button(11)) .layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { ui.spacing_mut().interact_size.y = 32.0; @@ -2169,7 +2134,7 @@ impl Editor { } }, ); - let pivot_rect = Rect::from_min_size(ink.point(tools_x + 76.0, tools_y), Vec2::splat(32.0)); + let pivot_rect = button(12); if edit_tool_icon(ui, pivot_rect, None, "Pivot", self.pivot_tool, self.effective_pivot(modifiers)) .on_hover_text("Pivot resize — click to toggle. Hold Ctrl for the same effect; if selected, Ctrl temporarily disables it. Combines with either grid. Drag the pivot: snaps to 9 layer anchors and enabled grids/edges; Alt bypasses anchors. Double-click to center. Applies to layer and crop/zoom handles.") .clicked() @@ -2186,15 +2151,12 @@ impl Editor { ); } for (slot, tool, name, icon) in [ - (3, NavigationTool::Select, "Select", ToolbarIcon::Select), - (4, NavigationTool::Move, "Move", ToolbarIcon::Move), - (5, NavigationTool::Pan, "Pan", ToolbarIcon::Pan), - (6, NavigationTool::Zoom, "Zoom", ToolbarIcon::Zoom), + (4, NavigationTool::Select, "Select", ToolbarIcon::Select), + (5, NavigationTool::Move, "Move", ToolbarIcon::Move), + (6, NavigationTool::Pan, "Pan", ToolbarIcon::Pan), + (7, NavigationTool::Zoom, "Zoom", ToolbarIcon::Zoom), ] { - let rect = Rect::from_min_size( - ink.point(tools_x + 76.0 + (slot - 2) as f32 * 44.0, tools_y), - Vec2::splat(32.0), - ); + let rect = button(slot); if self.navigation_tool == tool { ui.painter().rect( rect, @@ -2217,22 +2179,13 @@ impl Editor { ); } } - ink.card( - strip_x, - 20.0, - strip_width, - 36.0, - palette.field, - palette.line, - ); - ink.line([separator, 28.0], [separator, 48.0], palette.line); - for (x, text, icon) in [ - (positions[0], "Fit screen", ToolbarIcon::Screen), - (positions[1], "Zoom to selection", ToolbarIcon::Selection), - (positions[2], "Undo", ToolbarIcon::Undo), - (positions[3], "Redo", ToolbarIcon::Redo), + for (slot, text, icon) in [ + (8, "Fit screen", ToolbarIcon::Screen), + (9, "Zoom to selection", ToolbarIcon::Selection), + (2, "Undo", ToolbarIcon::Undo), + (3, "Redo", ToolbarIcon::Redo), ] { - let rect = Rect::from_min_size(ink.point(x, 22.0), Vec2::splat(32.0)); + let rect = button(slot); if toolbar_icon(ui, rect, text, icon).clicked() { match text { "Fit screen" => self.fit_screen(), @@ -2250,13 +2203,10 @@ impl Editor { } ui.scope_builder( egui::UiBuilder::new() - .max_rect(Rect::from_min_size( - ink.point(align_x, 21.0), - Vec2::new(36.0, 34.0), - )) + .max_rect(button(10)) .layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { - ui.spacing_mut().interact_size = Vec2::new(36.0, 34.0); + ui.spacing_mut().interact_size = Vec2::new(36.0, 32.0); ui.spacing_mut().button_padding.x = 11.0; let menu = toolbar_menu(ui, " ", |ui| self.alignment_menu(ui)); let response = menu.response.on_hover_text("Align selected layers"); @@ -2293,13 +2243,10 @@ impl Editor { ); ui.scope_builder( egui::UiBuilder::new() - .max_rect(Rect::from_min_size( - ink.point(import_x, 21.0), - Vec2::new(36.0, 34.0), - )) + .max_rect(button(1)) .layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { - ui.spacing_mut().interact_size = Vec2::new(36.0, 34.0); + ui.spacing_mut().interact_size = Vec2::new(36.0, 32.0); ui.spacing_mut().button_padding.x = 11.0; let menu = toolbar_menu(ui, " ", |ui| self.import_menu(ui)); let response = menu @@ -2340,25 +2287,19 @@ impl Editor { ); ui.scope_builder( egui::UiBuilder::new() - .max_rect(Rect::from_min_size( - ink.point(project_x, 21.0), - Vec2::new(100.0, 34.0), - )) + .max_rect(button(0)) .layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { - ui.spacing_mut().interact_size.y = 34.0; + ui.spacing_mut().interact_size.y = 32.0; toolbar_menu(ui, "Project", |ui| self.project_menu(ui)); }, ); ui.scope_builder( egui::UiBuilder::new() - .max_rect(Rect::from_min_size( - ink.point(options_x, 21.0), - Vec2::new(36.0, 34.0), - )) + .max_rect(button(14)) .layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { - ui.spacing_mut().interact_size = Vec2::new(36.0, 34.0); + ui.spacing_mut().interact_size = Vec2::new(36.0, 32.0); ui.spacing_mut().button_padding.x = 11.0; let menu = toolbar_menu(ui, " ", |ui| self.viewport_options_menu(ui)); let response = menu.response.on_hover_text("Viewport options"); @@ -2386,18 +2327,18 @@ impl Editor { }, ); if names { - for (x, label) in [ - (42.0, "Output"), - (positions[0] + 16.0, "Fit screen"), - (positions[1] + 16.0, "Zoom to selection"), - (positions[2] + 16.0, "Undo"), - (positions[3] + 16.0, "Redo"), - (align_x + 18.0, "Align"), - (import_x + 18.0, "Import"), - (options_x + 18.0, "Options"), + for (slot, label) in [ + (13, "Output"), + (8, "Fit screen"), + (9, "Fit selection"), + (2, "Undo"), + (3, "Redo"), + (10, "Align"), + (1, "Import"), + (14, "Options"), ] { ui.painter().text( - ink.point(x, 61.0), + button(slot).center_bottom() + Vec2::new(0.0, 7.0), egui::Align2::CENTER_TOP, label, egui::FontId::proportional(11.0), @@ -3462,6 +3403,58 @@ enum ToolbarIcon { Redo, } +// Keep related tools together when wrapping; reserve caption space per group. +fn toolbar_groups(width: f32, captions: bool) -> ([Rect; 15], Vec, f32) { + let mut buttons = [Rect::NOTHING; 15]; + let mut groups = Vec::new(); + let mut x = 24.0; + let mut y = 20.0; + let height = if captions { 58.0 } else { 36.0 }; + for slots in [ + &[0, 1][..], + &[2, 3], + &[4, 5], + &[6, 7, 8, 9], + &[10, 11, 12], + &[13, 14], + ] { + let cell_width = |slot| match slot { + 0 => 68.0, + 11 => 56.0, + 8 | 9 if captions => 80.0, + _ if captions => 56.0, + 1 | 10 | 14 => 44.0, + _ => 36.0, + }; + let group_width = slots.iter().map(|&slot| cell_width(slot)).sum::() + 8.0; + if x > 24.0 && x + group_width > width - 16.0 { + x = 24.0; + y += height + 8.0; + } + groups.push(Rect::from_min_size( + Pos2::new(x, y), + Vec2::new(group_width, 36.0), + )); + let mut cursor = x + 4.0; + for &slot in slots { + let cell = cell_width(slot); + let button_width = match slot { + 0 => 64.0, + 11 => 52.0, + 1 | 10 | 14 => 40.0, + _ => 32.0, + }; + buttons[slot] = Rect::from_min_size( + Pos2::new(cursor + (cell - button_width) / 2.0, y + 2.0), + Vec2::new(button_width, 32.0), + ); + cursor += cell; + } + x += group_width + 10.0; + } + (buttons, groups, y + height) +} + const SNAP_MENU_TITLE: &str = " ▾"; // Match the original toolbar's dark surfaces without changing popup controls. @@ -3481,7 +3474,11 @@ fn toolbar_menu( ] { visuals.bg_fill = fill; visuals.weak_bg_fill = fill; - visuals.bg_stroke = Stroke::new(1.0_f32, border); + visuals.bg_stroke = if border == palette.line { + Stroke::NONE + } else { + Stroke::new(1.0_f32, border) + }; visuals.corner_radius = egui::CornerRadius::same(4); visuals.fg_stroke.color = if border == palette.cyan { palette.cyan @@ -4383,6 +4380,39 @@ mod tests { assert_eq!(app.layout.layers()[1].width, before[1].width); } + #[test] + fn toolbar_groups_wrap_without_splitting_or_overlapping_controls() { + for width in [600.0, 720.0, 1000.0, 1440.0] { + for captions in [false, true] { + let (buttons, groups, bottom) = toolbar_groups(width, captions); + for (i, group) in groups.iter().enumerate() { + assert!(group.right() <= width - 16.0); + assert!(group.bottom() <= bottom); + assert!(groups[..i].iter().all(|other| !other.intersects(*group))); + } + for (i, button) in buttons.iter().enumerate() { + assert!(groups.iter().any(|group| group.contains_rect(*button))); + assert!(buttons[..i].iter().all(|other| !other.intersects(*button))); + } + for slots in [ + &[0, 1][..], + &[2, 3], + &[4, 5], + &[6, 7, 8, 9], + &[10, 11, 12], + &[13, 14], + ] { + assert!( + slots + .windows(2) + .all(|pair| buttons[pair[0]].top() == buttons[pair[1]].top() + && buttons[pair[0]].right() < buttons[pair[1]].left()) + ); + } + } + } + } + #[test] fn tool_icons_fit_above_the_canvas_with_captions_and_at_minimum_window_size() { for size in [Vec2::new(1000.0, 720.0), Vec2::new(1440.0, 940.0)] { @@ -4402,7 +4432,7 @@ mod tests { ); } let canvas = ctx.read_response(egui::Id::new("canvas")).unwrap().rect; - let mut previous: Option = None; + let mut seen: Vec = Vec::new(); for (kind, label) in [ ("edit-tool", "Pivot"), ("navigation-tool", "Select"), @@ -4416,10 +4446,8 @@ mod tests { .rect; assert!(rect.right() <= canvas.right()); assert!(rect.bottom() + if captions { 18.0 } else { 0.0 } < canvas.top()); - if let Some(before) = previous { - assert!(rect.left() > before.right()); - } - previous = Some(rect); + assert!(seen.iter().all(|before| !before.intersects(rect))); + seen.push(rect); } } } @@ -4722,7 +4750,7 @@ mod tests { let pos = if context { Pos2::new(400.0, 700.0) } else { - Pos2::new(408.0, 82.0) + toolbar_test_position(&app, &ctx, 14) }; let button = if context { egui::PointerButton::Secondary @@ -5144,7 +5172,7 @@ mod tests { let before = app.layout.layers().clone(); let formats = app.layout.formats().clone(); frame(&mut app, &ctx, vec![]); - let pos = Pos2::new(408.0, 82.0); + let pos = toolbar_test_position(&app, &ctx, 14); frame( &mut app, &ctx, @@ -5410,7 +5438,7 @@ mod tests { for label in ["Import", "Clipboard values"] { let output = frame(&mut app, &ctx, vec![]); let pos = if label == "Import" { - Pos2::new(298.0, 82.0) + toolbar_test_position(&app, &ctx, 1) } else { output .shapes @@ -6797,9 +6825,16 @@ mod tests { assert_eq!(app.layout.selected(), Some(1)); assert!(app.marquee.is_none()); } + fn toolbar_test_position(app: &Editor, ctx: &egui::Context, slot: usize) -> Pos2 { + let canvas = ctx.read_response(egui::Id::new("canvas")).unwrap().rect; + let (buttons, _, bottom) = + toolbar_groups(canvas.width() + 16.0, app.show_toolbar_icon_names); + buttons[slot].center() + Vec2::new(canvas.left() - 8.0, canvas.top() - bottom - 8.0) + } + fn open_alignment_test_menu(app: &mut Editor, ctx: &egui::Context) -> egui::FullOutput { frame(app, ctx, vec![]); - let pos = Pos2::new(248.0, 82.0); + let pos = toolbar_test_position(app, ctx, 10); frame( app, ctx, diff --git a/docs/midralayout/changelog.md b/docs/midralayout/changelog.md index 2717887..8c17401 100644 --- a/docs/midralayout/changelog.md +++ b/docs/midralayout/changelog.md @@ -4,6 +4,8 @@ ## Unreleased +- Reorder the viewport toolbar into shared bordered groups: Project/Import, Undo/Redo, Select/Move, view controls, Align/Snap/Pivot, and Output/Options. Groups wrap together on narrow windows and preserve optional icon captions. + - Snap moving layers, resize handles and pivots to screen center and quarter guides on both axes. Moving layer centers also snap to these interior guides; the existing screen snapping option controls all screen guides. - Use a crisp, theme-aware magnet icon for the snapping dropdown; preserve snapping controls and active grid indicators.