use crate::{
theme::color,
tokens::{
self, ColorRole, FontFamilyKey, SIDEBAR_WIDTH, TITLEBAR_HEIGHT, TRAFFIC_LIGHT_INSET,
TRAFFIC_LIGHT_OFFSET_Y,
},
typography::font,
};
use gpui_kit::{
AnyElement, App, BoxShadow, Hsla, IntoElement, ParentElement, Rems, RenderOnce, SharedString,
Styled, Window, div, prelude::FluentBuilder as _, px, rems, rgb,
};
pub fn traffic_colors(focused: bool) -> [Hsla; 3] {
if focused {
[
rgb(0xff5f57).into(),
rgb(0xfebc2e).into(),
rgb(0x28c840).into(),
]
} else {
[color(ColorRole::LayerAccent01); 3]
}
}
fn traffic(focused: bool) -> impl IntoElement {
let ring = vec![BoxShadow {
color: gpui_kit::hsla(0., 0., 0., 0.2),
offset: gpui_kit::point(px(0.), px(0.)),
blur_radius: px(0.),
spread_radius: px(0.5),
inset: true,
}];
div()
.absolute()
.left(rems(0.8125))
.top(TRAFFIC_LIGHT_OFFSET_Y)
.flex()
.gap(px(8.))
.children(traffic_colors(focused).map(|c| {
div()
.size(px(12.))
.rounded_full()
.bg(c)
.shadow(ring.clone())
}))
}
fn titlebar() -> gpui_kit::Div {
div()
.relative()
.flex()
.flex_none()
.items_center()
.gap(tokens::SPACING_03)
.h(TITLEBAR_HEIGHT)
}
#[derive(IntoElement)]
pub struct WindowChrome {
title: SharedString,
subtitle: Option<SharedString>,
actions: Vec<AnyElement>,
sidebar: Option<AnyElement>,
children: Vec<AnyElement>,
traffic_lights: bool,
focused: bool,
height: Option<Rems>,
}
impl WindowChrome {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
subtitle: None,
actions: Vec::new(),
sidebar: None,
children: Vec::new(),
traffic_lights: true,
focused: true,
height: None,
}
}
pub fn subtitle(mut self, subtitle: impl Into<SharedString>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn sidebar(mut self, sidebar: impl IntoElement) -> Self {
self.sidebar = Some(sidebar.into_any_element());
self
}
pub fn traffic_lights(mut self, on: bool) -> Self {
self.traffic_lights = on;
self
}
pub fn focused(mut self, focused: bool) -> Self {
self.focused = focused;
self
}
pub fn height(mut self, height: Rems) -> Self {
self.height = Some(height);
self
}
}
impl ParentElement for WindowChrome {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for WindowChrome {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let has_sidebar = self.sidebar.is_some();
let lights = self.traffic_lights;
let focused = self.focused;
let sidebar = self.sidebar.map(|content| {
div()
.flex()
.flex_col()
.flex_none()
.w(SIDEBAR_WIDTH)
.bg(color(ColorRole::SidebarMaterial))
.border_r(px(1.))
.border_color(color(ColorRole::TitlebarBorder))
.child(titlebar().when(lights, |t| t.child(traffic(focused))))
.child(content)
});
let title = div()
.flex()
.flex_1()
.min_w_0()
.items_baseline()
.gap(tokens::SPACING_03)
.text_size(rems(0.8125))
.text_color(color(if focused {
ColorRole::TextPrimary
} else {
ColorRole::TextDisabled
}))
.child(
div()
.min_w_0()
.font(font(FontFamilyKey::Sans, 600))
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(self.title.clone()),
)
.children(self.subtitle.clone().map(|s| {
div()
.min_w_0()
.text_size(rems(0.75))
.text_color(color(ColorRole::TextSecondary))
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(s)
}));
let inset = !has_sidebar && lights;
let bar = titlebar()
.pr(tokens::SPACING_03)
.pl(if inset {
TRAFFIC_LIGHT_INSET
} else {
tokens::SPACING_05
})
.bg(color(ColorRole::TitlebarBackground))
.border_b(px(1.))
.border_color(color(ColorRole::TitlebarBorder))
.when(inset, |t| t.child(traffic(focused)))
.child(title)
.child(
div()
.flex()
.items_center()
.gap(px(2.))
.children(self.actions),
);
let main = div().flex().flex_col().flex_1().min_w_0().child(bar).child(
div()
.flex_1()
.min_h_0()
.overflow_hidden()
.children(self.children),
);
div()
.flex()
.min_h_0()
.overflow_hidden()
.rounded(tokens::BORDER_RADIUS_08)
.bg(color(ColorRole::Background))
.shadow(vec![
BoxShadow {
color: color(ColorRole::BorderSubtle01),
offset: gpui_kit::point(px(0.), px(0.)),
blur_radius: px(0.),
spread_radius: px(1.),
inset: false,
},
BoxShadow {
color: color(ColorRole::Shadow),
offset: gpui_kit::point(px(0.), px(12.)),
blur_radius: px(32.),
spread_radius: px(0.),
inset: false,
},
])
.when_some(self.height, |d, h| d.h(h))
.children(sidebar)
.child(main)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn chrome_metrics_follow_the_tokens() {
assert_eq!(TITLEBAR_HEIGHT.px_value(), 38.);
assert_eq!(TRAFFIC_LIGHT_INSET.px_value(), 76.);
assert_eq!(TRAFFIC_LIGHT_OFFSET_Y.px_value(), 13.);
assert_eq!(SIDEBAR_WIDTH.px_value(), 240.);
}
#[test]
fn blurred_lights_go_gray() {
crate::theme::tests_set(CarbonTheme::G100);
assert_eq!(
traffic_colors(false),
[color_in(CarbonTheme::G100, ColorRole::LayerAccent01); 3]
);
assert_ne!(traffic_colors(true)[0], traffic_colors(true)[1]);
}
}