From 0a1b85f35a222b976b8aa2ffaf72ec9b2c96353d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 11 Dec 2022 16:26:57 +0100 Subject: [PATCH] Revert key-repeat behavior (#2429) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revert key-repeat behavior This fixes key-repeats everywhere in egui where it was broken,including: - Enter in TextEdit:s - Arrow keys for sliders and dragvalues - … * Update changelog * Remove old comment --- CHANGELOG.md | 5 ++- crates/egui/src/data/input.rs | 12 +++---- crates/egui/src/input_state.rs | 33 +++++++++----------- crates/egui/src/widgets/text_edit/builder.rs | 4 --- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c16ecb23954..b883358a781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,11 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG ## Unreleased +### Changed 🔧 +* `InputState`: all press functions again include key repeates (like in egui 0.19) ([#2429](https://github.com/emilk/egui/pull/2429)). + ### Fixed 🐛 -* Fix key-repeat for backspace and arrow keys in `TextEdit` ([#2416](https://github.com/emilk/egui/pull/2416)). +* Fix key-repeats for `TextEdit`, `Slider`s, etc ([#2429](https://github.com/emilk/egui/pull/2429)). ## 0.20.0 - 2022-12-08 - AccessKit, prettier text, overlapping widgets diff --git a/crates/egui/src/data/input.rs b/crates/egui/src/data/input.rs index b3fbcba081e..5dac1c34f3f 100644 --- a/crates/egui/src/data/input.rs +++ b/crates/egui/src/data/input.rs @@ -1,5 +1,7 @@ //! The input needed by egui. +#![allow(deprecated)] // TODO(emilk): remove + use crate::emath::*; /// What the integrations provides to egui at the start of each frame. @@ -191,13 +193,9 @@ pub enum Event { modifiers: Modifiers, }, - /// A key was repeated while pressed. - KeyRepeat { - key: Key, - - /// The state of the modifier keys at the time of the event. - modifiers: Modifiers, - }, + /// DEPRECATED - DO NOT USE + #[deprecated = "Do not use"] + KeyRepeat { key: Key, modifiers: Modifiers }, /// The mouse or touch moved to a new place. PointerMoved(Pos2), diff --git a/crates/egui/src/input_state.rs b/crates/egui/src/input_state.rs index 0d5593d8c3b..1ae2f12decc 100644 --- a/crates/egui/src/input_state.rs +++ b/crates/egui/src/input_state.rs @@ -166,20 +166,10 @@ impl InputState { let mut zoom_factor_delta = 1.0; for event in &mut new.events { match event { - Event::Key { - key, - pressed, - modifiers, - } => { + Event::Key { key, pressed, .. } => { if *pressed { - // We only retain presses that are novel (i.e. the first Press event, not those generated by key-repeat) - // key repeats are represented by KeyRepeat. - if !keys_down.insert(*key) { - *event = Event::KeyRepeat { - key: *key, - modifiers: *modifiers, - }; - } + keys_down.insert(*key); + // TODO(emilk): detect key repeats and mark the event accordingly! } else { keys_down.remove(key); } @@ -262,6 +252,8 @@ impl InputState { } /// Count presses of a key. If non-zero, the presses are consumed, so that this will only return non-zero once. + /// + /// Includes key-repeat events. pub fn count_and_consume_key(&mut self, modifiers: Modifiers, key: Key) -> usize { let mut count = 0usize; @@ -284,6 +276,8 @@ impl InputState { } /// Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once. + /// + /// Includes key-repeat events. pub fn consume_key(&mut self, modifiers: Modifiers, key: Key) -> bool { self.count_and_consume_key(modifiers, key) > 0 } @@ -291,28 +285,31 @@ impl InputState { /// Check if the given shortcut has been pressed. /// /// If so, `true` is returned and the key pressed is consumed, so that this will only return `true` once. + /// + /// Includes key-repeat events. pub fn consume_shortcut(&mut self, shortcut: &KeyboardShortcut) -> bool { let KeyboardShortcut { modifiers, key } = *shortcut; self.consume_key(modifiers, key) } /// Was the given key pressed this frame? + /// + /// Includes key-repeat events. pub fn key_pressed(&self, desired_key: Key) -> bool { self.num_presses(desired_key) > 0 } /// How many times was the given key pressed this frame? + /// + /// Includes key-repeat events. pub fn num_presses(&self, desired_key: Key) -> usize { self.events .iter() .filter(|event| { matches!( event, - Event::Key { - key, - pressed: true, - .. - } if *key == desired_key + Event::Key { key, pressed: true, .. } + if *key == desired_key ) }) .count() diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 97737116667..bc4f768a908 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -919,10 +919,6 @@ fn events( modifiers, } => on_key_press(&mut cursor_range, text, galley, *key, modifiers), - Event::KeyRepeat { key, modifiers } => { - on_key_press(&mut cursor_range, text, galley, *key, modifiers) - } - Event::CompositionStart => { state.has_ime = true; None