Skip to content

Commit

Permalink
Add function to manually control mouse-capture (#80)
Browse files Browse the repository at this point in the history
* feat(terminal): dont enable "MouseCapture" by default

* feat(terminal): add function "enable_mouse_capture" and "disable_mouse_capture"

---------

Co-authored-by: veeso <[email protected]>
  • Loading branch information
hasezoey and veeso authored Oct 13, 2024
1 parent 3c96b35 commit 0197f65
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`].
Expand Down
6 changes: 6 additions & 0 deletions src/terminal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;
}
10 changes: 10 additions & 0 deletions src/terminal/adapter/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions src/terminal/adapter/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 0197f65

Please sign in to comment.