From fd3db7306f931f5067db2dbf985818fd7c22063f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Schieli?= Date: Sat, 17 Jul 2021 17:31:11 +0200 Subject: [PATCH] Allow styling of the whole new tab button --- config/src/color.rs | 36 ++++++------- docs/config/appearance.md | 20 ++++++++ docs/config/lua/config/tab_bar_style.md | 5 ++ wezterm-gui/src/tabbar.rs | 68 ++++++++----------------- 4 files changed, 64 insertions(+), 65 deletions(-) diff --git a/config/src/color.rs b/config/src/color.rs index 44702f6d85c..0f87757d3a1 100644 --- a/config/src/color.rs +++ b/config/src/color.rs @@ -141,6 +141,14 @@ pub struct TabBarColors { /// Styling for an inactive tab with a mouse hovering #[serde(default = "default_inactive_tab_hover")] pub inactive_tab_hover: TabBarColor, + + /// Styling for the new tab button + #[serde(default = "default_inactive_tab")] + pub new_tab: TabBarColor, + + /// Styling for the new tab button with a mouse hovering + #[serde(default = "default_inactive_tab_hover")] + pub new_tab_hover: TabBarColor, } impl_lua_conversion!(TabBarColors); @@ -178,39 +186,31 @@ impl Default for TabBarColors { inactive_tab: default_inactive_tab(), inactive_tab_hover: default_inactive_tab_hover(), active_tab: default_active_tab(), + new_tab: default_inactive_tab(), + new_tab_hover: default_inactive_tab_hover(), } } } #[derive(Debug, Deserialize, Serialize, Clone)] pub struct TabBarStyle { - #[serde(default = "default_tab_left")] - pub new_tab_left: String, - #[serde(default = "default_tab_right")] - pub new_tab_right: String, - #[serde(default = "default_tab_left")] - pub new_tab_hover_left: String, - #[serde(default = "default_tab_right")] - pub new_tab_hover_right: String, + #[serde(default = "default_new_tab")] + pub new_tab: String, + #[serde(default = "default_new_tab")] + pub new_tab_hover: String, } impl Default for TabBarStyle { fn default() -> Self { Self { - new_tab_left: default_tab_left(), - new_tab_right: default_tab_right(), - new_tab_hover_left: default_tab_left(), - new_tab_hover_right: default_tab_right(), + new_tab: default_new_tab(), + new_tab_hover: default_new_tab(), } } } -fn default_tab_left() -> String { - format_as_escapes(vec![FormatItem::Text(" ".to_string())]).unwrap() -} - -fn default_tab_right() -> String { - format_as_escapes(vec![FormatItem::Text(" ".to_string())]).unwrap() +fn default_new_tab() -> String { + format_as_escapes(vec![FormatItem::Text(" + ".to_string())]).unwrap() } #[derive(Debug, Deserialize, Serialize, Clone)] diff --git a/docs/config/appearance.md b/docs/config/appearance.md index 6723befb3cf..11cd76ec478 100644 --- a/docs/config/appearance.md +++ b/docs/config/appearance.md @@ -195,6 +195,26 @@ return { -- The same options that were listed under the `active_tab` section above -- can also be used for `inactive_tab_hover`. + }, + + -- The new tab button that let you create new tabs + new_tab = { + bg_color = "#1b1032", + fg_color = "#808080", + + -- The same options that were listed under the `active_tab` section above + -- can also be used for `new_tab`. + }, + + -- You can configure some alternate styling when the mouse pointer + -- moves over the new tab button + new_tab_hover = { + bg_color = "#3b3052", + fg_color = "#909090", + italic = true, + + -- The same options that were listed under the `active_tab` section above + -- can also be used for `new_tab_hover`. } } } diff --git a/docs/config/lua/config/tab_bar_style.md b/docs/config/lua/config/tab_bar_style.md index 5fcea2cf9d1..f81c009e6cf 100644 --- a/docs/config/lua/config/tab_bar_style.md +++ b/docs/config/lua/config/tab_bar_style.md @@ -1,5 +1,10 @@ # `tab_bar_style` +*Since: nightly builds only* + +`new_tab_left`, `new_tab_right`, `new_tab_hover_left`, `new_tab_hover_right` +have been removed and replaced by the more flexible `new_tab` and `new_tab_hover` elements. + *Since: 20210502-154244-3f7122cb* `active_tab_left`, `active_tab_right`, `inactive_tab_left`, diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index f047f447f0f..c8298cb0e90 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -143,6 +143,12 @@ fn compute_tab_title( } } +fn is_tab_hover(mouse_x: Option, x: usize, tab_title_len: usize) -> bool { + return mouse_x + .map(|mouse_x| mouse_x >= x && mouse_x < x + tab_title_len) + .unwrap_or(false); +} + impl TabBarState { pub fn default() -> Self { Self { @@ -173,22 +179,13 @@ impl TabBarState { let active_cell_attrs = colors.active_tab.as_cell_attributes(); let inactive_hover_attrs = colors.inactive_tab_hover.as_cell_attributes(); let inactive_cell_attrs = colors.inactive_tab.as_cell_attributes(); + let new_tab_hover_attrs = colors.new_tab_hover.as_cell_attributes(); + let new_tab_attrs = colors.new_tab.as_cell_attributes(); - let new_tab_left = parse_status_text( - &config.tab_bar_style.new_tab_left, - inactive_cell_attrs.clone(), - ); - let new_tab_right = parse_status_text( - &config.tab_bar_style.new_tab_right, - inactive_cell_attrs.clone(), - ); - let new_tab_hover_left = parse_status_text( - &config.tab_bar_style.new_tab_hover_left, - inactive_hover_attrs.clone(), - ); - let new_tab_hover_right = parse_status_text( - &config.tab_bar_style.new_tab_hover_right, - inactive_hover_attrs.clone(), + let new_tab = parse_status_text(&config.tab_bar_style.new_tab, new_tab_attrs.clone()); + let new_tab_hover = parse_status_text( + &config.tab_bar_style.new_tab_hover, + new_tab_hover_attrs.clone(), ); // We ultimately want to produce a line looking like this: @@ -218,9 +215,8 @@ impl TabBarState { let titles_len: usize = tab_titles.iter().map(|s| s.len).sum(); let number_of_tabs = tab_titles.len(); - let available_cells = title_width.saturating_sub( - (number_of_tabs.saturating_sub(1)) + (new_tab_left.len() + new_tab_right.len() + 1), - ); + let available_cells = + title_width.saturating_sub((number_of_tabs.saturating_sub(1)) + (new_tab.len())); let tab_width_max = if available_cells >= titles_len { // We can render each title with its full width usize::max_value() @@ -238,10 +234,7 @@ impl TabBarState { for (tab_idx, tab_title) in tab_titles.iter().enumerate() { let tab_title_len = tab_title.len.min(tab_width_max); let active = tab_idx == active_tab_no; - let hover = !active - && mouse_x - .map(|mouse_x| mouse_x >= x && mouse_x < x + tab_title_len) - .unwrap_or(false); + let hover = !active && is_tab_hover(mouse_x, x, tab_title_len); // Recompute the title so that it factors in both the hover state // and the adjusted maximum tab width based on available space. @@ -286,35 +279,16 @@ impl TabBarState { // New tab button { - let hover = mouse_x - .map(|mouse_x| { - mouse_x >= x - && mouse_x < x + new_tab_hover_left.len() + new_tab_hover_right.len() + 1 - }) - .unwrap_or(false); - - let (cell_attrs, left, right) = if hover { - ( - &inactive_hover_attrs, - &new_tab_hover_left, - &new_tab_hover_right, - ) - } else { - (&inactive_cell_attrs, &new_tab_left, &new_tab_right) - }; + let hover = is_tab_hover(mouse_x, x, new_tab_hover.len()); - let button_start = x; + let cells = if hover { &new_tab_hover } else { &new_tab }; - for c in left { - line.set_cell(x, c.clone()); - x += 1; - } - line.set_cell(x, Cell::new('+', cell_attrs.clone())); - x += 1; + let button_start = x; - for c in right { + for c in cells { + let len = c.width(); line.set_cell(x, c.clone()); - x += 1; + x += len; } items.push(TabEntry {