From 36f41af81e0c19cab0cf8ae800f0b38638fd48f0 Mon Sep 17 00:00:00 2001 From: YuraIz <7516890@gmail.com> Date: Mon, 7 Nov 2022 21:39:44 +0300 Subject: [PATCH] integrated-title-bar: Add window buttons to the tabbar --- config/src/color.rs | 30 +++++ wezterm-gui/src/tabbar.rs | 148 +++++++++++++++++++++++ wezterm-gui/src/termwindow/mouseevent.rs | 42 ++++++- wezterm-gui/src/termwindow/render.rs | 75 ++++++++++++ wezterm-input-types/src/lib.rs | 6 + 5 files changed, 298 insertions(+), 3 deletions(-) diff --git a/config/src/color.rs b/config/src/color.rs index 459561e43..a7886ad1d 100644 --- a/config/src/color.rs +++ b/config/src/color.rs @@ -479,6 +479,18 @@ pub struct TabBarStyle { pub new_tab: String, #[dynamic(default = "default_new_tab")] pub new_tab_hover: String, + #[dynamic(default = "default_window_hide")] + pub window_hide: String, + #[dynamic(default = "default_window_hide")] + pub window_hide_hover: String, + #[dynamic(default = "default_window_maximize")] + pub window_maximize: String, + #[dynamic(default = "default_window_maximize")] + pub window_maximize_hover: String, + #[dynamic(default = "default_window_close")] + pub window_close: String, + #[dynamic(default = "default_window_close")] + pub window_close_hover: String, } impl Default for TabBarStyle { @@ -486,6 +498,12 @@ impl Default for TabBarStyle { Self { new_tab: default_new_tab(), new_tab_hover: default_new_tab(), + window_hide: default_window_hide(), + window_hide_hover: default_window_hide(), + window_maximize: default_window_maximize(), + window_maximize_hover: default_window_maximize(), + window_close: default_window_close(), + window_close_hover: default_window_close(), } } } @@ -494,6 +512,18 @@ fn default_new_tab() -> String { " + ".to_string() } +fn default_window_hide() -> String { + " . ".to_string() +} + +fn default_window_maximize() -> String { + " - ".to_string() +} + +fn default_window_close() -> String { + " X ".to_string() +} + #[derive(Debug, Clone, FromDynamic, ToDynamic)] pub struct WindowFrameConfig { #[dynamic(default = "default_inactive_titlebar_bg")] diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index f7ce935c1..dcfad89b8 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -24,6 +24,9 @@ pub enum TabBarItem { RightStatus, Tab { tab_idx: usize, active: bool }, NewTabButton, + WindowHideButton, + WindowMaximizeButton, + WindowCloseButton, } #[derive(Clone, Debug, PartialEq)] @@ -218,6 +221,61 @@ impl TabBarState { }, ); + let fancy_window_decorations = config + .window_decorations + .contains(window::WindowDecorations::FANCY); + + let window_hide = parse_status_text( + &config.tab_bar_style.window_hide, + if config.use_fancy_tab_bar { + CellAttributes::default() + } else { + new_tab_attrs.clone() + }, + ); + let window_hide_hover = parse_status_text( + &config.tab_bar_style.window_hide_hover, + if config.use_fancy_tab_bar { + CellAttributes::default() + } else { + new_tab_hover_attrs.clone() + }, + ); + + let window_maximize = parse_status_text( + &config.tab_bar_style.window_maximize, + if config.use_fancy_tab_bar { + CellAttributes::default() + } else { + new_tab_attrs.clone() + }, + ); + let window_maximize_hover = parse_status_text( + &config.tab_bar_style.window_maximize_hover, + if config.use_fancy_tab_bar { + CellAttributes::default() + } else { + new_tab_hover_attrs.clone() + }, + ); + + let window_close = parse_status_text( + &config.tab_bar_style.window_close, + if config.use_fancy_tab_bar { + CellAttributes::default() + } else { + new_tab_attrs.clone() + }, + ); + let window_close_hover = parse_status_text( + &config.tab_bar_style.window_close_hover, + if config.use_fancy_tab_bar { + CellAttributes::default() + } else { + new_tab_hover_attrs.clone() + }, + ); + // We ultimately want to produce a line looking like this: // ` | tab1-title x | tab2-title x | + . - X ` // Where the `+` sign will spawn a new tab (or show a context @@ -358,6 +416,17 @@ impl TabBarState { x += width; } + // Reserve place for buttons + let title_width = if fancy_window_decorations { + let hide_len = window_hide.len().max(window_hide_hover.len()); + let maximize_len = window_maximize.len().max(window_maximize_hover.len()); + let close_len = window_close.len().max(window_close_hover.len()); + + title_width.saturating_sub(hide_len + maximize_len + close_len) + } else { + title_width + }; + let status_space_available = title_width.saturating_sub(x); let mut right_status_line = parse_status_text(right_status, black_cell.attrs().clone()); @@ -377,6 +446,85 @@ impl TabBarState { line.insert_cell(x, black_cell.clone(), title_width, SEQ_ZERO); } + if fancy_window_decorations { + x = title_width; + + // Window hide button + { + let hover = is_tab_hover(mouse_x, x, window_hide_hover.len()); + + let window_hide_button = if hover { + &window_hide_hover + } else { + &window_hide + }; + + let button_start = x; + let width = window_hide_button.len(); + + line.append_line(window_hide_button.clone(), SEQ_ZERO); + + items.push(TabEntry { + item: TabBarItem::WindowHideButton, + title: window_hide_button.clone(), + x: button_start, + width, + }); + + x += width; + } + + // Window maximize button + { + let hover = is_tab_hover(mouse_x, x, window_maximize_hover.len()); + + let window_maximize_button = if hover { + &window_maximize_hover + } else { + &window_maximize + }; + + let button_start = x; + let width = window_maximize_button.len(); + + line.append_line(window_maximize_button.clone(), SEQ_ZERO); + + items.push(TabEntry { + item: TabBarItem::WindowMaximizeButton, + title: window_maximize_button.clone(), + x: button_start, + width, + }); + + x += width; + } + + // Window close button + { + let hover = is_tab_hover(mouse_x, x, window_close_hover.len()); + + let window_close_button = if hover { + &window_close_hover + } else { + &window_close + }; + + let button_start = x; + let width = window_close_button.len(); + + line.append_line(window_close_button.clone(), SEQ_ZERO); + + items.push(TabEntry { + item: TabBarItem::WindowCloseButton, + title: window_close_button.clone(), + x: button_start, + width, + }); + + // x += width; + } + } + Self { line, items } } diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs index 3a8407d6f..cde4ad0ec 100644 --- a/wezterm-gui/src/termwindow/mouseevent.rs +++ b/wezterm-gui/src/termwindow/mouseevent.rs @@ -478,6 +478,28 @@ impl super::TermWindow { } context.request_drag_move(); } + TabBarItem::WindowHideButton => { + if let Some(ref window) = self.window { + window.hide(); + } + } + TabBarItem::WindowMaximizeButton => { + if let Some(ref window) = self.window { + let maximized = self + .window_state + .intersects(WindowState::MAXIMIZED | WindowState::FULL_SCREEN); + if maximized { + window.restore(); + } else { + window.maximize(); + } + } + } + TabBarItem::WindowCloseButton => { + if let Some(ref window) = self.window { + self.close_requested(&window.clone()); + } + } }, WMEK::Press(MousePress::Middle) => match item { TabBarItem::Tab { tab_idx, .. } => { @@ -486,7 +508,12 @@ impl super::TermWindow { TabBarItem::NewTabButton { .. } => { self.do_new_tab_button_click(MousePress::Middle); } - TabBarItem::None | TabBarItem::LeftStatus | TabBarItem::RightStatus => {} + TabBarItem::None + | TabBarItem::LeftStatus + | TabBarItem::RightStatus + | TabBarItem::WindowHideButton + | TabBarItem::WindowMaximizeButton + | TabBarItem::WindowCloseButton => {} }, WMEK::Press(MousePress::Right) => match item { TabBarItem::Tab { .. } => { @@ -495,13 +522,22 @@ impl super::TermWindow { TabBarItem::NewTabButton { .. } => { self.do_new_tab_button_click(MousePress::Right); } - TabBarItem::None | TabBarItem::LeftStatus | TabBarItem::RightStatus => {} + TabBarItem::None + | TabBarItem::LeftStatus + | TabBarItem::RightStatus + | TabBarItem::WindowHideButton + | TabBarItem::WindowMaximizeButton + | TabBarItem::WindowCloseButton => {} }, WMEK::Move => match item { TabBarItem::None | TabBarItem::LeftStatus | TabBarItem::RightStatus => { context.set_window_drag_position(event.screen_coords); } - TabBarItem::Tab { .. } | TabBarItem::NewTabButton { .. } => {} + TabBarItem::WindowHideButton + | TabBarItem::WindowMaximizeButton + | TabBarItem::WindowCloseButton + | TabBarItem::Tab { .. } + | TabBarItem::NewTabButton { .. } => {} }, WMEK::VertWheel(n) => { if self.config.mouse_wheel_scrolls_tabs { diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 2ceb55f46..38c125016 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -145,6 +145,36 @@ const PLUS_BUTTON: &[Poly] = &[ }, ]; +const HIDE_BUTTON: &[Poly] = &[Poly { + path: &[ + PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::One), + PolyCommand::LineTo(BlockCoord::One, BlockCoord::One), + ], + intensity: BlockAlpha::Full, + style: PolyStyle::Outline, +}]; + +const MAXIMIZE_BUTTON: &[Poly] = &[Poly { + path: &[ + // PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::One), + PolyCommand::LineTo(BlockCoord::Zero, BlockCoord::One), + PolyCommand::LineTo(BlockCoord::One, BlockCoord::One), + PolyCommand::LineTo(BlockCoord::One, BlockCoord::Zero), + PolyCommand::LineTo(BlockCoord::Zero, BlockCoord::Zero), + ], + intensity: BlockAlpha::Full, + style: PolyStyle::Outline, +}]; + +fn poly_for_window_button(window_button: TabBarItem) -> &'static [Poly] { + match window_button { + TabBarItem::WindowHideButton => HIDE_BUTTON, + TabBarItem::WindowMaximizeButton => MAXIMIZE_BUTTON, + TabBarItem::WindowCloseButton => X_BUTTON, + _ => unreachable!(), + } +} + /// The data that we associate with a line; we use this to cache it shape hash #[derive(Debug)] pub struct CachedLineState { @@ -805,6 +835,45 @@ impl super::TermWindow { .into(), }) }), + TabBarItem::WindowHideButton + | TabBarItem::WindowMaximizeButton + | TabBarItem::WindowCloseButton => Element::new( + &font, + ElementContent::Poly { + line_width: metrics.underline_height.max(2), + poly: SizedPoly { + poly: poly_for_window_button(item.item.clone()), + width: Dimension::Pixels(metrics.cell_size.height as f32 / 2.), + height: Dimension::Pixels(metrics.cell_size.height as f32 / 2.), + }, + }, + ) + .zindex(1) + .vertical_align(VerticalAlign::Middle) + .item_type(UIItemType::TabBar(item.item.clone())) + .margin(BoxDimension { + left: Dimension::Cells(0.), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.), + bottom: Dimension::Cells(0.), + }) + .padding(BoxDimension { + left: Dimension::Cells(1.0), + right: Dimension::Cells(1.0), + top: Dimension::Cells(0.6), + bottom: Dimension::Cells(0.6), + }) + // .border(BoxDimension::new(Dimension::Pixels(1.))) + .colors(ElementColors { + border: BorderColor::default(), + bg: new_tab.bg_color.to_linear().into(), + text: new_tab.fg_color.to_linear().into(), + }) + .hover_colors(Some(ElementColors { + border: BorderColor::default(), + bg: new_tab_hover.bg_color.to_linear().into(), + text: new_tab_hover.fg_color.to_linear().into(), + })), } }; @@ -823,6 +892,12 @@ impl super::TermWindow { match item.item { TabBarItem::LeftStatus => left_status.push(item_to_elem(item)), TabBarItem::None | TabBarItem::RightStatus => right_eles.push(item_to_elem(item)), + TabBarItem::WindowHideButton + | TabBarItem::WindowMaximizeButton + | TabBarItem::WindowCloseButton => { + // TODO: Window buttons can be placed either at left and right + right_eles.push(item_to_elem(item)) + } TabBarItem::Tab { tab_idx, active } => { let mut elem = item_to_elem(item); elem.max_width = Some(Dimension::Pixels(max_tab_width)); diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index df3b71a03..b31cb5cd9 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1390,6 +1390,7 @@ bitflags! { // so that we effective have Option const MACOS_FORCE_DISABLE_SHADOW = 4; const MACOS_FORCE_ENABLE_SHADOW = 4|8; + const FANCY = 16; } } @@ -1402,6 +1403,9 @@ impl Into for &WindowDecorations { if self.contains(WindowDecorations::RESIZE) { s.push("RESIZE"); } + if self.contains(WindowDecorations::FANCY) { + s.push("FANCY"); + } if self.contains(WindowDecorations::MACOS_FORCE_ENABLE_SHADOW) { s.push("MACOS_FORCE_ENABLE_SHADOW"); } else if self.contains(WindowDecorations::MACOS_FORCE_DISABLE_SHADOW) { @@ -1431,6 +1435,8 @@ impl TryFrom for WindowDecorations { flags |= Self::MACOS_FORCE_DISABLE_SHADOW; } else if ele == "MACOS_FORCE_ENABLE_SHADOW" { flags |= Self::MACOS_FORCE_ENABLE_SHADOW; + } else if ele == "FANCY" { + flags |= Self::FANCY; } else { return Err(format!("invalid WindowDecoration name {} in {}", ele, s)); }