Files
Ely-GPUI-Components/src/canvas/history.rs
T
ZacharyZhang-NY c474a3eb33 Canvas · Tools: select, move and snap, resize, draw, pen, text, brush
ToolLayer lies over an InfiniteCanvas and carries its tools. Select
takes the topmost shape a press reaches, Shift adds or drops one, a
drag moves the selection and snaps its nearest edge or middle to
another shape's within reach, a handle resizes it, Shift keeping a
corner's shape, and a drag on empty space draws a SelectionBox that
takes what lies wholly inside. The shape tools draw by a drag toward
any corner, Shift keeping them square; the pen adds a point per press
and a double press ends the path; text drops a line where pressed; the
brush strokes a path its width. A drag may end in the layer or out of
it; each gesture ends once and asks the owner. SelectionBox,
TransformHandles and SnapIndicator draw the marks.

ToolPalette picks a tool from icons named in tips; BrushSettings sets a
brush's width and hue; HistoryPanel lists the steps done, the undone
ones quiet, and takes the drawing to a step on Enter or a double press.

The model changes: lines and arrows end at a named corner, so they run
either way, and paths carry their width. Three Lucide icons: hexagon,
slash and move-up-right.
2026-09-27 07:27:27 -04:00

86 lines
2.7 KiB
Rust

use std::rc::Rc;
use gpui::{App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Window, div};
use crate::{
lists::{ListItem, SelectableList},
primitives::{Icon, IconName},
theme::{ActiveTheme, IconSize},
};
type OnStep = Rc<dyn Fn(usize, &mut Window, &mut App)>;
/// What was done, oldest first, with the document at step `at`: the steps after it were undone and read quiet. Enter or a double press on a step takes the document back or forward to it.
#[derive(IntoElement)]
pub struct HistoryPanel {
id: ElementId,
steps: Vec<SharedString>,
at: usize,
on_step: Option<OnStep>,
}
impl HistoryPanel {
/// `at` counts the steps in force; none undone is `steps.len()`.
pub fn new(
id: impl Into<ElementId>,
steps: impl IntoIterator<Item = impl Into<SharedString>>,
at: usize,
) -> Self {
let steps: Vec<SharedString> = steps.into_iter().map(Into::into).collect();
assert!(
at <= steps.len(),
"history at {at} of {} steps",
steps.len()
);
Self {
id: id.into(),
steps,
at,
on_step: None,
}
}
/// Gets how many steps to keep in force.
pub fn on_step(mut self, handler: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
self.on_step = Some(Rc::new(handler));
self
}
}
impl RenderOnce for HistoryPanel {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let at = self.at;
let rows = self.steps.into_iter().enumerate().fold(
SelectableList::new((self.id.clone(), "steps")),
|list, (ix, step)| {
let done = ix < at;
let mark = Icon::new(if done {
IconName::Check
} else {
IconName::RotateCcw
})
.size(IconSize::Sm)
.color(if done {
theme.colors.fg_muted
} else {
theme.colors.fg_subtle
});
let row = ListItem::new((self.id.clone(), format!("step-{ix}")), step)
.leading(mark)
.quiet(!done)
.current(ix + 1 == at);
list.row(format!("{ix}"), row)
},
);
let on_step = self.on_step;
div().child(rows.on_activate(move |key, window, cx| {
let ix: usize = key.parse().expect("a step's key is its place");
log::info!("history panel: back to step {}", ix + 1);
if let Some(on_step) = &on_step {
on_step(ix + 1, window, cx);
}
}))
}
}