From 0197f65732e7c5890834307dbfb660d966418242 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Sun, 13 Oct 2024 18:37:32 +0200 Subject: [PATCH] Add function to manually control mouse-capture (#80) * feat(terminal): dont enable "MouseCapture" by default * feat(terminal): add function "enable_mouse_capture" and "disable_mouse_capture" --------- Co-authored-by: veeso --- CHANGELOG.md | 2 ++ src/terminal.rs | 12 ++++++++++++ src/terminal/adapter.rs | 6 ++++++ src/terminal/adapter/crossterm.rs | 10 ++++++++++ src/terminal/adapter/termion.rs | 8 ++++++++ 5 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9369adb..b6f42d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,8 @@ Released on 13/10/2024 - Added new `subclause_and!(Id::Foo, Id::Bar, Id::Baz)` and `subclause_or!(Id::Foo, Id::Bar, Id::Baz)` macros. - Removed `InputListener`. Now use `CrosstermInputListener` or `TermionInputListener`. - Bump `ratatui` version to `0.28` +- Dont enable `MouseCapture` by default +- Add function `enable_mouse_capture` and `disable_mouse_capture` to `TerminalBridge` ## 1.9.2 diff --git a/src/terminal.rs b/src/terminal.rs index 95d6feb..4b9ef28 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -41,6 +41,8 @@ pub enum TerminalError { CannotClear, #[error("backend doesn't support this command")] Unsupported, + #[error("cannot activate / deactivate mouse capture")] + CannotToggleMouseCapture, } /// An helper around [`Terminal`] to quickly setup and perform on terminal. @@ -148,6 +150,16 @@ where self.terminal.disable_raw_mode() } + /// Enable mouse-event capture, if the backend supports it + pub fn enable_mouse_capture(&mut self) -> TerminalResult<()> { + self.terminal.enable_mouse_capture() + } + + /// Disable mouse-event capture, if the backend supports it + pub fn disable_mouse_capture(&mut self) -> TerminalResult<()> { + self.terminal.disable_mouse_capture() + } + /// Draws a single frame to the terminal. /// /// Returns a [`CompletedFrame`] if successful, otherwise a [`TerminalError`]. diff --git a/src/terminal/adapter.rs b/src/terminal/adapter.rs index 87db013..131b3d3 100644 --- a/src/terminal/adapter.rs +++ b/src/terminal/adapter.rs @@ -54,4 +54,10 @@ pub trait TerminalAdapter { /// Leave the alternate screen using the terminal adapter fn leave_alternate_screen(&mut self) -> TerminalResult<()>; + + /// Enable mouse capture using the terminal adapter + fn enable_mouse_capture(&mut self) -> TerminalResult<()>; + + /// Disable mouse capture using the terminal adapter + fn disable_mouse_capture(&mut self) -> TerminalResult<()>; } diff --git a/src/terminal/adapter/crossterm.rs b/src/terminal/adapter/crossterm.rs index 4012fc0..26b1652 100644 --- a/src/terminal/adapter/crossterm.rs +++ b/src/terminal/adapter/crossterm.rs @@ -75,4 +75,14 @@ impl TerminalAdapter for CrosstermTerminalAdapter { ) .map_err(|_| TerminalError::CannotLeaveAlternateMode) } + + fn enable_mouse_capture(&mut self) -> TerminalResult<()> { + execute!(self.raw_mut().backend_mut(), EnableMouseCapture) + .map_err(|_| TerminalError::CannotToggleMouseCapture) + } + + fn disable_mouse_capture(&mut self) -> TerminalResult<()> { + execute!(self.raw_mut().backend_mut(), DisableMouseCapture) + .map_err(|_| TerminalError::CannotToggleMouseCapture) + } } diff --git a/src/terminal/adapter/termion.rs b/src/terminal/adapter/termion.rs index 1e49547..2357387 100644 --- a/src/terminal/adapter/termion.rs +++ b/src/terminal/adapter/termion.rs @@ -74,4 +74,12 @@ impl TerminalAdapter for TermionTerminalAdapter { fn leave_alternate_screen(&mut self) -> TerminalResult<()> { Err(TerminalError::Unsupported) } + + fn disable_mouse_capture(&mut self) -> TerminalResult<()> { + Err(TerminalError::Unsupported) + } + + fn enable_mouse_capture(&mut self) -> TerminalResult<()> { + Err(TerminalError::Unsupported) + } }