wallet: bugfix editbox chars randomly repeating. sometimes miniquad doesn't send the key_up event.

This commit is contained in:
darkfi
2024-07-04 16:58:37 +02:00
parent 2a08980807
commit 3ce7d4cdcb
3 changed files with 44 additions and 7 deletions

View File

@@ -309,6 +309,9 @@ pub struct GraphicsEventPublisher {
lock_resize: SyncMutex<Option<SubscriptionId>>,
resize: PublisherPtr<(f32, f32)>,
lock_char: SyncMutex<Option<SubscriptionId>>,
chr: PublisherPtr<(char, KeyMods, bool)>,
}
impl GraphicsEventPublisher {
@@ -320,6 +323,8 @@ impl GraphicsEventPublisher {
key_up: Publisher::new(),
lock_resize: SyncMutex::new(None),
resize: Publisher::new(),
lock_char: SyncMutex::new(None),
chr: Publisher::new(),
})
}
@@ -344,6 +349,13 @@ impl GraphicsEventPublisher {
*self.lock_resize.lock().unwrap() = None;
}
fn lock_char(&self, sub_id: SubscriptionId) {
*self.lock_char.lock().unwrap() = Some(sub_id);
}
fn unlock_char(&self) {
*self.lock_char.lock().unwrap() = None;
}
fn notify_key_down(&self, key: KeyCode, mods: KeyMods, repeat: bool) {
let ev = (key, mods, repeat);
@@ -374,6 +386,16 @@ impl GraphicsEventPublisher {
self.resize.notify(ev);
}
}
fn notify_char(&self, chr: char, mods: KeyMods, repeat: bool) {
let ev = (chr, mods, repeat);
let locked = self.lock_char.lock().unwrap().clone();
if let Some(locked) = locked {
self.chr.notify_with_include(ev, &[locked]);
} else {
self.chr.notify(ev);
}
}
pub fn subscribe_key_down(&self) -> Subscription<(KeyCode, KeyMods, bool)> {
self.key_down.clone().subscribe()
@@ -384,6 +406,9 @@ impl GraphicsEventPublisher {
pub fn subscribe_resize(&self) -> Subscription<(f32, f32)> {
self.resize.clone().subscribe()
}
pub fn subscribe_char(&self) -> Subscription<(char, KeyMods, bool)> {
self.chr.clone().subscribe()
}
}
struct Stage {
@@ -604,7 +629,7 @@ impl EventHandler for Stage {
}
fn char_event(&mut self, chr: char, mods: KeyMods, repeat: bool) {
//debug!("chr: {chr}, mods: {:?} repeat: {repeat}", mods);
self.event_pub.notify_char(chr, mods, repeat);
}
}

View File

@@ -110,7 +110,9 @@ fn main() {
miniquad::KeyCode::LeftShift | miniquad::KeyCode::LeftSuper => continue,
_ => {}
}
debug!(target: "main", "key_down event: {:?} {:?} {}", key, mods, repeat);
if !repeat {
debug!(target: "main", "key_down event: {:?} {:?} {}", key, mods, repeat);
}
}
});
async_runtime.push_task(ev_relay_task);

View File

@@ -1,4 +1,4 @@
use miniquad::{BufferId, KeyCode, KeyMods, TextureId};
use miniquad::{window, BufferId, KeyCode, KeyMods, TextureId};
use rand::{rngs::OsRng, Rng};
use std::{
collections::HashMap,
@@ -46,12 +46,15 @@ impl PressedKeysSmoothRepeat {
}
fn key_down(&mut self, key: KeyCode, repeat: bool) -> u32 {
debug!(target: "PressedKeysSmoothRepeat", "key_up({:?}, {})", key, repeat);
if !repeat {
self.pressed_keys.remove(&key);
return 1;
}
// Insert key if not exists
if !self.pressed_keys.contains_key(&key) {
debug!(target: "PressedKeysSmoothRepeat", "insert key {:?}", key);
self.pressed_keys.insert(key, RepeatingKeyTimer::new());
}
@@ -60,6 +63,8 @@ impl PressedKeysSmoothRepeat {
}
fn key_up(&mut self, key: &KeyCode) {
debug!(target: "PressedKeysSmoothRepeat", "key_up({:?})", key);
assert!(self.pressed_keys.contains_key(key));
self.pressed_keys.remove(key);
}
}
@@ -76,6 +81,8 @@ impl RepeatingKeyTimer {
fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
let elapsed = self.start.elapsed().as_millis();
debug!(target: "RepeatingKeyTimer", "update() elapsed={}, actions={}",
elapsed, self.actions);
if elapsed < start_delay as u128 {
return 0
}
@@ -160,6 +167,9 @@ impl EditBox {
)
.await;
// testing
//window::show_keyboard(true);
let self_ = Arc::new_cyclic(|me: &Weak<Self>| {
// Start a task monitoring for key down events
let ev_sub = event_pub.subscribe_key_down();
@@ -180,6 +190,7 @@ impl EditBox {
let mut repeater = self_.key_repeat.lock().unwrap();
repeater.key_down(key, repeat)
};
debug!(target: "ui::editbox", "Key {:?} has {} actions", key, actions);
for _ in 0..actions {
self_.do_key_action(&key, &mods).await;
}
@@ -200,10 +211,8 @@ impl EditBox {
panic!("self destroyed before key_up_task was stopped!");
};
let actions = {
let mut repeater = self_.key_repeat.lock().unwrap();
repeater.key_up(&key)
};
let mut repeater = self_.key_repeat.lock().unwrap();
repeater.key_up(&key);
}
});
@@ -457,6 +466,7 @@ fn keycode_to_string(key: &KeyCode) -> Option<String> {
KeyCode::O => Some("O".to_string()),
KeyCode::P => Some("P".to_string()),
KeyCode::Q => Some("Q".to_string()),
KeyCode::R => Some("R".to_string()),
KeyCode::S => Some("S".to_string()),
KeyCode::T => Some("T".to_string()),
KeyCode::U => Some("U".to_string()),