From d097b5f1d5eb8e8be755f28f99b48614904ab89b Mon Sep 17 00:00:00 2001 From: Hendrik Norkowski Date: Fri, 21 Apr 2023 22:59:33 +0200 Subject: [PATCH] feat(ui): dimm cursor when terminal loses focus POC --- helix-term/src/ui/editor.rs | 26 ++++++++++++++++++++++++-- runtime/themes/catppuccin_mocha.toml | 2 ++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index fd8e8fb21b471..fa7cfbff94831 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -43,6 +43,8 @@ pub struct EditorView { pub(crate) last_insert: (commands::MappableCommand, Vec), pub(crate) completion: Option, spinners: ProgressSpinners, + // Tracks if the terminal window is focused by reaction to terminal focus events + terminal_focused: bool, } #[derive(Debug, Clone)] @@ -68,6 +70,7 @@ impl EditorView { last_insert: (commands::MappableCommand::normal_mode, Vec::new()), completion: None, spinners: ProgressSpinners::default(), + terminal_focused: true, } } @@ -148,6 +151,7 @@ impl EditorView { view, theme, &config.cursor_shape, + self.terminal_focused, ), ); let focused_view_elements = Self::highlight_focused_view_elements(view, doc, theme); @@ -391,6 +395,7 @@ impl EditorView { view: &View, theme: &Theme, cursor_shape_config: &CursorShapeConfig, + is_terminal_focused: bool, ) -> Vec<(usize, std::ops::Range)> { let text = doc.text().slice(..); let selection = doc.selection(view.id); @@ -412,6 +417,9 @@ impl EditorView { let base_primary_cursor_scope = theme .find_scope_index("ui.cursor.primary") .unwrap_or(base_cursor_scope); + let base_primary_cursor_unfocused_scope = theme + .find_scope_index("ui.cursor.primary_unfocused") + .unwrap_or(base_cursor_scope); let cursor_scope = match mode { Mode::Insert => theme.find_scope_index_exact("ui.cursor.insert"), @@ -426,12 +434,22 @@ impl EditorView { Mode::Normal => theme.find_scope_index_exact("ui.cursor.primary.normal"), } .unwrap_or(base_primary_cursor_scope); + let primary_cursor_unfocused_scope = match mode { + Mode::Insert => theme.find_scope_index_exact("ui.cursor.primary_unfocused.insert"), + Mode::Select => theme.find_scope_index_exact("ui.cursor.primary_unfocused.select"), + Mode::Normal => theme.find_scope_index_exact("ui.cursor.primary_unfocused.normal"), + } + .unwrap_or(base_primary_cursor_unfocused_scope); let mut spans: Vec<(usize, std::ops::Range)> = Vec::new(); for (i, range) in selection.iter().enumerate() { let selection_is_primary = i == primary_idx; let (cursor_scope, selection_scope) = if selection_is_primary { - (primary_cursor_scope, primary_selection_scope) + if is_terminal_focused { + (primary_cursor_scope, primary_selection_scope) + } else { + (primary_cursor_unfocused_scope, primary_selection_scope) + } } else { (cursor_scope, selection_scope) }; @@ -1344,13 +1362,17 @@ impl Component for EditorView { Event::Mouse(event) => self.handle_mouse_event(event, &mut cx), Event::IdleTimeout => self.handle_idle_timeout(&mut cx), - Event::FocusGained => EventResult::Ignored(None), + Event::FocusGained => { + self.terminal_focused = true; + EventResult::Consumed(None) + } Event::FocusLost => { if context.editor.config().auto_save { if let Err(e) = commands::typed::write_all_impl(context, false, false) { context.editor.set_error(format!("{}", e)); } } + self.terminal_focused = false; EventResult::Consumed(None) } } diff --git a/runtime/themes/catppuccin_mocha.toml b/runtime/themes/catppuccin_mocha.toml index 126613bc70b8a..a886be02d2c7b 100644 --- a/runtime/themes/catppuccin_mocha.toml +++ b/runtime/themes/catppuccin_mocha.toml @@ -93,6 +93,7 @@ "ui.cursor" = { fg = "base", bg = "secondary_cursor" } "ui.cursor.primary" = { fg = "base", bg = "rosewater" } +"ui.cursor.primary_unfocused" = { fg = "base", bg = "rosewater_dimmed" } "ui.cursor.match" = { fg = "peach", modifiers = ["bold"] } "ui.cursorline.primary" = { bg = "cursorline" } @@ -115,6 +116,7 @@ hint = "teal" [palette] # catppuccin palette colors rosewater = "#f5e0dc" +rosewater_dimmed = "#474249" flamingo = "#f2cdcd" pink = "#f5c2e7" mauve = "#cba6f7"