mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
ui-contract: a press of a key that is down already is a repeat, as egui reads it: egui-winit sends every press as no repeat and leaves egui to tell, so the key stage's repeat rules (shortcuts, Escape, Tab) never saw a held key's repeats; the trials that pressed one key twice release it between
This commit is contained in:
1 parent
47c6d53a1f
commit
fd80adccc0
7 files changed
+105
-10
No files matched your search
@@ -43,6 +43,7 @@ pub(crate) mod hosting {
|
||||
use crate::dialog::{DialogHost, DialogResult, FieldRefusal, HeldOk, StackDialog};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::testing::access::AccessTree;
|
||||
use crate::testing::focus::key_release;
|
||||
use crate::testing::input::key_press;
|
||||
use crate::testing::pointer::{click, moved, press, release};
|
||||
use crate::testing::{Frames, trial};
|
||||
@@ -109,7 +110,8 @@ pub(crate) mod hosting {
|
||||
host.borrow_mut().open(workspace());
|
||||
frames.idle(10, &mut ui);
|
||||
assert!(dialog_bounds(&mut frames).is_some(), "a reopened workspace shows");
|
||||
frames.frame(vec![key_press(Key::Escape, Modifiers::NONE)], &mut ui);
|
||||
// Each round releases its Escape: a second press with no release between is a repeat.
|
||||
frames.frame(vec![key_press(Key::Escape, Modifiers::NONE), key_release(Key::Escape, Modifiers::NONE)], &mut ui);
|
||||
frames.idle(2, &mut ui);
|
||||
assert_eq!(host.borrow_mut().take::<Hosted>(), Some(DialogResult::Cancel), "Escape cancels");
|
||||
}
|
||||
|
||||
@@ -77,8 +77,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. `down` holds the keys down before the event. `at` is the event's
|
||||
/// arrival time (`Arrived::at`).
|
||||
/// with the press, in this pass or a later one. `down` holds the keys down before the event; a press of a key
|
||||
/// already down is a repeat, as egui reads it, whatever the platform said. `at` is the event's arrival time
|
||||
/// (`Arrived::at`).
|
||||
fn event(&self, batch: &mut Batch, alt: &mut AltTrack, consumed: &mut Vec<Key>, down: &[Key], event: KeyEvent, at: Option<f64>) -> bool {
|
||||
if matches!(event, KeyEvent::Clip { .. }) {
|
||||
// A clipboard event stands for its chord's press, which types nothing.
|
||||
@@ -87,6 +88,7 @@ impl Strip<'_> {
|
||||
match event {
|
||||
KeyEvent::Press { key, input, repeat } => {
|
||||
alt.other_key = true;
|
||||
let repeat = repeat || input.physical.or(input.typed).is_some_and(|held| down.contains(&held));
|
||||
let pushed = batch.presses.len();
|
||||
let claims = batch.claimed.len();
|
||||
let menu_keys = batch.menu.len();
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::runtime::focus::{EditSet, FocusPolicy, FocusTarget, KeepKeys, KeyUse,
|
||||
use crate::runtime::frame::{ChromeToggle, take_output};
|
||||
use crate::runtime::keys::with_window_intents;
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use crate::testing::focus::{Stop, egui_focused, named_id, pop_scope, push_scope, register_stop, widget};
|
||||
use crate::testing::focus::{Stop, egui_focused, key_release, named_id, pop_scope, push_scope, register_stop, widget};
|
||||
use crate::testing::input::{key_press, key_reached, slop};
|
||||
use crate::testing::pointer::{self, moved, press, release};
|
||||
use crate::testing::sensing::{self, ProbeRoot};
|
||||
@@ -44,8 +44,9 @@ fn seen(host: &RefCell<Host>) -> (Vec<Key>, usize) {
|
||||
(std::mem::take(&mut host.reached), std::mem::take(&mut host.cancels))
|
||||
}
|
||||
|
||||
/// A press of `key` and its release, in one frame: a second press with no release between is a repeat.
|
||||
fn press_key(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), key: Key) {
|
||||
frames.frame(vec![key_press(key, Modifiers::NONE)], &mut *show);
|
||||
frames.frame(vec![key_press(key, Modifiers::NONE), key_release(key, Modifiers::NONE)], &mut *show);
|
||||
}
|
||||
|
||||
/// A window with a framework button and the host's own egui text field, with what the host saw.
|
||||
@@ -660,7 +661,7 @@ fn a_window_shortcut_runs_from_a_pane_title_and_not_from_a_modal_dialog_or_a_tex
|
||||
// The ids of the commands a press of the shortcut's key fired.
|
||||
let fired = |frames: &mut Frames, show: &mut dyn FnMut(&mut Ui)| -> Vec<String> {
|
||||
take_output(&frames.ctx);
|
||||
frames.frame(vec![key_press(Key::Q, Modifiers::NONE)], &mut *show);
|
||||
frames.frame(vec![key_press(Key::Q, Modifiers::NONE), key_release(Key::Q, Modifiers::NONE)], &mut *show);
|
||||
frames.frame(Vec::new(), &mut *show);
|
||||
take_output(&frames.ctx).fired.into_iter().map(|fired| fired.command.id).collect()
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::runtime::env::RootEnv;
|
||||
use crate::runtime::focus::{FocusTarget, KeyUse, request};
|
||||
use crate::runtime::frame::{ChromeToggle, take_output};
|
||||
use crate::runtime::keys::{focus_holds, intents_of, take_shortcuts};
|
||||
use crate::testing::focus::{Stop, egui_focused, named_id, press_shape, register_stop, widget};
|
||||
use crate::testing::focus::{Stop, egui_focused, key_release, named_id, press_shape, register_stop, widget};
|
||||
use crate::testing::input::{key_press, key_repeat};
|
||||
use crate::testing::pointer::click;
|
||||
use crate::testing::{Frames, trial};
|
||||
@@ -51,8 +51,9 @@ fn shown(enabled: bool, show: &mut impl FnMut(&mut Ui)) -> Frames {
|
||||
frames
|
||||
}
|
||||
|
||||
/// A press of `key` with `modifiers` and its release, in one frame: a second press with no release between is a repeat.
|
||||
fn press(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), key: Key, modifiers: Modifiers) -> Vec<(ViewportId, ChromeToggle)> {
|
||||
frames.frame(vec![key_press(key, modifiers)], &mut *show);
|
||||
frames.frame(vec![key_press(key, modifiers), key_release(key, modifiers)], &mut *show);
|
||||
take_output(&frames.ctx).chrome_toggles
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ pub(crate) mod memory;
|
||||
pub(crate) mod pen;
|
||||
pub(crate) mod pointer;
|
||||
pub(crate) mod theme_app;
|
||||
pub(crate) mod typed_keys;
|
||||
pub(crate) mod wheel;
|
||||
pub(crate) mod window_move;
|
||||
pub(crate) mod windows;
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
//! What the key stage makes of the events that carry text or a chord without a plain press of their own: a text with
|
||||
//! no press, a press an input method typed through with Alt, a second press of a held key, and a clipboard event with
|
||||
//! Shift held. Each trial binds the window's Toggle Panels command to the chord under test and
|
||||
//! reads whether it ran.
|
||||
|
||||
use craft_behavior::{EditCommand, Input};
|
||||
use craft_keymap::{Chord, Key as ChordKey, Mods, Overrides};
|
||||
use craft_ui::runtime::commands::{ChromeCommand, CommandInput, CommandSet as _};
|
||||
use craft_ui::runtime::focus::{FocusTarget, KeyUse, request};
|
||||
use craft_ui::runtime::frame::take_output;
|
||||
use craft_ui::testing::focus::{Stop, intents_of, key_release, named_id, register_stop, widget};
|
||||
use craft_ui::testing::input::{key_press, text};
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use egui::{Event, ImeEvent, Key, Modifiers, Ui};
|
||||
use std::cell::RefCell;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// What the window's text field read in a pass: the texts egui's input held for it and the edits routed to it.
|
||||
#[derive(Default)]
|
||||
struct Field {
|
||||
typed: Vec<String>,
|
||||
edits: Vec<EditCommand>,
|
||||
}
|
||||
|
||||
/// Frames of a window with a document canvas and a text field, whose Toggle Panels command is bound to `chord` alone.
|
||||
fn frames_for(chord: Chord) -> Frames {
|
||||
let mut overrides = Overrides::default();
|
||||
overrides.insert(ChromeCommand::TogglePanels.id(), vec![chord.to_string()]);
|
||||
let mut frames = Frames::new();
|
||||
frames.commands(CommandInput::new().overrides(Arc::new(overrides)));
|
||||
frames
|
||||
}
|
||||
|
||||
/// The window's pass: the canvas and the field, which records what it read into `field`.
|
||||
fn window(field: &RefCell<Field>) -> impl FnMut(&mut Ui) + '_ {
|
||||
move |ui: &mut Ui| {
|
||||
let (canvas, id) = (named_id("typed_canvas"), named_id("typed_field"));
|
||||
widget(ui, canvas);
|
||||
register_stop(ui.ctx(), Stop::new(canvas, KeyUse::CANVAS));
|
||||
widget(ui, id);
|
||||
register_stop(ui.ctx(), Stop::new(id, KeyUse::of(Input::Text)).text());
|
||||
let mut field = field.borrow_mut();
|
||||
ui.input(|input| {
|
||||
field.typed.extend(input.events.iter().filter_map(|event| match event {
|
||||
Event::Text(text) => Some(text.clone()),
|
||||
_ => None,
|
||||
}))
|
||||
});
|
||||
let mut intents = intents_of(ui.ctx(), Some(id), false);
|
||||
while let Some(edit) = intents.take_edit() {
|
||||
field.edits.push(edit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs one frame of `events` and returns whether the bound command ran, clearing what the field read before it.
|
||||
fn ran(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), field: &RefCell<Field>, events: Vec<Event>) -> bool {
|
||||
*field.borrow_mut() = Field::default();
|
||||
frames.frame(events, &mut *show);
|
||||
!take_output(&frames.ctx).chrome_toggles.is_empty()
|
||||
}
|
||||
|
||||
/// Settles the window with the field focused, or nothing focused.
|
||||
fn settle(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), focus_field: bool) {
|
||||
frames.idle(2, &mut *show);
|
||||
if focus_field {
|
||||
request(&frames.ctx, FocusTarget::Id(named_id("typed_field")));
|
||||
frames.idle(2, &mut *show);
|
||||
}
|
||||
take_output(&frames.ctx);
|
||||
}
|
||||
|
||||
/// A press of a key that is down already is a repeat, whatever the platform says: it runs no shortcut again. Guards
|
||||
/// against a key stage that read the repeat flag from the raw event only.
|
||||
#[trial]
|
||||
fn a_second_press_of_a_held_key_runs_no_shortcut_again() {
|
||||
let field = RefCell::new(Field::default());
|
||||
let mut frames = frames_for(Chord::plain(ChordKey::S));
|
||||
let mut show = window(&field);
|
||||
settle(&mut frames, &mut show, false);
|
||||
assert!(ran(&mut frames, &mut show, &field, vec![key_press(Key::S, Modifiers::NONE)]), "the first press runs the command");
|
||||
assert!(!ran(&mut frames, &mut show, &field, vec![key_press(Key::S, Modifiers::NONE)]), "a second press with S still down is a repeat");
|
||||
frames.frame(vec![key_release(Key::S, Modifiers::NONE)], &mut show);
|
||||
assert!(ran(&mut frames, &mut show, &field, vec![key_press(Key::S, Modifiers::NONE)]), "a press after the release runs it again");
|
||||
}
|
||||
@@ -4,6 +4,7 @@ use craft_ui::runtime::commands::{CommandState, Commands as _, EditCommands, Fir
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use craft_ui::runtime::frame::take_output;
|
||||
use craft_ui::runtime::measure::chord_texts;
|
||||
use craft_ui::testing::focus::key_release;
|
||||
use craft_ui::testing::input::key_press;
|
||||
use craft_ui::testing::pointer::{moved, press, release};
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
@@ -73,10 +74,12 @@ fn an_enclosing_scopes_shortcut_beats_the_applications() {
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(3, &mut show);
|
||||
focus_on(&mut frames, tap.get().unwrap(), &mut show);
|
||||
frames.frame(vec![key_press(Key::J, Modifiers::COMMAND | Modifiers::ALT)], &mut show);
|
||||
// Each press is released: a second press with no release between is a repeat.
|
||||
let chord = || vec![key_press(Key::J, Modifiers::COMMAND | Modifiers::ALT), key_release(Key::J, Modifiers::COMMAND | Modifiers::ALT)];
|
||||
frames.frame(chord(), &mut show);
|
||||
assert_eq!((closed.get(), take_output(&frames.ctx).fired.len()), (1, 0), "the dock takes it");
|
||||
focus_on(&mut frames, outside.get().unwrap(), &mut show);
|
||||
frames.frame(vec![key_press(Key::J, Modifiers::COMMAND | Modifiers::ALT)], &mut show);
|
||||
frames.frame(chord(), &mut show);
|
||||
assert_eq!((closed.get(), ids(&take_output(&frames.ctx).fired)), (1, vec!["trial.jump"]), "outside the dock the application's command fires");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user