gpui_material_extras/window_chrome/
platform.rs1use std::rc::Rc;
2
3use gpui::{App, Decorations, Pixels, Window, WindowDecorations, WindowOptions, point, px};
4
5pub const DEFAULT_TITLE_BAR_HEIGHT: Pixels = px(48.);
7
8pub const DEFAULT_MACOS_TRAFFIC_LIGHT_PADDING: Pixels = px(78.);
10
11pub const DEFAULT_WINDOWS_CAPTION_BUTTON_WIDTH: Pixels = px(46.);
13
14pub const DEFAULT_LINUX_CAPTION_BUTTON_WIDTH: Pixels = px(40.);
16
17pub(super) type CloseHandler = Rc<dyn Fn(&mut Window, &mut App)>;
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
21pub enum ChromePlatform {
22 Windows,
25 MacOs,
27 Linux,
30}
31
32impl ChromePlatform {
33 pub const fn current() -> Self {
35 #[cfg(target_os = "macos")]
36 {
37 Self::MacOs
38 }
39 #[cfg(windows)]
40 {
41 Self::Windows
42 }
43 #[cfg(not(any(windows, target_os = "macos")))]
44 {
45 Self::Linux
46 }
47 }
48
49 pub const fn uses_traffic_lights(self) -> bool {
51 matches!(self, Self::MacOs)
52 }
53
54 pub const fn uses_caption_buttons(self) -> bool {
56 matches!(self, Self::Windows | Self::Linux)
57 }
58
59 pub(super) const fn default_caption_button_width(self) -> Pixels {
60 match self {
61 Self::Windows => DEFAULT_WINDOWS_CAPTION_BUTTON_WIDTH,
62 Self::MacOs | Self::Linux => DEFAULT_LINUX_CAPTION_BUTTON_WIDTH,
63 }
64 }
65}
66
67pub fn title_bar_window_options(options: WindowOptions) -> WindowOptions {
75 title_bar_window_options_with_height(options, DEFAULT_TITLE_BAR_HEIGHT)
76}
77
78pub fn title_bar_window_options_with_height(
86 options: WindowOptions,
87 height: Pixels,
88) -> WindowOptions {
89 configure_window_options(options, height, ChromePlatform::current())
90}
91
92pub(super) fn configure_window_options(
93 mut options: WindowOptions,
94 height: Pixels,
95 platform: ChromePlatform,
96) -> WindowOptions {
97 let height = sanitize_positive(height, DEFAULT_TITLE_BAR_HEIGHT);
98 let titlebar = options.titlebar.get_or_insert_default();
99 titlebar.appears_transparent = true;
100
101 match platform {
102 ChromePlatform::MacOs => {
103 if titlebar.traffic_light_position.is_none() {
104 let y = ((height - px(14.)) * 0.5).max(px(6.));
106 titlebar.traffic_light_position = Some(point(px(12.), y));
107 }
108 }
109 ChromePlatform::Linux => {
110 if options.window_decorations.is_none() {
111 options.window_decorations = Some(WindowDecorations::Client);
112 }
113 }
114 ChromePlatform::Windows => {}
115 }
116
117 options
118}
119
120pub(super) fn sanitize_positive(value: Pixels, fallback: Pixels) -> Pixels {
121 let raw = f32::from(value);
122 if raw.is_finite() && raw > 0. {
123 value
124 } else {
125 fallback
126 }
127}
128
129pub(super) fn sanitize_nonnegative(value: Pixels, fallback: Pixels) -> Pixels {
130 let raw = f32::from(value);
131 if raw.is_finite() && raw >= 0. {
132 value
133 } else {
134 fallback
135 }
136}
137
138pub(super) fn should_show_caption_buttons(
139 window: &Window,
140 platform: ChromePlatform,
141 explicit: Option<bool>,
142) -> bool {
143 if window.is_fullscreen() {
144 return false;
145 }
146 if let Some(show) = explicit {
147 return show;
148 }
149 match platform {
150 ChromePlatform::Windows => true,
151 ChromePlatform::MacOs => false,
152 ChromePlatform::Linux => matches!(window.window_decorations(), Decorations::Client { .. }),
153 }
154}
155
156pub(super) fn read_window_title(window: &Window) -> String {
157 #[cfg(windows)]
158 {
159 if let Some(title) = win32::window_title(window) {
160 return title;
161 }
162 }
163 window.window_title()
164}
165
166pub(super) fn activate_minimize(window: &mut Window) {
167 window.minimize_window();
168}
169
170pub(super) fn activate_maximize_or_restore(window: &mut Window) {
171 #[cfg(windows)]
172 {
173 if window.is_maximized() && win32::restore_window(window) {
174 return;
175 }
176 }
177 window.zoom_window();
178}
179
180pub(super) fn activate_close(window: &mut Window, cx: &mut App, on_close: Option<&CloseHandler>) {
181 if let Some(handler) = on_close {
182 handler(window, cx);
183 return;
184 }
185 #[cfg(windows)]
186 {
187 if win32::close_window(window) {
188 return;
189 }
190 }
191 window.remove_window();
192}
193
194#[cfg(windows)]
195mod win32 {
196 use gpui::Window;
197 use raw_window_handle::{HasWindowHandle, RawWindowHandle};
198 use windows::Win32::{
199 Foundation::{HWND, LPARAM, WPARAM},
200 UI::WindowsAndMessaging::{
201 GetWindowTextLengthW, GetWindowTextW, PostMessageW, SC_RESTORE, WM_CLOSE, WM_SYSCOMMAND,
202 },
203 };
204
205 pub(super) fn window_title(window: &Window) -> Option<String> {
206 let hwnd = window_hwnd(window)?;
207 let length = unsafe { GetWindowTextLengthW(hwnd) };
210 if length == 0 {
211 return Some(String::new());
212 }
213 let mut title = vec![0; length as usize + 1];
214 let copied = unsafe { GetWindowTextW(hwnd, &mut title) };
215 Some(String::from_utf16_lossy(&title[..copied as usize]))
216 }
217
218 pub(super) fn restore_window(window: &Window) -> bool {
219 post_window_message(window, WM_SYSCOMMAND, WPARAM(SC_RESTORE as usize))
220 }
221
222 pub(super) fn close_window(window: &Window) -> bool {
223 post_window_message(window, WM_CLOSE, WPARAM(0))
224 }
225
226 fn post_window_message(window: &Window, message: u32, wparam: WPARAM) -> bool {
227 let Some(hwnd) = window_hwnd(window) else {
228 return false;
229 };
230 let _ = unsafe { PostMessageW(Some(hwnd), message, wparam, LPARAM(0)) };
233 true
234 }
235
236 fn window_hwnd(window: &Window) -> Option<HWND> {
237 let handle = HasWindowHandle::window_handle(window).ok()?;
238 let RawWindowHandle::Win32(handle) = handle.as_raw() else {
239 return None;
240 };
241 Some(HWND(handle.hwnd.get() as *mut _))
242 }
243}