Keys: a press's release or a clipboard event ends the press a following text links to

This commit is contained in:
aiongg
2026-11-12 22:54:16 +00:00
parent 788c89b024
commit c08ad7196b
3 changed files with 43 additions and 3 deletions
+2 -1
View File
@@ -128,7 +128,8 @@ struct Batch {
menu: Vec<MenuKey>,
/// Whether the last Enter event of the pass was a press (`true`) or a release, `None` when there was none.
enter_down: Option<bool>,
/// The latest key press of the pass, which a following text event was typed by, and its place in `presses`.
/// The latest key press of the pass, which a following text event was typed by, and its place in `presses`; its
/// release or a clipboard event ends it.
last_press: Option<(KeyInput, Option<usize>)>,
/// What this pass's typed text taught: the physical key, the modifiers and the text.
learned: Vec<(Key, Mods, String)>,
+11 -2
View File
@@ -53,6 +53,10 @@ impl Strip<'_> {
/// `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 {
if matches!(event, KeyEvent::Clip { .. }) {
// A clipboard event stands for its chord's press, which types nothing.
batch.last_press = None;
}
match event {
KeyEvent::Press { key, input, repeat } => {
alt.other_key = true;
@@ -80,11 +84,16 @@ impl Strip<'_> {
kept
}
KeyEvent::Release { input } => {
if let Some(at) = input.physical.or(input.typed).and_then(|held| consumed.iter().position(|k| *k == held)) {
// The press's text comes before its release: a text after it is no press's.
let key = input.physical.or(input.typed);
if batch.last_press.as_ref().is_some_and(|(pressed, _)| pressed.physical.or(pressed.typed) == key) {
batch.last_press = None;
}
if let Some(at) = key.and_then(|held| consumed.iter().position(|k| *k == held)) {
consumed.swap_remove(at);
return false;
}
if input.physical.or(input.typed) == Some(Key::Enter) {
if key == Some(Key::Enter) {
batch.enter_down = Some(false);
}
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, typed: false, press: None });
@@ -272,6 +272,36 @@ fn arrows_wrap_at_the_ends_of_a_wrapping_list() {
assert_eq!(seen.leads().last(), Some(&last), "Up on the first row wraps to the last");
}
/// The keys whose presses reached egui's input in a pass, where the app reads its own keys.
fn app_read(ui: &Ui, saw: &RefCell<Vec<Key>>) {
ui.input(|input| {
saw.borrow_mut().extend(input.events.iter().filter_map(|event| match event {
Event::Key { key, pressed: true, .. } => Some(*key),
_ => None,
}));
});
}
/// A text that comes after a press's release was typed by no press: the type-ahead takes it, and the released press,
/// which typed nothing, still reaches the app.
#[trial]
fn a_text_after_a_release_takes_no_press_with_it() {
let words = Words::flat(&labels());
let seen = Seen::default();
let saw = RefCell::new(Vec::new());
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
app_read(ui, &saw);
show_words(ui, List::new(SelectMode::Single).type_ahead(), &words, &seen);
};
frames.idle(2, &mut show);
focus_list(&mut frames, &seen, &mut show);
let release = craft_ui::testing::focus::key_release(Key::F5, Modifiers::NONE);
frames.frame(vec![input::key_press(Key::F5, Modifiers::NONE), release, Event::Text("c".into())], &mut show);
assert_eq!(seen.leads().last(), Some(&4), "the type-ahead takes the text");
assert!(saw.borrow().contains(&Key::F5), "and the app reads the press that typed nothing");
}
/// A list without type-ahead leaves typed letters to the app's tool keys: the lead stays where it is.
#[trial]
fn a_list_without_type_ahead_leaves_typed_letters_alone() {