fix: nullify_event_read(), mouse event output fix

This commit is contained in:
Jack Wills
2023-03-02 03:48:21 +00:00
parent 495d5ae789
commit 93f7c07f70
+17 -6
View File
@@ -1,6 +1,6 @@
use anyhow::Result; use anyhow::Result;
use crossterm::{ use crossterm::{
event::{self, DisableMouseCapture, Event}, event::{self, DisableMouseCapture, Event, EnableMouseCapture},
execute, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}; };
@@ -8,6 +8,7 @@ use parking_lot::Mutex;
use std::{ use std::{
io::{self, Stdout, Write}, io::{self, Stdout, Write},
sync::{atomic::Ordering, Arc}, sync::{atomic::Ordering, Arc},
time::Duration,
}; };
use std::{sync::atomic::AtomicBool, time::Instant}; use std::{sync::atomic::AtomicBool, time::Instant};
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
@@ -33,13 +34,14 @@ pub struct Ui {
app_data: Arc<Mutex<AppData>>, app_data: Arc<Mutex<AppData>>,
docker_sx: Sender<DockerMessage>, docker_sx: Sender<DockerMessage>,
gui_state: Arc<Mutex<GuiState>>, gui_state: Arc<Mutex<GuiState>>,
input_poll_rate: Duration,
is_running: Arc<AtomicBool>, is_running: Arc<AtomicBool>,
now: Instant, now: Instant,
sender: Sender<InputMessages>, sender: Sender<InputMessages>,
terminal: Terminal<CrosstermBackend<Stdout>>, terminal: Terminal<CrosstermBackend<Stdout>>,
} }
/// Enable moust capture, but don't enable all the mouse movements, which improves performance, and fixes the weird mouse event output bug /// Enable mouse capture, but don't enable all the mouse movements, which improves performance, and is part of the fix for the weird mouse event output bug
pub fn enable_mouse_capture() { pub fn enable_mouse_capture() {
io::stdout() io::stdout()
.write_all( .write_all(
@@ -67,6 +69,7 @@ impl Ui {
app_data, app_data,
docker_sx, docker_sx,
gui_state, gui_state,
input_poll_rate: std::time::Duration::from_millis(100),
is_running, is_running,
now: Instant::now(), now: Instant::now(),
sender, sender,
@@ -93,16 +96,23 @@ impl Ui {
Terminal::new(backend) Terminal::new(backend)
} }
// This is a fix for mouse-events being printed to screen, read an event and do nothing with it
fn nullify_event_read(&self) {
if crossterm::event::poll(self.input_poll_rate).unwrap_or(true) {
event::read().ok();
}
}
/// reset the terminal back to default settings /// reset the terminal back to default settings
pub fn reset_terminal(&mut self) -> Result<()> { pub fn reset_terminal(&mut self) -> Result<()> {
self.terminal.clear()?; self.terminal.clear()?;
disable_raw_mode()?;
execute!( execute!(
self.terminal.backend_mut(), self.terminal.backend_mut(),
LeaveAlternateScreen, LeaveAlternateScreen,
DisableMouseCapture DisableMouseCapture
)?; )?;
disable_raw_mode()?;
self.terminal.show_cursor()?; self.terminal.show_cursor()?;
Ok(()) Ok(())
} }
@@ -119,6 +129,8 @@ impl Ui {
} }
} }
// This is a fix for mouse-events being printed to screen
self.nullify_event_read();
if self if self
.terminal .terminal
@@ -133,7 +145,6 @@ impl Ui {
/// The loop for drawing the main UI to the terminal /// The loop for drawing the main UI to the terminal
async fn gui_loop(&mut self) -> Result<(), AppError> { async fn gui_loop(&mut self) -> Result<(), AppError> {
let input_poll_rate = std::time::Duration::from_millis(100);
let update_duration = let update_duration =
std::time::Duration::from_millis(u64::from(self.app_data.lock().args.docker_interval)); std::time::Duration::from_millis(u64::from(self.app_data.lock().args.docker_interval));
@@ -145,7 +156,7 @@ impl Ui {
{ {
return Err(AppError::Terminal); return Err(AppError::Terminal);
} }
if crossterm::event::poll(input_poll_rate).unwrap_or(false) { if crossterm::event::poll(self.input_poll_rate).unwrap_or(false) {
if let Ok(event) = event::read() { if let Ok(event) = event::read() {
if let Event::Key(key) = event { if let Event::Key(key) = event {
self.sender self.sender
@@ -186,7 +197,7 @@ impl Ui {
} else { } else {
self.gui_loop().await?; self.gui_loop().await?;
} }
self.nullify_event_read();
Ok(()) Ok(())
} }
} }