drop poll in input thread; may revert but it improves performance

The issue seems to be related to a crossterm update, but not sure.
This commit is contained in:
ClementTsang 2021-12-31 17:41:08 -05:00
parent 9340044648
commit b60704ccc1
3 changed files with 37 additions and 43 deletions

View File

@ -192,6 +192,14 @@ impl AppState {
// FIXME: Redraw with new screen, save old screen state if main
}
}
fn toggle_freeze(&mut self) -> Status {
self.frozen_state = match self.frozen_state {
FrozenState::NotFrozen => FrozenState::Frozen(Box::new(self.data_collection.clone())),
FrozenState::Frozen(_) => FrozenState::NotFrozen,
};
Status::Captured
}
}
impl Application for AppState {
@ -296,6 +304,7 @@ impl Application for AppState {
Event::Keyboard(event) => {
if event.modifiers.is_empty() {
match event.code {
KeyCode::Char('f') | KeyCode::Char('F') => self.toggle_freeze(),
KeyCode::Char('q') | KeyCode::Char('Q') => on_quit(messages),
_ => Status::Ignored,
}

View File

@ -38,7 +38,7 @@ fn main() -> Result<()> {
// Set up input handling
let (sender, receiver) = mpsc::channel();
let input_thread = create_input_thread(sender.clone(), thread_termination_lock.clone());
let _input_thread = create_input_thread(sender.clone());
// Cleaning loop
// TODO: [Refactor, Optimization (Potentially, maybe not)] Probably worth spinning this off into an async thread or something...
@ -95,7 +95,6 @@ fn main() -> Result<()> {
*thread_termination_lock.lock().unwrap() = true;
thread_termination_cvar.notify_all();
let _ = input_thread.join();
cleanup_terminal(&mut terminal);
Ok(())

View File

@ -183,7 +183,6 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) {
pub fn create_input_thread(
sender: std::sync::mpsc::Sender<RuntimeEvent<AppMessages>>,
termination_ctrl_lock: Arc<Mutex<bool>>,
) -> std::thread::JoinHandle<()> {
thread::spawn(move || {
// TODO: [Optimization, Input] Maybe experiment with removing these timers. Look into using buffers instead?
@ -191,16 +190,6 @@ pub fn create_input_thread(
let mut keyboard_timer = Instant::now();
loop {
if let Ok(is_terminated) = termination_ctrl_lock.try_lock() {
// We don't block.
if *is_terminated {
drop(is_terminated);
break;
}
}
if let Ok(poll) = poll(Duration::from_millis(20)) {
if poll {
if let Ok(event) = read() {
match event {
crossterm::event::Event::Key(event) => {
@ -218,8 +207,7 @@ pub fn create_input_thread(
MouseEventKind::Drag(_) => {}
MouseEventKind::Moved => {}
_ => {
if Instant::now().duration_since(mouse_timer).as_millis() >= 20
{
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
if sender
.send(RuntimeEvent::UserInterface(Event::Mouse(event)))
.is_err()
@ -238,8 +226,6 @@ pub fn create_input_thread(
}
}
}
}
}
})
}