From 21c91a29e7c18b88c2715b2984f3a60e34ff126c Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 22 Aug 2022 18:17:14 +0200 Subject: [PATCH] Add the ability to hide the titlebar when creating windows Co-Authored-By: Nathan Sobo --- crates/gpui/src/platform.rs | 15 ++++++++--- crates/gpui/src/platform/mac/window.rs | 36 +++++++++++++++++--------- crates/zed/src/zed.rs | 10 ++++--- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 38e8348ac3..e31cc46213 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -136,8 +136,13 @@ pub trait WindowContext { #[derive(Debug)] pub struct WindowOptions<'a> { pub bounds: WindowBounds, + pub titlebar: Option>, +} + +#[derive(Debug)] +pub struct TitlebarOptions<'a> { pub title: Option<&'a str>, - pub titlebar_appears_transparent: bool, + pub appears_transparent: bool, pub traffic_light_position: Option, } @@ -246,9 +251,11 @@ impl<'a> Default for WindowOptions<'a> { fn default() -> Self { Self { bounds: WindowBounds::Maximized, - title: Default::default(), - titlebar_appears_transparent: Default::default(), - traffic_light_position: Default::default(), + titlebar: Some(TitlebarOptions { + title: Default::default(), + appears_transparent: Default::default(), + traffic_light_position: Default::default(), + }), } } } diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 9b52041d80..5ceb8c96be 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -339,13 +339,19 @@ impl Window { WindowBounds::Fixed(rect) => rect, } .to_ns_rect(); - let mut style_mask = NSWindowStyleMask::NSClosableWindowMask - | NSWindowStyleMask::NSMiniaturizableWindowMask - | NSWindowStyleMask::NSResizableWindowMask - | NSWindowStyleMask::NSTitledWindowMask; - if options.titlebar_appears_transparent { - style_mask |= NSWindowStyleMask::NSFullSizeContentViewWindowMask; + let mut style_mask; + if let Some(titlebar) = options.titlebar.as_ref() { + style_mask = NSWindowStyleMask::NSClosableWindowMask + | NSWindowStyleMask::NSMiniaturizableWindowMask + | NSWindowStyleMask::NSResizableWindowMask + | NSWindowStyleMask::NSTitledWindowMask; + + if titlebar.appears_transparent { + style_mask |= NSWindowStyleMask::NSFullSizeContentViewWindowMask; + } + } else { + style_mask = NSWindowStyleMask::empty(); } let native_window: id = msg_send![WINDOW_CLASS, alloc]; @@ -409,7 +415,10 @@ impl Window { command_queue: device.new_command_queue(), last_fresh_keydown: None, layer, - traffic_light_position: options.traffic_light_position, + traffic_light_position: options + .titlebar + .as_ref() + .and_then(|titlebar| titlebar.traffic_light_position), previous_modifiers_changed_event: None, ime_state: ImeState::None, ime_text: None, @@ -425,12 +434,15 @@ impl Window { Rc::into_raw(window.0.clone()) as *const c_void, ); - if let Some(title) = options.title.as_ref() { - native_window.setTitle_(NSString::alloc(nil).init_str(title)); - } - if options.titlebar_appears_transparent { - native_window.setTitlebarAppearsTransparent_(YES); + if let Some(titlebar) = options.titlebar { + if let Some(title) = titlebar.title { + native_window.setTitle_(NSString::alloc(nil).init_str(title)); + } + if titlebar.appears_transparent { + native_window.setTitlebarAppearsTransparent_(YES); + } } + native_window.setAcceptsMouseMovedEvents_(YES); native_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable); diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index a8f10a1579..59e9f8de9d 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -20,7 +20,7 @@ use gpui::{ geometry::vector::vec2f, impl_actions, platform::{WindowBounds, WindowOptions}, - AssetSource, AsyncAppContext, ViewContext, + AssetSource, AsyncAppContext, TitlebarOptions, ViewContext, }; use language::Rope; pub use lsp; @@ -330,9 +330,11 @@ pub fn initialize_workspace( pub fn build_window_options() -> WindowOptions<'static> { WindowOptions { bounds: WindowBounds::Maximized, - title: None, - titlebar_appears_transparent: true, - traffic_light_position: Some(vec2f(8., 8.)), + titlebar: Some(TitlebarOptions { + title: None, + appears_transparent: true, + traffic_light_position: Some(vec2f(8., 8.)), + }), } }