Fix external editor delay. (#1579)

The default polling rate of 1 second causes a 1 second delay when
queuing the event to launch the external editor, causing latency.

However, a slower polling helps reduce CPU usage, so let's
have a short polling duration as long as there are input events, and
slow poll otherwise.

Since the external editor among other components (not tested) is always
launched in response to an input event, we reduce the latency to ~100ms,
which is the fast poll duration.

Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com>
This commit is contained in:
Kieran Siek 2023-08-12 09:28:59 -07:00 committed by GitHub
parent 6ec647710d
commit bf31f20657
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,8 @@ use std::{
time::Duration,
};
static POLL_DURATION: Duration = Duration::from_millis(1000);
static FAST_POLL_DURATION: Duration = Duration::from_millis(100);
static SLOW_POLL_DURATION: Duration = Duration::from_millis(1000);
///
#[derive(Clone, Copy, Debug)]
@ -103,6 +104,7 @@ impl Input {
arc_current: &Arc<AtomicBool>,
tx: &Sender<InputEvent>,
) -> Result<()> {
let mut poll_duration = SLOW_POLL_DURATION;
loop {
if arc_desired.get() {
if !arc_current.load(Ordering::Relaxed) {
@ -112,14 +114,18 @@ impl Input {
}
arc_current.store(true, Ordering::Relaxed);
if let Some(e) = Self::poll(POLL_DURATION)? {
if let Some(e) = Self::poll(poll_duration)? {
// windows send key release too, only process key press
if let Key(key) = e {
if key.kind != KeyEventKind::Press {
continue;
}
}
tx.send(InputEvent::Input(e))?;
poll_duration = FAST_POLL_DURATION;
} else {
poll_duration = SLOW_POLL_DURATION;
}
} else {
if arc_current.load(Ordering::Relaxed) {