Group view and history actions into an icon toolbar

This commit is contained in:
Dandaedre 2026-11-13 13:58:12 +01:00
parent 23f927914a
commit fe172dab79
2 changed files with 111 additions and 9 deletions

View File

@ -971,14 +971,18 @@ impl Editor {
12.0,
MUTED,
);
for (x, w, text) in [
(24.0, 103.0, "Fit screen"),
(139.0, 103.0, "Fit selected"),
(254.0, 80.0, "Undo"),
(346.0, 80.0, "Redo"),
for (x, width, title) in [(24.0, 184.0, "VIEW"), (220.0, 206.0, "HISTORY")] {
ink.card(x, 64.0, width, 40.0, FIELD, LINE);
ink.text(x + 12.0, 78.0, title, 10.0, MUTED);
}
for (x, text, icon) in [
(120.0, "Fit screen", ToolbarIcon::Screen),
(162.0, "Fit selected", ToolbarIcon::Selection),
(340.0, "Undo", ToolbarIcon::Undo),
(382.0, "Redo", ToolbarIcon::Redo),
] {
ink.button(x, 67.0, w, text, false);
if click(ui, &ink, [x, 67.0, w, 34.0], text) {
let rect = Rect::from_min_size(ink.point(x, 67.0), Vec2::splat(34.0));
if toolbar_icon(ui, rect, text, icon).clicked() {
match text {
"Fit screen" => {
self.zoom = 1.0;
@ -1529,6 +1533,103 @@ impl eframe::App for Editor {
}
}
#[derive(Clone, Copy)]
enum ToolbarIcon {
Screen,
Selection,
Undo,
Redo,
}
fn toolbar_icon(ui: &mut egui::Ui, rect: Rect, label: &str, icon: ToolbarIcon) -> egui::Response {
let response = ui.interact(rect, ui.id().with(("toolbar", label)), egui::Sense::click());
response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, label));
let painter = ui.painter();
let focused = response.hovered() || response.has_focus();
if focused || response.is_pointer_button_down_on() {
painter.rect_filled(
rect.shrink(1.0),
4.0,
if response.is_pointer_button_down_on() {
PURPLE
} else {
PANEL
},
);
}
let stroke = Stroke::new(1.6_f32, if focused { CYAN } else { TEXT });
let c = rect.center();
let point = |x, y| c + Vec2::new(x, y);
match icon {
ToolbarIcon::Screen => {
painter.rect_stroke(
Rect::from_center_size(point(0.0, -2.0), Vec2::new(20.0, 14.0)),
2.0,
stroke,
StrokeKind::Inside,
);
painter.line_segment([point(0.0, 5.0), point(0.0, 9.0)], stroke);
painter.line_segment([point(-5.0, 9.0), point(5.0, 9.0)], stroke);
}
ToolbarIcon::Selection => {
for (x, y) in [(-1.0, -1.0), (1.0, -1.0), (-1.0, 1.0), (1.0, 1.0)] {
painter.add(egui::Shape::line(
vec![
point(x * 5.0, y * 10.0),
point(x * 10.0, y * 10.0),
point(x * 10.0, y * 5.0),
],
stroke,
));
}
painter.rect_stroke(
Rect::from_center_size(c, Vec2::new(8.0, 6.0)),
0.0,
stroke,
StrokeKind::Inside,
);
}
ToolbarIcon::Undo | ToolbarIcon::Redo => {
let sign = if matches!(icon, ToolbarIcon::Undo) {
1.0
} else {
-1.0
};
let points = [
(-8.0, -4.0),
(-3.0, -7.0),
(3.0, -7.0),
(7.0, -4.0),
(9.0, 0.0),
(8.0, 5.0),
(4.0, 8.0),
(0.0, 8.0),
];
painter.add(egui::Shape::line(
points
.into_iter()
.map(|(x, y)| point(x * sign, y))
.collect(),
stroke,
));
painter.add(egui::Shape::line(
vec![
point(-8.0 * sign, -10.0),
point(-8.0 * sign, -4.0),
point(-2.0 * sign, -4.0),
],
stroke,
));
}
}
response.on_hover_text(match icon {
ToolbarIcon::Screen => "Fit screen — show the entire output",
ToolbarIcon::Selection => "Fit selected — frame the selected layers",
ToolbarIcon::Undo => "Undo (Ctrl/Cmd+Z)",
ToolbarIcon::Redo => "Redo (Ctrl/Cmd+Shift+Z)",
})
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -116,11 +116,12 @@ clipboard testing against a running draw.io instance remains to be done.
corner's offsets. Invalid values leave the document unchanged.
- **Escape** cancels a drag or restores uncommitted numeric fields. In the paste
dialog it closes the preview without applying it. Group drags cancel atomically.
- **Undo/Redo** buttons or Ctrl/Cmd-Z, Ctrl/Cmd-Shift-Z (also Ctrl/Cmd-Y) reverse
- The **History** toolbar icons (**Undo/Redo**) or Ctrl/Cmd-Z, Ctrl/Cmd-Shift-Z (also Ctrl/Cmd-Y) reverse
edits. Each drag, alignment, applied paste or numeric edit is one step; retain 100 undo
records. View changes and selection are not document edits.
- Scroll over the viewport to zoom around the pointer. Space-drag or middle-drag
pans. **Fit screen** resets the view; **Fit selected** frames the entire selection.
pans. In the **View** toolbar group, **Fit screen** resets the view and
**Fit selected** frames the entire selection. Hover an icon for its label.
Camera changes do not modify coordinates. Off-screen layers retain their exact
coordinates, display a warning and remain selectable through the layer list.
- **Copy position values** (or **Copy selected positions** for multiple layers)