Skip to content

Commit 3a4b31d

Browse files
committed
Stop polling for events when in external editor
Adds a global atomic counter to indicate whether the background thread looking for input events should be polling or not. Even though this methods stops gitui snatching input events from the editor, it seems that _one_ key input event is still captured. It looks like it happens after approximately 2 seconds (the `MAX_POLL_DURATION` used by the event poller).
1 parent 29f58b7 commit 3a4b31d

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

Diff for: src/components/commit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ impl CommitComponent {
130130
anyhow!("unable to read editor command")
131131
})?;
132132

133+
crate::poll::SHOULD_DO_POLL
134+
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
133135
Command::new(command).args(editor).status()?;
136+
crate::poll::SHOULD_DO_POLL
137+
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
134138

135139
let mut message = String::new();
136140

Diff for: src/poll.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use asyncgit::AsyncNotification;
22
use crossbeam_channel::{unbounded, Receiver};
33
use crossterm::event::{self, Event};
4+
use std::sync::atomic::{AtomicUsize, Ordering};
45
use std::time::{Duration, Instant};
56

7+
// TODO: Do this properly!!!
8+
pub static SHOULD_DO_POLL: AtomicUsize = AtomicUsize::new(0);
69
///
710
#[derive(Clone, Copy)]
811
pub enum QueueEvent {
@@ -25,23 +28,27 @@ pub fn start_polling_thread() -> Receiver<Vec<QueueEvent>> {
2528
let mut batch = Vec::new();
2629

2730
loop {
28-
let timeout = if batch.is_empty() {
29-
MAX_POLL_DURATION
30-
} else {
31-
MIN_POLL_DURATION
32-
};
33-
if let Some(e) =
34-
poll(timeout).expect("failed to pull events.")
35-
{
36-
batch.push(QueueEvent::InputEvent(e));
37-
}
31+
// TODO: This doesn't start working immediately if it's currently
32+
// polling with a 2 seconds timeout duration
33+
if SHOULD_DO_POLL.load(Ordering::SeqCst) % 2 == 0 {
34+
let timeout = if batch.is_empty() {
35+
MAX_POLL_DURATION
36+
} else {
37+
MIN_POLL_DURATION
38+
};
39+
if let Some(e) =
40+
poll(timeout).expect("failed to pull events.")
41+
{
42+
batch.push(QueueEvent::InputEvent(e));
43+
}
3844

39-
if !batch.is_empty()
40-
&& last_send.elapsed() > MAX_BATCHING_DURATION
41-
{
42-
tx.send(batch).expect("send input event failed");
43-
batch = Vec::new();
44-
last_send = Instant::now();
45+
if !batch.is_empty()
46+
&& last_send.elapsed() > MAX_BATCHING_DURATION
47+
{
48+
tx.send(batch).expect("send input event failed");
49+
batch = Vec::new();
50+
last_send = Instant::now();
51+
}
4552
}
4653
}
4754
});

0 commit comments

Comments
 (0)