diff options
author | Aram Drevekenin <aram@poor.dev> | 2022-11-21 20:07:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-21 20:07:24 +0100 |
commit | 63e7531c48f1c8fbe916e3ac5475cf36201f52b8 (patch) | |
tree | 534e56cf9cf2924cec94a32bef1b6bcf92760209 /default-plugins | |
parent | 5ad0429adc3bbb33b2578d3e699f2e61d6d0b940 (diff) |
performance(rendering): improve rendering performance (#1960)
* refactor(plugins): plugins now need to explicitly ask to be rendered
* performance(render): remove various needless renders
* performance(render): cache boundaries
* performance(render): adjust tests and cache cursor location/shape
* style(comment): remove outdated
* style(fmt): rustfmt
Diffstat (limited to 'default-plugins')
-rw-r--r-- | default-plugins/compact-bar/src/main.rs | 32 | ||||
-rw-r--r-- | default-plugins/status-bar/src/main.rs | 28 | ||||
-rw-r--r-- | default-plugins/strider/src/main.rs | 32 | ||||
-rw-r--r-- | default-plugins/tab-bar/src/main.rs | 30 |
4 files changed, 102 insertions, 20 deletions
diff --git a/default-plugins/compact-bar/src/main.rs b/default-plugins/compact-bar/src/main.rs index 505dc926d..c6ce9bbcd 100644 --- a/default-plugins/compact-bar/src/main.rs +++ b/default-plugins/compact-bar/src/main.rs @@ -22,7 +22,7 @@ struct State { active_tab_idx: usize, mode_info: ModeInfo, mouse_click_pos: usize, - should_render: bool, + should_change_tab: bool, } static ARROW_SEPARATOR: &str = ""; @@ -39,13 +39,23 @@ impl ZellijPlugin for State { ]); } - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> bool { + let mut should_render = false; match event { - Event::ModeUpdate(mode_info) => self.mode_info = mode_info, + Event::ModeUpdate(mode_info) => { + if self.mode_info != mode_info { + should_render = true; + } + self.mode_info = mode_info + }, Event::TabUpdate(tabs) => { if let Some(active_tab_index) = tabs.iter().position(|t| t.active) { // tabs are indexed starting from 1 so we need to add 1 - self.active_tab_idx = active_tab_index + 1; + let active_tab_idx = active_tab_index + 1; + if self.active_tab_idx != active_tab_idx || self.tabs != tabs { + should_render = true; + } + self.active_tab_idx = active_tab_idx; self.tabs = tabs; } else { eprintln!("Could not find active tab."); @@ -53,13 +63,18 @@ impl ZellijPlugin for State { }, Event::Mouse(me) => match me { Mouse::LeftClick(_, col) => { + if self.mouse_click_pos != col { + should_render = true; + self.should_change_tab = true; + } self.mouse_click_pos = col; - self.should_render = true; }, Mouse::ScrollUp(_) => { + should_render = true; switch_tab_to(min(self.active_tab_idx + 1, self.tabs.len()) as u32); }, Mouse::ScrollDown(_) => { + should_render = true; switch_tab_to(max(self.active_tab_idx.saturating_sub(1), 1) as u32); }, _ => {}, @@ -67,7 +82,8 @@ impl ZellijPlugin for State { _ => { eprintln!("Got unrecognized event: {:?}", event); }, - } + }; + should_render } fn render(&mut self, _rows: usize, cols: usize) { @@ -111,7 +127,7 @@ impl ZellijPlugin for State { for bar_part in tab_line { s = format!("{}{}", s, &bar_part.part); - if self.should_render + if self.should_change_tab && self.mouse_click_pos >= len_cnt && self.mouse_click_pos < len_cnt + bar_part.len && bar_part.tab_index.is_some() @@ -134,6 +150,6 @@ impl ZellijPlugin for State { print!("{}\u{1b}[48;5;{}m\u{1b}[0K", s, color); }, } - self.should_render = false; + self.should_change_tab = false; } } diff --git a/default-plugins/status-bar/src/main.rs b/default-plugins/status-bar/src/main.rs index 25e31d2f5..97f2bd2c6 100644 --- a/default-plugins/status-bar/src/main.rs +++ b/default-plugins/status-bar/src/main.rs @@ -182,26 +182,50 @@ impl ZellijPlugin for State { ]); } - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> bool { + let mut should_render = false; match event { Event::ModeUpdate(mode_info) => { + if self.mode_info != mode_info { + should_render = true; + } self.mode_info = mode_info; }, Event::TabUpdate(tabs) => { + if self.tabs != tabs { + should_render = true; + } self.tabs = tabs; }, Event::CopyToClipboard(copy_destination) => { + match self.text_copy_destination { + Some(text_copy_destination) => { + if text_copy_destination != copy_destination { + should_render = true; + } + }, + None => { + should_render = true; + }, + } self.text_copy_destination = Some(copy_destination); }, Event::SystemClipboardFailure => { + should_render = true; self.display_system_clipboard_failure = true; }, Event::InputReceived => { + if self.text_copy_destination.is_some() + || self.display_system_clipboard_failure == true + { + should_render = true; + } self.text_copy_destination = None; self.display_system_clipboard_failure = false; }, _ => {}, - } + }; + should_render } fn render(&mut self, rows: usize, cols: usize) { diff --git a/default-plugins/strider/src/main.rs b/default-plugins/strider/src/main.rs index fcac6ec6a..6868c6706 100644 --- a/default-plugins/strider/src/main.rs +++ b/default-plugins/strider/src/main.rs @@ -13,7 +13,8 @@ impl ZellijPlugin for State { subscribe(&[EventType::Key, EventType::Mouse]); } - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> bool { + let mut should_render = false; let prev_event = if self.ev_history.len() == 2 { self.ev_history.pop_front() } else { @@ -23,13 +24,22 @@ impl ZellijPlugin for State { match event { Event::Key(key) => match key { Key::Up | Key::Char('k') => { + let currently_selected = self.selected(); *self.selected_mut() = self.selected().saturating_sub(1); + if currently_selected != self.selected() { + should_render = true; + } }, Key::Down | Key::Char('j') => { + let currently_selected = self.selected(); let next = self.selected().saturating_add(1); *self.selected_mut() = min(self.files.len().saturating_sub(1), next); + if currently_selected != self.selected() { + should_render = true; + } }, Key::Right | Key::Char('\n') | Key::Char('l') if !self.files.is_empty() => { + should_render = true; self.traverse_dir_or_open_file(); self.ev_history.clear(); }, @@ -39,11 +49,13 @@ impl ZellijPlugin for State { // the reason this is a hard-coded number (2) and not "== ROOT" // or some such is that there are certain cases in which self.path // is empty and this will work then too + should_render = true; self.path.pop(); refresh_directory(self); } }, Key::Char('.') => { + should_render = true; self.toggle_hidden_files(); refresh_directory(self); }, @@ -52,15 +64,23 @@ impl ZellijPlugin for State { }, Event::Mouse(mouse_event) => match mouse_event { Mouse::ScrollDown(_) => { + let currently_selected = self.selected(); let next = self.selected().saturating_add(1); *self.selected_mut() = min(self.files.len().saturating_sub(1), next); + if currently_selected != self.selected() { + should_render = true; + } }, Mouse::ScrollUp(_) => { + let currently_selected = self.selected(); *self.selected_mut() = self.selected().saturating_sub(1); + if currently_selected != self.selected() { + should_render = true; + } }, Mouse::Release(line, _) => { if line < 0 { - return; + return should_render; } let mut should_select = true; if let Some((Event::Mouse(Mouse::Release(prev_line, _)), t)) = prev_event { @@ -70,10 +90,15 @@ impl ZellijPlugin for State { self.traverse_dir_or_open_file(); self.ev_history.clear(); should_select = false; + should_render = true; } } if should_select && self.scroll() + (line as usize) < self.files.len() { + let currently_selected = self.selected(); *self.selected_mut() = self.scroll() + (line as usize); + if currently_selected != self.selected() { + should_render = true; + } } }, _ => {}, @@ -81,7 +106,8 @@ impl ZellijPlugin for State { _ => { dbg!("Unknown event {:?}", event); }, - } + }; + should_render } fn render(&mut self, rows: usize, cols: usize) { diff --git a/default-plugins/tab-bar/src/main.rs b/default-plugins/tab-bar/src/main.rs index f03102610..6f0456a8f 100644 --- a/default-plugins/tab-bar/src/main.rs +++ b/default-plugins/tab-bar/src/main.rs @@ -22,7 +22,7 @@ struct State { active_tab_idx: usize, mode_info: ModeInfo, mouse_click_pos: usize, - should_render: bool, + should_change_tab: bool, } static ARROW_SEPARATOR: &str = ""; @@ -39,13 +39,23 @@ impl ZellijPlugin for State { ]); } - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> bool { + let mut should_render = false; match event { - Event::ModeUpdate(mode_info) => self.mode_info = mode_info, + Event::ModeUpdate(mode_info) => { + if self.mode_info != mode_info { + should_render = true; + } + self.mode_info = mode_info; + }, Event::TabUpdate(tabs) => { if let Some(active_tab_index) = tabs.iter().position(|t| t.active) { // tabs are indexed starting from 1 so we need to add 1 - self.active_tab_idx = active_tab_index + 1; + let active_tab_idx = active_tab_index + 1; + if self.active_tab_idx != active_tab_idx || self.tabs != tabs { + should_render = true; + } + self.active_tab_idx = active_tab_idx; self.tabs = tabs; } else { eprintln!("Could not find active tab."); @@ -53,13 +63,18 @@ impl ZellijPlugin for State { }, Event::Mouse(me) => match me { Mouse::LeftClick(_, col) => { + if self.mouse_click_pos != col { + should_render = true; + self.should_change_tab = true; + } self.mouse_click_pos = col; - self.should_render = true; }, Mouse::ScrollUp(_) => { + should_render = true; switch_tab_to(min(self.active_tab_idx + 1, self.tabs.len()) as u32); }, Mouse::ScrollDown(_) => { + should_render = true; switch_tab_to(max(self.active_tab_idx.saturating_sub(1), 1) as u32); }, _ => {}, @@ -68,6 +83,7 @@ impl ZellijPlugin for State { eprintln!("Got unrecognized event: {:?}", event); }, } + should_render } fn render(&mut self, _rows: usize, cols: usize) { @@ -110,7 +126,7 @@ impl ZellijPlugin for State { for bar_part in tab_line { s = format!("{}{}", s, &bar_part.part); - if self.should_render + if self.should_change_tab && self.mouse_click_pos >= len_cnt && self.mouse_click_pos < len_cnt + bar_part.len && bar_part.tab_index.is_some() @@ -133,6 +149,6 @@ impl ZellijPlugin for State { print!("{}\u{1b}[48;5;{}m\u{1b}[0K", s, color); }, } - self.should_render = false; + self.should_change_tab = false; } } |