323 windowbuilder ext macos (#423)

* macOS: Allow hiding the title from the titlebar

* macOS: Allow making the titlebar transparent

* macOS: Give control over content view size

Allows setting `NSFullSizeContentViewWindowMask` with WindowBuilder.

* macOS: add `.with_titlebar_hidden` to WindowBuilderExt

* macOS: adds `titlebar_buttons_hidden` platform specific attribute
This commit is contained in:
Joe Moon 2018-03-22 09:57:35 -07:00 committed by Pierre Krieger
parent 5dcde83b4c
commit ce7a426bb5
2 changed files with 102 additions and 12 deletions

View file

@ -61,9 +61,23 @@ 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_titlebar_hidden`
/// - `with_titlebar_buttons_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_titlebar_hidden(self, titlebar_hidden: bool) -> WindowBuilder;
fn with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> WindowBuilder;
fn with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder;
}
impl WindowBuilderExt for WindowBuilder {
@ -80,6 +94,41 @@ 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 titlebar
#[inline]
fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> WindowBuilder {
self.platform_specific.titlebar_hidden = titlebar_hidden;
self
}
/// Hides the window titlebar buttons
#[inline]
fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> WindowBuilder {
self.platform_specific.titlebar_buttons_hidden = titlebar_buttons_hidden;
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.

View file

@ -13,7 +13,7 @@ use objc::declare::ClassDecl;
use cocoa;
use cocoa::base::{id, nil};
use cocoa::foundation::{NSPoint, NSRect, NSSize, NSString};
use cocoa::appkit::{self, NSApplication, NSColor, NSView, NSWindow, NSWindowStyleMask};
use cocoa::appkit::{self, NSApplication, NSColor, NSView, NSWindow, NSWindowStyleMask, NSWindowButton};
use core_graphics::display::CGDisplay;
@ -255,6 +255,11 @@ 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 titlebar_hidden: bool,
pub titlebar_buttons_hidden: bool,
pub fullsize_content_view: bool,
}
pub struct Window2 {
@ -404,17 +409,37 @@ 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_hidden {
NSWindowStyleMask::NSBorderlessWindowMask |
NSWindowStyleMask::NSResizableWindowMask
} 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_(
@ -429,15 +454,31 @@ impl Window2 {
window.setTitle_(*title);
window.setAcceptsMouseMovedEvents_(YES);
if pl_attrs.titlebar_transparent {
window.setTitlebarAppearsTransparent_(YES);
}
if pl_attrs.title_hidden {
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
}
if pl_attrs.titlebar_buttons_hidden {
let button = window.standardWindowButton_(NSWindowButton::NSWindowFullScreenButton);
msg_send![button, setHidden:YES];
let button = window.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton);
msg_send![button, setHidden:YES];
let button = window.standardWindowButton_(NSWindowButton::NSWindowCloseButton);
msg_send![button, setHidden:YES];
let button = window.standardWindowButton_(NSWindowButton::NSWindowZoomButton);
msg_send![button, setHidden:YES];
}
if pl_attrs.movable_by_window_background {
window.setMovableByWindowBackground_(YES);
}
if !attrs.decorations {
window.setTitleVisibility_(appkit::NSWindowTitleVisibility::NSWindowTitleHidden);
window.setTitlebarAppearsTransparent_(YES);
}
if pl_attrs.movable_by_window_background {
window.setMovableByWindowBackground_(YES);
}
if screen.is_some() {
window.setLevel_(appkit::NSMainMenuWindowLevel as i64 + 1);
}