canvas-widgets-w14: net change of 2 commits

- canvas-widgets-w14: review fixes (review-1)
- canvas-widgets-w14: review fixes (round 2)
This commit is contained in:
aiongg
2026-11-15 09:18:08 +00:00
parent 91c7978bb3
commit 8d2b4c67a3
7 changed files with 346 additions and 72 deletions
+33 -21
View File
@@ -120,7 +120,22 @@ impl Momentary {
if in_gesture {
return self.latched;
}
let tool = match self.held {
let tool = self.pick(self.held, mods);
self.latched = tool;
tool
}
/// The tool for a press that automation queued. `queued` is the command that the keys the injection queue holds at
/// the press run through the keymap; without one, the command whose window key is down counts. Picked with `mods`
/// and latched for the gesture, as [`tool`](Self::tool) does for a pointer press.
pub(crate) fn press(&mut self, queued: Option<MomentaryCommands>, mods: Mods) -> Option<MomentaryTool> {
let tool = self.pick(queued.or(self.held), mods);
self.latched = tool;
tool
}
fn pick(&self, held: Option<MomentaryCommands>, mods: Mods) -> Option<MomentaryTool> {
match held {
Some(held) => {
let tool = MomentaryTool::from(held);
Some(if mods.alt {
@@ -135,26 +150,23 @@ impl Momentary {
None if self.hand_selected && mods.alt => Some(MomentaryTool::ZoomOut),
None if self.hand_selected && (mods.cmd || mods.ctrl) => Some(MomentaryTool::ZoomIn),
None => None,
};
self.latched = tool;
tool
}
/// The tool the host runs this pass: the momentary one in effect, else `selected`. The second answer is the
/// momentary Hand when it is not the tool of the pass but still glides after a flick.
pub(crate) fn substitute<'t>(
&'t mut self,
selected: &'t mut dyn CanvasTool,
mods: Mods,
in_gesture: bool,
) -> (&'t mut dyn CanvasTool, Option<&'t mut HandTool>) {
match self.tool(mods, in_gesture) {
Some(MomentaryTool::Hand) => (&mut self.hand, None),
Some(tool) => {
self.zoom.out = tool.zooms_out();
(&mut self.zoom, self.hand.is_gliding().then_some(&mut self.hand))
}
None => (selected, self.hand.is_gliding().then_some(&mut self.hand)),
}
}
/// The tool the host runs for `tool`, the momentary tool in effect: the set's Hand or Zoom, else `selected`.
pub(crate) fn run_as<'t>(&'t mut self, tool: Option<MomentaryTool>, selected: &'t mut dyn CanvasTool) -> &'t mut dyn CanvasTool {
match tool {
Some(MomentaryTool::Hand) => &mut self.hand,
Some(tool) => {
self.zoom.out = tool.zooms_out();
&mut self.zoom
}
None => selected,
}
}
/// The momentary Hand while it glides after a flick.
pub(crate) fn gliding_hand(&mut self) -> Option<&mut HandTool> {
self.hand.is_gliding().then_some(&mut self.hand)
}
}
+152 -43
View File
@@ -46,7 +46,12 @@
//! Stand-ins: [`ToolHost::stand_in`] gives the tool a press runs while Command or Alt is held and the selected tool
//! does not take the press, or answers it [`ToolAction::Ignored`]. The order is the momentary tool, then the Command
//! stand-in, then the Alt stand-in, then the selected tool, and the gesture keeps the tool it began with. While the key
//! is held and no gesture runs, hover, cursor and paint are the stand-in's.
//! is held and no gesture runs, hover, cursor and paint are the stand-in's. [`ToolHost::cancel`], on a host built
//! with the same stand-ins and momentary set, ends a gesture with the tool that holds it, so the app never decides
//! which tool that is.
//!
//! Automation: [`ToolHost::inject`] queues pointer events and held keys in one queue, which the next pass delivers in
//! order; a queued held key is down ([`ToolContext::held`]) for exactly the queued events after it until it is let go.
//!
//! Readouts: a tool's [`ToolContext::readout`] rows paint only while the root env's `ShowReadouts` holds; its
//! [`ToolContext::hud`] rows always paint. A secondary press the tool did not take is reported in
@@ -68,7 +73,6 @@ use super::momentary::{Momentary, MomentaryCommands, MomentaryTool};
use super::navigate::Autoscroll;
use super::readout::{paint_readout, shown as readouts_shown};
use super::tool::{CanvasTool, March, ToolAccess, ToolAction, ToolClaims, ToolContext, ToolKey, ToolPointer, ToolRequests};
use super::tools::HandTool;
use super::zoom_motion::{self, Steer};
use super::{CanvasOutput, CanvasSpace, Snap, context_click};
use crate::canvas_controls::paint_lines;
@@ -97,8 +101,8 @@ use crate::runtime::windows::WindowHost;
use egui::{Context, Id, Pos2, Rect, Ui, Vec2, ViewportId};
use std::cell::RefCell;
/// A pointer event from automation, queued with [`ToolHost::inject`] and delivered on the next pass as if the
/// pointer did it. Positions are screen points.
/// A pointer event or held key from automation, queued with [`ToolHost::inject`] and delivered on the next pass in
/// the order queued, as if the pointer and the keyboard did it. Positions are screen points.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum ToolInput {
@@ -134,6 +138,19 @@ pub enum ToolInput {
/// The modifiers held.
mods: Mods,
},
/// A key goes down: the inputs queued after it run with the key held ([`ToolContext::held`]) until its
/// [`ToolInput::LetGo`], in this pass or a later one.
#[non_exhaustive]
Hold {
/// The key.
key: Key,
},
/// A key a [`ToolInput::Hold`] held comes up: the inputs queued after it run without it.
#[non_exhaustive]
LetGo {
/// The key.
key: Key,
},
}
impl ToolInput {
@@ -152,21 +169,32 @@ impl ToolInput {
ToolInput::Up { pos, mods: Mods::NONE }
}
/// The input with the modifiers `held`.
/// `key` going down, held for the inputs queued after it.
pub const fn hold(key: Key) -> Self {
ToolInput::Hold { key }
}
/// `key`, held by an earlier [`ToolInput::hold`], coming up.
pub const fn let_go(key: Key) -> Self {
ToolInput::LetGo { key }
}
/// The input with the modifiers `held`; a held key carries none.
#[must_use]
pub fn mods(mut self, held: Mods) -> Self {
match &mut self {
ToolInput::Down { mods, .. } | ToolInput::Move { mods, .. } | ToolInput::Up { mods, .. } => *mods = held,
ToolInput::Hold { .. } | ToolInput::LetGo { .. } => {}
}
self
}
/// The input with the pen's reading `pen`; a button coming up has none.
/// The input with the pen's reading `pen`; a button coming up and a held key have none.
#[must_use]
pub fn sample(mut self, pen: PointerSample) -> Self {
match &mut self {
ToolInput::Down { sample, .. } | ToolInput::Move { sample, .. } => *sample = pen,
ToolInput::Up { .. } => {}
ToolInput::Up { .. } | ToolInput::Hold { .. } | ToolInput::LetGo { .. } => {}
}
self
}
@@ -298,6 +326,8 @@ struct HostState {
/// The open press was refused, ignored or finished whole: its later phases are not the tool's.
spent: bool,
queued: Vec<ToolInput>,
/// The keys the delivered [`ToolInput::Hold`]s hold down until their [`ToolInput::LetGo`].
injected_held: Vec<Key>,
/// The keys the tool claimed at the end of the last pass.
claims: ToolClaims,
/// The canvas of the last pass, for [`ToolHost::cancel`] outside a pass.
@@ -325,6 +355,20 @@ fn held_at(key: StandInKey, mods: Mods) -> bool {
}
}
/// The tool the host runs for `tool`, the momentary tool in effect: the set's Hand or Zoom, else `selected`.
fn run_as<'t>(set: Option<&'t mut Momentary>, tool: Option<MomentaryTool>, selected: &'t mut dyn CanvasTool) -> &'t mut dyn CanvasTool {
match set {
Some(set) => set.run_as(tool, selected),
None => selected,
}
}
/// The momentary command a key the injection queue holds runs at a press with `mods`: the keymap's command for the key
/// with the press's modifiers, else for the key alone, since modifiers pressed after a momentary key still count.
fn queued_momentary(ctx: &Context, canvas: Id, around: &commands::CommandChain, key: Key, mods: Mods) -> Option<MomentaryCommands> {
[mods, Mods::NONE].into_iter().find_map(|mods| commands::bound_around::<MomentaryCommands>(ctx, canvas, around, &Chord::new(mods, key)))
}
/// The on-canvas tool host. Built each pass, shown once with the canvas's output and the tool.
#[derive(Default)]
pub struct ToolHost<'m> {
@@ -389,7 +433,7 @@ impl<'m> ToolHost<'m> {
/// Delivers the pass's pointer and key events to `tool`, paints it over the canvas, and paints the readout it
/// asked for beside the pointer.
pub fn run(self, ui: &Ui, canvas: &CanvasOutput, tool: &mut dyn CanvasTool) -> ToolHostResponse {
let ToolHost { disabled, momentary, mut command_stand_in, mut alt_stand_in, smart_guide_color, precise_cursors } = self;
let ToolHost { disabled, mut momentary, mut command_stand_in, mut alt_stand_in, smart_guide_color, precise_cursors } = self;
let ctx = ui.ctx().clone();
let env = Env::of(ui);
let key = state_id(canvas.response.id);
@@ -407,38 +451,62 @@ impl<'m> ToolHost<'m> {
let cancelled_view = if measuring { None } else { state.cancelled_view.take() };
let space = canvas.space;
let down = keys::down(&ctx);
// The keys each queued input runs with: the window's, and the ones the queue's own holds keep down at it. A press
// also gets the momentary command those held keys run through the keymap, as the window's keys fire it.
let queued_down: Vec<(Vec<Key>, Option<MomentaryCommands>)> = if measuring {
Vec::new()
} else {
queued
.iter()
.map(|input| {
match *input {
ToolInput::Hold { key } if !state.injected_held.contains(&key) => state.injected_held.push(key),
ToolInput::LetGo { key } => state.injected_held.retain(|held| *held != key),
_ => {}
}
let mut keys = down.clone();
keys.extend(state.injected_held.iter().filter(|key| !down.contains(key)));
let held = match *input {
ToolInput::Down { mods, .. } => {
state.injected_held.iter().find_map(|key| queued_momentary(&ctx, canvas.response.id, env.view().command_chain(), *key, mods))
}
_ => None,
};
(keys, held)
})
.collect()
};
let caps = lock_keys::caps_lock(&ctx);
let rect = canvas.response.rect;
let input = pointer::frame(&ctx);
let mut glide = None;
let fired = std::iter::from_fn(|| {
if measuring {
return None;
}
commands::take_around::<MomentaryCommands>(&ctx, canvas.response.id, env.view().command_chain())
});
let mut momentary_tool = None;
let tool: &mut dyn CanvasTool = match momentary {
let momentary_tool = match momentary.as_deref_mut() {
Some(momentary) => {
for (command, event) in fired {
momentary.fired(command, event.phase);
}
momentary_tool = momentary.tool(input.mods, state.gesture.is_some());
let (tool, hand) = momentary.substitute(tool, input.mods, state.gesture.is_some());
glide = hand;
tool
momentary.tool(input.mods, state.gesture.is_some())
}
None => {
fired.for_each(drop);
tool
None
}
};
if !measuring && tool.view_only() {
let (view_only, takes_primary) = {
let pass = run_as(momentary.as_deref_mut(), momentary_tool, &mut *tool);
(pass.view_only(), pass.takes(PointerButton::Primary, input.mods))
};
if !measuring && view_only {
sampler::pass_tool_gesture(&ctx, canvas.response.id, CanvasGesture::PRIMARY);
}
// With no gesture and no momentary tool, a held stand-in key makes the stand-in the tool of hover, cursor and
// paint while the selected tool does not take a primary press.
let lent = if state.gesture.is_none() && momentary_tool.is_none() && !tool.takes(PointerButton::Primary, input.mods) {
let lent = if state.gesture.is_none() && momentary_tool.is_none() && !takes_primary {
[StandInKey::Command, StandInKey::Alt].into_iter().find(|key| {
let set = match key {
StandInKey::Command => command_stand_in.is_some(),
@@ -450,7 +518,8 @@ impl<'m> ToolHost<'m> {
None
};
let mut run = Run {
tool: &mut *tool,
selected: &mut *tool,
set: momentary.as_deref_mut(),
command: command_stand_in.as_deref_mut(),
alt: alt_stand_in.as_deref_mut(),
lent: None,
@@ -476,25 +545,25 @@ impl<'m> ToolHost<'m> {
};
if !measuring {
run.call(None, |tool, cx| tool.each_pass(cx));
run.hover_scrolls = run.tool.autoscrolls_on_hover();
run.hover_scrolls = run.pick_as(None).autoscrolls_on_hover();
run.lent = lent;
if let Some(hand) = glide {
run.glide(hand);
}
run.glide();
if disabled {
run.cancel(CancelReason::Dismissed);
run.state.spent |= canvas.press.phase.is_press();
} else {
run.phase(&canvas.press, &input);
run.hover_outside(canvas.response.id, &canvas.press, &input);
for event in queued {
run.injected(event);
for (event, (keys, held)) in queued.into_iter().zip(&queued_down) {
run.down = keys.as_slice();
run.injected(event, *held);
}
run.down = down.as_slice();
run.keys(&ctx, canvas.response.id, env.direction.is_rtl());
run.menu_key();
}
}
let Run { mut response, requests, .. } = run;
let Run { mut response, requests, momentary: momentary_tool, .. } = run;
let before = space.map(|space| space.target());
if !measuring {
if let Some(past) = response.past_edge {
@@ -527,11 +596,12 @@ impl<'m> ToolHost<'m> {
let over = phase.is_press() || matches!(phase, PressPhase::Hover) || state.gesture.is_some();
let current = state.gesture.map(|active| active.last).or_else(|| over.then(|| hover_pointer(&input, canvas.press)).flatten());
// The tool of hover, cursor, paint and claims: the one the running gesture latched, else the stand-in lent for
// the held key, else the selected tool.
// the held key, else the momentary tool in effect, else the selected tool.
let pass: &dyn CanvasTool = run_as(momentary.as_deref_mut(), momentary_tool, &mut *tool);
let effective: &dyn CanvasTool = match state.gesture.map_or(lent, |active| active.stand_in) {
Some(StandInKey::Command) => command_stand_in.as_deref().unwrap_or(&*tool),
Some(StandInKey::Alt) => alt_stand_in.as_deref().unwrap_or(&*tool),
None => &*tool,
Some(StandInKey::Command) => command_stand_in.as_deref().unwrap_or(pass),
Some(StandInKey::Alt) => alt_stand_in.as_deref().unwrap_or(pass),
None => pass,
};
let context = || {
let cx = ToolContext::new(rect, space)
@@ -596,8 +666,14 @@ impl<'m> ToolHost<'m> {
response
}
/// Queues an automation pointer event for the host of the canvas `canvas` (`CanvasOutput::response.id`); it is
/// delivered on the next pass, after the pointer's own events. A `Down` while a gesture runs is dropped.
/// Queues an automation input for the host of the canvas `canvas` (`CanvasOutput::response.id`); the queue is
/// delivered on the next pass, in order, after the pointer's own events. A `Down` while a gesture runs is dropped.
/// A key is held for exactly the inputs queued between its [`ToolInput::hold`] and its [`ToolInput::let_go`]
/// (the window's own keys are held for every one), so one queue orders the keys against the pointer. A held key
/// that the keymap binds to a [`MomentaryCommands`](super::MomentaryCommands) command starts that momentary tool
/// for the presses queued while it is down, as the real key does (a queued Space pans with the Hand). The window
/// host owes the canvas's viewport a pass for the queue, so [`WindowHost::injected_idle`] turns true only once the
/// pass took it.
pub fn inject(ctx: &Context, canvas: Id, input: ToolInput) {
let viewport = with_session(ctx, state_id(canvas), |state: &mut HostState| {
state.queued.push(input);
@@ -608,9 +684,12 @@ impl<'m> ToolHost<'m> {
}
/// Ends the gesture in progress on the canvas `canvas` now, without a commit (the app switched tools or
/// documents, a modal opened). `tool` is the tool that holds the gesture, so call it before the app replaces
/// it; it runs against the canvas of the last pass. Returns whether a gesture was cancelled.
pub fn cancel(ctx: &Context, canvas: Id, tool: &mut dyn CanvasTool, reason: CancelReason) -> bool {
/// documents, a modal opened). Build the host as for [`ToolHost::run`], with the same stand-ins and momentary
/// set: it cancels the gesture with the tool that holds it, picked as the pass picks it (the stand-in or
/// momentary tool the press latched, else `selected`). Call it before the app replaces its tools; it runs against
/// the canvas of the last pass. Returns whether a gesture was cancelled.
pub fn cancel(self, ctx: &Context, canvas: Id, selected: &mut dyn CanvasTool, reason: CancelReason) -> bool {
let ToolHost { mut momentary, command_stand_in, alt_stand_in, .. } = self;
// The press is still held: its later moves and release are not the next gesture's.
let taken = with_session(ctx, state_id(canvas), |slot: &mut HostState| {
let taken = slot.gesture.take().map(|active| (active, slot.rect, slot.space));
@@ -618,6 +697,17 @@ impl<'m> ToolHost<'m> {
taken
});
let Some((active, rect, space)) = taken else { return false };
// A press that latched a stand-in ran without the momentary set, so the stand-in comes first.
let stand_in = match active.stand_in {
Some(StandInKey::Command) => command_stand_in,
Some(StandInKey::Alt) => alt_stand_in,
None => None,
};
let latched = momentary.as_deref_mut().and_then(|momentary| momentary.tool(Mods::NONE, true));
let tool: &mut dyn CanvasTool = match stand_in {
Some(stand_in) => stand_in,
None => run_as(momentary, latched, selected),
};
let down = keys::down(ctx);
let mut cx = ToolContext::new(rect.unwrap_or(Rect::NOTHING), space)
.pointer(Some(active.last))
@@ -790,7 +880,10 @@ fn cancel_reason(reason: PressCancel) -> CancelReason {
///
/// `'t` is the stand-ins' own lifetime: the host lends them for one run, so `Run`'s borrows need not outlive the host.
struct Run<'a, 't> {
tool: &'a mut dyn CanvasTool,
/// The selected tool.
selected: &'a mut dyn CanvasTool,
/// The momentary set: its tool in `momentary` runs in place of the selected one.
set: Option<&'a mut Momentary>,
space: Option<CanvasSpace>,
slop: f32,
down: &'a [Key],
@@ -820,7 +913,7 @@ struct Run<'a, 't> {
lent: Option<StandInKey>,
/// A press may run a stand-in: no momentary tool is in effect.
stand_ins: bool,
/// The momentary tool in effect this pass.
/// The momentary tool in effect: the pass's, until an injected press picks its own.
momentary: Option<MomentaryTool>,
/// Preferences > Cursors > Other Cursors is Precise.
precise: bool,
@@ -864,8 +957,9 @@ impl<'a, 't: 'a> Run<'a, 't> {
}
}
/// The tool `who` names: a stand-in, or the selected tool for `None` and for a stand-in that is not set.
fn pick_as(&mut self, who: Option<StandInKey>) -> &mut (dyn CanvasTool + 'a) {
/// The tool `who` names: a stand-in, or for `None` and for a stand-in that is not set the momentary tool in effect,
/// else the selected tool.
fn pick_as(&mut self, who: Option<StandInKey>) -> &mut dyn CanvasTool {
let stand = match who {
Some(StandInKey::Command) => self.command.as_deref_mut(),
Some(StandInKey::Alt) => self.alt.as_deref_mut(),
@@ -873,7 +967,16 @@ impl<'a, 't: 'a> Run<'a, 't> {
};
match stand {
Some(stand) => stand,
None => &mut *self.tool,
None => run_as(self.set.as_deref_mut(), self.momentary, &mut *self.selected),
}
}
/// Picks the momentary tool for an injected press: `queued`, the command the injection queue's held keys run, else
/// the window's held key, with the press's `mods`. A press under a momentary tool runs no stand-in.
fn pick_momentary(&mut self, queued: Option<MomentaryCommands>, mods: Mods) {
if let Some(set) = self.set.as_deref_mut() {
self.momentary = set.press(queued, mods);
self.stand_ins = self.momentary.is_none();
}
}
@@ -921,8 +1024,12 @@ impl<'a, 't: 'a> Run<'a, 't> {
}
/// Advances the glide of a momentary Hand that is not the tool of this pass.
fn glide(&mut self, hand: &mut HandTool) {
fn glide(&mut self) {
if self.momentary == Some(MomentaryTool::Hand) {
return;
}
let mut cx = self.context(None);
let Some(hand) = self.set.as_deref_mut().and_then(Momentary::gliding_hand) else { return };
hand.each_pass(&mut cx);
self.absorb(cx);
}
@@ -938,7 +1045,7 @@ impl<'a, 't: 'a> Run<'a, 't> {
/// tool answers `Ignored` goes to that stand-in too. A refused, ignored or whole press is spent.
fn begin(&mut self, pointer: ToolPointer, injected: bool) {
let mut who = None;
if !self.tool.takes(pointer.button, pointer.mods) {
if !self.pick_as(None).takes(pointer.button, pointer.mods) {
who = self.stand_for(&pointer);
if who.is_none() {
self.state.spent = true;
@@ -1148,10 +1255,12 @@ impl<'a, 't: 'a> Run<'a, 't> {
}
}
fn injected(&mut self, input: ToolInput) {
/// Delivers one queued input. `queued` is the momentary command the queue's held keys run at a press.
fn injected(&mut self, input: ToolInput, queued: Option<MomentaryCommands>) {
let own = self.state.gesture.is_none_or(|active| active.injected);
match input {
ToolInput::Down { button, pos, sample, mods, clicks } if self.state.gesture.is_none() => {
self.pick_momentary(queued, mods);
let pointer = ToolPointer::new(pos, button, self.time).mods(mods).sample(sample).clicks(clicks);
self.begin(pointer, true);
self.state.spent = false;
+103 -5
View File
@@ -128,7 +128,7 @@ fn a_gesture_cancelled_by_the_app_starts_no_new_one_while_the_button_stays_down(
let from = fixture.canvas().center();
fixture.press_at(from);
let (ctx, id) = (fixture.fixture().ctx().clone(), fixture.canvas_id());
assert!(ToolHost::cancel(&ctx, id, &mut *fixture.tool_mut(), CancelReason::Dismissed), "a gesture was running");
assert!(ToolHost::new().cancel(&ctx, id, &mut *fixture.tool_mut(), CancelReason::Dismissed), "a gesture was running");
fixture.move_to(from + BY).move_to(from + BY * 2.0).release_at(from + BY * 2.0);
assert_eq!(fixture.tool().calls, [Call::Press, Call::Cancel(CancelReason::Dismissed)], "the held button is not the tool's any more");
@@ -390,6 +390,8 @@ struct Probe {
every_move: bool,
snaps: bool,
drags: Vec<(Pos2, f64)>,
/// Whether Space was held at each drag.
space_drags: Vec<bool>,
pass_time: f64,
log: Log,
}
@@ -404,6 +406,7 @@ impl Probe {
every_move: false,
snaps: false,
drags: Vec::new(),
space_drags: Vec::new(),
pass_time: 0.0,
log: Rc::clone(log),
}
@@ -435,9 +438,10 @@ impl CanvasTool for Probe {
if holds(self.ignores, pointer.mods) { ToolAction::Ignored } else { ToolAction::Claimed }
}
fn drag(&mut self, pointer: &ToolPointer, _: &mut ToolContext<'_>) {
fn drag(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) {
self.note("drag");
self.drags.push((pointer.pos, pointer.time));
self.space_drags.push(cx.held(craft_keymap::Key::Space));
}
fn release(&mut self, _: &ToolPointer, _: &mut ToolContext<'_>) {
@@ -493,6 +497,8 @@ struct Rig {
contexts: Rc<RefCell<Vec<ContextRequest>>>,
snapped: Rc<RefCell<Vec<SnapLine>>>,
cursor: Rc<RefCell<Option<Cursor>>>,
command: Rc<RefCell<Probe>>,
alt: Rc<RefCell<Probe>>,
}
impl Rig {
@@ -506,7 +512,8 @@ impl Rig {
let cursor = Rc::new(RefCell::new(None));
let (shown_main, shown_canvas, shown_contexts, shown_snapped, shown_cursor) =
(Rc::clone(&main), Rc::clone(&canvas), Rc::clone(&contexts), Rc::clone(&snapped), Rc::clone(&cursor));
let (mut command, mut alt) = (Probe::new("command", log), Probe::new("alt", log));
let (command, alt) = (Rc::new(RefCell::new(Probe::new("command", log))), Rc::new(RefCell::new(Probe::new("alt", log))));
let (shown_command, shown_alt) = (Rc::clone(&command), Rc::clone(&alt));
let mut fixture = Fixture::new(move |ui| {
let room = ui.available_rect_before_wrap();
let mut shown = Canvas::new("Document").document(DOCUMENT).view(CanvasView::new(room, Point::new(600.0, 450.0), 1.0));
@@ -515,7 +522,9 @@ impl Rig {
}
let shown = shown.show(ui, |_, _| {});
shown_canvas.set(shown.output.response.id);
let mut host = ToolHost::new().stand_in(StandInKey::Command, &mut command).stand_in(StandInKey::Alt, &mut alt).smart_guide_color(setup.smart_color);
let (mut command, mut alt) = (shown_command.borrow_mut(), shown_alt.borrow_mut());
let mut host =
ToolHost::new().stand_in(StandInKey::Command, &mut *command).stand_in(StandInKey::Alt, &mut *alt).smart_guide_color(setup.smart_color);
if let Some(momentary) = setup.momentary.as_mut() {
host = host.momentary(momentary);
}
@@ -530,7 +539,7 @@ impl Rig {
}
});
fixture.settle();
Rig { fixture, main, canvas, log: Rc::clone(log), contexts, snapped, cursor }
Rig { fixture, main, command, alt, canvas, log: Rc::clone(log), contexts, snapped, cursor }
}
fn centre(&mut self) -> Pos2 {
@@ -634,6 +643,95 @@ fn a_held_command_or_alt_runs_its_stand_in_for_one_gesture() {
assert!(!rig.calls().iter().any(|call| call == "command press"), "Cmd over the selected Hand is Zoom In: {:?}", rig.calls());
}
/// The app's cancel ends a gesture a stand-in holds on that stand-in: the host built with the pass's stand-ins picks the
/// holder, so the selected tool is not cancelled for a gesture it never had.
#[trial]
fn a_gesture_a_stand_in_holds_is_cancelled_on_the_stand_in() {
let log = Log::default();
let mut main = Probe::new("main", &log);
main.refuses = Some(StandInKey::Command);
let mut rig = Rig::new(main, &log, None);
let at = rig.centre();
rig.fixture.hold(Modifiers::COMMAND).frame(vec![pointer::press(at)]).frame(vec![pointer::moved(at + BY)]);
rig.log.borrow_mut().clear();
let (ctx, canvas) = (rig.fixture.ctx().clone(), rig.canvas.get());
let cancelled = {
let (mut command, mut alt, mut main) = (rig.command.borrow_mut(), rig.alt.borrow_mut(), rig.main.borrow_mut());
ToolHost::new().stand_in(StandInKey::Command, &mut *command).stand_in(StandInKey::Alt, &mut *alt).cancel(
&ctx,
canvas,
&mut *main,
CancelReason::HostChanged,
)
};
assert!(cancelled, "the Command stand-in's gesture was running");
assert_eq!(rig.calls(), ["command cancel"], "the stand-in that holds the gesture is cancelled, the selected tool is not");
}
/// A key queued with the pointer events is down for exactly the events queued after it until it is let go.
#[trial]
fn a_queued_space_covers_the_moves_queued_after_it_and_not_those_after_its_release() {
let log = Log::default();
let mut rig = Rig::new(Probe::new("main", &log), &log, None);
let at = rig.centre();
let (ctx, canvas) = (rig.fixture.ctx().clone(), rig.canvas.get());
let space = craft_keymap::Key::Space;
for input in [
ToolInput::down(PointerButton::Primary, at),
ToolInput::moved(at + BY),
ToolInput::hold(space),
ToolInput::moved(at + BY * 2.0),
ToolInput::moved(at + BY * 3.0),
ToolInput::let_go(space),
ToolInput::moved(at + BY * 4.0),
ToolInput::up(at + BY * 4.0),
] {
ToolHost::inject(&ctx, canvas, input);
}
rig.fixture.idle(1);
assert_eq!(rig.main.borrow().space_drags, [false, true, true, false], "Space is held for the two moves queued while it is down");
}
/// A queued Space starts the momentary Hand as the real key does: the press it is held over pans the view, and the
/// press after its release runs the selected tool.
#[trial]
fn a_queued_space_held_over_a_press_pans_with_the_hand_and_the_press_after_its_release_runs_the_selected_tool() {
let log = Log::default();
let main = Rc::new(RefCell::new(Probe::new("main", &log)));
let (view, canvas) = (Rc::new(Cell::new(None::<CanvasView>)), Rc::new(Cell::new(Id::NULL)));
let (shown_main, shown_view, shown_canvas) = (Rc::clone(&main), Rc::clone(&view), Rc::clone(&canvas));
let mut set = Momentary::new(NavigationOptions::default());
let mut fixture = Fixture::new(move |ui| {
let room = ui.available_rect_before_wrap();
// Zoomed in, so the document is larger than the canvas and the Hand has room to pan.
let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 4.0));
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show(ui, |_, _| {});
shown_canvas.set(shown.output.response.id);
let reply = ToolHost::new().momentary(&mut set).run(ui, &shown.output, &mut *shown_main.borrow_mut());
shown_view.set(Some(reply.view.unwrap_or(current)));
});
fixture.settle();
let at = fixture.by_role(Role::CANVAS)[0].rect.center();
let before = view.get().expect("the canvas ran").center;
let space = craft_keymap::Key::Space;
for input in [
ToolInput::hold(space),
ToolInput::down(PointerButton::Primary, at),
ToolInput::moved(at + BY),
ToolInput::up(at + BY),
ToolInput::let_go(space),
ToolInput::down(PointerButton::Primary, at),
ToolInput::up(at),
] {
ToolHost::inject(fixture.ctx(), canvas.get(), input);
}
fixture.idle(1);
assert_ne!(view.get().expect("the canvas ran").center, before, "the press under the queued Space pans with the Hand");
let calls = log.borrow().clone();
assert_eq!(calls.iter().filter(|call| *call == "main press").count(), 1, "only the press after the release is the selected tool's: {calls:?}");
assert!(!calls.iter().any(|call| call == "main drag"), "the Hand's drag never reaches the selected tool: {calls:?}");
}
/// A canvas under a dialog that keeps the canvas: returns the fixture, the app's view and the canvas's rect.
fn kept_canvas<T: CanvasTool + 'static>(tool: Rc<RefCell<T>>) -> (Fixture, Rc<Cell<Option<CanvasView>>>, Rc<Cell<Rect>>) {
let view = Rc::new(Cell::new(None::<CanvasView>));
@@ -980,6 +980,28 @@ pub fn take_around<S: CommandSet>(ctx: &Context, at: Id, around: &CommandChain)
take(ctx, if own { Some(at) } else { nearest }, is_app(scope), S::from_ref)
}
/// The command of set `S` that `chord` runs at `at` (a canvas that is an instance of a scope itself) inside `around`,
/// through the keymap and the scopes the window's keys match in: the lookup for a key that automation holds, so it
/// starts what the real key starts.
#[doc(hidden)]
pub fn bound_around<S: CommandSet>(ctx: &Context, at: Id, around: &CommandChain, chord: &Chord) -> Option<S> {
with(ctx, |board| {
let keymap = board.keymap.as_ref().map(|cache| Arc::clone(&cache.keymap))?;
let mut chain: Vec<Id> = around.ids().collect();
if !chain.contains(&at) {
chain.push(at);
}
let scopes = board.scopes_around(&chain);
let (panel, outer) = scopes.split_first().map_or((None, &[][..]), |(panel, outer)| (Some(*panel), outer));
let active = Active { panel, contexts: &[], typing: false };
match keymap.lookup_nested(chord, false, &active, outer) {
Lookup::Run(command) => S::from_ref(&command),
Lookup::Cycle(commands) => commands.iter().find_map(S::from_ref),
_ => None,
}
})
}
/// H7: the key stage's shortcut events, matched against the keymap in the scopes around the focused component,
/// through the gate, to their commands.
/// Whether the keymap binds `chord` to a scoped command of the focused component's command chain. A key the focused
@@ -25,7 +25,7 @@ pub use board::focus_within;
pub(crate) use board::publish as publish_for;
pub(crate) use board::take as take_for_instance;
#[doc(hidden)]
pub use board::take_around;
pub use board::{bound_around, take_around};
pub(crate) use board::{
begin, binds_scoped, edit_chords, end, install, note_focus, platform, register_instance, set_input, take_delivered, take_fired, window_shortcuts,
};
+16 -2
View File
@@ -2,7 +2,7 @@
use crate::runtime::commands::CommandInput;
use crate::runtime::egui::access::AccessTree;
use crate::runtime::egui::{areas, fixture};
use crate::runtime::egui::{areas, fixture, plugin};
use crate::runtime::env::RootEnv;
use crate::runtime::frame::{FrameInput, announce, set_input};
use craft_host::Host;
@@ -85,10 +85,24 @@ impl Frames {
self.paint.as_mut().map(std::mem::take)
}
/// Frames on `ctx`, a context the caller made (an app's own, which its code reads outside the frames), with the
/// runtime installed on it unless it already is. The context keeps its own fonts and options.
pub fn over(ctx: Context) -> Self {
let mut frames = Self::on(ctx);
if !plugin::is_registered(&frames.ctx) {
frames.install();
}
frames
}
/// A fresh context without the runtime, for frames that ran before `install`.
pub fn uninstalled() -> Self {
Self::on(fixture::bare_context())
}
fn on(ctx: Context) -> Self {
Frames {
ctx: fixture::bare_context(),
ctx,
time: 0.0,
root: RootEnv::default(),
cursor: egui::CursorIcon::Default,
@@ -235,3 +235,22 @@ fn an_unchanged_gate_reruns_no_settled_region() {
frames.commands(gate(true));
assert!(scene.frame(&mut frames) > 0, "a gate that refuses the region's command runs it");
}
/// Frames over a given context run on it: its passes are the frames', the runtime is installed on it once, and a
/// second driver over the same context runs on it without installing again.
#[trial]
fn frames_over_a_given_context_run_on_it() {
let ctx = egui::Context::default();
let mut frames = Frames::over(ctx.clone());
let mut ran_on = Vec::new();
frames.frame(vec![], |ui| ran_on.push(ui.ctx() == &ctx));
assert!(!ran_on.is_empty() && ran_on.iter().all(|on| *on), "every pass ran on the given context");
assert!(
matches!(craft_ui::install(&ctx, craft_ui::testing::install_spec()), Err(craft_ui::runtime::InstallError::AlreadyInstalled)),
"the driver installed the runtime on the given context"
);
let mut again = Frames::over(ctx.clone());
let mut second = false;
again.frame(vec![], |ui| second |= ui.ctx() == &ctx);
assert!(second, "a second driver over the installed context runs on it");
}