Skip to content

Commit

Permalink
feat(ui): dimm cursor when terminal loses focus POC
Browse files Browse the repository at this point in the history
  • Loading branch information
hnorkowski committed Apr 23, 2023
1 parent 61ff2bc commit d097b5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 24 additions & 2 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct EditorView {
pub(crate) last_insert: (commands::MappableCommand, Vec<InsertEvent>),
pub(crate) completion: Option<Completion>,
spinners: ProgressSpinners,
// Tracks if the terminal window is focused by reaction to terminal focus events
terminal_focused: bool,
}

#[derive(Debug, Clone)]
Expand All @@ -68,6 +70,7 @@ impl EditorView {
last_insert: (commands::MappableCommand::normal_mode, Vec::new()),
completion: None,
spinners: ProgressSpinners::default(),
terminal_focused: true,
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -391,6 +395,7 @@ impl EditorView {
view: &View,
theme: &Theme,
cursor_shape_config: &CursorShapeConfig,
is_terminal_focused: bool,
) -> Vec<(usize, std::ops::Range<usize>)> {
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
Expand All @@ -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"),
Expand All @@ -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<usize>)> = 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)
};
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/themes/catppuccin_mocha.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -115,6 +116,7 @@ hint = "teal"
[palette]
# catppuccin palette colors
rosewater = "#f5e0dc"
rosewater_dimmed = "#474249"
flamingo = "#f2cdcd"
pink = "#f5c2e7"
mauve = "#cba6f7"
Expand Down

0 comments on commit d097b5f

Please sign in to comment.