on master help sign in
use crossterm cursor in editor when out of focus (#6858)

Use crossterm cursor in the editor when the terminal is out of focus to achieve consistent out-of-focus cursor behaviour

commit71fd85894b71e4be3298fcb09b2165caee2968e0 authorHendrik Norkowski <hnorkowski@mail.de> dateMon 27 Nov 2023, 20:11 (3 years ago) parentb7f98d1
1 file changed+17−4
helix-term/src/ui/editor.rs+17−4
helix-term/src/ui/editor.rs+17−4
@@ -158,6 +158,7 @@ impl EditorView {
158158 view,
159159 theme,
160160 &config.cursor_shape,
161+ self.terminal_focused,
161162 ),
162163 );
163164 let focused_view_elements = Self::highlight_focused_view_elements(view, doc, theme);
@@ -420,6 +421,7 @@ impl EditorView {
420421 view: &View,
421422 theme: &Theme,
422423 cursor_shape_config: &CursorShapeConfig,
424+ is_terminal_focused: bool,
423425 ) -> Vec<(usize, std::ops::Range<usize>)> {
424426 let text = doc.text().slice(..);
425427 let selection = doc.selection(view.id);
@@ -490,13 +492,17 @@ impl EditorView {
490492 cursor_start
491493 };
492494 spans.push((selection_scope, range.anchor..selection_end));
493− if !selection_is_primary || cursor_is_block {
495+ // add block cursors
496+ // skip primary cursor if terminal is unfocused - crossterm cursor is used in that case
497+ if !selection_is_primary || (cursor_is_block && is_terminal_focused) {
494498 spans.push((cursor_scope, cursor_start..range.head));
495499 }
496500 } else {
497501 // Reverse case.
498502 let cursor_end = next_grapheme_boundary(text, range.head);
499− if !selection_is_primary || cursor_is_block {
503+ // add block cursors
504+ // skip primary cursor if terminal is unfocused - crossterm cursor is used in that case
505+ if !selection_is_primary || (cursor_is_block && is_terminal_focused) {
500506 spans.push((cursor_scope, range.head..cursor_end));
501507 }
502508 // non block cursors look like they exclude the cursor
@@ -1520,8 +1526,15 @@ impl Component for EditorView {
15201526
15211527 fn cursor(&self, _area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
15221528 match editor.cursor() {
1523− // All block cursors are drawn manually
1524− (pos, CursorKind::Block) => (pos, CursorKind::Hidden),
1529+ // all block cursors are drawn manually
1530+ (pos, CursorKind::Block) => {
1531+ if self.terminal_focused {
1532+ (pos, CursorKind::Hidden)
1533+ } else {
1534+ // use crossterm cursor when terminal loses focus
1535+ (pos, CursorKind::Underline)
1536+ }
1537+ }
15251538 cursor => cursor,
15261539 }
15271540 }