on master help sign in
Don't render selections/cursors on views not in focus.
446a7e5Blaž Hrastnik
helix-term/src/ui/editor.rs+21−14
@@ -36,6 +36,7 @@ impl EditorView {
36 viewport: Rect,
36 viewport: Rect,
37 surface: &mut Surface,
37 surface: &mut Surface,
38 theme: &Theme,
38 theme: &Theme,
39+ is_focused: bool,
39 ) {
40 ) {
40 let area = Rect::new(
41 let area = Rect::new(
41 viewport.x + OFFSET,
42 viewport.x + OFFSET,
@@ -43,14 +44,14 @@ impl EditorView {
43 viewport.width - OFFSET,
44 viewport.width - OFFSET,
44 viewport.height - 2,
45 viewport.height - 2,
45 ); // - 2 for statusline and prompt
46 ); // - 2 for statusline and prompt
46− self.render_buffer(view, area, surface, theme);
47+ self.render_buffer(view, area, surface, theme, is_focused);
47
48
48 // clear with background color
49 // clear with background color
49 // TODO: this seems to prevent setting style later
50 // TODO: this seems to prevent setting style later
50 // surface.set_style(viewport, theme.get("ui.background"));
51 // surface.set_style(viewport, theme.get("ui.background"));
51
52
52 let area = Rect::new(viewport.x, viewport.height - 2, viewport.width, 1);
53 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);
54 }
55 }
55
56
56 // TODO: ideally not &mut View but highlights require it because of cursor cache
57 // TODO: ideally not &mut View but highlights require it because of cursor cache
@@ -60,6 +61,7 @@ impl EditorView {
60 viewport: Rect,
61 viewport: Rect,
61 surface: &mut Surface,
62 surface: &mut Surface,
62 theme: &Theme,
63 theme: &Theme,
64+ is_focused: bool,
63 ) {
65 ) {
64 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
66 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
65 let source_code = view.doc.text().to_string();
67 let source_code = view.doc.text().to_string();
@@ -93,16 +95,20 @@ impl EditorView {
93 let mut spans = Vec::new();
95 let mut spans = Vec::new();
94 let mut visual_x = 0;
96 let mut visual_x = 0;
95 let mut line = 0u16;
97 let mut line = 0u16;
96− let visible_selections: Vec<Range> = view
98+ let visible_selections: Vec<Range> = if is_focused {
97− .doc
99+ view.doc
98− .state
100+ .state
99− .selection()
101+ .selection()
100− .ranges()
102+ .ranges()
101− .iter()
103+ .iter()
102− // TODO: limit selection to one in viewport
104+ // TODO: limit selection to one in viewport
103− // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
105+ // .filter(|range| !range.is_empty()) // && range.overlaps(&Range::new(start, end + 1))
104− .copied()
106+ .copied()
105− .collect();
107+ .collect()
108+ } else {
109+ // don't render selections on unfocused windows
110+ Vec::new()
111+ };
106
112
107 'outer: for event in highlights {
113 'outer: for event in highlights {
108 match event.unwrap() {
114 match event.unwrap() {
@@ -233,6 +239,7 @@ impl EditorView {
233 viewport: Rect,
239 viewport: Rect,
234 surface: &mut Surface,
240 surface: &mut Surface,
235 theme: &Theme,
241 theme: &Theme,
242+ is_focused: bool,
236 ) {
243 ) {
237 let text_color = text_color();
244 let text_color = text_color();
238 let mode = match doc.mode() {
245 let mode = match doc.mode() {
@@ -322,9 +329,9 @@ impl Component for EditorView {
322 // SAFETY: we cheat around the view_mut() borrow because it doesn't allow us to also borrow
329 // SAFETY: we cheat around the view_mut() borrow because it doesn't allow us to also borrow
323 // theme. Theme is immutable mutating view won't disrupt theme_ref.
330 // theme. Theme is immutable mutating view won't disrupt theme_ref.
324 let theme_ref = unsafe { &*(&cx.editor.theme as *const Theme) };
331 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() {
326 // TODO: use parent area
333 // 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);
328 }
335 }
329
336
330 // TODO: drop unwrap
337 // TODO: drop unwrap
helix-view/src/editor.rs+3−6
@@ -10,7 +10,6 @@ use anyhow::Error;
10pub struct Editor {
10pub struct Editor {
11 pub tree: Tree,
11 pub tree: Tree,
12 // pub documents: Vec<Document>,
12 // pub documents: Vec<Document>,
13− pub focus: Key,
14 pub should_close: bool,
13 pub should_close: bool,
15 pub theme: Theme, // TODO: share one instance
14 pub theme: Theme, // TODO: share one instance
16 pub language_servers: helix_lsp::Registry,
15 pub language_servers: helix_lsp::Registry,
@@ -23,7 +22,6 @@ impl Editor {
23
22
24 Self {
23 Self {
25 tree: Tree::new(area),
24 tree: Tree::new(area),
26− focus: Key::default(),
27 should_close: false,
25 should_close: false,
28 theme,
26 theme,
29 language_servers,
27 language_servers,
@@ -52,16 +50,15 @@ impl Editor {
52 }
50 }
53
51
54 let view = View::new(doc)?;
52 let view = View::new(doc)?;
55− let pos = self.tree.insert(view);
53+ self.tree.insert(view);
56− self.focus = pos;
57 Ok(())
54 Ok(())
58 }
55 }
59
56
60 pub fn view(&self) -> &View {
57 pub fn view(&self) -> &View {
61− self.tree.get(self.focus)
58+ self.tree.get(self.tree.focus)
62 }
59 }
63
60
64 pub fn view_mut(&mut self) -> &mut View {
61 pub fn view_mut(&mut self) -> &mut View {
65− self.tree.get_mut(self.focus)
62+ self.tree.get_mut(self.tree.focus)
66 }
63 }
67}
64}