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.
pull/6837/head
Michael Davis 2 years ago committed by GitHub
parent 78088ac185
commit ee7413a3fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -361,6 +361,9 @@ impl Application {
ConfigEvent::Update(editor_config) => { ConfigEvent::Update(editor_config) => {
let mut app_config = (*self.config.load().clone()).clone(); let mut app_config = (*self.config.load().clone()).clone();
app_config.editor = *editor_config; app_config.editor = *editor_config;
if let Err(err) = self.terminal.reconfigure(app_config.editor.clone().into()) {
self.editor.set_error(err.to_string());
};
self.config.store(Arc::new(app_config)); self.config.store(Arc::new(app_config));
} }
} }
@ -419,6 +422,8 @@ impl Application {
.map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?; .map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?;
self.refresh_language_config()?; self.refresh_language_config()?;
self.refresh_theme(&default_config)?; self.refresh_theme(&default_config)?;
self.terminal
.reconfigure(default_config.editor.clone().into())?;
// Store new config // Store new config
self.config.store(Arc::new(default_config)); self.config.store(Arc::new(default_config));
Ok(()) Ok(())

@ -63,6 +63,7 @@ pub struct CrosstermBackend<W: Write> {
buffer: W, buffer: W,
capabilities: Capabilities, capabilities: Capabilities,
supports_keyboard_enhancement_protocol: OnceCell<bool>, supports_keyboard_enhancement_protocol: OnceCell<bool>,
mouse_capture_enabled: bool,
} }
impl<W> CrosstermBackend<W> impl<W> CrosstermBackend<W>
@ -74,6 +75,7 @@ where
buffer, buffer,
capabilities: Capabilities::from_env_or_default(config), capabilities: Capabilities::from_env_or_default(config),
supports_keyboard_enhancement_protocol: OnceCell::new(), supports_keyboard_enhancement_protocol: OnceCell::new(),
mouse_capture_enabled: false,
} }
} }
@ -123,6 +125,7 @@ where
execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?; execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?;
if config.enable_mouse_capture { if config.enable_mouse_capture {
execute!(self.buffer, EnableMouseCapture)?; execute!(self.buffer, EnableMouseCapture)?;
self.mouse_capture_enabled = true;
} }
if self.supports_keyboard_enhancement_protocol() { if self.supports_keyboard_enhancement_protocol() {
execute!( execute!(
@ -136,6 +139,19 @@ where
Ok(()) Ok(())
} }
fn reconfigure(&mut self, config: Config) -> io::Result<()> {
if self.mouse_capture_enabled != config.enable_mouse_capture {
if config.enable_mouse_capture {
execute!(self.buffer, EnableMouseCapture)?;
} else {
execute!(self.buffer, DisableMouseCapture)?;
}
self.mouse_capture_enabled = config.enable_mouse_capture;
}
Ok(())
}
fn restore(&mut self, config: Config) -> io::Result<()> { fn restore(&mut self, config: Config) -> io::Result<()> {
// reset cursor shape // reset cursor shape
write!(self.buffer, "\x1B[0 q")?; write!(self.buffer, "\x1B[0 q")?;

@ -14,6 +14,7 @@ pub use self::test::TestBackend;
pub trait Backend { pub trait Backend {
fn claim(&mut self, config: Config) -> Result<(), io::Error>; fn claim(&mut self, config: Config) -> Result<(), io::Error>;
fn reconfigure(&mut self, config: Config) -> Result<(), io::Error>;
fn restore(&mut self, config: Config) -> Result<(), io::Error>; fn restore(&mut self, config: Config) -> Result<(), io::Error>;
fn force_restore() -> Result<(), io::Error>; fn force_restore() -> Result<(), io::Error>;
fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error> fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>

@ -111,6 +111,10 @@ impl Backend for TestBackend {
Ok(()) Ok(())
} }
fn reconfigure(&mut self, _config: Config) -> Result<(), io::Error> {
Ok(())
}
fn restore(&mut self, _config: Config) -> Result<(), io::Error> { fn restore(&mut self, _config: Config) -> Result<(), io::Error> {
Ok(()) Ok(())
} }

@ -116,6 +116,10 @@ where
self.backend.claim(config) self.backend.claim(config)
} }
pub fn reconfigure(&mut self, config: Config) -> io::Result<()> {
self.backend.reconfigure(config)
}
pub fn restore(&mut self, config: Config) -> io::Result<()> { pub fn restore(&mut self, config: Config) -> io::Result<()> {
self.backend.restore(config) self.backend.restore(config)
} }

Loading…
Cancel
Save