on master help sign in
Don't render selections/cursors on views not in focus.
commit446a7e574359aa2da778d22ca9c5e3f98704fac7 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 4 Feb 2021, 16:49 (6 years ago) parent448c1ab
2 files changed+24−20
helix-term/src/ui/editor.rs+21−14 helix-view/src/editor.rs+3−6
helix-term/src/ui/editor.rs+21−14
@@ -36,6 +36,7 @@ impl EditorView {
3636 viewport: Rect,
3737 surface: &mut Surface,
3838 theme: &Theme,
39+ is_focused: bool,
3940 ) {
4041 let area = Rect::new(
4142 viewport.x + OFFSET,
@@ -43,14 +44,14 @@ impl EditorView {
4344 viewport.width - OFFSET,
4445 viewport.height - 2,
4546 ); // - 2 for statusline and prompt
46− self.render_buffer(view, area, surface, theme);
47+ self.render_buffer(view, area, surface, theme, is_focused);
4748
4849 // clear with background color
4950 // TODO: this seems to prevent setting style later
5051 // surface.set_style(viewport, theme.get("ui.background"));
5152
5253 let area = Rect::new(viewport.x, viewport.height - 2, viewport.width, 1);
53− self.render_statusline(&view.doc, area, surface, theme);
54+ self.render_statusline(&view.doc, area, surface, theme, is_focused);
5455 }
5556
5657 // TODO: ideally not &mut View but highlights require it because of cursor cache
@@ -60,6 +61,7 @@ impl EditorView {
6061 viewport: Rect,
6162 surface: &mut Surface,
6263 theme: &Theme,
64+ is_focused: bool,
6365 ) {
6466 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
6567 let source_code = view.doc.text().to_string();
@@ -93,16 +95,20 @@ impl EditorView {
9395 let mut spans = Vec::new();
9496 let mut visual_x = 0;
9597 let mut line = 0u16;
96− let visible_selections: Vec<Range> = view
97− .doc
98− .state
99− .selection()
100− .ranges()
101− .iter()
102− // TODO: limit selection to one in viewport
103− // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
104− .copied()
105− .collect();
98+ let visible_selections: Vec<Range> = if is_focused {
99+ view.doc
100+ .state
101+ .selection()
102+ .ranges()
103+ .iter()
104+ // TODO: limit selection to one in viewport
105+ // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
106+ .copied()
107+ .collect()
108+ } else {
109+ // don't render selections on unfocused windows
110+ Vec::new()
111+ };
106112
107113 'outer: for event in highlights {
108114 match event.unwrap() {
@@ -233,6 +239,7 @@ impl EditorView {
233239 viewport: Rect,
234240 surface: &mut Surface,
235241 theme: &Theme,
242+ is_focused: bool,
236243 ) {
237244 let text_color = text_color();
238245 let mode = match doc.mode() {
@@ -322,9 +329,9 @@ impl Component for EditorView {
322329 // SAFETY: we cheat around the view_mut() borrow because it doesn't allow us to also borrow
323330 // theme. Theme is immutable mutating view won't disrupt theme_ref.
324331 let theme_ref = unsafe { &*(&cx.editor.theme as *const Theme) };
325− for view in cx.editor.tree.views() {
332+ for (view, is_focused) in cx.editor.tree.views() {
326333 // TODO: use parent area
327− self.render_view(view, view.area, surface, theme_ref);
334+ self.render_view(view, view.area, surface, theme_ref, is_focused);
328335 }
329336
330337 // TODO: drop unwrap
helix-view/src/editor.rs+3−6
@@ -10,7 +10,6 @@ use anyhow::Error;
1010pub struct Editor {
1111 pub tree: Tree,
1212 // pub documents: Vec<Document>,
13− pub focus: Key,
1413 pub should_close: bool,
1514 pub theme: Theme, // TODO: share one instance
1615 pub language_servers: helix_lsp::Registry,
@@ -23,7 +22,6 @@ impl Editor {
2322
2423 Self {
2524 tree: Tree::new(area),
26− focus: Key::default(),
2725 should_close: false,
2826 theme,
2927 language_servers,
@@ -52,16 +50,15 @@ impl Editor {
5250 }
5351
5452 let view = View::new(doc)?;
55− let pos = self.tree.insert(view);
56− self.focus = pos;
53+ self.tree.insert(view);
5754 Ok(())
5855 }
5956
6057 pub fn view(&self) -> &View {
61− self.tree.get(self.focus)
58+ self.tree.get(self.tree.focus)
6259 }
6360
6461 pub fn view_mut(&mut self) -> &mut View {
65− self.tree.get_mut(self.focus)
62+ self.tree.get_mut(self.tree.focus)
6663 }
6764}