Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macOS] WindowBuilderExt additions for the titlebar #389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/os/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,19 @@ impl From<ActivationPolicy> for NSApplicationActivationPolicy {
}

/// Additional methods on `WindowBuilder` that are specific to MacOS.
///
/// **Note:** Properties dealing with the titlebar will be overwritten by the `with_decorations` method
/// on the base `WindowBuilder`:
///
/// - `with_titlebar_transparent`
/// - `with_title_hidden`
/// - `with_fullsize_content_view`
pub trait WindowBuilderExt {
fn with_activation_policy(self, activation_policy: ActivationPolicy) -> WindowBuilder;
fn with_movable_by_window_background(self, movable_by_window_background: bool) -> WindowBuilder;
fn with_titlebar_transparent(self, titlebar_transparent: bool) -> WindowBuilder;
fn with_title_hidden(self, title_hidden: bool) -> WindowBuilder;
fn with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder;
}

impl WindowBuilderExt for WindowBuilder {
Expand All @@ -80,6 +90,27 @@ impl WindowBuilderExt for WindowBuilder {
self.platform_specific.movable_by_window_background = movable_by_window_background;
self
}

/// Makes the titlebar transparent and allows the content to appear behind it
#[inline]
fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> WindowBuilder {
self.platform_specific.titlebar_transparent = titlebar_transparent;
self
}

/// Hides the window title
#[inline]
fn with_title_hidden(mut self, title_hidden: bool) -> WindowBuilder {
self.platform_specific.title_hidden = title_hidden;
self
}

/// Makes the window content appear behind the titlebar
#[inline]
fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> WindowBuilder {
self.platform_specific.fullsize_content_view = fullsize_content_view;
self
}
}

/// Additional methods on `MonitorId` that are specific to MacOS.
Expand Down
46 changes: 36 additions & 10 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ impl Drop for WindowDelegate {
pub struct PlatformSpecificWindowBuilderAttributes {
pub activation_policy: ActivationPolicy,
pub movable_by_window_background: bool,
pub titlebar_transparent: bool,
pub title_hidden: bool,
pub fullsize_content_view: bool,
}

pub struct Window2 {
Expand Down Expand Up @@ -423,17 +426,34 @@ impl Window2 {

let masks = if screen.is_some() {
// Fullscreen window
NSWindowStyleMask::NSBorderlessWindowMask | NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSBorderlessWindowMask |
NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSTitledWindowMask
} else if attrs.decorations {
// Window2 with a titlebar
NSWindowStyleMask::NSClosableWindowMask | NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSResizableWindowMask | NSWindowStyleMask::NSTitledWindowMask
} else {
} else if !attrs.decorations {
// Window2 without a titlebar
NSWindowStyleMask::NSClosableWindowMask | NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSClosableWindowMask |
NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSFullSizeContentViewWindowMask
} else if !pl_attrs.titlebar_transparent {
// Window2 with a titlebar
NSWindowStyleMask::NSClosableWindowMask |
NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSTitledWindowMask
} else if pl_attrs.fullsize_content_view {
// Window2 with a transparent titlebar and fullsize content view
NSWindowStyleMask::NSClosableWindowMask |
NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSTitledWindowMask |
NSWindowStyleMask::NSFullSizeContentViewWindowMask
} else {
// Window2 with a transparent titlebar and regular content view
NSWindowStyleMask::NSClosableWindowMask |
NSWindowStyleMask::NSMiniaturizableWindowMask |
NSWindowStyleMask::NSResizableWindowMask |
NSWindowStyleMask::NSTitledWindowMask
};

let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
Expand All @@ -448,15 +468,21 @@ impl Window2 {
window.setTitle_(*title);
window.setAcceptsMouseMovedEvents_(YES);

if !attrs.decorations {
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
if pl_attrs.titlebar_transparent {
window.setTitlebarAppearsTransparent_(YES);
}

if pl_attrs.title_hidden {
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
}
if pl_attrs.movable_by_window_background {
window.setMovableByWindowBackground_(YES);
}

if !attrs.decorations {
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
window.setTitlebarAppearsTransparent_(YES);
}

if screen.is_some() {
window.setLevel_(appkit::NSMainMenuWindowLevel as i64 + 1);
}
Expand Down