Skip to content

Commit

Permalink
fix: nullify_event_read(), mouse event output fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Mar 2, 2023
1 parent 495d5ae commit 93f7c07
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::Result;
use crossterm::{
event::{self, DisableMouseCapture, Event},
event::{self, DisableMouseCapture, Event, EnableMouseCapture},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use parking_lot::Mutex;
use std::{
io::{self, Stdout, Write},
sync::{atomic::Ordering, Arc},
time::Duration,
};
use std::{sync::atomic::AtomicBool, time::Instant};
use tokio::sync::mpsc::Sender;
Expand All @@ -33,13 +34,14 @@ pub struct Ui {
app_data: Arc<Mutex<AppData>>,
docker_sx: Sender<DockerMessage>,
gui_state: Arc<Mutex<GuiState>>,
input_poll_rate: Duration,
is_running: Arc<AtomicBool>,
now: Instant,
sender: Sender<InputMessages>,
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() {
io::stdout()
.write_all(
Expand Down Expand Up @@ -67,6 +69,7 @@ impl Ui {
app_data,
docker_sx,
gui_state,
input_poll_rate: std::time::Duration::from_millis(100),
is_running,
now: Instant::now(),
sender,
Expand All @@ -93,16 +96,23 @@ impl Ui {
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
pub fn reset_terminal(&mut self) -> Result<()> {
self.terminal.clear()?;

disable_raw_mode()?;
execute!(
self.terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
disable_raw_mode()?;
self.terminal.show_cursor()?;
Ok(())
}
Expand All @@ -114,11 +124,13 @@ impl Ui {
if self.now.elapsed() >= std::time::Duration::from_secs(1) {
seconds -= 1;
self.now = Instant::now();
if seconds < 1 {
break;
}
if seconds < 1 {
break;
}
}

// This is a fix for mouse-events being printed to screen
self.nullify_event_read();

if self
.terminal
Expand All @@ -133,7 +145,6 @@ impl Ui {

/// The loop for drawing the main UI to the terminal
async fn gui_loop(&mut self) -> Result<(), AppError> {
let input_poll_rate = std::time::Duration::from_millis(100);
let update_duration =
std::time::Duration::from_millis(u64::from(self.app_data.lock().args.docker_interval));

Expand All @@ -145,7 +156,7 @@ impl Ui {
{
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 Event::Key(key) = event {
self.sender
Expand Down Expand Up @@ -186,7 +197,7 @@ impl Ui {
} else {
self.gui_loop().await?;
}

self.nullify_event_read();
Ok(())
}
}
Expand Down

0 comments on commit 93f7c07

Please sign in to comment.