Trials: session keys before the tool, Insert on an anchor, the keyed pasteboard menu, Transform Symmetry's press

This commit is contained in:
aiongg
2026-11-12 14:32:40 +00:00
parent 4a5a6831e9
commit 714301c54f
5 changed files with 173 additions and 5 deletions
+12
View File
@@ -357,3 +357,15 @@ fn hit_test_matches_the_overlays_press_priority() {
// ---- the anchor tools and the Curvature tool ----
// Arrows, select all and Escape belong to Direct, Convert and Curvature; Add, Delete and Auto Add/Delete have none.
#[trial]
fn insert_on_a_picked_anchor_adds_one_halfway_along_the_segment_leaving_it() {
let mut h = two_paths();
// The canvas, then the overlay; Home picks the first anchor of path 1.
h.fixture.key(egui::Key::Tab, egui::Modifiers::NONE).key(egui::Key::Tab, egui::Modifiers::NONE).key(egui::Key::Home, egui::Modifiers::NONE);
h.fixture.key(egui::Key::Insert, egui::Modifiers::NONE).settle();
assert_eq!(h.state.borrow().inserts, [(1, kr(0), 0.5)]);
// End picks the last anchor of path 2's open line: no segment leaves it.
h.fixture.key(egui::Key::End, egui::Modifiers::NONE).key(egui::Key::Insert, egui::Modifiers::NONE).settle();
assert_eq!(h.state.borrow().inserts.len(), 1, "nothing to insert after the last anchor of an open subpath");
}
+42 -2
View File
@@ -1,5 +1,4 @@
//! Painting symmetry: the model's geometry, the menu, the handles of Transform Symmetry and the tool that carries its
//! keys.
//! Painting symmetry: the model's geometry, and the handles and tool of an open Transform Symmetry.
use craft_behavior::kurbo::{Point, Size};
use craft_draw::ui::symmetry::{Symmetry, SymmetryMode};
@@ -23,3 +22,44 @@ fn segments_clip_to_the_page_and_vanish_when_the_centre_line_misses_it() {
let off = Symmetry::new(SymmetryMode::Horizontal, Point::new(50.0, -5.0), 0.0).expect("a valid symmetry");
assert!(off.segments(Size::new(100.0, 100.0)).is_empty());
}
/// Transform Symmetry open on a canvas at 100 % with the page's centre in the middle: Escape with nothing focused
/// reaches the handles, and a press away from them is spent by the session's tool, so it never paints.
#[trial]
fn the_open_transform_symmetry_takes_escape_and_every_press_on_the_canvas() {
use craft_draw::ui::symmetry::{SymmetryHandles, SymmetryTool, SymmetryTransform};
use craft_ui::canvas::{Canvas, ToolHost};
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
use craft_ui::testing::Fixture;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Default)]
struct Seen {
discarded: bool,
ignored_presses: usize,
}
let page = Size::new(400.0, 300.0);
let symmetry = Symmetry::new(SymmetryMode::Vertical, Point::new(200.0, 150.0), 0.0).expect("a valid symmetry");
let session = Rc::new(RefCell::new(SymmetryTransform::begin(symmetry)));
let seen = Rc::new(RefCell::new(Seen::default()));
let (shown, sn) = (Rc::clone(&session), Rc::clone(&seen));
let mut fixture = Fixture::new(move |ui| {
let view = craft_behavior::CanvasView::new(ui.available_rect_before_wrap(), Point::new(200.0, 150.0), 1.0);
let mut preview = shown.borrow().preview();
let canvas = Canvas::new("Document")
.view(view)
.show(ui, |ui, body| SymmetryHandles::new("Symmetry", Bind::bound(&mut preview), page).session().show_at(ui, body.rect));
if let Some(out) = canvas.inner {
out.edit.events.iter().for_each(|event| shown.borrow_mut().apply(event));
sn.borrow_mut().discarded |= out.discard;
}
let held = shown.borrow();
let reply = ToolHost::new().run(ui, &canvas.output, &mut SymmetryTool::new(&held, true));
sn.borrow_mut().ignored_presses += usize::from(reply.ignored_press.is_some());
});
fixture.settle().key(egui::Key::Escape, egui::Modifiers::NONE).settle();
assert!(seen.borrow().discarded, "Escape with nothing focused discards the session");
fixture.click_at(egui::pos2(40.0, 40.0)).settle();
assert_eq!(seen.borrow().ignored_presses, 0, "the press away from the handles is the session tool's");
}
+2
View File
@@ -4,6 +4,8 @@ mod crop;
mod navigate;
mod pasteboard;
mod raster;
mod scrollbars;
@@ -0,0 +1,30 @@
//! `Pasteboard`: the Menu key or Shift+F10 on the focused canvas asks for the pasteboard menu at the view's centre, as
//! a secondary click on the pasteboard does where it lands.
use craft_behavior::CanvasView;
use craft_behavior::kurbo::{Point, Rect};
use craft_ui::canvas::{Canvas, PageFrame, Pasteboard};
use craft_ui::contract::{Show as _, ShowContainer as _};
use craft_ui::testing::{Fixture, trial};
use egui::{Key, Modifiers};
use std::cell::Cell;
use std::rc::Rc;
#[trial]
fn the_menu_key_on_the_focused_canvas_asks_for_the_pasteboard_menu_at_the_views_centre() {
let centre = Point::new(200.0, 150.0);
let asked: Rc<Cell<Option<Point>>> = Rc::new(Cell::new(None));
let seen = Rc::clone(&asked);
let pages = [PageFrame::new(Rect::new(100.0, 80.0, 300.0, 220.0)).name("Artboard 1")];
let mut fixture = Fixture::new(move |ui| {
let view = CanvasView::new(ui.available_rect_before_wrap(), centre, 1.0);
let shown = Canvas::new("Document").view(view).show(ui, |ui, body| Pasteboard::new("Pasteboard", &pages).show_at(ui, body.rect));
if let Some(at) = shown.inner.and_then(|out| out.context_at) {
seen.set(Some(at));
}
});
fixture.settle().key(Key::Tab, Modifiers::NONE).settle();
fixture.key(Key::F10, Modifiers::SHIFT).settle();
// The centre lies on the page: a keyed request has no point of its own to test against the pages.
assert_eq!(asked.get(), Some(centre));
}
+87 -3
View File
@@ -3,15 +3,22 @@
//! focused canvas, a tool takes the deletes it claims in its state at the end of the last pass: the plain ones, and
//! with Alt held while it keeps the keyboard (Photoshop's polygonal lasso removes its last point while Alt holds the
//! freehand switch). Unclaimed deletes are the app's (Edit > Clear, Fill). Enter and Escape reach a tool as `Submit`
//! and `Cancel` with Alt carried, and Shift+Delete is no delete (Photoshop opens Fill, which the app binds).
//! and `Cancel` with Alt carried, and Shift+Delete is no delete (Photoshop opens Fill, which the app binds). A
//! session control in the canvas body (a crop frame) takes the arrows, Enter and Escape before the tool, with the
//! canvas focused or nothing focused; with none shown, the arrows reach the tool with nothing focused.
use craft_behavior::CancelReason;
use craft_ui::canvas::{CanvasTool, ToolAction, ToolClaims, ToolContext, ToolKey, ToolPath, ToolPointer};
use craft_behavior::geometry::CropBox;
use craft_behavior::kurbo::{Point, Rect};
use craft_behavior::{CancelReason, CanvasView};
use craft_ui::canvas::{CanvasTool, CropOverlay, ToolAction, ToolClaims, ToolContext, ToolKey, ToolPath, ToolPointer};
use craft_ui::contract::{Bind, Show as _};
use craft_ui::primitives::Draw;
use craft_ui::testing::focus::key_release;
use craft_ui::testing::input::key_press;
use craft_ui::testing::{ToolFixture, pointer, trial};
use egui::{Key, Modifiers};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq)]
enum Seen {
@@ -82,3 +89,80 @@ fn a_tool_whose_release_opens_an_outline_takes_alt_backspace_in_the_next_frame()
canvas.fixture().hold(Modifiers::NONE).settle();
assert_eq!(canvas.tool().seen, [Seen::Delete { forward: false }], "the outline the release opened takes Alt+Backspace");
}
/// A tool that takes every key the host offers it.
#[derive(Default)]
struct Taker {
seen: Vec<ToolKey>,
}
impl CanvasTool for Taker {
fn press(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) -> ToolAction {
ToolAction::Ignored
}
fn drag(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) {}
fn release(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) {}
fn cancel(&mut self, _: CancelReason, _: &mut ToolContext<'_>) {}
fn key(&mut self, key: &ToolKey, _: &mut ToolContext<'_>) -> bool {
self.seen.push(key.clone());
true
}
fn paint(&self, _: &Draw<'_>, _: &ToolContext<'_>) {}
fn command_form(&self) -> ToolPath {
ToolPath::Command("the tool's form")
}
}
/// What the crop frame reported: applied, discarded.
type Reports = Rc<Cell<(bool, bool)>>;
/// A canvas at 100 % with the document point (200, 150) at its centre, the `Taker` tool and, when `frame`, a crop
/// frame over (100, 80)-(300, 220).
fn session(frame: bool) -> (ToolFixture<Taker>, Rc<RefCell<CropBox>>, Reports) {
let crop = Rc::new(RefCell::new(CropBox::new(Rect::new(100.0, 80.0, 300.0, 220.0), 0.0)));
let reports: Reports = Rc::new(Cell::new((false, false)));
let (c, r) = (Rc::clone(&crop), Rc::clone(&reports));
let view = |rect| CanvasView::new(rect, Point::new(200.0, 150.0), 1.0);
let canvas = ToolFixture::with_view("Document", Taker::default(), view, move |ui, body| {
if !frame {
return;
}
let out = CropOverlay::new("Crop", Bind::bound(&mut *c.borrow_mut())).show_at(ui, body.rect);
let (applied, discarded) = r.get();
r.set((applied || out.apply, discarded || out.discard));
});
(canvas, crop, reports)
}
#[trial]
fn a_crop_frame_takes_the_arrows_enter_and_escape_before_the_tool() {
let (mut canvas, crop, reports) = session(true);
let start = crop.borrow().rect;
// Nothing focused: the window's arrows and Escape reach the frame.
canvas.fixture().key(Key::ArrowRight, Modifiers::NONE).settle();
assert_eq!(crop.borrow().rect, start + craft_behavior::kurbo::Vec2::new(1.0, 0.0), "the arrow moves the whole frame");
canvas.fixture().key(Key::Escape, Modifiers::NONE).settle();
assert_eq!(reports.get(), (false, true), "Escape discards");
// The canvas focused: it is the first Tab stop, the frame the next.
canvas.fixture().key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(canvas.fixture().focused(), Some(canvas.canvas_id()), "Tab focuses the canvas");
canvas.fixture().key(Key::ArrowDown, Modifiers::SHIFT).settle();
assert_eq!(crop.borrow().rect, start + craft_behavior::kurbo::Vec2::new(1.0, 10.0), "Shift moves the frame by ten");
canvas.fixture().key(Key::Enter, Modifiers::NONE).settle();
assert_eq!(reports.get(), (true, true), "Enter applies");
assert!(canvas.tool().seen.is_empty(), "the tool gets none of the frame's keys: {:?}", canvas.tool().seen);
}
#[trial]
fn with_no_session_control_the_arrows_and_enter_reach_the_tool_with_nothing_focused() {
let (mut canvas, ..) = session(false);
canvas.fixture().key(Key::ArrowRight, Modifiers::NONE).key(Key::Enter, Modifiers::NONE).settle();
let seen = &canvas.tool().seen;
assert!(matches!(seen[..], [ToolKey::Nudge { .. }, ToolKey::Submit]), "{seen:?}");
}