on master help sign in
tui: Allow toggling mouse capture at runtime (#6675)

This picks up changes to the `editor.mouse` option at runtime - either through `:set-option` or `:config-reload`. When the value changes, we tell the terminal to enable or disable mouse capture sequences.

commitee7413a3fd723015843e569924abbfbf32b56182 authorMichael Davis <mcarsondavis@gmail.com> dateThu 20 Apr 2023, 22:53 (4 years ago) parent78088ac
5 files changed+30
helix-term/src/application.rs+5 helix-tui/src/backend/crossterm.rs+16 helix-tui/src/backend/mod.rs+1 helix-tui/src/backend/test.rs+4 helix-tui/src/terminal.rs+4
helix-term/src/application.rs+5
@@ -361,6 +361,9 @@ impl Application {
361361 ConfigEvent::Update(editor_config) => {
362362 let mut app_config = (*self.config.load().clone()).clone();
363363 app_config.editor = *editor_config;
364+ if let Err(err) = self.terminal.reconfigure(app_config.editor.clone().into()) {
365+ self.editor.set_error(err.to_string());
366+ };
364367 self.config.store(Arc::new(app_config));
365368 }
366369 }
@@ -419,6 +422,8 @@ impl Application {
419422 .map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?;
420423 self.refresh_language_config()?;
421424 self.refresh_theme(&default_config)?;
425+ self.terminal
426+ .reconfigure(default_config.editor.clone().into())?;
422427 // Store new config
423428 self.config.store(Arc::new(default_config));
424429 Ok(())
helix-tui/src/backend/crossterm.rs+16
@@ -63,6 +63,7 @@ pub struct CrosstermBackend<W: Write> {
6363 buffer: W,
6464 capabilities: Capabilities,
6565 supports_keyboard_enhancement_protocol: OnceCell<bool>,
66+ mouse_capture_enabled: bool,
6667}
6768
6869impl<W> CrosstermBackend<W>
@@ -74,6 +75,7 @@ where
7475 buffer,
7576 capabilities: Capabilities::from_env_or_default(config),
7677 supports_keyboard_enhancement_protocol: OnceCell::new(),
78+ mouse_capture_enabled: false,
7779 }
7880 }
7981
@@ -123,6 +125,7 @@ where
123125 execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?;
124126 if config.enable_mouse_capture {
125127 execute!(self.buffer, EnableMouseCapture)?;
128+ self.mouse_capture_enabled = true;
126129 }
127130 if self.supports_keyboard_enhancement_protocol() {
128131 execute!(
@@ -136,6 +139,19 @@ where
136139 Ok(())
137140 }
138141
142+ fn reconfigure(&mut self, config: Config) -> io::Result<()> {
143+ if self.mouse_capture_enabled != config.enable_mouse_capture {
144+ if config.enable_mouse_capture {
145+ execute!(self.buffer, EnableMouseCapture)?;
146+ } else {
147+ execute!(self.buffer, DisableMouseCapture)?;
148+ }
149+ self.mouse_capture_enabled = config.enable_mouse_capture;
150+ }
151+
152+ Ok(())
153+ }
154+
139155 fn restore(&mut self, config: Config) -> io::Result<()> {
140156 // reset cursor shape
141157 write!(self.buffer, "\x1B[0 q")?;
helix-tui/src/backend/mod.rs+1
@@ -14,6 +14,7 @@ pub use self::test::TestBackend;
1414
1515pub trait Backend {
1616 fn claim(&mut self, config: Config) -> Result<(), io::Error>;
17+ fn reconfigure(&mut self, config: Config) -> Result<(), io::Error>;
1718 fn restore(&mut self, config: Config) -> Result<(), io::Error>;
1819 fn force_restore() -> Result<(), io::Error>;
1920 fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>
helix-tui/src/backend/test.rs+4
@@ -111,6 +111,10 @@ impl Backend for TestBackend {
111111 Ok(())
112112 }
113113
114+ fn reconfigure(&mut self, _config: Config) -> Result<(), io::Error> {
115+ Ok(())
116+ }
117+
114118 fn restore(&mut self, _config: Config) -> Result<(), io::Error> {
115119 Ok(())
116120 }
helix-tui/src/terminal.rs+4
@@ -116,6 +116,10 @@ where
116116 self.backend.claim(config)
117117 }
118118
119+ pub fn reconfigure(&mut self, config: Config) -> io::Result<()> {
120+ self.backend.reconfigure(config)
121+ }
122+
119123 pub fn restore(&mut self, config: Config) -> io::Result<()> {
120124 self.backend.restore(config)
121125 }