fix: correctly set gui error

This commit is contained in:
Jack Wills
2023-03-02 05:18:06 +00:00
parent cda0913616
commit 340893a860
2 changed files with 24 additions and 24 deletions
+9 -8
View File
@@ -80,22 +80,23 @@ impl InputHandler {
/// Toggle the mouse capture (via input of the 'm' key) /// Toggle the mouse capture (via input of the 'm' key)
fn m_key(&mut self) { fn m_key(&mut self) {
if self.mouse_capture { if self.mouse_capture {
match execute!(std::io::stdout(), DisableMouseCapture) { if execute!(std::io::stdout(), DisableMouseCapture).is_ok() {
Ok(_) => self self.gui_state
.gui_state
.lock() .lock()
.set_info_box("✖ mouse capture disabled".to_owned()), .set_info_box("✖ mouse capture disabled".to_owned());
Err(_) => { } else {
self.app_data self.app_data
.lock() .lock()
.set_error(AppError::MouseCapture(false)); .set_error(AppError::MouseCapture(false));
self.gui_state.lock().status_push(Status::Error);
} }
} } else if Ui::enable_mouse_capture().is_ok() {
} else {
Ui::enable_mouse_capture();
self.gui_state self.gui_state
.lock() .lock()
.set_info_box("✓ mouse capture enabled".to_owned()); .set_info_box("✓ mouse capture enabled".to_owned());
} else {
self.app_data.lock().set_error(AppError::MouseCapture(true));
self.gui_state.lock().status_push(Status::Error);
}; };
// If the info box sleep handle is currently being executed, as in 'm' is pressed twice within a 4000ms window // If the info box sleep handle is currently being executed, as in 'm' is pressed twice within a 4000ms window
+7 -8
View File
@@ -43,17 +43,16 @@ pub struct Ui {
impl Ui { impl Ui {
/// Enable mouse capture, but don't enable capture of all the mouse movements, doing so will improve performance, and is part of the fix for the weird mouse event output bug /// Enable mouse capture, but don't enable capture of all the mouse movements, doing so will improve performance, and is part of the fix for the weird mouse event output bug
pub fn enable_mouse_capture() { pub fn enable_mouse_capture() -> Result<()> {
io::stdout() io::stdout().write_all(
.write_all(
concat!( concat!(
crossterm::csi!("?1000h"), crossterm::csi!("?1000h"),
crossterm::csi!("?1015h"), crossterm::csi!("?1015h"),
crossterm::csi!("?1006h"), crossterm::csi!("?1006h"),
) )
.as_bytes(), .as_bytes(),
) )?;
.unwrap_or(()); Ok(())
} }
/// Create a new Ui struct, and execute the drawing loop /// Create a new Ui struct, and execute the drawing loop
@@ -87,13 +86,13 @@ impl Ui {
} }
/// Setup the terminal for full-screen drawing mode, with mouse capture /// Setup the terminal for full-screen drawing mode, with mouse capture
fn setup_terminal() -> io::Result<Terminal<CrosstermBackend<Stdout>>> { fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
enable_raw_mode()?; enable_raw_mode()?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen)?; execute!(stdout, EnterAlternateScreen)?;
Self::enable_mouse_capture(); Self::enable_mouse_capture()?;
let backend = CrosstermBackend::new(stdout); let backend = CrosstermBackend::new(stdout);
Terminal::new(backend) Ok(Terminal::new(backend)?)
} }
/// This is a fix for mouse-events being printed to screen, read an event and do nothing with it /// This is a fix for mouse-events being printed to screen, read an event and do nothing with it