From 8a224f93e6f9c76b71e19d0aa9c72f278770e5c7 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Sun, 24 Jul 2022 09:50:57 -0400 Subject: [PATCH] Autosave all when the terminal loses focus --- Cargo.lock | 3 +-- helix-term/Cargo.toml | 2 +- helix-term/src/application.rs | 6 +++++- helix-term/src/commands/typed.rs | 4 ++-- helix-term/src/ui/editor.rs | 8 ++++++++ helix-tui/Cargo.toml | 2 +- helix-view/Cargo.toml | 2 +- helix-view/src/input.rs | 10 +++++++++- 8 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb94d1e0809f..2df832311007 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,8 +132,7 @@ dependencies = [ [[package]] name = "crossterm" version = "0.24.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab9f7409c70a38a56216480fba371ee460207dd8926ccf5b4160591759559170" +source = "git+https://github.com/groves/crossterm.git?branch=emit_focus_events#d75ec2013df66acc106a4b924e6e3e1a11b0ef43" dependencies = [ "bitflags", "crossterm_winapi", diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index b0e22896a4ec..cc0235a7b80f 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -37,7 +37,7 @@ which = "4.2" tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] } tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] } -crossterm = { version = "0.24", features = ["event-stream"] } +crossterm = { version = "0.24.0", features = ["event-stream"], git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" } signal-hook = "0.3" tokio-stream = "0.1" futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 3ee5481f1f0c..34f305ec9a13 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -28,7 +28,9 @@ use std::{ use anyhow::{Context, Error}; use crossterm::{ - event::{DisableMouseCapture, EnableMouseCapture, Event}, + event::{ + DisableFocusChange, DisableMouseCapture, EnableFocusChange, EnableMouseCapture, Event, + }, execute, terminal, tty::IsTty, }; @@ -779,6 +781,7 @@ impl Application { let mut stdout = stdout(); execute!(stdout, terminal::EnterAlternateScreen)?; execute!(stdout, terminal::Clear(terminal::ClearType::All))?; + execute!(stdout, EnableFocusChange)?; if self.config.load().editor.mouse { execute!(stdout, EnableMouseCapture)?; } @@ -792,6 +795,7 @@ impl Application { // Ignore errors on disabling, this might trigger on windows if we call // disable without calling enable previously let _ = execute!(stdout, DisableMouseCapture); + execute!(stdout, DisableFocusChange)?; execute!(stdout, terminal::LeaveAlternateScreen)?; terminal::disable_raw_mode()?; Ok(()) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index d6db117e67e4..e4067111a83a 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -594,7 +594,7 @@ fn write_all_impl( bail!(errors) } -fn write_all( +pub fn write_all( cx: &mut compositor::Context, args: &[Cow], event: PromptEvent, @@ -2007,7 +2007,7 @@ pub static TYPABLE_COMMAND_MAP: Lazy self.handle_mouse_event(event, &mut cx), + Event::FocusGained => EventResult::Ignored(None), + Event::FocusLost => { + if let Err(e) = commands::typed::write_all(context, &[], PromptEvent::Validate) { + context.editor.set_error(format!("{}", e)); + } + EventResult::Consumed(None) + } } } diff --git a/helix-tui/Cargo.toml b/helix-tui/Cargo.toml index 25e32b500003..0318fd9e0bbd 100644 --- a/helix-tui/Cargo.toml +++ b/helix-tui/Cargo.toml @@ -19,7 +19,7 @@ default = ["crossterm"] bitflags = "1.3" cassowary = "0.3" unicode-segmentation = "1.9" -crossterm = { version = "0.24", optional = true } +crossterm = { version = "0.24.0", optional = true, git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" } serde = { version = "1", "optional" = true, features = ["derive"]} helix-view = { version = "0.6", path = "../helix-view", features = ["term"] } helix-core = { version = "0.6", path = "../helix-core" } diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 91921dd3ce68..cb3e62d6873f 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -19,7 +19,7 @@ anyhow = "1" helix-core = { version = "0.6", path = "../helix-core" } helix-lsp = { version = "0.6", path = "../helix-lsp" } helix-dap = { version = "0.6", path = "../helix-dap" } -crossterm = { version = "0.24", optional = true } +crossterm = { version = "0.24.0", optional = true, git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" } # Conversion traits once_cell = "1.13" diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs index 093006c4f831..e1f40ec465f2 100644 --- a/helix-view/src/input.rs +++ b/helix-view/src/input.rs @@ -221,7 +221,13 @@ impl<'de> Deserialize<'de> for KeyEvent { #[cfg(feature = "term")] impl From for KeyEvent { - fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self { + fn from( + crossterm::event::KeyEvent { + code, + modifiers, + kind: _, + }: crossterm::event::KeyEvent, + ) -> Self { if code == crossterm::event::KeyCode::BackTab { // special case for BackTab -> Shift-Tab let mut modifiers: KeyModifiers = modifiers.into(); @@ -249,11 +255,13 @@ impl From for crossterm::event::KeyEvent { crossterm::event::KeyEvent { code: crossterm::event::KeyCode::BackTab, modifiers: modifiers.into(), + kind: crossterm::event::KeyEventKind::Press, } } else { crossterm::event::KeyEvent { code: code.into(), modifiers: modifiers.into(), + kind: crossterm::event::KeyEventKind::Press, } } }