1use gpui::{
2 AnyElement, Context, Decorations, Entity, Hsla, InteractiveElement, IntoElement, MouseButton,
3 ParentElement, Pixels, StatefulInteractiveElement, Styled, Window, WindowControlArea, div, px,
4};
5use smallvec::SmallVec;
6use std::mem;
7use ui::prelude::*;
8
9use crate::{
10 platforms::{platform_linux, platform_mac, platform_windows},
11 system_window_tabs::SystemWindowTabs,
12};
13
14pub struct PlatformTitleBar {
15 id: ElementId,
16 platform_style: PlatformStyle,
17 children: SmallVec<[AnyElement; 2]>,
18 should_move: bool,
19 system_window_tabs: Entity<SystemWindowTabs>,
20}
21
22impl PlatformTitleBar {
23 pub fn new(id: impl Into<ElementId>, cx: &mut Context<Self>) -> Self {
24 let platform_style = PlatformStyle::platform();
25 let system_window_tabs = cx.new(|_cx| SystemWindowTabs::new());
26
27 Self {
28 id: id.into(),
29 platform_style,
30 children: SmallVec::new(),
31 should_move: false,
32 system_window_tabs,
33 }
34 }
35
36 #[cfg(not(target_os = "windows"))]
37 pub fn height(window: &mut Window) -> Pixels {
38 (1.75 * window.rem_size()).max(px(34.))
39 }
40
41 #[cfg(target_os = "windows")]
42 pub fn height(_window: &mut Window) -> Pixels {
43 // todo(windows) instead of hard coded size report the actual size to the Windows platform API
44 px(32.)
45 }
46
47 pub fn title_bar_color(&self, window: &mut Window, cx: &mut Context<Self>) -> Hsla {
48 if cfg!(any(target_os = "linux", target_os = "freebsd")) {
49 if window.is_window_active() && !self.should_move {
50 cx.theme().colors().title_bar_background
51 } else {
52 cx.theme().colors().title_bar_inactive_background
53 }
54 } else {
55 cx.theme().colors().title_bar_background
56 }
57 }
58
59 pub fn set_children<T>(&mut self, children: T)
60 where
61 T: IntoIterator<Item = AnyElement>,
62 {
63 self.children = children.into_iter().collect();
64 }
65}
66
67impl Render for PlatformTitleBar {
68 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
69 let supported_controls = window.window_controls();
70 let decorations = window.window_decorations();
71 let height = Self::height(window);
72 let titlebar_color = self.title_bar_color(window, cx);
73 let close_action = Box::new(workspace::CloseWindow);
74 let children = mem::take(&mut self.children);
75
76 let title_bar = h_flex()
77 .window_control_area(WindowControlArea::Drag)
78 .w_full()
79 .h(height)
80 .map(|this| {
81 if window.is_fullscreen() {
82 this.pl_2()
83 } else if self.platform_style == PlatformStyle::Mac {
84 this.pl(px(platform_mac::TRAFFIC_LIGHT_PADDING))
85 } else {
86 this.pl_2()
87 }
88 })
89 .map(|el| match decorations {
90 Decorations::Server => el,
91 Decorations::Client { tiling, .. } => el
92 .when(!(tiling.top || tiling.right), |el| {
93 el.rounded_tr(theme::CLIENT_SIDE_DECORATION_ROUNDING)
94 })
95 .when(!(tiling.top || tiling.left), |el| {
96 el.rounded_tl(theme::CLIENT_SIDE_DECORATION_ROUNDING)
97 })
98 // this border is to avoid a transparent gap in the rounded corners
99 .mt(px(-1.))
100 .border(px(1.))
101 .border_color(titlebar_color),
102 })
103 .bg(titlebar_color)
104 .content_stretch()
105 .child(
106 div()
107 .id(self.id.clone())
108 .flex()
109 .flex_row()
110 .items_center()
111 .justify_between()
112 .w_full()
113 // Note: On Windows the title bar behavior is handled by the platform implementation.
114 .when(self.platform_style == PlatformStyle::Mac, |this| {
115 this.on_click(|event, window, _| {
116 if event.click_count() == 2 {
117 window.titlebar_double_click();
118 }
119 })
120 })
121 .when(self.platform_style == PlatformStyle::Linux, |this| {
122 this.on_click(|event, window, _| {
123 if event.click_count() == 2 {
124 window.zoom_window();
125 }
126 })
127 })
128 .children(children),
129 )
130 .when(!window.is_fullscreen(), |title_bar| {
131 match self.platform_style {
132 PlatformStyle::Mac => title_bar,
133 PlatformStyle::Linux => {
134 if matches!(decorations, Decorations::Client { .. }) {
135 title_bar
136 .child(platform_linux::LinuxWindowControls::new(close_action))
137 .when(supported_controls.window_menu, |titlebar| {
138 titlebar
139 .on_mouse_down(MouseButton::Right, move |ev, window, _| {
140 window.show_window_menu(ev.position)
141 })
142 })
143 .on_mouse_move(cx.listener(move |this, _ev, window, _| {
144 if this.should_move {
145 this.should_move = false;
146 window.start_window_move();
147 }
148 }))
149 .on_mouse_down_out(cx.listener(move |this, _ev, _window, _cx| {
150 this.should_move = false;
151 }))
152 .on_mouse_up(
153 MouseButton::Left,
154 cx.listener(move |this, _ev, _window, _cx| {
155 this.should_move = false;
156 }),
157 )
158 .on_mouse_down(
159 MouseButton::Left,
160 cx.listener(move |this, _ev, _window, _cx| {
161 this.should_move = true;
162 }),
163 )
164 } else {
165 title_bar
166 }
167 }
168 PlatformStyle::Windows => {
169 title_bar.child(platform_windows::WindowsWindowControls::new(height))
170 }
171 }
172 });
173
174 v_flex()
175 .w_full()
176 .child(title_bar)
177 .child(self.system_window_tabs.clone().into_any_element())
178 }
179}
180
181impl ParentElement for PlatformTitleBar {
182 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
183 self.children.extend(elements)
184 }
185}