Group snapping tools in a toolbar dropdown

This commit is contained in:
Dandaedre 2026-11-14 12:49:06 +01:00
parent 59a342cbc3
commit a7038e5f0a
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
3 changed files with 67 additions and 50 deletions

View File

@ -2040,7 +2040,7 @@ impl Editor {
390.0,
)
};
let tools_wrapped = r.width() < options_x + 52.0 + 264.0 + 16.0;
let tools_wrapped = r.width() < options_x + 52.0 + 240.0 + 16.0;
let tools_x = if tools_wrapped {
24.0
} else {
@ -2055,53 +2055,60 @@ impl Editor {
extra_height += if names { 56.0 } else { 40.0 };
}
let modifiers = ui.input(|i| i.modifiers);
for (slot, grid, label, tooltip) in [
(
0,
Some(10),
"10 px",
"10 px grid — click to toggle. Hold Shift for the same effect; if selected, Shift temporarily disables it. Ctrl+Shift switches to 100 px. Alt overrides grids.",
),
(
1,
Some(100),
"100 px",
"100 px grid — click to toggle. Hold Ctrl+Shift for the same effect; if selected, Ctrl+Shift temporarily disables it. Shift switches to 10 px. Alt overrides grids.",
),
(
2,
None,
ui.scope_builder(
egui::UiBuilder::new()
.max_rect(Rect::from_min_size(ink.point(tools_x, tools_y), Vec2::new(64.0, 32.0)))
.layout(egui::Layout::left_to_right(egui::Align::Center)),
|ui| {
ui.spacing_mut().interact_size.y = 32.0;
let title = match self.effective_grid(modifiers) {
Some(10) => "10 px ▾",
Some(100) => "100 px ▾",
_ => "Snap ▾",
};
toolbar_menu(ui, title, |ui| {
ui.label("Grid tools");
for (spacing, label, shortcut) in [(10, "10 px", "Shift"), (100, "100 px", "Ctrl+Shift")] {
ui.horizontal(|ui| {
let (_, rect) = ui.allocate_space(Vec2::splat(32.0));
let selected = self.grid_tool == Some(spacing);
let effective = self.effective_grid(modifiers) == Some(spacing);
let icon = edit_tool_icon(ui, rect, Some(spacing), label, selected, effective);
let text = ui.selectable_label(selected, format!("{label} grid"));
let tooltip = format!("Toggle {label} grid. Hold {shortcut} for the same effect; when selected, the modifier temporarily disables it. Alt overrides grids.");
if icon.on_hover_text(&tooltip).clicked() || text.on_hover_text(tooltip).clicked() {
self.grid_tool = if selected { None } else { Some(spacing) };
ui.close_menu();
}
});
}
ui.separator();
let before = self.snapping;
self.snapping_options(ui);
if before != self.snapping {
self.settings.save(&self.application_settings(ui.ctx()));
}
if let Some(error) = &self.settings.error {
ui.colored_label(ui.visuals().error_fg_color, error);
}
}).response.on_hover_text("Snapping tools and options. Shift: 10 px · Ctrl+Shift: 100 px · Alt: invert snapping.");
},
);
let pivot_rect = Rect::from_min_size(ink.point(tools_x + 76.0, tools_y), Vec2::splat(32.0));
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 handles.")
.clicked()
{
self.pivot_tool = !self.pivot_tool;
}
if names {
ui.painter().text(
pivot_rect.center_bottom() + Vec2::new(0.0, 7.0),
Align2::CENTER_TOP,
"Pivot",
"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 handles.",
),
] {
let rect = Rect::from_min_size(
ink.point(tools_x + slot as f32 * 44.0, tools_y),
Vec2::splat(32.0),
FontId::proportional(11.0),
palette.muted,
);
let selected = grid.map_or(self.pivot_tool, |n| self.grid_tool == Some(n));
let effective = grid.map_or(self.effective_pivot(modifiers), |n| {
self.effective_grid(modifiers) == Some(n)
});
if edit_tool_icon(ui, rect, grid, label, selected, effective)
.on_hover_text(tooltip)
.clicked()
{
if let Some(n) = grid {
self.grid_tool = if selected { None } else { Some(n) };
} else {
self.pivot_tool = !self.pivot_tool;
}
}
if names {
ui.painter().text(
rect.center_bottom() + Vec2::new(0.0, 7.0),
Align2::CENTER_TOP,
label,
FontId::proportional(11.0),
palette.muted,
);
}
}
for (slot, tool, name, icon) in [
(3, NavigationTool::Move, "Move", ToolbarIcon::Move),
@ -2109,7 +2116,7 @@ impl Editor {
(5, NavigationTool::Zoom, "Zoom", ToolbarIcon::Zoom),
] {
let rect = Rect::from_min_size(
ink.point(tools_x + slot as f32 * 44.0, tools_y),
ink.point(tools_x + 76.0 + (slot - 2) as f32 * 44.0, tools_y),
Vec2::splat(32.0),
);
if self.navigation_tool == tool {
@ -4217,8 +4224,6 @@ mod tests {
let canvas = ctx.read_response(egui::Id::new("canvas")).unwrap().rect;
let mut previous: Option<Rect> = None;
for (kind, label) in [
("edit-tool", "10 px"),
("edit-tool", "100 px"),
("edit-tool", "Pivot"),
("navigation-tool", "Move"),
("navigation-tool", "Pan"),
@ -4292,6 +4297,14 @@ mod tests {
("100 px", None, true),
("Pivot", None, false),
] {
if label != "Pivot" {
let title = match app.grid_tool {
Some(10) => "10 px ▾",
Some(100) => "100 px ▾",
_ => "Snap ▾",
};
click_text(&mut app, &ctx, title);
}
frame(&mut app, &ctx, vec![]);
let pos = ctx
.read_response(egui::Id::new(("edit-tool", label)))

View File

@ -342,7 +342,9 @@ or document history. Existing project saves still preserve zoom/pan values.
### Persistent editing tools
The toolbar has **10 px grid**, **100 px grid**, and **Pivot** icon toggles.
The toolbar’s **Snap ▾** dropdown groups the **10 px grid** and **100 px grid**
icon toggles with the snapping options. Its label shows the effective grid,
including modifier overrides. **Pivot** stays beside it as an independent toggle.
Click again to turn a tool off. Only one grid can stay selected; Pivot can combine
with either. A cyan outline marks the selected toggle; the fill shows its current
effect, including temporary modifier changes. Tooltips explain the shortcuts.

View File

@ -4,6 +4,8 @@
## Unreleased
- Group grid snapping tools and snapping options in a toolbar dropdown; the label shows the active grid. Pivot remains independently accessible.
- Save gravity points per layer across selection changes and reopening; placement/reset supports Undo. Project v9 migrates older files to centered pivots.
- Pivot dragging snaps to corners, side midpoints and center even with snapping off; also honors grid/pixel/screen/layer options. Alt bypasses built-in anchors; guides and linked pivots follow the result.