From 7e1c70964d54b2bdc33aad316b310570ede1fdcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20G=C3=BCnzler?= Date: Mon, 22 Jan 2018 18:07:51 +0000 Subject: [PATCH] [macOS] Move the window if there is no title bar (#382) * macOS: Move the window if there is no title bar On macOS by default windows can only be moved by clicking and dragging on the titlebar, if we spawn a window without one we need to set the `movableByWindowBackground` property. Partial fix for #368 * macOS: Make moveByWindowBackground optional Implements setting the property via WindowBuilderExt: WindowBuilder::new() .with_decorations(false) .with_movable_by_window_background(true) * Update CHANGELOG --- CHANGELOG.md | 1 + src/os/macos.rs | 8 ++++++++ src/platform/macos/window.rs | 12 ++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 734814eb..0b7282bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- Added method `os::macos::WindowBuilderExt::with_movable_by_window_background(bool)` that allows to move a window without a titlebar - `with_decorations(false)` - Implement `Window::set_fullscreen`, `Window::set_maximized` and `Window::set_decorations` for Wayland. # Version 0.10.0 (2017-12-27) diff --git a/src/os/macos.rs b/src/os/macos.rs index b3cd0289..5a6ef1bb 100755 --- a/src/os/macos.rs +++ b/src/os/macos.rs @@ -63,6 +63,7 @@ impl From for NSApplicationActivationPolicy { /// Additional methods on `WindowBuilder` that are specific to MacOS. 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; } impl WindowBuilderExt for WindowBuilder { @@ -72,6 +73,13 @@ impl WindowBuilderExt for WindowBuilder { self.platform_specific.activation_policy = activation_policy; self } + + /// Enables click-and-drag behavior for the entire window, not just the titlebar + #[inline] + fn with_movable_by_window_background(mut self, movable_by_window_background: bool) -> WindowBuilder { + self.platform_specific.movable_by_window_background = movable_by_window_background; + self + } } /// Additional methods on `MonitorId` that are specific to MacOS. diff --git a/src/platform/macos/window.rs b/src/platform/macos/window.rs index 1eb41235..8c1e5842 100644 --- a/src/platform/macos/window.rs +++ b/src/platform/macos/window.rs @@ -254,6 +254,7 @@ impl Drop for WindowDelegate { #[derive(Clone, Default)] pub struct PlatformSpecificWindowBuilderAttributes { pub activation_policy: ActivationPolicy, + pub movable_by_window_background: bool, } pub struct Window2 { @@ -312,7 +313,7 @@ impl Window2 { None => { return Err(OsError(format!("Couldn't create NSApplication"))); }, }; - let window = match Window2::create_window(win_attribs) + let window = match Window2::create_window(win_attribs, pl_attribs) { Some(window) => window, None => { return Err(OsError(format!("Couldn't create NSWindow"))); }, @@ -381,7 +382,10 @@ impl Window2 { } } - fn create_window(attrs: &WindowAttributes) -> Option { + fn create_window( + attrs: &WindowAttributes, + pl_attrs: &PlatformSpecificWindowBuilderAttributes) + -> Option { unsafe { let screen = match attrs.fullscreen { Some(ref monitor_id) => { @@ -449,6 +453,10 @@ impl Window2 { window.setTitlebarAppearsTransparent_(YES); } + if pl_attrs.movable_by_window_background { + window.setMovableByWindowBackground_(YES); + } + if screen.is_some() { window.setLevel_(appkit::NSMainMenuWindowLevel as i64 + 1); }