Add the ability to hide the titlebar when creating windows

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-08-22 18:17:14 +02:00
parent ca618b02b6
commit 21c91a29e7
3 changed files with 41 additions and 20 deletions

View file

@ -136,8 +136,13 @@ pub trait WindowContext {
#[derive(Debug)] #[derive(Debug)]
pub struct WindowOptions<'a> { pub struct WindowOptions<'a> {
pub bounds: WindowBounds, pub bounds: WindowBounds,
pub titlebar: Option<TitlebarOptions<'a>>,
}
#[derive(Debug)]
pub struct TitlebarOptions<'a> {
pub title: Option<&'a str>, pub title: Option<&'a str>,
pub titlebar_appears_transparent: bool, pub appears_transparent: bool,
pub traffic_light_position: Option<Vector2F>, pub traffic_light_position: Option<Vector2F>,
} }
@ -246,9 +251,11 @@ impl<'a> Default for WindowOptions<'a> {
fn default() -> Self { fn default() -> Self {
Self { Self {
bounds: WindowBounds::Maximized, bounds: WindowBounds::Maximized,
title: Default::default(), titlebar: Some(TitlebarOptions {
titlebar_appears_transparent: Default::default(), title: Default::default(),
traffic_light_position: Default::default(), appears_transparent: Default::default(),
traffic_light_position: Default::default(),
}),
} }
} }
} }

View file

@ -339,13 +339,19 @@ impl Window {
WindowBounds::Fixed(rect) => rect, WindowBounds::Fixed(rect) => rect,
} }
.to_ns_rect(); .to_ns_rect();
let mut style_mask = NSWindowStyleMask::NSClosableWindowMask
| NSWindowStyleMask::NSMiniaturizableWindowMask
| NSWindowStyleMask::NSResizableWindowMask
| NSWindowStyleMask::NSTitledWindowMask;
if options.titlebar_appears_transparent { let mut style_mask;
style_mask |= NSWindowStyleMask::NSFullSizeContentViewWindowMask; 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]; let native_window: id = msg_send![WINDOW_CLASS, alloc];
@ -409,7 +415,10 @@ impl Window {
command_queue: device.new_command_queue(), command_queue: device.new_command_queue(),
last_fresh_keydown: None, last_fresh_keydown: None,
layer, 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, previous_modifiers_changed_event: None,
ime_state: ImeState::None, ime_state: ImeState::None,
ime_text: None, ime_text: None,
@ -425,12 +434,15 @@ impl Window {
Rc::into_raw(window.0.clone()) as *const c_void, Rc::into_raw(window.0.clone()) as *const c_void,
); );
if let Some(title) = options.title.as_ref() { if let Some(titlebar) = options.titlebar {
native_window.setTitle_(NSString::alloc(nil).init_str(title)); if let Some(title) = titlebar.title {
} native_window.setTitle_(NSString::alloc(nil).init_str(title));
if options.titlebar_appears_transparent { }
native_window.setTitlebarAppearsTransparent_(YES); if titlebar.appears_transparent {
native_window.setTitlebarAppearsTransparent_(YES);
}
} }
native_window.setAcceptsMouseMovedEvents_(YES); native_window.setAcceptsMouseMovedEvents_(YES);
native_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable); native_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);

View file

@ -20,7 +20,7 @@ use gpui::{
geometry::vector::vec2f, geometry::vector::vec2f,
impl_actions, impl_actions,
platform::{WindowBounds, WindowOptions}, platform::{WindowBounds, WindowOptions},
AssetSource, AsyncAppContext, ViewContext, AssetSource, AsyncAppContext, TitlebarOptions, ViewContext,
}; };
use language::Rope; use language::Rope;
pub use lsp; pub use lsp;
@ -330,9 +330,11 @@ pub fn initialize_workspace(
pub fn build_window_options() -> WindowOptions<'static> { pub fn build_window_options() -> WindowOptions<'static> {
WindowOptions { WindowOptions {
bounds: WindowBounds::Maximized, bounds: WindowBounds::Maximized,
title: None, titlebar: Some(TitlebarOptions {
titlebar_appears_transparent: true, title: None,
traffic_light_position: Some(vec2f(8., 8.)), appears_transparent: true,
traffic_light_position: Some(vec2f(8., 8.)),
}),
} }
} }