mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
Merge branch 'red/t4' into fw/mac1
This commit is contained in:
@@ -59,6 +59,11 @@ impl SnapKinds {
|
||||
}
|
||||
}
|
||||
|
||||
/// The set of `kind` alone.
|
||||
pub const fn only(kind: SnapKind) -> Self {
|
||||
SnapKinds(Self::bit(kind))
|
||||
}
|
||||
|
||||
/// This set without `kind`.
|
||||
pub fn without(self, kind: SnapKind) -> Self {
|
||||
SnapKinds(self.0 & !Self::bit(kind))
|
||||
@@ -313,8 +318,8 @@ impl SnapTargets {
|
||||
self.snap_point_of(point, tolerance, SnapKinds::ALL)
|
||||
}
|
||||
|
||||
/// [`snap_point`](Self::snap_point) to the targets of a kind in `kinds` only (a Distort corner skips layer edges
|
||||
/// and centres).
|
||||
/// [`snap_point`](Self::snap_point) to the targets of a kind in `kinds` only (a Distort corner snaps to guides
|
||||
/// only).
|
||||
///
|
||||
/// Public: craft-ui-canvas's `Snap::point` filters through it.
|
||||
pub fn snap_point_of(&self, point: Point, tolerance: f64, kinds: SnapKinds) -> (Point, Vec<SnapLine>) {
|
||||
|
||||
@@ -580,13 +580,14 @@ impl<T: Copy> PressMachine<T> {
|
||||
released = Some((open.target, open.start, up, open.dragging, click));
|
||||
self.open = None;
|
||||
}
|
||||
// A middle or secondary press captures for a drag (a canvas pan, a drag with the secondary button) and
|
||||
// never clicks or starts over an open press; a primary press does not replace one of them. A target
|
||||
// that senses `MIDDLE_CLICK` also takes a middle press that can click (a tab closes on it), and one
|
||||
// that senses `CONTEXT_CLICK` takes a secondary press, which a click-only target would not capture.
|
||||
// An open press keeps the pointer until its button comes up: no press starts over it, of its own
|
||||
// button (a repeated Down, as a window never sends one) or another. Its moves and its release still
|
||||
// reach it when a modal opened over its target since. A middle or secondary press captures for a
|
||||
// drag (a canvas pan, a drag with the secondary button) and never clicks. A target that senses
|
||||
// `MIDDLE_CLICK` also takes a middle press that can click (a tab closes on it), and one that senses
|
||||
// `CONTEXT_CLICK` takes a secondary press, which a click-only target would not capture.
|
||||
let capture_only = button != PointerButton::Primary;
|
||||
let over_other = self.open.is_some_and(|open| capture_only || open.start.button != button);
|
||||
if !matches!(button, PointerButton::Primary | PointerButton::Middle | PointerButton::Secondary) || over_other {
|
||||
if !matches!(button, PointerButton::Primary | PointerButton::Middle | PointerButton::Secondary) || self.open.is_some() {
|
||||
continue;
|
||||
}
|
||||
self.open = pick(at)
|
||||
@@ -713,6 +714,32 @@ mod tests {
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
/// A press of the held button, where nothing takes a press (a modal opened over the drag's target), leaves the open
|
||||
/// press its drag, and the release ends that press from its own start. Replacing it would end the drag with no
|
||||
/// release, and its owner would never see the button come up.
|
||||
#[test]
|
||||
fn a_press_of_the_held_button_leaves_the_open_drag_its_moves_and_release() {
|
||||
let first = pos2(0.0, 0.0);
|
||||
let mut machine = PressMachine::new();
|
||||
let mut step = |events: &[PointerEvent]| {
|
||||
machine.step(PassInput::new(events, SLOP, first, true), |at| (at == first).then_some((1u8, Sense::DRAG)))
|
||||
};
|
||||
step(&[press(0.0, 1.0)]);
|
||||
step(&[PointerEvent::Moved { at: pos2(40.0, 0.0), sample: None }]);
|
||||
let repeated = step(&[press(50.0, 1.2)]);
|
||||
assert_eq!(repeated.target, Some(1), "the open press keeps its target");
|
||||
assert!(matches!(repeated.phase, Some(PressPhase::Drag { start, .. }) if start.origin == first), "{:?}", repeated.phase);
|
||||
let moved = step(&[PointerEvent::Moved { at: pos2(60.0, 0.0), sample: None }]);
|
||||
assert!(matches!(moved.phase, Some(PressPhase::Drag { pos, .. }) if pos == pos2(60.0, 0.0)), "{:?}", moved.phase);
|
||||
let up = step(&[PointerEvent::released(pos2(60.0, 0.0))]);
|
||||
assert!(
|
||||
matches!(up.phase, Some(PressPhase::Release { start, pos, .. }) if start.origin == first && pos == pos2(60.0, 0.0)),
|
||||
"{:?}",
|
||||
up.phase
|
||||
);
|
||||
assert!(machine.target().is_none(), "the release ends the press");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn held_reports_the_pointer_of_its_pass_within_slop() {
|
||||
let mut machine = PressMachine::new();
|
||||
|
||||
@@ -588,12 +588,15 @@ impl<W: TransformWarp> TransformSession<W> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Undo or redo one step; refused while a gesture runs.
|
||||
/// Undo or redo one step; refused while a gesture runs. A change not yet at rest (a run of nudges inside the
|
||||
/// nudge pause) becomes its own step first, so Undo takes it back and Redo finds nothing after it.
|
||||
fn step(&mut self, step: Step) -> bool {
|
||||
// pc: crates/ui-egui/src/tools/transform_tool/commit.rs:441 (`step` settles before it pops).
|
||||
if self.dragging() {
|
||||
return false;
|
||||
}
|
||||
self.last_nudge = None;
|
||||
self.record_rest();
|
||||
let moved = self.history.step(&mut self.draft, step);
|
||||
self.rested = self.draft.clone();
|
||||
self.changed = false;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#[cfg(feature = "screen-testing")]
|
||||
mod stories;
|
||||
|
||||
use craft_behavior::geometry::SnapLine;
|
||||
use craft_behavior::geometry::{SNAP_PX, SnapKind, SnapKinds, SnapLine};
|
||||
use craft_behavior::geometry::quad::{self, BoxAllow, BoxDrag, BoxPart, BoxRules, BoxState, ScaleRule, TransformMode};
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2 as DocVec};
|
||||
use craft_behavior::{
|
||||
@@ -252,9 +252,11 @@ fn slop_sense(part: BoxPart) -> Sense {
|
||||
/// corner winning where both reach), the inside or the outside. Parts the [rules](Self::rules) refuse take no
|
||||
/// press, except the outside in [`RotateZone::Outside`], where a click applies. An Alt press away from the handles
|
||||
/// moves the reference point there and drags it, whether it shows or not.
|
||||
/// - Snapping is the canvas's ([`canvas_snap`]): a dragged point snaps through [`Snap::point`], and the lines it
|
||||
/// snapped to are [`TransformBoxOutput::snapped`] for the host's one drawing site. Without a canvas snap nothing
|
||||
/// snaps. A pass reports the lines of its own drag events only.
|
||||
/// - Snapping is the canvas's ([`canvas_snap`]): a dragged handle snaps through [`Snap::point`] and a box moved from
|
||||
/// inside snaps its bounds through [`Snap::rect_of`]; the lines the result reaches are
|
||||
/// [`TransformBoxOutput::snapped`] for the host's one drawing site. Without a canvas snap nothing snaps but the
|
||||
/// reference point, which snaps to the box's nine points only. In Distort only the corners snap, to guides. A pass
|
||||
/// reports the lines of its own drag events only.
|
||||
/// - The box is one Tab stop with one part group, whose arrival is the picked part. Page Down and Page Up pick the
|
||||
/// next and previous part (the whole box, the corners and edges clockwise from the top left, the reference
|
||||
/// point), Home and End the first and last. The arrows move the picked part as a plain drag of it would, one
|
||||
@@ -403,31 +405,55 @@ impl<'a> TransformBox<'a> {
|
||||
quad::drag(state, state, part, at, at + by, Mods::NONE, &self.rules)
|
||||
}
|
||||
|
||||
/// The box the pointer at screen position `pos` gives a drag, snapped through the canvas's `snap` and rounded, with
|
||||
/// the lines it snapped to. A corner, an edge or the reference point snaps the grabbed point, not the pointer: a
|
||||
/// press a few pixels off the handle still lands the handle on the line. The Control key suspends the snapping
|
||||
/// unless it changes what the drag does (off macOS Ctrl is ⌘: a corner distorts, an edge skews).
|
||||
/// The box the pointer at screen position `pos` gives a drag, snapped and rounded, with the lines it snapped to.
|
||||
///
|
||||
/// - A corner or an edge snaps the grabbed handle through the canvas's `snap`, not the pointer: a press a few
|
||||
/// pixels off the handle still lands the handle on the line. A distorting corner reports only the lines it
|
||||
/// reaches once the box's folding rule has placed it: a corner stopped short of a line claims no alignment.
|
||||
/// - A drag from inside snaps the bounds of the moved box (its edges and centre).
|
||||
/// - The reference point snaps to the nine points of the box at the press (corners, edge midpoints, centre) and
|
||||
/// to nothing else, with or without the canvas's snap, the Control key held or not.
|
||||
/// - In [`TransformMode::Distort`] only the corners snap, and to guides only. The outside (rotating) never snaps.
|
||||
/// - The Control key suspends the snapping unless the drag reads it (off macOS Ctrl is ⌘): where it changes what
|
||||
/// the drag does (a corner distorts, an edge skews) or on a corner that distorts.
|
||||
fn proposal(&self, space: &CanvasSpace, snap: Option<&Snap>, grip: &Grip, pos: Pos2, mods: Mods) -> (BoxState, Vec<SnapLine>) {
|
||||
let to = space.view.to_doc(pos);
|
||||
let reads_control = self.rules.drag_kind(grip.part, mods) != self.rules.drag_kind(grip.part, Mods { cmd: false, ..mods });
|
||||
let quad = &grip.start.quad;
|
||||
let grabbed = match grip.part {
|
||||
BoxPart::Corner(i) => Some(quad[usize::from(i % 4)]),
|
||||
BoxPart::Edge(i) => Some(quad[usize::from(i % 4)].midpoint(quad[usize::from(i + 1) % 4])),
|
||||
BoxPart::Pivot => Some(grip.start.pivot),
|
||||
_ => None,
|
||||
};
|
||||
let (to, lines) = match (snap, grabbed) {
|
||||
(Some(snap), Some(grabbed)) => {
|
||||
let kind = self.rules.drag_kind(grip.part, mods);
|
||||
let reads_control = mods.cmd && (kind != self.rules.drag_kind(grip.part, Mods { cmd: false, ..mods }) || kind == Some(BoxDrag::Distort));
|
||||
let distort_mode = self.rules.effective_mode() == TransformMode::Distort;
|
||||
let kinds = if distort_mode { SnapKinds::only(SnapKind::Guide) } else { SnapKinds::ALL };
|
||||
let gesture = GestureSnap::default().reads_control(reads_control).kinds(kinds);
|
||||
let grabbed = handle_point(&grip.start, grip.part);
|
||||
let (to, mut lines) = match (grip.part, snap, grabbed) {
|
||||
(BoxPart::Pivot, _, Some(grabbed)) if !distort_mode => {
|
||||
let off = grip.from - grabbed;
|
||||
let (at, lines) = snap.point(to - off, mods, GestureSnap::default().reads_control(reads_control));
|
||||
(box_point_pull(&grip.start, to - off, space.view.doc_len(SNAP_PX as f32)) + off, Vec::new())
|
||||
}
|
||||
(BoxPart::Corner(_), Some(snap), Some(grabbed)) | (BoxPart::Edge(_), Some(snap), Some(grabbed))
|
||||
if !distort_mode || matches!(grip.part, BoxPart::Corner(_)) =>
|
||||
{
|
||||
let off = grip.from - grabbed;
|
||||
let (at, lines) = snap.point(to - off, mods, gesture);
|
||||
(at + off, lines)
|
||||
}
|
||||
(Some(snap), None) => snap.point(to, mods, GestureSnap::default().reads_control(reads_control)),
|
||||
(None, _) => (to, Vec::new()),
|
||||
(BoxPart::Inside, Some(snap), _) if !distort_mode => {
|
||||
let moved = bounds_of(&grip.start.quad) + (to - grip.from);
|
||||
let (by, lines) = snap.rect_of(moved, mods, kinds);
|
||||
(to + by, lines)
|
||||
}
|
||||
_ => (to, Vec::new()),
|
||||
};
|
||||
let dragged = quad::drag(&grip.start, &grip.last, grip.part, grip.from, to, mods, &self.rules);
|
||||
(if self.pixel_grid { on_pixel_grid(dragged) } else { dragged }, lines)
|
||||
let dragged = if self.pixel_grid { on_pixel_grid(dragged) } else { dragged };
|
||||
if kind == Some(BoxDrag::Distort)
|
||||
&& let Some(at) = handle_point(&dragged, grip.part)
|
||||
{
|
||||
lines.retain(|line| {
|
||||
let across = if matches!(line.along, Axis::Y) { at.x } else { at.y };
|
||||
(across - line.position).abs() < 1e-6
|
||||
});
|
||||
}
|
||||
(dragged, lines)
|
||||
}
|
||||
|
||||
/// The pointer for `part` at `pointer`, `dragging` during its drag.
|
||||
@@ -968,6 +994,19 @@ fn handle_point(state: &BoxState, part: BoxPart) -> Option<Point> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The bounds of `quad`.
|
||||
fn bounds_of(quad: &[Point; 4]) -> DocRect {
|
||||
quad[1..].iter().fold(DocRect::from_points(quad[0], quad[0]), |r, p| r.union_pt(*p))
|
||||
}
|
||||
|
||||
/// `p` pulled onto the nearest of the nine points of `state`'s box (its corners, edge midpoints and centre) within
|
||||
/// `reach` document units, else `p`.
|
||||
fn box_point_pull(state: &BoxState, p: Point, reach: f64) -> Point {
|
||||
let q = &state.quad;
|
||||
let points = [q[0], q[1], q[2], q[3], q[0].midpoint(q[1]), q[1].midpoint(q[2]), q[2].midpoint(q[3]), q[3].midpoint(q[0]), q[0].midpoint(q[2]).midpoint(q[1].midpoint(q[3]))];
|
||||
points.into_iter().map(|a| (a.distance(p), a)).filter(|(d, _)| *d <= reach).min_by(|a, b| a.0.total_cmp(&b.0)).map_or(p, |(_, a)| a)
|
||||
}
|
||||
|
||||
/// The corner of `screen` nearest `p` and its distance.
|
||||
fn nearest_corner(screen: &BoxState, p: Pos2) -> (usize, f32) {
|
||||
let p = Point::new(f64::from(p.x), f64::from(p.y));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! The Crop tool and its session through the public API: the pass, the keys the overlay leaves to the tool, the
|
||||
//! session's box history, and one drag through the overlay on a canvas.
|
||||
//! session's box history, drags through the overlay on a canvas, and the snapping of a Straighten line's ends.
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::geometry::CropBox;
|
||||
@@ -475,6 +475,39 @@ fn a_straighten_line_fits_when_armed_turns_in_place_when_held_and_always_spends_
|
||||
assert!(!host.tool.straighten_armed());
|
||||
}
|
||||
|
||||
/// A Straighten line drawn inside a frame levels the frame on the line as drawn: its ends snap as points, never by the
|
||||
/// offset that would snap the frame if the press moved it. Moved by the line's (277, 30), the frame's left edge would
|
||||
/// land 3 units from the guide at x = 300.
|
||||
#[trial]
|
||||
fn a_straighten_line_inside_the_frame_snaps_its_ends_as_points_not_as_a_frame_move() {
|
||||
use craft_behavior::geometry::{SnapKind, SnapOptions, SnapTarget, SnapTargets};
|
||||
use std::sync::Arc;
|
||||
|
||||
let angle_of = |from: (f64, f64), to: (f64, f64)| {
|
||||
let mut rig = Rig::new(classic());
|
||||
let mut targets = SnapTargets::new();
|
||||
targets.add(SnapTarget::new(craft_behavior::Axis::Y, 300.0, SnapKind::Guide));
|
||||
{
|
||||
let mut host = rig.host.borrow_mut();
|
||||
host.snap = Some(Snap::new(Arc::new(targets), SnapOptions::default()));
|
||||
let _ = host.tool.crop_to(upright(20.0, 20.0, 390.0, 290.0));
|
||||
host.tool.toggle_straighten();
|
||||
}
|
||||
rig.run();
|
||||
rig.drag(from, to);
|
||||
rig.frame().expect("a frame").angle
|
||||
};
|
||||
let level = |from: (f64, f64), to: (f64, f64)| CropBox::level_angle(Point::new(from.0, from.1), Point::new(to.0, to.1)).expect("a line");
|
||||
|
||||
let angle = angle_of((60.0, 100.0), (337.0, 130.0));
|
||||
let drawn = level((60.0, 100.0), (337.0, 130.0));
|
||||
assert!((angle - drawn).abs() < 1e-3, "{angle} vs {drawn}: both ends clear of the guide stay where the pointer was");
|
||||
|
||||
let angle = angle_of((60.0, 100.0), (298.0, 130.0));
|
||||
let snapped = level((60.0, 100.0), (300.0, 130.0));
|
||||
assert!((angle - snapped).abs() < 1e-3, "{angle} vs {snapped}: an end within reach of the guide snaps onto it");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn image_mode_moves_and_turns_the_view_and_classic_mode_the_frame() {
|
||||
// A turned view comes upright when the tool runs, in either mode.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! `TransformBox`, `TransformOverlay` and `TransformTool`: the parts the pointer takes, what each drag does, the
|
||||
//! keys, the readout and the overlay's edits.
|
||||
|
||||
use craft_behavior::geometry::quad::{BoxPart, BoxRules, BoxState, ScaleRule};
|
||||
use craft_behavior::geometry::quad::{BoxPart, BoxRules, BoxState, ScaleRule, TransformMode};
|
||||
use craft_behavior::geometry::{SnapKind, SnapOptions, SnapTarget, SnapTargets};
|
||||
use craft_behavior::kurbo::{Point, Rect};
|
||||
use craft_behavior::{CanvasView, EditEvent, RulerUnits, Ticks};
|
||||
@@ -51,6 +51,8 @@ struct Config {
|
||||
f32_host: bool,
|
||||
centre_mark: bool,
|
||||
line: Option<LineLook>,
|
||||
/// Snap targets beside the guides at [`SNAP_XS`].
|
||||
targets: Vec<SnapTarget>,
|
||||
}
|
||||
|
||||
/// What the host keeps of the box and what the passes reported.
|
||||
@@ -65,6 +67,8 @@ struct Host {
|
||||
id: egui::Id,
|
||||
view: CanvasView,
|
||||
guide_events: usize,
|
||||
/// The snap lines the last pass reported.
|
||||
snapped: usize,
|
||||
}
|
||||
|
||||
struct Harness {
|
||||
@@ -92,6 +96,7 @@ fn defaults() -> Config {
|
||||
f32_host: false,
|
||||
centre_mark: false,
|
||||
line: None,
|
||||
targets: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +122,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
|
||||
id: egui::Id::NULL,
|
||||
view: CanvasView::new(egui::Rect::NOTHING, Point::ZERO, 1.0),
|
||||
guide_events: 0,
|
||||
snapped: 0,
|
||||
}));
|
||||
let shown_host = Rc::clone(&host);
|
||||
let mut guides = vec![Guide::new(GuideKey::new(1), craft_behavior::Axis::Y, 300.0)];
|
||||
@@ -124,6 +130,9 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
|
||||
for x in SNAP_XS {
|
||||
targets.add(SnapTarget::new(craft_behavior::Axis::Y, x, SnapKind::Guide));
|
||||
}
|
||||
for target in &shown_config.borrow().targets {
|
||||
targets.add(*target);
|
||||
}
|
||||
let snap = Snap::new(Arc::new(targets), SnapOptions::default());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
@@ -172,6 +181,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
|
||||
if let Some((output, guide_edits)) = canvas.inner {
|
||||
let mut host = shown_host.borrow_mut();
|
||||
host.guide_events += guide_edits;
|
||||
host.snapped = output.snapped.len();
|
||||
host.id = output.edit.response.id;
|
||||
host.applied += usize::from(output.apply.is_some());
|
||||
host.discarded += usize::from(output.discard);
|
||||
@@ -790,3 +800,102 @@ fn a_secondary_click_on_the_box_opens_the_mode_menu() {
|
||||
right_click(&mut fixture, screen(Point::new(200.0, 150.0)));
|
||||
assert!(fixture.by_label("Distort").is_some(), "a secondary click on the box opens the mode menu");
|
||||
}
|
||||
|
||||
/// A drag from inside snaps the moved box's edges, not the pointer: the right edge lands on the guide at 310 while the
|
||||
/// pointer stays far from both guides.
|
||||
#[trial]
|
||||
fn a_box_moved_from_inside_snaps_its_edges() {
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| config.snap = true);
|
||||
let from = h.screen(Point::new(130.0, 120.0));
|
||||
frame(&mut h, vec![pointer::moved(from)]);
|
||||
frame(&mut h, vec![pointer::press(from)]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(4.0, 0.0))]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(7.0, 0.0))]);
|
||||
assert_eq!(h.host.borrow().snapped, 1, "the right edge's line");
|
||||
frame(&mut h, vec![pointer::release(from + by(7.0, 0.0))]);
|
||||
let moved = h.state().quad[0].x - RECT.x0;
|
||||
assert!((moved - 10.0).abs() < 1e-6, "the box moved {moved}, its right edge onto 310");
|
||||
}
|
||||
|
||||
/// Free Transform's corner snaps to a layer edge and a guide alike; an explicit Distort corner snaps to guides only,
|
||||
/// and a corner the folding rule stops short of a guide crossing claims no line.
|
||||
#[trial]
|
||||
fn a_distort_corner_snaps_to_guides_only_and_claims_no_line_it_stops_short_of() {
|
||||
let layer_edge = SnapTarget::new(craft_behavior::Axis::X, 230.0, SnapKind::LayerEdge);
|
||||
for (mode, expected, lines) in [(TransformMode::Free, Point::new(310.0, 230.0), 2), (TransformMode::Distort, Point::new(310.0, 227.0), 1)] {
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| {
|
||||
config.snap = true;
|
||||
config.rules = BoxRules::in_mode(mode).with_aspect_linked(false);
|
||||
config.targets = vec![layer_edge];
|
||||
});
|
||||
let from = h.screen(Point::new(300.0, 220.0));
|
||||
frame(&mut h, vec![pointer::moved(from)]);
|
||||
frame(&mut h, vec![pointer::press(from)]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(4.0, 4.0))]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(7.0, 7.0))]);
|
||||
let corner = h.state().quad[2];
|
||||
assert!(corner.distance(expected) < 1e-6, "{mode:?}: {corner:?}, expected {expected:?}");
|
||||
assert_eq!(h.host.borrow().snapped, lines, "{mode:?}");
|
||||
}
|
||||
|
||||
// The guides cross at (250, 180), past the diagonal from corner 1 to corner 3: corner 0 there would fold the box.
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| {
|
||||
config.snap = true;
|
||||
config.rules = BoxRules::in_mode(TransformMode::Distort);
|
||||
config.targets = vec![
|
||||
SnapTarget::new(craft_behavior::Axis::Y, 250.0, SnapKind::Guide),
|
||||
SnapTarget::new(craft_behavior::Axis::X, 180.0, SnapKind::Guide),
|
||||
];
|
||||
});
|
||||
let from = h.screen(Point::new(100.0, 80.0));
|
||||
frame(&mut h, vec![pointer::moved(from)]);
|
||||
frame(&mut h, vec![pointer::press(from)]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(4.0, 4.0))]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(147.0, 97.0))]);
|
||||
let q = h.state().quad;
|
||||
assert!(q[0].distance(Point::new(250.0, 180.0)) > 1.0, "the crossing would fold the box: {q:?}");
|
||||
for i in 0..4 {
|
||||
let (a, b, c) = (q[i], q[(i + 1) % 4], q[(i + 2) % 4]);
|
||||
assert!((b - a).cross(c - b) > 0.0, "still convex: {q:?}");
|
||||
}
|
||||
assert_eq!(h.host.borrow().snapped, 0, "a stopped corner reports no guide line");
|
||||
}
|
||||
|
||||
/// Off macOS Control is ⌘: on an explicit Distort corner it keeps the snapping, in Skew and Perspective it suspends it.
|
||||
#[trial]
|
||||
fn control_keeps_snapping_on_an_explicit_distort_corner_only() {
|
||||
for (mode, snaps) in [(TransformMode::Distort, true), (TransformMode::Skew, false), (TransformMode::Perspective, false)] {
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| {
|
||||
config.snap = true;
|
||||
config.mods = Some(Mods::CMD);
|
||||
config.rules = BoxRules::in_mode(mode);
|
||||
});
|
||||
h.fixture.os(Platform::Windows);
|
||||
let from = h.screen(Point::new(300.0, 220.0));
|
||||
frame(&mut h, vec![pointer::moved(from)]);
|
||||
frame(&mut h, vec![pointer::press(from)]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(4.0, 0.0))]);
|
||||
frame(&mut h, vec![pointer::moved(from + by(7.0, 0.0))]);
|
||||
let x = h.state().quad[2].x;
|
||||
assert_eq!((x - 310.0).abs() < 1e-6, snaps, "{mode:?}: corner at x {x}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The reference point snaps to the turned box's nine points and to nothing else, with the canvas's snapping on or
|
||||
/// off: 2 units off the guide at 160 it stays put.
|
||||
#[trial]
|
||||
fn the_reference_point_snaps_to_the_turned_boxs_points_only() {
|
||||
let turned = BoxState::from_rect(RECT).turned_about_pivot(30f64.to_radians());
|
||||
for snap in [true, false] {
|
||||
let mut h = harness(turned, |config| config.snap = snap);
|
||||
let centre = turned.pivot;
|
||||
drag(&mut h, centre, Point::new(158.0, 150.0));
|
||||
let pivot = h.state().pivot;
|
||||
assert!(pivot.distance(Point::new(158.0, 150.0)) < 1e-6, "snap {snap}: no pull to the guide: {pivot:?}");
|
||||
let corner = turned.quad[1];
|
||||
drag(&mut h, pivot, corner + kurbo_vec(-2.0, 1.0));
|
||||
let pivot = h.state().pivot;
|
||||
assert!(pivot.distance(corner) < 1e-6, "snap {snap}: onto the corner: {pivot:?} vs {corner:?}");
|
||||
assert_eq!(h.state().quad, turned.quad, "the box never moved");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +117,22 @@ fn nudges_within_the_pause_fold_into_one_step() {
|
||||
assert_eq!(steps(&mut plain), 2);
|
||||
}
|
||||
|
||||
// Undo or Redo inside the nudge pause ends the run first: the nudges are a step of their own, which drops what was
|
||||
// undone before them.
|
||||
#[trial]
|
||||
fn undo_or_redo_inside_the_nudge_pause_keeps_the_nudges_as_their_own_step() {
|
||||
let at = |s: &TransformSession<Warp>| s.quad()[0];
|
||||
let mut s = session().nudge_pause(Some(1.0));
|
||||
drag(&mut s, moved(doc(30.0, 0.0)), 0.0);
|
||||
drag(&mut s, moved(doc(60.0, 0.0)), 0.1);
|
||||
assert!(s.undo());
|
||||
nudge(&mut s, moved(doc(31.0, 0.0)), 0.2);
|
||||
assert!(!s.redo(), "the nudge dropped the undone drag");
|
||||
assert!(near(at(&s), Point::new(31.0, 0.0)), "{:?}", at(&s));
|
||||
assert!(s.undo(), "the nudge is a step");
|
||||
assert!(near(at(&s), Point::new(30.0, 0.0)), "{:?}", at(&s));
|
||||
}
|
||||
|
||||
// Between a Box Begin and its Commit, undo and redo return false and change nothing.
|
||||
|
||||
// `homography` maps the frame's corners onto the box's corners and its centre into the box; a box
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! [`MarqueeTool`]: the Rectangular, Elliptical, Single Row and Single Column Marquees on the tool host.
|
||||
|
||||
use craft_behavior::geometry::{
|
||||
ConstrainStyle, Constraint, MarqueeGesture, MarqueeRect, SelectionOperation, fixed_size_pixels, marquee_pixels, square_pixels,
|
||||
ConstrainStyle, Constraint, MarqueeGesture, MarqueeRect, SelectionOperation, SnapKind, SnapKinds, fixed_size_pixels, marquee_pixels,
|
||||
square_pixels,
|
||||
};
|
||||
use craft_behavior::{CancelReason, Cursor, CursorIcon, EditEvent, PaintedCursor};
|
||||
use craft_keymap::{Key, Mods};
|
||||
@@ -343,7 +344,12 @@ impl CanvasTool for SelectToolPass<'_, MarqueeTool> {
|
||||
let unmoved = inside.clone();
|
||||
let event = inside.end(raw, pointer.mods, place.zoom(), &mut |rect| moved_selection_snap(cx, rect));
|
||||
let still = matches!(&event, SelectEvent::DragSelection(EditEvent::Commit { after, .. }) if after.delta == [0, 0]);
|
||||
let picked = line.filter(|_| still).and_then(|line| Some((single_line(tool.kind, line.size, line.at)?, line.operation)));
|
||||
// The click selects the line at the press snapped as a point, not to the selection it replaces (on a
|
||||
// one-pixel line its edges would pick the line beside it).
|
||||
let not_selection = SnapKinds::ALL.without(SnapKind::Selection);
|
||||
let picked = line
|
||||
.filter(|_| still)
|
||||
.and_then(|line| Some((single_line(tool.kind, line.size, cx.snap_of(line.at, not_selection))?, line.operation)));
|
||||
match picked {
|
||||
Some((rect, operation)) => {
|
||||
// The press was a click: the drag ends with no change, and the click selects the line.
|
||||
|
||||
@@ -478,7 +478,8 @@ fn a_marquee_corner_and_a_dragged_selection_snap_to_a_guide() {
|
||||
hold(&mut rig, Modifiers::NONE);
|
||||
assert_eq!(tags(&rig.tool_mut().events()), ["begin", "update 58,0", "commit 58,0"], "Control suspends the box snap");
|
||||
|
||||
// A click inside a selection whose edge lies within the tolerance of the guide moves nothing and calls no snap.
|
||||
// A click inside a selection whose edge lies within the tolerance of the guide moves nothing and calls no snap of
|
||||
// its bounds.
|
||||
let near = Rect::new(72.0, 30.0, 97.0, 60.0);
|
||||
let mut rig = snapped_rig("Marquee", MarqueeTool::new(MarqueeKind::Rectangle), Fake::selected(near));
|
||||
click(&mut rig, (80.0, 45.0), NONE);
|
||||
@@ -488,6 +489,11 @@ fn a_marquee_corner_and_a_dragged_selection_snap_to_a_guide() {
|
||||
let events = rig.tool_mut().events();
|
||||
assert_eq!(tags(&events[..2]), ["begin", "cancel"], "{events:?}");
|
||||
assert_eq!(events[2..], [commit((0.0, 45.0, 400.0, 46.0), SelectionOperation::New)], "the click selects the row");
|
||||
// The clicked line snaps as a point: 4.5 pixels left of the guide, the column on it.
|
||||
let mut rig = snapped_rig("Marquee", MarqueeTool::new(MarqueeKind::SingleColumn), Fake::selected(near));
|
||||
click(&mut rig, (95.5, 45.0), NONE);
|
||||
let events = rig.tool_mut().events();
|
||||
assert_eq!(events[2..], [commit((100.0, 0.0, 101.0, 300.0), SelectionOperation::New)], "the click selects the guide's column");
|
||||
}
|
||||
|
||||
// Inside the click distance a selection drag still moves: one that snapping pulls back to its start (its edge on the
|
||||
|
||||
@@ -28,7 +28,7 @@ pub struct Snap {
|
||||
pub struct GestureSnap {
|
||||
/// Whether the gesture reads the Control key itself, so Control does not suspend snapping.
|
||||
pub reads_control: bool,
|
||||
/// The kinds of target the gesture snaps to (Distort corners skip layer edges and centres).
|
||||
/// The kinds of target the gesture snaps to (an explicit Distort snaps its corners to guides only).
|
||||
pub kinds: craft_behavior::geometry::SnapKinds,
|
||||
}
|
||||
|
||||
|
||||
@@ -406,6 +406,12 @@ impl<'a> ToolContext<'a> {
|
||||
Point::new(self.snap_along(Axis::Y, point.x), self.snap_along(Axis::X, point.y))
|
||||
}
|
||||
|
||||
/// [`snap`](Self::snap) to the targets of a kind in `kinds` only, with no lines: a click that selects at the point
|
||||
/// leaves out the selection it replaces.
|
||||
pub fn snap_of(&self, point: Point, kinds: SnapKinds) -> Point {
|
||||
self.snap.map_or(point, |snap| snap.point(point, self.mods, super::GestureSnap::default().kinds(kinds)).0)
|
||||
}
|
||||
|
||||
/// The coordinate `position` across a guide that would run along `along`, snapped as [`snap`](Self::snap) does:
|
||||
/// an x coordinate is across `Axis::Y`.
|
||||
pub fn snap_along(&self, along: Axis, position: f64) -> f64 {
|
||||
|
||||
@@ -527,7 +527,8 @@ pub(crate) fn inject_release(ctx: &Context, viewport: ViewportId, at: Option<Pos
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum InjectedPointer {
|
||||
/// `button` goes down at `at`, after a move there when the injected pointer is elsewhere.
|
||||
/// `button` goes down at `at`, after a move there when the injected pointer is elsewhere. A button injected input
|
||||
/// holds already queues nothing.
|
||||
#[non_exhaustive]
|
||||
Down {
|
||||
/// The button.
|
||||
@@ -585,6 +586,10 @@ pub(crate) fn inject_pointer(ctx: &Context, viewport: ViewportId, step: Injected
|
||||
let (Some(pressed_button), Some(at)) = (egui_button(button), finite(at)) else { return };
|
||||
let mods = platform_modifiers(ctx, mods);
|
||||
queue(ctx, viewport, |request| {
|
||||
// A window never sends a press of a button that is down already.
|
||||
if request.held.buttons.contains(&pressed_button) {
|
||||
return;
|
||||
}
|
||||
request.move_to(at, mods, pen);
|
||||
request.step(mods, [self::button(at, pressed_button, true, mods)]).pen = pen;
|
||||
});
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! `runtime::windows`: the input hook per viewport, window info per viewport, the press release after an OS
|
||||
//! resize (H4), the raise of a modal's window, the mount of a full-window container (in a window of its own, or in a
|
||||
//! full-screen or Wayland-maximized main window), the size of a content-sized window, the app id of child windows,
|
||||
//! injected keys across a focus loss, non-finite injected points, the window that has the keys, and `act`.
|
||||
//! injected keys across a focus loss, non-finite injected points, a repeated injected press, the window that has the
|
||||
//! keys, and `act`.
|
||||
|
||||
use craft_behavior::{Cursor, CursorIcon, PointerButton};
|
||||
use craft_host::target::{Backend, Caps, Os, ShellInfo, WindowChrome, WindowInfo, WindowKind};
|
||||
@@ -467,6 +468,44 @@ fn non_finite_injected_points_queue_nothing_and_a_release_there_lets_go() {
|
||||
assert_eq!(presses.get(), 1);
|
||||
}
|
||||
|
||||
/// An injected press of a button injected input holds already queues nothing, as a window never sends one: the
|
||||
/// pointer stays where the first press went down, and one release lets the button go. A modal that opens during a
|
||||
/// drag on a canvas sees no second press, and the drag keeps its moves and its release.
|
||||
#[trial]
|
||||
fn a_press_of_a_held_injected_button_queues_nothing() {
|
||||
use craft_ui::runtime::windows::InjectedPointer;
|
||||
|
||||
let windows = Windows::new();
|
||||
let host = WindowHost::of(&windows.ctx);
|
||||
let presses = Cell::new(0);
|
||||
let down = Cell::new(false);
|
||||
let latest = Cell::new(None);
|
||||
let frames = |count| {
|
||||
for _ in 0..count {
|
||||
windows.frame(vec![], vec![], |ui| {
|
||||
ui.ctx().input(|input| {
|
||||
presses.set(presses.get() + u32::from(input.pointer.primary_pressed()));
|
||||
down.set(input.pointer.primary_down());
|
||||
latest.set(input.pointer.latest_pos());
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
frames(1);
|
||||
let (first, second) = (Pos2::new(40.0, 40.0), Pos2::new(90.0, 60.0));
|
||||
host.inject_pointer(ViewportId::ROOT, InjectedPointer::down(PointerButton::Primary, first, Mods::NONE, None));
|
||||
frames(4);
|
||||
assert!(down.get(), "the first press is held");
|
||||
host.inject_pointer(ViewportId::ROOT, InjectedPointer::down(PointerButton::Primary, second, Mods::NONE, None));
|
||||
frames(4);
|
||||
assert_eq!(presses.get(), 1, "the repeated press reaches nothing");
|
||||
assert_eq!(latest.get(), Some(first), "nor moves the pointer");
|
||||
host.inject_pointer(ViewportId::ROOT, InjectedPointer::up(second, Mods::NONE));
|
||||
frames(4);
|
||||
assert!(!down.get(), "one release lets the button go");
|
||||
assert_eq!(presses.get(), 1);
|
||||
}
|
||||
|
||||
/// The window the platform reports focused has the keys: `typing` reads its text field, and a key equivalent queued
|
||||
/// for it reaches that field, not the main window. With no window reported focused, the main window has them.
|
||||
#[trial]
|
||||
|
||||
Reference in New Issue
Block a user