A fired command carries the input time of its event: a key's press and release fire with the time they arrived at, events held to a later frame keep theirs; the double-press matcher and the opacity digit pairs read that time

This commit is contained in:
aiongg
2026-11-17 07:07:07 +00:00
parent 110f62f255
commit b0a40c30e8
9 changed files with 116 additions and 39 deletions
+4 -4
View File
@@ -102,13 +102,13 @@ impl OpacityDigit {
}
}
/// What `fired` sets, through the pair state `keys`, at `now` seconds (the pass time): `None` for another command,
/// a release, or a held key's repeat, which types no second digit.
pub fn entry(keys: &mut DigitKeys, fired: &Fired, now: f64) -> Option<DigitEntry> {
/// What `fired` sets, through the pair state `keys`, at the key's own time (`Fired::time`): `None` for another
/// command, a release, or a held key's repeat, which types no second digit.
pub fn entry(keys: &mut DigitKeys, fired: &Fired) -> Option<DigitEntry> {
if fired.phase != FirePhase::Press {
return None;
}
let (digit, second) = OpacityDigit::from_ref(&fired.command)?.digit();
keys.press(digit, second, now)
keys.press(digit, second, fired.time)
}
}
@@ -16,8 +16,7 @@ fn a_digit_key_sets_the_opacity_and_shift_with_it_the_second_value() {
let mut keys = DigitKeys::default();
let mut press = |frames: &mut Frames, key: Key, modifiers: Modifiers| {
frames.frame(vec![key_press(key, modifiers)], &mut show);
let now = frames.ctx.input(|input| input.time);
let entries: Vec<_> = take_output(&frames.ctx).fired.iter().filter_map(|fired| OpacityDigit::entry(&mut keys, fired, now)).collect();
let entries: Vec<_> = take_output(&frames.ctx).fired.iter().filter_map(|fired| OpacityDigit::entry(&mut keys, fired)).collect();
assert_eq!(entries.len(), 1, "{key:?} with {modifiers:?} makes one entry");
(entries[0].percent, entries[0].second)
};
@@ -923,7 +923,7 @@ pub(crate) fn fall_through(ctx: &Context, command: craft_behavior::EditCommand)
let Some(command) = EditCommands::of(command) else { return };
let command = CommandRef::from(command);
let target = with(ctx, |board| board.resolve(ctx, &command, None, Reach::Focus, &board.here).0.instance);
let fired = Fired { target, ..Fired::new(command, FireSource::Key, FirePhase::Press) };
let fired = Fired { target, ..Fired::new(command, FireSource::Key, FirePhase::Press, pass::now(ctx)) };
with(ctx, |board| if fired.target.is_some() { board.queue.push(fired) } else { board.out.push(fired) });
repaint::request_of(ctx, ViewportId::ROOT);
}
@@ -938,7 +938,7 @@ pub(crate) fn fire_resolved(ctx: &Context, command: &ResolvedCommand, source: Fi
if !command.enabled || !crate::runtime::measure::effect(ctx, "board::fire_resolved") {
return false;
}
queue(ctx, Fired { target: command.instance, ..Fired::new(command.command.clone(), source, FirePhase::Press) });
queue(ctx, Fired { target: command.instance, ..Fired::new(command.command.clone(), source, FirePhase::Press, pass::now(ctx)) });
true
}
@@ -964,7 +964,7 @@ fn fire_in(ctx: &Context, command: &CommandRef, source: FireSource, from: Option
}
// An app-scope command a modal layer's instance declares is delivered to that instance.
let target = if app { resolved.instance } else { instance };
queue(ctx, Fired { target, ..Fired::new(command.clone(), source, FirePhase::Press) });
queue(ctx, Fired { target, ..Fired::new(command.clone(), source, FirePhase::Press, pass::now(ctx)) });
true
}
@@ -1186,6 +1186,8 @@ pub(crate) fn begin(ctx: &Context) {
let Some((keys, mut matcher)) = taken else { return };
let mut fired = Vec::new();
for shortcut in &shortcuts.keys {
// When the key event arrived: a frame may run it later, and a double press and a hold read the key's own times.
let time = shortcut.at.unwrap_or(now);
match shortcut.phase {
FirePhase::Release => {
let key = shortcut.input.physical.or(shortcut.input.typed);
@@ -1198,11 +1200,12 @@ pub(crate) fn begin(ctx: &Context) {
phase: FirePhase::Release,
cycle: false,
alt_variant: false,
time,
});
}
}
_ => {
let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, shortcuts.typing && !shortcut.canvas, Some((&mut matcher, now))) else {
let Some(hit) = keys.press(&shortcuts.layout, &shortcut.input, shortcuts.typing && !shortcut.canvas, Some((&mut matcher, time))) else {
continue;
};
let (app, instance) = with(ctx, |board| board.target_of(ctx, &hit.command, &board.here));
@@ -1228,6 +1231,7 @@ pub(crate) fn begin(ctx: &Context) {
phase: FirePhase::Press,
cycle: hit.cycle.len() > 1,
alt_variant: hit.alt_variant,
time,
});
}
}
@@ -123,12 +123,17 @@ pub struct Fired {
pub cycle: bool,
/// The chord pressed was a bound chord of the command with Alt added, and the command opted in.
pub alt_variant: bool,
/// The input time, in seconds of egui's input clock, of the event that fired it: for a key, when its press or
/// release arrived, also when the runtime held the event back to a later frame; the pass time for the other
/// sources. A key's hold is the time between its press and its release, however late a frame runs them.
pub time: f64,
}
impl Fired {
/// `command` fired from `source` in `phase`, for the app scope (no target), without a group cycle or the Alt variant.
pub fn new(command: CommandRef, source: FireSource, phase: FirePhase) -> Fired {
Fired { command, target: None, source, phase, cycle: false, alt_variant: false }
/// `command` fired from `source` in `phase` at the input time `time`, for the app scope (no target), without a group
/// cycle or the Alt variant.
pub fn new(command: CommandRef, source: FireSource, phase: FirePhase, time: f64) -> Fired {
Fired { command, target: None, source, phase, cycle: false, alt_variant: false, time }
}
}
+36 -10
View File
@@ -12,21 +12,29 @@ use egui::{Context, Event, Id, ImeEvent, Key, MouseWheelSource, MouseWheelUnit,
pub use egui::RawInput;
/// The events that wait for the next frame, for one viewport: the one-press rule's and those the key stage cuts at a
/// focus move (`keys::input_hook`).
/// focus move (`keys::input_hook`). Each keeps the input time it arrived at, `None` until the begin hook of the frame
/// it arrived in reads the clock (`stamp`).
#[derive(Default)]
pub(crate) struct HeldInput {
events: Vec<Event>,
events: Vec<(Event, Option<f64>)>,
frame: Option<u64>,
}
impl HeldInput {
/// Holds `events`, which a later step of this pass's input hook cut, for the next frame, ahead of the events held
/// already: they arrived before them.
pub(crate) fn hold_ahead(&mut self, mut events: Vec<Event>) {
pub(crate) fn hold_ahead(&mut self, mut events: Vec<(Event, Option<f64>)>) {
events.append(&mut self.events);
self.events = events;
}
/// Gives the events this frame's input hook held the frame's input time `now`, which the begin hook reads.
pub(crate) fn stamp(&mut self, now: f64) {
for (_, arrived) in self.events.iter_mut().filter(|(_, arrived)| arrived.is_none()) {
*arrived = Some(now);
}
}
/// Whether events wait for the next frame.
#[cfg(feature = "testing")]
pub(crate) fn holds(&self) -> bool {
@@ -34,21 +42,39 @@ impl HeldInput {
}
}
/// The events an earlier frame held that lead this frame's input, in order, each with the input time it arrived at.
#[derive(Default)]
pub(crate) struct Refed(Vec<(Event, f64)>);
impl Refed {
/// The input time `event` arrived at when it is a held event, read in input order; `None` for an event of this
/// frame's own input, which arrived at the pass time. Held events lead the input and a stage may have removed some,
/// so the held events before a match are gone, and the first event that matches none ends them.
pub(crate) fn arrival(&mut self, event: &Event) -> Option<f64> {
let Some(at) = self.0.iter().position(|(held, _)| held == event) else {
self.0.clear();
return None;
};
self.0.drain(..=at).last().map(|(_, arrived)| arrived)
}
}
/// H0: this pass carries at most one pointer press, as its last pointer event. Events after the first press
/// wait in `held` and lead the next frame's events. A repeated pass of the same frame, a discard pass,
/// receives no input events, so it gets none of the held ones. Returns whether events were held, so the hook
/// asks for a repaint.
pub(crate) fn hold_back_presses(input: &mut RawInput, frame: u64, held: &mut HeldInput) -> bool {
/// asks for a repaint, and the held events this pass's input carries with their arrival times.
pub(crate) fn hold_back_presses(input: &mut RawInput, frame: u64, held: &mut HeldInput) -> (bool, Refed) {
if held.frame == Some(frame) {
return false;
return (false, Refed::default());
}
held.frame = Some(frame);
let mut events = std::mem::take(&mut held.events);
events.append(&mut input.events);
let (now, later) = split_after_first_press(events, |event| matches!(event, Event::PointerButton { pressed: true, .. }));
input.events = now;
events.extend(input.events.drain(..).map(|event| (event, None)));
let (now, later) = split_after_first_press(events, |(event, _)| matches!(event, Event::PointerButton { pressed: true, .. }));
let refed = now.iter().filter_map(|(event, arrived)| arrived.map(|arrived| (event.clone(), arrived))).collect();
input.events = now.into_iter().map(|(event, _)| event).collect();
held.events = later;
!held.events.is_empty()
(!held.events.is_empty(), Refed(refed))
}
/// Takes key presses and texts out of the running pass's input. `places` and `texts` are the places the key stage
+4 -2
View File
@@ -569,10 +569,10 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) {
crate::runtime::env::system::input_hook(ctx, &input.events);
// H0: one press per pass.
let frame = pass::frame_nr(ctx, viewport);
let held_back = PerViewport::<ViewportRecord>::with(ctx, viewport, |record| raw_input::hold_back_presses(input, frame, &mut record.held));
let (held_back, refed) = PerViewport::<ViewportRecord>::with(ctx, viewport, |record| raw_input::hold_back_presses(input, frame, &mut record.held));
// H3: the key stage strips what the runtime owns; egui decides on a repaint from the events left.
ime::input_hook(ctx, &mut input.events, viewport);
let (stripped, later) = keys::input_hook(ctx, input);
let (stripped, later) = keys::input_hook(ctx, input, refed);
if !later.is_empty() {
PerViewport::<ViewportRecord>::with(ctx, viewport, |record| record.held.hold_ahead(later));
}
@@ -602,6 +602,8 @@ pub(crate) fn begin_pass(ui: &mut Ui) {
let now = pass::now(&ctx);
let (pass_nr, frame_nr) = (pass::pass_nr(&ctx, viewport), ctx.cumulative_frame_nr_for(viewport));
let (elapsed, cancelled) = PerViewport::<ViewportRecord>::with(&ctx, viewport, |record| {
// The events this frame's input hook held arrived at the frame's input time.
record.held.stamp(now);
let elapsed = record.now.replace(now).map_or(0.0, |previous| (now - previous).max(0.0));
(elapsed, std::mem::take(&mut record.press_cancelled))
});
+3
View File
@@ -91,6 +91,9 @@ pub(crate) struct Shortcut {
press: Option<usize>,
/// The press is the canvas's Space ([`Press::canvas`]): it fires its command whatever the focused stop types.
pub(crate) canvas: bool,
/// The input time the event arrived at when an earlier frame held it back; `None` for the pass's own input, which
/// arrived at the pass time.
pub(crate) at: Option<f64>,
}
/// What the intent routing did with one press.
+17 -14
View File
@@ -3,7 +3,7 @@
use super::route::layout_of;
use super::{AltTrack, Batch, LastPress, MenuKey, NavKeys, Press, Shortcut, Stage, TabPress, Typed, current, with};
use crate::runtime::commands::{self, EditCommands, FirePhase, NavCommand, WindowShortcut};
use crate::runtime::egui::input::{self as raw_input, KeyEvent, RawInput};
use crate::runtime::egui::input::{self as raw_input, KeyEvent, RawInput, Refed};
use crate::runtime::env::Env;
use crate::runtime::focus::{self, EndKey, KeyUse};
use crate::runtime::frame::{ChromeToggle, request_chrome_toggle};
@@ -53,8 +53,9 @@ impl Strip<'_> {
/// Records `event` in `batch` and returns whether egui keeps it.
///
/// `consumed` holds the keys whose press the strip took and whose release has not come yet: the release goes
/// with the press, in this pass or a later one.
fn event(&self, batch: &mut Batch, alt: &mut AltTrack, consumed: &mut Vec<Key>, event: KeyEvent) -> bool {
/// with the press, in this pass or a later one. `at` is the input time the event arrived at when an earlier frame
/// held it (`Refed`), `None` for this pass's own input.
fn event(&self, batch: &mut Batch, alt: &mut AltTrack, consumed: &mut Vec<Key>, event: KeyEvent, at: Option<f64>) -> bool {
if matches!(event, KeyEvent::Clip { .. }) {
// A clipboard event stands for its chord's press, which types nothing.
batch.last_press = None;
@@ -67,7 +68,7 @@ impl Strip<'_> {
}
let pushed = batch.presses.len();
let claims = batch.claimed.len();
let kept = self.press(batch, key, &input, repeat);
let kept = self.press(batch, key, &input, repeat, at);
// The canvas's Space keeps its text from every field, as a claim does.
let canvas = batch.presses.get(pushed).is_some_and(|press| press.canvas);
batch.last_press = Some(LastPress {
@@ -105,7 +106,7 @@ impl Strip<'_> {
if key == Some(Key::Enter) {
batch.enter_down = Some(false);
}
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, press: None, canvas: false });
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, press: None, canvas: false, at });
true
}
KeyEvent::Text(text) => {
@@ -150,9 +151,9 @@ impl Strip<'_> {
.any(|shortcut| shortcut.phase == FirePhase::Press && shortcut.input.mods == Mods::CMD && shortcut.input.typed == Some(key));
if !sent && self.records {
// A component that reads every chord gets the event as the chord's press, the way the key made it.
self.press(batch, key, &input, false);
self.press(batch, key, &input, false, at);
} else if !sent {
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None, canvas: false });
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, press: None, canvas: false, at });
}
// The event stands for the chord, so the shortcut path is its only reader: egui does not copy or paste
// on top of the command.
@@ -244,7 +245,7 @@ impl Strip<'_> {
}
}
fn press(&self, batch: &mut Batch, key: Key, input: &KeyInput, repeat: bool) -> bool {
fn press(&self, batch: &mut Batch, key: Key, input: &KeyInput, repeat: bool, at: Option<f64>) -> bool {
let mods = input.mods;
match self.use_of(key, input) {
PressUse::Claimed(chord) => {
@@ -261,7 +262,7 @@ impl Strip<'_> {
// keeps its momentary command from firing (production pans under a dialog whatever has focus).
batch.presses.push(Press { key, mods, repeat, in_egui: None, release: None, text: None, canvas: true });
if !repeat {
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: true });
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: true, at });
}
false
}
@@ -300,7 +301,7 @@ impl Strip<'_> {
PressUse::Press => {
batch.presses.push(Press { key, mods, repeat, in_egui: None, release: None, text: None, canvas: false });
if !repeat {
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: false });
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, press: Some(batch.presses.len() - 1), canvas: false, at });
}
true
}
@@ -389,8 +390,9 @@ pub(super) fn chrome_toggle_of(ctx: &Context, input: &KeyInput) -> Option<Chrome
/// The input hook's key steps (H3). Reads only `RawInput` and records keyed by `input.viewport_id`. Returns
/// whether it removed an event, so the hook asks for a repaint, and the events that wait for the next frame
/// (`Strip::ends_pass`), in order.
pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> (bool, Vec<egui::Event>) {
/// (`Strip::ends_pass`), in order, with the input time each arrived at when an earlier frame held it. `refed` is the
/// held events of earlier frames the input carries (H0).
pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput, mut refed: Refed) -> (bool, Vec<(egui::Event, Option<f64>)>) {
let viewport = input.viewport_id;
// The canvas a modal layer keeps open takes the events its reach admits and nothing else (`runtime::sampler`).
let (hole, trimmed) = sampler::hook(ctx, input);
@@ -444,8 +446,9 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> (bool, Vec<egui
stage.keyed = false;
input.events.retain(|event| {
let read = raw_input::key_event(event, os, &latin);
let at = refed.arrival(event);
if !later.is_empty() || strip.ends_pass(&batch, &read) {
later.push(event.clone());
later.push((event.clone(), at));
return false;
}
note_down(&mut stage.down, &read);
@@ -455,7 +458,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> (bool, Vec<egui
KeyEvent::Press { .. } | KeyEvent::Text(_) => Some(true),
_ => visible,
};
strip.event(&mut batch, &mut stage.alt, &mut stage.consumed, read)
strip.event(&mut batch, &mut stage.alt, &mut stage.consumed, read, at)
});
for (physical, mods, text) in std::mem::take(&mut batch.learned) {
stage.layout.observe(physical, mods, &text);
@@ -98,6 +98,41 @@ fn an_app_catalog_command_fires_by_its_chord_and_a_focused_text_field_keeps_a_ty
assert_eq!(*name.borrow(), "q", "the focused field types the letter");
}
/// A key's press and its release fire with the input times they arrived at, whichever frame runs them: a hold is
/// read from the key's own times. Keys behind a pointer press wait for the next frame (one press per pass) and keep
/// the time they arrived at.
#[trial]
fn a_keys_press_and_release_fire_with_the_times_they_arrived_at() {
let mut show = |ui: &mut Ui| {
AppShell::new(WindowKey::main()).show(ui, &mut ());
};
let mut frames = Frames::new();
frames.commands(catalog(1));
frames.idle(3, &mut show);
let times = |frames: &Frames| take_output(&frames.ctx).fired.into_iter().map(|fired| (fired.command.id, fired.phase, fired.time)).collect::<Vec<_>>();
let now = |frames: &Frames| frames.ctx.input(|input| input.time);
frames.frame(vec![key_press(Key::J, Modifiers::NONE)], &mut show);
let pressed = now(&frames);
frames.frame(vec![key_release(Key::J, Modifiers::NONE)], &mut show);
let released = now(&frames);
assert!(pressed < released);
assert_eq!(times(&frames), [(BRUSH.to_owned(), FirePhase::Press, pressed), (BRUSH.to_owned(), FirePhase::Release, released)]);
let at = egui::pos2(400.0, 300.0);
frames.frame(vec![moved(at)], &mut show);
let _ = times(&frames);
frames.frame(vec![press(at), key_press(Key::J, Modifiers::NONE), key_release(Key::J, Modifiers::NONE)], &mut show);
let arrived = now(&frames);
assert!(times(&frames).is_empty(), "the keys after the pointer press wait for the next frame");
frames.frame(vec![release(at)], &mut show);
assert!(now(&frames) > arrived);
assert_eq!(
times(&frames),
[(BRUSH.to_owned(), FirePhase::Press, arrived), (BRUSH.to_owned(), FirePhase::Release, arrived)],
"the held keys fire with the time they arrived at"
);
}
/// Count relation: frames given a catalog of the generation already built, even as a new value, build no keymap; a new
/// generation builds one, in its first frame, and its new command fires by its chord.
#[trial]