From 446a7e574359aa2da778d22ca9c5e3f98704fac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Thu, 4 Feb 2021 16:49:55 +0900 Subject: [PATCH] Don't render selections/cursors on views not in focus. --- helix-term/src/ui/editor.rs | 35 +++++++++++++++++++++-------------- helix-view/src/editor.rs | 9 +++------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 721dccc0..f05579e2 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -36,6 +36,7 @@ impl EditorView { viewport: Rect, surface: &mut Surface, theme: &Theme, + is_focused: bool, ) { let area = Rect::new( viewport.x + OFFSET, @@ -43,14 +44,14 @@ impl EditorView { viewport.width - OFFSET, viewport.height - 2, ); // - 2 for statusline and prompt - self.render_buffer(view, area, surface, theme); + self.render_buffer(view, area, surface, theme, is_focused); // clear with background color // TODO: this seems to prevent setting style later // surface.set_style(viewport, theme.get("ui.background")); let area = Rect::new(viewport.x, viewport.height - 2, viewport.width, 1); - self.render_statusline(&view.doc, area, surface, theme); + self.render_statusline(&view.doc, area, surface, theme, is_focused); } // TODO: ideally not &mut View but highlights require it because of cursor cache @@ -60,6 +61,7 @@ impl EditorView { viewport: Rect, surface: &mut Surface, theme: &Theme, + is_focused: bool, ) { // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|) let source_code = view.doc.text().to_string(); @@ -93,16 +95,20 @@ impl EditorView { let mut spans = Vec::new(); let mut visual_x = 0; let mut line = 0u16; - let visible_selections: Vec = view - .doc - .state - .selection() - .ranges() - .iter() - // TODO: limit selection to one in viewport - // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1)) - .copied() - .collect(); + let visible_selections: Vec = if is_focused { + view.doc + .state + .selection() + .ranges() + .iter() + // TODO: limit selection to one in viewport + // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1)) + .copied() + .collect() + } else { + // don't render selections on unfocused windows + Vec::new() + }; 'outer: for event in highlights { match event.unwrap() { @@ -233,6 +239,7 @@ impl EditorView { viewport: Rect, surface: &mut Surface, theme: &Theme, + is_focused: bool, ) { let text_color = text_color(); let mode = match doc.mode() { @@ -322,9 +329,9 @@ impl Component for EditorView { // SAFETY: we cheat around the view_mut() borrow because it doesn't allow us to also borrow // theme. Theme is immutable mutating view won't disrupt theme_ref. let theme_ref = unsafe { &*(&cx.editor.theme as *const Theme) }; - for view in cx.editor.tree.views() { + for (view, is_focused) in cx.editor.tree.views() { // TODO: use parent area - self.render_view(view, view.area, surface, theme_ref); + self.render_view(view, view.area, surface, theme_ref, is_focused); } // TODO: drop unwrap diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index eb745066..be400935 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -10,7 +10,6 @@ use anyhow::Error; pub struct Editor { pub tree: Tree, // pub documents: Vec, - pub focus: Key, pub should_close: bool, pub theme: Theme, // TODO: share one instance pub language_servers: helix_lsp::Registry, @@ -23,7 +22,6 @@ impl Editor { Self { tree: Tree::new(area), - focus: Key::default(), should_close: false, theme, language_servers, @@ -52,16 +50,15 @@ impl Editor { } let view = View::new(doc)?; - let pos = self.tree.insert(view); - self.focus = pos; + self.tree.insert(view); Ok(()) } pub fn view(&self) -> &View { - self.tree.get(self.focus) + self.tree.get(self.tree.focus) } pub fn view_mut(&mut self) -> &mut View { - self.tree.get_mut(self.focus) + self.tree.get_mut(self.tree.focus) } }