on master help sign in
tui: Allow toggling mouse capture at runtime (#6675)
ee7413aMichael Davis
helix-term/src/application.rs+5
@@ -361,6 +361,9 @@ impl Application {
361 ConfigEvent::Update(editor_config) => {
361 ConfigEvent::Update(editor_config) => {
362 let mut app_config = (*self.config.load().clone()).clone();
362 let mut app_config = (*self.config.load().clone()).clone();
363 app_config.editor = *editor_config;
363 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+ };
364 self.config.store(Arc::new(app_config));
367 self.config.store(Arc::new(app_config));
365 }
368 }
366 }
369 }
@@ -419,6 +422,8 @@ impl Application {
419 .map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?;
422 .map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?;
420 self.refresh_language_config()?;
423 self.refresh_language_config()?;
421 self.refresh_theme(&default_config)?;
424 self.refresh_theme(&default_config)?;
425+ self.terminal
426+ .reconfigure(default_config.editor.clone().into())?;
422 // Store new config
427 // Store new config
423 self.config.store(Arc::new(default_config));
428 self.config.store(Arc::new(default_config));
424 Ok(())
429 Ok(())
helix-tui/src/backend/crossterm.rs+16
@@ -63,6 +63,7 @@ pub struct CrosstermBackend<W: Write> {
63 buffer: W,
63 buffer: W,
64 capabilities: Capabilities,
64 capabilities: Capabilities,
65 supports_keyboard_enhancement_protocol: OnceCell<bool>,
65 supports_keyboard_enhancement_protocol: OnceCell<bool>,
66+ mouse_capture_enabled: bool,
66}
67}
67
68
68impl<W> CrosstermBackend<W>
69impl<W> CrosstermBackend<W>
@@ -74,6 +75,7 @@ where
74 buffer,
75 buffer,
75 capabilities: Capabilities::from_env_or_default(config),
76 capabilities: Capabilities::from_env_or_default(config),
76 supports_keyboard_enhancement_protocol: OnceCell::new(),
77 supports_keyboard_enhancement_protocol: OnceCell::new(),
78+ mouse_capture_enabled: false,
77 }
79 }
78 }
80 }
79
81
@@ -123,6 +125,7 @@ where
123 execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?;
125 execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?;
124 if config.enable_mouse_capture {
126 if config.enable_mouse_capture {
125 execute!(self.buffer, EnableMouseCapture)?;
127 execute!(self.buffer, EnableMouseCapture)?;
128+ self.mouse_capture_enabled = true;
126 }
129 }
127 if self.supports_keyboard_enhancement_protocol() {
130 if self.supports_keyboard_enhancement_protocol() {
128 execute!(
131 execute!(
@@ -136,6 +139,19 @@ where
136 Ok(())
139 Ok(())
137 }
140 }
138
141
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+
139 fn restore(&mut self, config: Config) -> io::Result<()> {
155 fn restore(&mut self, config: Config) -> io::Result<()> {
140 // reset cursor shape
156 // reset cursor shape
141 write!(self.buffer, "\x1B[0 q")?;
157 write!(self.buffer, "\x1B[0 q")?;
helix-tui/src/backend/mod.rs+1
@@ -14,6 +14,7 @@ pub use self::test::TestBackend;
14
14
15pub trait Backend {
15pub trait Backend {
16 fn claim(&mut self, config: Config) -> Result<(), io::Error>;
16 fn claim(&mut self, config: Config) -> Result<(), io::Error>;
17+ fn reconfigure(&mut self, config: Config) -> Result<(), io::Error>;
17 fn restore(&mut self, config: Config) -> Result<(), io::Error>;
18 fn restore(&mut self, config: Config) -> Result<(), io::Error>;
18 fn force_restore() -> Result<(), io::Error>;
19 fn force_restore() -> Result<(), io::Error>;
19 fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>
20 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 {
111 Ok(())
111 Ok(())
112 }
112 }
113
113
114+ fn reconfigure(&mut self, _config: Config) -> Result<(), io::Error> {
115+ Ok(())
116+ }
117+
114 fn restore(&mut self, _config: Config) -> Result<(), io::Error> {
118 fn restore(&mut self, _config: Config) -> Result<(), io::Error> {
115 Ok(())
119 Ok(())
116 }
120 }
helix-tui/src/terminal.rs+4
@@ -116,6 +116,10 @@ where
116 self.backend.claim(config)
116 self.backend.claim(config)
117 }
117 }
118
118
119+ pub fn reconfigure(&mut self, config: Config) -> io::Result<()> {
120+ self.backend.reconfigure(config)
121+ }
122+
119 pub fn restore(&mut self, config: Config) -> io::Result<()> {
123 pub fn restore(&mut self, config: Config) -> io::Result<()> {
120 self.backend.restore(config)
124 self.backend.restore(config)
121 }
125 }