use std::rc::Rc;
use gpui_base::LogicalStyledExt as _;
use crate::{
ActiveTheme, Icon, IconName, InteractiveElementExt as _, Sizable as _, StyledExt, h_flex,
};
#[cfg(target_os = "linux")]
use gpui::Subscription;
use gpui::{
AnyElement, App, Background, ClickEvent, Context, Decorations, Hsla, InteractiveElement,
IntoElement, MouseButton, ParentElement, Pixels, Render, RenderOnce, Rgba,
StatefulInteractiveElement as _, StyleRefinement, Styled, TitlebarOptions, Window,
WindowButton, WindowButtonLayout, WindowControlArea, WindowOptions, div, linear_color_stop,
linear_gradient, prelude::FluentBuilder as _, px, rems,
};
use gpui_base::TestSupportExt as _;
use smallvec::SmallVec;
pub const TITLE_BAR_HEIGHT: Pixels = px(34.);
#[cfg(target_os = "macos")]
const TITLE_BAR_LEFT_PADDING: Pixels = px(80.);
#[cfg(not(target_os = "macos"))]
const TITLE_BAR_LEFT_PADDING: Pixels = px(12.);
fn default_title_bar_background(title_bar: Hsla, background: Hsla) -> Background {
let title_bar_rgb = title_bar.to_rgb();
let background_rgb = background.to_rgb();
let mixed = Hsla::from(Rgba {
r: title_bar_rgb.r * 0.55 + background_rgb.r * 0.45,
g: title_bar_rgb.g * 0.55 + background_rgb.g * 0.45,
b: title_bar_rgb.b * 0.55 + background_rgb.b * 0.45,
a: title_bar_rgb.a * 0.55 + background_rgb.a * 0.45,
});
linear_gradient(
180.,
linear_color_stop(mixed, 0.),
linear_color_stop(title_bar, 1.),
)
}
#[derive(IntoElement)]
pub struct TitleBar {
style: StyleRefinement,
children: SmallVec<[AnyElement; 1]>,
on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>,
}
impl TitleBar {
pub fn new() -> Self {
Self {
style: StyleRefinement::default(),
children: SmallVec::new(),
on_close_window: None,
}
}
pub fn title_bar_options() -> TitlebarOptions {
TitlebarOptions {
title: None,
appears_transparent: true,
traffic_light_position: Some(gpui::point(px(9.0), px(9.0))),
}
}
pub fn window_options() -> WindowOptions {
WindowOptions {
titlebar: Some(Self::title_bar_options()),
app_owns_titlebar_drag: true,
..Default::default()
}
}
pub fn on_close_window(
mut self,
f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
if cfg!(target_os = "linux") {
self.on_close_window = Some(Rc::new(Box::new(f)));
}
self
}
}
#[derive(IntoElement, Clone)]
enum ControlIcon {
Minimize,
Restore,
Maximize,
Close {
on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>,
},
}
fn window_button_layout(_cx: &App) -> WindowButtonLayout {
#[cfg(target_os = "linux")]
{
_cx.button_layout()
.unwrap_or_else(WindowButtonLayout::linux_default)
}
#[cfg(not(target_os = "linux"))]
{
WindowButtonLayout {
left: [None; gpui::MAX_BUTTONS_PER_SIDE],
right: [
Some(WindowButton::Minimize),
Some(WindowButton::Maximize),
Some(WindowButton::Close),
],
}
}
}
fn supported_buttons(
buttons: [Option<WindowButton>; gpui::MAX_BUTTONS_PER_SIDE],
supported: gpui::WindowControls,
) -> SmallVec<[WindowButton; gpui::MAX_BUTTONS_PER_SIDE]> {
buttons
.into_iter()
.flatten()
.filter(|button| match button {
WindowButton::Minimize => supported.minimize,
WindowButton::Maximize => supported.maximize,
WindowButton::Close => true,
})
.collect()
}
impl ControlIcon {
fn minimize() -> Self {
Self::Minimize
}
fn restore() -> Self {
Self::Restore
}
fn maximize() -> Self {
Self::Maximize
}
fn close(on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>) -> Self {
Self::Close { on_close_window }
}
fn id(&self) -> &'static str {
match self {
Self::Minimize => "minimize",
Self::Restore => "restore",
Self::Maximize => "maximize",
Self::Close { .. } => "close",
}
}
fn icon(&self) -> IconName {
match self {
Self::Minimize => IconName::WindowMinimize,
Self::Restore => IconName::WindowRestore,
Self::Maximize => IconName::WindowMaximize,
Self::Close { .. } => IconName::WindowClose,
}
}
fn window_control_area(&self) -> WindowControlArea {
match self {
Self::Minimize => WindowControlArea::Min,
Self::Restore | Self::Maximize => WindowControlArea::Max,
Self::Close { .. } => WindowControlArea::Close,
}
}
fn is_close(&self) -> bool {
matches!(self, Self::Close { .. })
}
#[inline]
fn hover_fg(&self, cx: &App) -> Hsla {
if self.is_close() {
cx.theme().danger_foreground
} else {
cx.theme().secondary_foreground
}
}
#[inline]
fn hover_bg(&self, cx: &App) -> Hsla {
if self.is_close() {
cx.theme().danger
} else {
cx.theme().secondary_hover
}
}
#[inline]
fn active_bg(&self, cx: &mut App) -> Hsla {
if self.is_close() {
cx.theme().danger_active
} else {
cx.theme().secondary_active
}
}
}
impl RenderOnce for ControlIcon {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let is_linux = cfg!(target_os = "linux");
let is_windows = cfg!(target_os = "windows");
let hover_fg = self.hover_fg(cx);
let hover_bg = self.hover_bg(cx);
let active_bg = self.active_bg(cx);
let icon = self.clone();
let on_close_window = match &self {
ControlIcon::Close { on_close_window } => on_close_window.clone(),
_ => None,
};
div()
.id(self.id())
.test_support()
.flex()
.w(TITLE_BAR_HEIGHT)
.h_full()
.flex_shrink_0()
.justify_center()
.content_center()
.items_center()
.text_color(cx.theme().foreground)
.hover(|style| style.bg(hover_bg).text_color(hover_fg))
.active(|style| style.bg(active_bg).text_color(hover_fg))
.when(is_windows, |this| {
this.window_control_area(self.window_control_area())
})
.when(is_linux, |this| {
this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
window.prevent_default();
cx.stop_propagation();
})
.on_click(move |_, window, cx| {
cx.stop_propagation();
match icon {
Self::Minimize => window.minimize_window(),
Self::Restore | Self::Maximize => window.zoom_window(),
Self::Close { .. } => {
if let Some(f) = on_close_window.clone() {
f(&ClickEvent::default(), window, cx);
} else {
window.remove_window();
}
}
}
})
})
.child(Icon::new(self.icon()).small())
}
}
#[derive(IntoElement)]
struct WindowControls {
id: &'static str,
buttons: [Option<WindowButton>; gpui::MAX_BUTTONS_PER_SIDE],
on_close_window: Option<Rc<Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>>>,
}
impl RenderOnce for WindowControls {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
if cfg!(target_os = "macos") || cfg!(target_family = "wasm") {
return div().id(self.id).test_support();
}
#[cfg(target_os = "linux")]
if !matches!(window.window_decorations(), Decorations::Client { .. }) {
return div().id(self.id).test_support();
}
let supported = window.window_controls();
h_flex()
.flex_row_dir(cx)
.id(self.id)
.test_support()
.items_center()
.flex_shrink_0()
.h_full()
.children(
supported_buttons(self.buttons, supported)
.into_iter()
.map(|button| match button {
WindowButton::Minimize => ControlIcon::minimize(),
WindowButton::Maximize if window.is_maximized() => ControlIcon::restore(),
WindowButton::Maximize => ControlIcon::maximize(),
WindowButton::Close => ControlIcon::close(self.on_close_window.clone()),
}),
)
}
}
impl Styled for TitleBar {
fn style(&mut self) -> &mut gpui::StyleRefinement {
&mut self.style
}
}
impl ParentElement for TitleBar {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
struct TitleBarState {
should_move: bool,
#[cfg(target_os = "linux")]
_button_layout_subscription: Subscription,
}
impl Render for TitleBarState {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
}
}
impl RenderOnce for TitleBar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let is_client_decorated = matches!(window.window_decorations(), Decorations::Client { .. });
let is_web = cfg!(target_family = "wasm");
let is_linux = cfg!(target_os = "linux");
let is_macos = cfg!(target_os = "macos");
let state = window.use_state(cx, |_window, _state_cx| TitleBarState {
should_move: false,
#[cfg(target_os = "linux")]
_button_layout_subscription: _state_cx
.observe_button_layout_changed(_window, |_, _, cx| cx.notify()),
});
let radii = crate::window_corner_radii(window, cx);
let button_layout = window_button_layout(cx);
div().flex_shrink_0().child(
div()
.id("title-bar")
.flex_row_dir(cx)
.when(is_linux, |this| this.flex_row())
.items_center()
.justify_between()
.h(TITLE_BAR_HEIGHT)
.map(|this| {
if is_macos || is_linux {
this.pl(TITLE_BAR_LEFT_PADDING)
} else {
this.ps(TITLE_BAR_LEFT_PADDING, cx)
}
})
.rounded_tl(radii.top_left)
.rounded_tr(radii.top_right)
.border_b_1()
.border_color(cx.theme().title_bar_border)
.bg(default_title_bar_background(
cx.theme().title_bar,
cx.theme().background,
))
.refine_style(&self.style)
.when(is_linux, |this| {
this.on_double_click(|_, window, _| window.zoom_window())
})
.when(is_macos, |this| {
this.on_double_click(|_, window, _| window.titlebar_double_click())
})
.test_support()
.on_mouse_down_out(window.listener_for(&state, |state, _, _, _| {
state.should_move = false;
}))
.on_mouse_down(
MouseButton::Left,
window.listener_for(&state, |state, _, window, cx| {
state.should_move = !window.default_prevented();
crate::GlobalState::suppress_text_selection(cx);
}),
)
.on_mouse_up(
MouseButton::Left,
window.listener_for(&state, |state, _, _, _| {
state.should_move = false;
}),
)
.on_mouse_move(window.listener_for(
&state,
|state, event: &gpui::MouseMoveEvent, window, cx| {
if event.pressed_button != Some(MouseButton::Left) {
state.should_move = false;
return;
}
if state.should_move {
state.should_move = false;
gpui_base::TextSelection::end(window, cx);
cx.stop_active_drag(window);
window.start_window_move();
}
},
))
.when(is_linux, |this| {
this.child(WindowControls {
id: "window-controls-start",
buttons: button_layout.left,
on_close_window: self.on_close_window.clone(),
})
})
.child(
h_flex()
.id("bar")
.h_full()
.justify_between()
.flex_shrink_0()
.flex_1()
.when(!is_web, |this| {
this.window_control_area(WindowControlArea::Drag)
.when(window.is_fullscreen(), |this| this.ps(rems(0.75), cx))
.when(is_linux && is_client_decorated, |this| {
this.child(
div()
.top_0()
.left_0()
.absolute()
.size_full()
.h_full()
.on_mouse_down(
MouseButton::Right,
move |ev, window, _| {
window.show_window_menu(ev.position)
},
),
)
})
})
.children(self.children),
)
.child(WindowControls {
id: "window-controls",
buttons: button_layout.right,
on_close_window: self.on_close_window,
}),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{
AppContext as _, Modifiers, Rgba, TestAppContext, VisualTestContext, linear_color_stop,
linear_gradient, point, px,
};
use std::cell::Cell;
use crate::button::Button;
#[test]
fn linux_window_controls_follow_layout_order_and_capabilities() {
let layout = WindowButtonLayout {
left: [
Some(WindowButton::Close),
Some(WindowButton::Minimize),
None,
],
right: [
Some(WindowButton::Maximize),
Some(WindowButton::Close),
None,
],
};
let supported = gpui::WindowControls {
fullscreen: true,
maximize: false,
minimize: true,
window_menu: true,
};
assert_eq!(
supported_buttons(layout.left, supported).as_slice(),
[WindowButton::Close, WindowButton::Minimize]
);
assert_eq!(
supported_buttons(layout.right, supported).as_slice(),
[WindowButton::Close]
);
}
const BUTTON: &str = "title-bar-button";
struct BarWithButton {
clicks: Rc<Cell<usize>>,
}
impl Render for BarWithButton {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let clicks = self.clicks.clone();
TitleBar::new().child(div().w(px(200.)).child("App")).child(
Button::new("open")
.label("Open")
.debug_selector(|| BUTTON.to_string())
.on_click(move |_, _, _| clicks.set(clicks.get() + 1)),
)
}
}
fn open(cx: &mut TestAppContext) -> (VisualTestContext, Rc<Cell<usize>>) {
cx.update(crate::init);
let clicks = Rc::new(Cell::new(0));
let view = cx.new(|_| BarWithButton {
clicks: clicks.clone(),
});
let window = cx.update(|cx| {
cx.open_window(Default::default(), |window, cx| {
cx.new(|cx| crate::Root::new(view.clone(), window, cx))
})
.unwrap()
});
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.update(|window, cx| window.draw(cx).clear(cx));
(cx, clicks)
}
fn click_with_drift(cx: &mut VisualTestContext, at: gpui::Point<Pixels>) {
let drift = point(at.x + px(1.), at.y);
cx.simulate_mouse_down(at, MouseButton::Left, Modifiers::default());
cx.simulate_mouse_move(drift, Some(MouseButton::Left), Modifiers::default());
cx.simulate_mouse_up(drift, MouseButton::Left, Modifiers::default());
}
#[gpui::test]
fn a_press_on_a_control_keeps_its_click(cx: &mut TestAppContext) {
let (mut cx, clicks) = open(cx);
let at = cx
.debug_bounds(BUTTON)
.expect("the button is drawn")
.center();
click_with_drift(&mut cx, at);
assert_eq!(clicks.get(), 1);
}
#[gpui::test]
fn a_press_on_the_bar_does_not_start_a_text_selection(cx: &mut TestAppContext) {
let (mut cx, _) = open(cx);
let button = cx.debug_bounds(BUTTON).expect("the button is drawn");
let bar = point(button.left() - px(100.), button.center().y);
cx.simulate_mouse_down(bar, MouseButton::Left, Modifiers::default());
cx.update(|_, cx| assert!(crate::GlobalState::is_text_selection_suppressed(cx)));
}
#[gpui::test]
#[should_panic(expected = "not implemented")]
fn a_press_on_the_bar_itself_still_moves_the_window(cx: &mut TestAppContext) {
let (mut cx, _) = open(cx);
let button = cx.debug_bounds(BUTTON).expect("the button is drawn");
click_with_drift(&mut cx, point(button.left() - px(100.), button.center().y));
}
#[test]
fn test_default_title_bar_background() {
let title_bar = Hsla::black();
let background = Hsla::white();
assert_eq!(
default_title_bar_background(title_bar, background),
linear_gradient(
180.,
linear_color_stop(
Hsla::from(Rgba {
r: 0.45,
g: 0.45,
b: 0.45,
a: 1.,
}),
0.,
),
linear_color_stop(title_bar, 1.),
)
);
}
}