Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

A: a box around points can start over another shape

Indicode BVf4b16ed28b9f
diff --git a/crates/app/src/editor.rs b/crates/app/src/editor.rs
index f2310a6..42ea2f0 100644
--- a/crates/app/src/editor.rs
+++ b/crates/app/src/editor.rs
@@ -687,6 +687,7 @@ pub(crate) struct Editor {
direct: Option<Direct>,
shaped: Option<Shaped>,
point_drag: Option<PointDrag>,
+ pick_later: bool,
path_commit: Option<PathCommit>,
simplify_px: f32,
under: Vec<String>,
@@ -865,6 +866,7 @@ impl Editor {
direct: None,
shaped: None,
point_drag: None,
+ pick_later: false,
path_commit: None,
simplify_px: 1.5,
under: Vec::new(),
@@ -2080,6 +2082,7 @@ impl Editor {
fn direct_down(&mut self, view: View, rect: Rect, pos: Pos2, c: Vec2) -> Result<()> {
let (alt, shift) = (self.modifiers.alt, self.modifiers.shift);
+ self.pick_later = false;
enum Action {
Handle(usize, Side),
Anchor(usize, bool),
@@ -2152,59 +2155,87 @@ impl Editor {
}
Action::Insert(segment, t) => return self.commit_path(PathEdit::Insert { segment, t }),
Action::Elsewhere => {
- self.under = overlay::leaf_hits(&self.scene.bundle.doc, self.scene.frame, (c.x, c.y))?;
- let zoom = self.view.map_or(1.0, |v| v.zoom);
- let mut on_stroke = false;
- if self.under.is_empty()
- && let Some((path, mark)) = self.mark_at(c, 5.0 / zoom)
- {
- if path != self.scene.active {
- self.activate(path)?;
- }
- let picked = self
- .scene
- .active_marks()
- .iter()
- .find(|m| m.id() == mark)
- .map(|m| m.as_stroke().is_some());
- match (picked, self.scene.active_cel()) {
- (Some(true), Some(cel)) => {
- let address = markedit::address(&cel, mark);
- if self.direct.as_ref().map(|d| &d.node) != Some(&address) {
- self.pick_shape(address);
- return Ok(());
- }
- on_stroke = true;
- }
- (Some(false), _) => {
- self.say(Tone::Info, "that is a fill: A reshapes strokes and vector shapes")
- }
- _ => {}
+ let on_shape = match self.direct.is_some() && !self.on_picked_shape(c)? {
+ true => {
+ self.pick_later = true;
+ Some(false)
}
- }
- let current = self.direct.as_ref().map(|d| d.node.clone());
- let hit = match &current {
- Some(id) if self.under.contains(id) => Some(id.clone()),
- _ => self.under.first().cloned(),
+ false => self.pick_elsewhere(c)?,
};
- match hit {
- Some(id) if Some(&id) != current.as_ref() => self.pick_shape(id),
- hit => {
- self.point_drag = Some(PointDrag {
- grab: PointGrab::Marquee {
- on_shape: hit.is_some() || on_stroke,
- },
- start: c,
- now: c,
- free: false,
- });
- }
+ if let Some(on_shape) = on_shape {
+ self.point_drag = Some(PointDrag {
+ grab: PointGrab::Marquee { on_shape },
+ start: c,
+ now: c,
+ free: false,
+ });
}
}
}
Ok(())
}
+ fn on_picked_shape(&self, c: Vec2) -> Result<bool> {
+ let Some(current) = self.direct.as_ref().map(|d| &d.node) else {
+ return Ok(false);
+ };
+ let under = overlay::leaf_hits(&self.scene.bundle.doc, self.scene.frame, (c.x, c.y))?;
+ if !under.is_empty() {
+ return Ok(under.contains(current));
+ }
+ let zoom = self.view.map_or(1.0, |v| v.zoom);
+ Ok(self.mark_at(c, 5.0 / zoom).is_some_and(|(path, mark)| {
+ path == self.scene.active
+ && self
+ .scene
+ .active_cel()
+ .is_some_and(|cel| markedit::address(&cel, mark) == *current)
+ }))
+ }
+
+ fn pick_elsewhere(&mut self, c: Vec2) -> Result<Option<bool>> {
+ self.under = overlay::leaf_hits(&self.scene.bundle.doc, self.scene.frame, (c.x, c.y))?;
+ let zoom = self.view.map_or(1.0, |v| v.zoom);
+ let mut on_stroke = false;
+ if self.under.is_empty()
+ && let Some((path, mark)) = self.mark_at(c, 5.0 / zoom)
+ {
+ if path != self.scene.active {
+ self.activate(path)?;
+ }
+ let picked = self
+ .scene
+ .active_marks()
+ .iter()
+ .find(|m| m.id() == mark)
+ .map(|m| m.as_stroke().is_some());
+ match (picked, self.scene.active_cel()) {
+ (Some(true), Some(cel)) => {
+ let address = markedit::address(&cel, mark);
+ if self.direct.as_ref().map(|d| &d.node) != Some(&address) {
+ self.pick_shape(address);
+ return Ok(None);
+ }
+ on_stroke = true;
+ }
+ (Some(false), _) => self.say(Tone::Info, "that is a fill: A reshapes strokes and vector shapes"),
+ _ => {}
+ }
+ }
+ let current = self.direct.as_ref().map(|d| d.node.clone());
+ let hit = match &current {
+ Some(id) if self.under.contains(id) => Some(id.clone()),
+ _ => self.under.first().cloned(),
+ };
+ match hit {
+ Some(id) if Some(&id) != current.as_ref() => {
+ self.pick_shape(id);
+ Ok(None)
+ }
+ hit => Ok(Some(hit.is_some() || on_stroke)),
+ }
+ }
+
fn pen_down(&mut self, view: View, rect: Rect, pos: Pos2, c: Vec2) -> Result<()> {
let vector = self
.scene
@@ -2389,6 +2420,13 @@ impl Editor {
let moved = (drag.now - drag.start).length() * zoom > 2.0;
match drag.grab {
PointGrab::Marquee { on_shape } => {
+ let later = std::mem::take(&mut self.pick_later);
+ if !moved && later {
+ if self.pick_elsewhere(drag.start)? == Some(false) {
+ self.direct = None;
+ }
+ return Ok(());
+ }
if !moved {
let current = self.direct.as_ref().map(|d| d.node.clone());
let below = current
@@ -3796,7 +3834,7 @@ impl Editor {
}
}
ui.small(
- "drag points or handles; shift click adds a point to the selection, drag on empty canvas selects in a box\n\
+ "drag points or handles; drag a box around points to select them, shift adds to the selection; drag one to move them all\n\
alt click the outline: new point; alt click a point: corner or smooth; alt drag a handle: break it",
);
if self.path_commit.is_some() {
diff --git a/crates/app/src/editor/ui_tests.rs b/crates/app/src/editor/ui_tests.rs
index 4b370e7..01fab7e 100644
--- a/crates/app/src/editor/ui_tests.rs
+++ b/crates/app/src/editor/ui_tests.rs
@@ -2539,6 +2539,75 @@ fn the_brush_window_edits_a_brush_shows_it_and_saves_it_in_the_drawing() {
let _ = std::fs::remove_dir_all(&path);
}
+#[test]
+fn a_box_dragged_from_another_shape_picks_points_and_shift_adds_to_them() {
+ let path = sample("point-box");
+ let mut bundle = Bundle::open(&path).unwrap();
+ let shapes = bundle.doc.find_layer("shapes").unwrap().id.clone();
+ bundle
+ .apply(
+ &parse_lines(&format!(
+ r##"{{"op":"add","node":{{"type":"rect","x":0,"y":0,"w":512,"h":384,"fill":"#203040"}},"in":"{shapes}","frame":0}}
+{{"op":"add","node":{{"type":"rect","x":200,"y":150,"w":80,"h":60,"fill":"#d0b040"}},"in":"{shapes}","frame":0}}"##
+ ))
+ .unwrap(),
+ )
+ .unwrap();
+ let (back, small) = {
+ let nodes = &bundle.doc.find_layer("shapes").unwrap().cels()[0].nodes;
+ (nodes[nodes.len() - 2].id.clone(), nodes[nodes.len() - 1].id.clone())
+ };
+ let mut h = harness(editor(&path), vec2(1440.0, 900.0));
+ activate(&mut h, "shapes");
+ h.key_press(Key::A);
+ h.run_steps(2);
+ let view = h.state().view.unwrap();
+ let rect = h.state().canvas_rect;
+ let at = |x: f32, y: f32| rect.min + view.to_screen(egui::Vec2::new(x, y), rect.size());
+ let picked = |h: &Harness<'static, Editor>| {
+ let ed = h.state();
+ let direct = ed.direct.clone().unwrap();
+ let anchors = klad_core::pathedit::anchors(&ed.shaped.as_ref().unwrap().subs);
+ let ys: Vec<f32> = direct.points.iter().map(|&i| anchors[i].1.at[1]).collect();
+ (direct.node, ys)
+ };
+ click_with(&mut h, at(240.0, 180.0), Modifiers::NONE);
+ assert_eq!(picked(&h), (small.clone(), vec![]));
+ drag(&mut h, at(190.0, 140.0), at(290.0, 170.0));
+ h.run_steps(2);
+ assert_eq!(
+ picked(&h),
+ (small.clone(), vec![150.0, 150.0]),
+ "a box from the shape behind"
+ );
+ h.event(egui::Event::ModifiersChanged(Modifiers::SHIFT));
+ drag(&mut h, at(190.0, 195.0), at(290.0, 225.0));
+ h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
+ h.run_steps(2);
+ let (node, mut ys) = picked(&h);
+ ys.sort_by(f32::total_cmp);
+ assert_eq!(
+ (node, ys),
+ (small.clone(), vec![150.0, 150.0, 210.0, 210.0]),
+ "shift adds the box"
+ );
+ let corner = at(200.0, 150.0);
+ drag(&mut h, corner, corner + vec2(0.0, -30.0));
+ settle(&mut h, |ed| ed.point_drag.is_none() && ed.path_commit.is_none());
+ h.run_steps(2);
+ let moved = klad_core::pathedit::anchors(&h.state().shaped.as_ref().unwrap().subs)
+ .iter()
+ .map(|(_, a)| a.at[1])
+ .fold(f32::MAX, f32::min);
+ assert!(
+ (moved - (150.0 - 30.0 / view.zoom)).abs() < 1.0,
+ "all four moved together, top at {moved}"
+ );
+ click_with(&mut h, at(60.0, 300.0), Modifiers::NONE);
+ assert_eq!(picked(&h), (back, vec![]), "a click on the shape behind still picks it");
+ let _ = std::fs::remove_dir_all(&path);
+}
+
#[test]
fn a_bends_a_brush_stroke_and_the_stroke_keeps_its_pressure() {
let path = sample("reshape");
diff --git a/docs/guide/shapes-and-text.md b/docs/guide/shapes-and-text.md
index 44a09f8..c980e0a 100644
--- a/docs/guide/shapes-and-text.md
+++ b/docs/guide/shapes-and-text.md
@@ -1,4 +1,4 @@
-_Last updated: 2026-11-08 14:50_
+_Last updated: 2026-11-10 09:30_
# Shapes, paths and text
@@ -16,6 +16,8 @@ Press **P**, click to place corners and drag to pull out curves. Click the first
Press **A**, click the shape and drag its points and handles. Alt click on the outline adds a point.
+To move several points together, drag a box around them, then drag one of them. **Shift** adds a box or a click to the points you have.
+
## Fill a shape with a gradient
Select the shape with **V**, then in layer settings switch its fill to **linear** or **radial** and edit the stops.