diff options
author | imsuck <imsuck12@gmail.com> | 2022-11-17 20:53:41 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-17 22:53:41 +0900 |
commit | a00183397546459d7bb2acd4ba43fd0295c7101e (patch) | |
tree | e21fcb80c217599eb41ef7fd388fee7742aa790d | |
parent | 6c152926675fd28d898b70d20d470a752be6eec8 (diff) |
fix(compact-bar): mouse-click in simplified-ui (#1917)
* fix(compact-bar): mouse-click in simplified-ui
* fix(compact-bar): fix formatting
-rw-r--r-- | default-plugins/compact-bar/src/line.rs | 32 | ||||
-rw-r--r-- | default-plugins/compact-bar/src/main.rs | 19 | ||||
-rw-r--r-- | default-plugins/compact-bar/src/tab.rs | 43 |
3 files changed, 54 insertions, 40 deletions
diff --git a/default-plugins/compact-bar/src/line.rs b/default-plugins/compact-bar/src/line.rs index 675f46a9f..04720766c 100644 --- a/default-plugins/compact-bar/src/line.rs +++ b/default-plugins/compact-bar/src/line.rs @@ -26,8 +26,23 @@ fn populate_tabs_in_tab_line( loop { let left_count = tabs_before_active.len(); let right_count = tabs_after_active.len(); - let collapsed_left = left_more_message(left_count, palette, tab_separator(capabilities)); - let collapsed_right = right_more_message(right_count, palette, tab_separator(capabilities)); + + // left_more_tab_index is the tab to the left of the leftmost visible tab + let left_more_tab_index = left_count.saturating_sub(1); + let collapsed_left = left_more_message( + left_count, + palette, + tab_separator(capabilities), + left_more_tab_index, + ); + // right_more_tab_index is the tab to the right of the rightmost visible tab + let right_more_tab_index = left_count + tabs_to_render.len(); + let collapsed_right = right_more_message( + right_count, + palette, + tab_separator(capabilities), + right_more_tab_index, + ); let total_size = collapsed_left.len + middle_size + collapsed_right.len; @@ -90,7 +105,12 @@ fn populate_tabs_in_tab_line( } } -fn left_more_message(tab_count_to_the_left: usize, palette: Palette, separator: &str) -> LinePart { +fn left_more_message( + tab_count_to_the_left: usize, + palette: Palette, + separator: &str, + tab_index: usize, +) -> LinePart { if tab_count_to_the_left == 0 { return LinePart::default(); } @@ -114,6 +134,7 @@ fn left_more_message(tab_count_to_the_left: usize, palette: Palette, separator: LinePart { part: more_styled_text, len: more_text_len, + tab_index: Some(tab_index), } } @@ -121,6 +142,7 @@ fn right_more_message( tab_count_to_the_right: usize, palette: Palette, separator: &str, + tab_index: usize, ) -> LinePart { if tab_count_to_the_right == 0 { return LinePart::default(); @@ -144,6 +166,7 @@ fn right_more_message( LinePart { part: more_styled_text, len: more_text_len, + tab_index: Some(tab_index), } } @@ -173,6 +196,7 @@ fn tab_line_prefix( let mut parts = vec![LinePart { part: prefix_styled_text.to_string(), len: prefix_text_len, + tab_index: None, }]; if let Some(name) = session_name { let name_part = format!("({}) ", name); @@ -186,6 +210,7 @@ fn tab_line_prefix( parts.push(LinePart { part: name_part_styled_text.to_string(), len: name_part_len, + tab_index: None, }) } } @@ -209,6 +234,7 @@ fn tab_line_prefix( parts.push(LinePart { part: format!("{}", mode_part_styled_text), len: mode_part_len, + tab_index: None, }) } parts diff --git a/default-plugins/compact-bar/src/main.rs b/default-plugins/compact-bar/src/main.rs index 5dc8f2ffa..505dc926d 100644 --- a/default-plugins/compact-bar/src/main.rs +++ b/default-plugins/compact-bar/src/main.rs @@ -13,6 +13,7 @@ use crate::tab::tab_style; pub struct LinePart { part: String, len: usize, + tab_index: Option<usize>, } #[derive(Default)] @@ -88,12 +89,10 @@ impl ZellijPlugin for State { } let tab = tab_style( tabname, - t.active, + t, is_alternate_tab, - t.is_sync_panes_active, self.mode_info.style.colors, self.mode_info.capabilities, - t.other_focused_clients.as_slice(), ); is_alternate_tab = !is_alternate_tab; all_tabs.push(tab); @@ -109,17 +108,17 @@ impl ZellijPlugin for State { ); let mut s = String::new(); let mut len_cnt = 0; - for (idx, bar_part) in tab_line.iter().enumerate() { + for bar_part in tab_line { s = format!("{}{}", s, &bar_part.part); if self.should_render - && self.mouse_click_pos > len_cnt - && self.mouse_click_pos <= len_cnt + bar_part.len - && idx > 3 + && self.mouse_click_pos >= len_cnt + && self.mouse_click_pos < len_cnt + bar_part.len + && bar_part.tab_index.is_some() { - // First three elements of tab_line are "Zellij", session name and mode, hence the idx > 3 condition. - // Tabs are indexed starting from 1, therefore we need subtract 3 below. - switch_tab_to(TryInto::<u32>::try_into(idx).unwrap() - 3); + // Tabs are indexed starting from 1, therefore we need add 1 to tab_index. + let tab_index: u32 = bar_part.tab_index.unwrap().try_into().unwrap(); + switch_tab_to(tab_index + 1); } len_cnt += bar_part.len; } diff --git a/default-plugins/compact-bar/src/tab.rs b/default-plugins/compact-bar/src/tab.rs index 080b587be..f6776b738 100644 --- a/default-plugins/compact-bar/src/tab.rs +++ b/default-plugins/compact-bar/src/tab.rs @@ -19,19 +19,19 @@ fn cursors(focused_clients: &[ClientId], palette: Palette) -> (Vec<ANSIString>, pub fn render_tab( text: String, + tab: &TabInfo, + is_alternate_tab: bool, palette: Palette, separator: &str, - focused_clients: &[ClientId], - active: bool, - is_alternate_tab: bool, ) -> LinePart { + let focused_clients = tab.other_focused_clients.as_slice(); let separator_width = separator.width(); let alternate_tab_color = match palette.theme_hue { // TODO: only do this if we don't have the arrow capabilities ThemeHue::Dark => palette.white, ThemeHue::Light => palette.black, }; - let background_color = if active { + let background_color = if tab.active { palette.green } else if is_alternate_tab { alternate_tab_color @@ -43,8 +43,7 @@ pub fn render_tab( ThemeHue::Light => palette.white, }; let left_separator = style!(foreground_color, background_color).paint(separator); - let mut tab_text_len = - text.width() + (separator_width * 2) + separator.width() * (separator_width * 2); // 2 for left and right separators, 2 for the text padding + let mut tab_text_len = text.width() + (separator_width * 2) + 2; // + 2 for padding let tab_styled_text = style!(foreground_color, background_color) .bold() @@ -78,35 +77,25 @@ pub fn render_tab( LinePart { part: tab_styled_text, len: tab_text_len, + tab_index: Some(tab.position), } } pub fn tab_style( - text: String, - is_active_tab: bool, - is_alternate_tab: bool, - is_sync_panes_active: bool, + mut tabname: String, + tab: &TabInfo, + mut is_alternate_tab: bool, palette: Palette, capabilities: PluginCapabilities, - focused_clients: &[ClientId], ) -> LinePart { let separator = tab_separator(capabilities); - let mut tab_text = text; - if is_sync_panes_active { - tab_text.push_str(" (Sync)"); + if tab.is_sync_panes_active { + tabname.push_str(" (Sync)"); } // we only color alternate tabs differently if we can't use the arrow fonts to separate them - let is_alternate_tab = if !capabilities.arrow_fonts { - false - } else { - is_alternate_tab - }; - render_tab( - tab_text, - palette, - separator, - focused_clients, - is_active_tab, - is_alternate_tab, - ) + if !capabilities.arrow_fonts { + is_alternate_tab = false; + } + + render_tab(tabname, tab, is_alternate_tab, palette, separator) } |