mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:51:05 +01:00
Selection: Cmd/Ctrl lends Direct Selection until it is released
With the Selection tool chosen, holding Cmd (Ctrl on Windows and Linux) now lends Direct Selection, or Group Selection when that was chosen last, for each press; the Selection tool is back on the release. With Group Selection chosen, Cmd lends the Selection tool, as it already did with Direct Selection. Selection & Anchor Display > Command Click to Select Objects Behind keeps a Cmd press on a selected object, away from its anchors, handles, mesh points and spine, for selecting the object behind it. A wide stroke's painted area counts as on the object there. - The double-click or triple-click after Cmd-clicks goes to the tool the clicks went to. - Cmd pressed once a drag is under way lends nothing, and the pointer stays the active tool's. - While Cmd is held, the status bar's Current Tool and the help bar name the lent tool, and the canvas shows the lent tool's marks and widgets (blend, Shaper, mirror, trace divider and repeat handles) for the pointer's position in the same frame. - The help bar has hints for Group Selection. The helper that finds a selected object's editable parts under the pointer follows the one in pull request 6597 by SofiaAFigueredo. Docs: docs/ui-parity.md, docs/control-protocol.md (ui.pointer). Co-authored-by: SofiaAFigueredo <223278896+SofiaAFigueredo@users.noreply.github.com>
This commit is contained in:
1 parent
9fff48d88f
commit
dcff4edc3d
19 files changed
+532
-76
No files matched your search
@@ -385,23 +385,26 @@ pub(crate) fn in_scope(doc: &vectorcraft_doc::Document, scope: Option<NodeId>, i
|
||||
/// shapes, its first child, are left out unless they are the isolated object (Construction mode),
|
||||
/// as clicks leave them out.
|
||||
fn walk_pickable<'a>(doc: &'a vectorcraft_doc::Document, scope: Option<NodeId>, mut f: impl FnMut(&'a Node)) {
|
||||
fn walk<'a>(n: &'a Node, f: &mut impl FnMut(&'a Node)) {
|
||||
f(n);
|
||||
for c in n.children().into_iter().flatten().skip(usize::from(n.extras().shaper.is_some())) {
|
||||
walk(c, f);
|
||||
}
|
||||
}
|
||||
if let Some(id) = scope
|
||||
&& let Some(n) = doc.node(id)
|
||||
{
|
||||
walk(n, &mut f);
|
||||
walk_pickable_from(n, &mut f);
|
||||
} else if scope.is_none() {
|
||||
for l in &doc.layers {
|
||||
walk(l, &mut f);
|
||||
walk_pickable_from(l, &mut f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Visit `n` and the nodes inside it that Direct Selection may pick: a Shaper Group's original
|
||||
/// shapes are left out ([`walk_pickable`]).
|
||||
fn walk_pickable_from<'a>(n: &'a Node, f: &mut impl FnMut(&'a Node)) {
|
||||
f(n);
|
||||
for c in n.children().into_iter().flatten().skip(usize::from(n.extras().shaper.is_some())) {
|
||||
walk_pickable_from(c, f);
|
||||
}
|
||||
}
|
||||
|
||||
/// The spines of the editable blends, topmost first: (blend, spine).
|
||||
pub(crate) fn spines(cx: &ToolContext) -> Vec<(NodeId, PathData)> {
|
||||
let mut v = vec![];
|
||||
@@ -594,6 +597,56 @@ fn hit_anchor(cx: &ToolContext, p: Point, tol: f64) -> Option<(NodeId, usize, us
|
||||
})
|
||||
}
|
||||
|
||||
/// Is `p` on a part of a selected object that Direct Selection edits: a gradient mesh point, a
|
||||
/// blend's spine point or segment, an anchor, the end of a direction handle or a segment, within
|
||||
/// the selection tolerance? A selected group or compound path counts through the editable paths
|
||||
/// inside it.
|
||||
pub fn over_selected_part(cx: &ToolContext, p: Point) -> bool {
|
||||
if cx.selection.objects.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let tol = cx.point_tol();
|
||||
let seg_tol = cx.pick_tol();
|
||||
if hit_handle(cx, p, tol).is_some()
|
||||
|| MeshEdit::default().hit(cx, &cx.selection.objects, p).is_some()
|
||||
|| selected_spines(cx).iter().any(|(_, path)| path.anchors().any(|(_, _, a)| a.p.distance(p) <= tol))
|
||||
|| hit_spine_segment(cx, p, seg_tol).is_some()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// The editable paths' control bounds are listed once per document state and selection; a
|
||||
// hover measures only the paths whose bounds it is near.
|
||||
let boxes = crate::select::selected_part_boxes(cx, || {
|
||||
let editable = cx.doc.editable();
|
||||
let mut v = vec![];
|
||||
for root in cx.selection.objects.iter().filter_map(|&id| cx.doc.node(id)) {
|
||||
walk_pickable_from(root, &mut |n| {
|
||||
if editable(n.id)
|
||||
&& let Some(b) = editable_path(n).and_then(|pd| pd.control_bounds())
|
||||
{
|
||||
v.push((n.id, b));
|
||||
}
|
||||
});
|
||||
}
|
||||
v.into()
|
||||
});
|
||||
let reach = tol.max(seg_tol);
|
||||
boxes.iter().any(|&(id, b)| {
|
||||
b.inflate(reach, reach).contains(p)
|
||||
&& cx.doc.node(id).and_then(editable_path).is_some_and(|pd| {
|
||||
#[cfg(test)]
|
||||
MEASURED_PARTS.set(MEASURED_PARTS.get() + 1);
|
||||
nearest_anchor(&pd, p, tol, None).is_some() || pd.nearest(p).is_some_and(|(.., d)| d <= seg_tol)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many paths [`over_selected_part`] has measured on this thread.
|
||||
static MEASURED_PARTS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Is `p` within a press's reach of an anchor of type on a path's path? The anchor wins over a
|
||||
/// bracket standing on it (the end bracket on the last anchor, #3980); the bracket's stem above it
|
||||
/// still drags the bracket.
|
||||
|
||||
@@ -2794,3 +2794,30 @@ fn a_marquee_skips_the_anchors_and_segments_of_guides_that_cannot_be_picked() {
|
||||
assert!(!crossed(&marquee(false, (140.0, 80.0), (160.0, 120.0))), "locked or hidden: no segment");
|
||||
assert!(crossed(&marquee(true, (140.0, 80.0), (160.0, 120.0))), "shown and unlocked");
|
||||
}
|
||||
|
||||
/// A hover over a large selected group measures only the paths near the pointer when it asks
|
||||
/// whether a Cmd press there lands on a part Direct Selection edits.
|
||||
#[test]
|
||||
fn over_selected_part_measures_only_the_paths_near_the_pointer() {
|
||||
use vectorcraft_doc::{Appearance, Node};
|
||||
let mut d = vectorcraft_doc::Document::new(2000.0, 2000.0);
|
||||
let l = d.layers[0].id;
|
||||
let squares: Vec<std::sync::Arc<Node>> = (0..400)
|
||||
.map(|i| {
|
||||
let (x, y) = (f64::from(i % 20) * 50.0, f64::from(i / 20) * 50.0);
|
||||
let r = vectorcraft_geom::shapes::rectangle(Rect::new(x, y, x + 20.0, y + 20.0));
|
||||
std::sync::Arc::new(Node::path(d.alloc_id(), r, Appearance::default_art()))
|
||||
})
|
||||
.collect();
|
||||
let g = d.alloc_id();
|
||||
d.insert(Some(l), 0, Node::group(g, squares)).unwrap();
|
||||
let p = paint();
|
||||
let mut picked = Selection::default();
|
||||
picked.add(g);
|
||||
let c = cx(&d, &picked, &p);
|
||||
let before = MEASURED_PARTS.get();
|
||||
assert!(over_selected_part(&c, Point::new(520.0, 520.0)), "on a square's corner");
|
||||
assert!(!over_selected_part(&c, Point::new(535.0, 535.0)), "between the squares");
|
||||
let measured = MEASURED_PARTS.get() - before;
|
||||
assert!(measured < 10, "{measured} of 400 paths measured");
|
||||
}
|
||||
@@ -518,6 +518,10 @@ struct Measured {
|
||||
bounds: Option<Option<Rect>>,
|
||||
oriented: Option<Option<OrientedBox>>,
|
||||
by_object: HashMap<NodeId, Option<Option<Rect>>>,
|
||||
/// [`painted_bounds`].
|
||||
painted: Option<Option<Rect>>,
|
||||
/// [`selected_part_boxes`].
|
||||
part_boxes: Option<std::sync::Arc<[(NodeId, Rect)]>>,
|
||||
}
|
||||
|
||||
impl Measured {
|
||||
@@ -549,6 +553,8 @@ impl PreviewMemo {
|
||||
bounds: None,
|
||||
oriented: None,
|
||||
by_object: HashMap::new(),
|
||||
painted: None,
|
||||
part_boxes: None,
|
||||
};
|
||||
if !st.entry.as_ref().is_some_and(|m| m.is_for(cx)) {
|
||||
st.entry = Some(fresh());
|
||||
@@ -594,6 +600,19 @@ pub fn selection_box(cx: &ToolContext) -> Option<OrientedBox> {
|
||||
remembered(cx, |m| &mut m.oriented, || vectorcraft_effects::preview::oriented_bounds_with(cx.doc, &cx.selection.objects, cx.bounds_measure()))
|
||||
}
|
||||
|
||||
/// The selection's visual bounds as drawn, its strokes and effects included, whatever Use Preview
|
||||
/// Bounds says: every point where a hit test finds a selected object is inside them.
|
||||
fn painted_bounds(cx: &ToolContext) -> Option<Rect> {
|
||||
remembered(cx, |m| &mut m.painted, || vectorcraft_effects::preview::bounds_of(cx.doc, &cx.selection.objects, true))
|
||||
}
|
||||
|
||||
/// `measure`'s list of the selected objects' editable paths with their control bounds, kept in the
|
||||
/// session's [`PreviewMemo`] while the document and the selection stay as they are
|
||||
/// ([`crate::direct::over_selected_part`]).
|
||||
pub(crate) fn selected_part_boxes(cx: &ToolContext, measure: impl FnOnce() -> std::sync::Arc<[(NodeId, Rect)]>) -> std::sync::Arc<[(NodeId, Rect)]> {
|
||||
remembered(cx, |m| &mut m.part_boxes, measure)
|
||||
}
|
||||
|
||||
/// [`selection_box`] while the canvas shows it (View › Show Bounding Box, and no anchors selected
|
||||
/// on their own): its handles take a press only then. A path the Pen is drawing keeps its end
|
||||
/// anchor selected, so Cmd/Ctrl lending the Selection tool doesn't scale it by hidden handles (#4980).
|
||||
@@ -1499,6 +1518,21 @@ impl Tool for SelectionTool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Does a Cmd/Ctrl press at `p` with the Selection tool select behind: is Select Behind on and a
|
||||
/// selected object under the pointer, away from the anchors, handles and segments Direct
|
||||
/// Selection edits ([`crate::direct::over_selected_part`])? Elsewhere Cmd lends the press to
|
||||
/// Direct Selection or Group Selection.
|
||||
pub fn selects_behind_at(cx: &ToolContext, p: Point) -> bool {
|
||||
let tol = cx.pick_tol();
|
||||
// The selection's painted bounds come first: away from them, no document walk is needed. They
|
||||
// take in a wide stroke's outer half, which the bounding box leaves out without Use Preview
|
||||
// Bounds.
|
||||
cx.select_behind
|
||||
&& painted_bounds(cx).is_some_and(|b| b.inflate(tol, tol).contains(p))
|
||||
&& objects_at(cx.doc, p, cx.selection_hit_options(), cx.isolation).iter().any(|id| cx.selection.contains(*id))
|
||||
&& !crate::direct::over_selected_part(cx, p)
|
||||
}
|
||||
|
||||
/// Select Behind: of the objects under `p` (topmost first), the one below the lowest selected
|
||||
/// one, back to the topmost after the bottom one; the topmost when none is selected.
|
||||
fn object_behind(cx: &ToolContext, p: Point) -> Option<NodeId> {
|
||||
|
||||
Reference in new issue
Block a user