From 514ab043f21447ec1fd6f5cc09ee9b5e65663517 Mon Sep 17 00:00:00 2001 From: TakWolf <6064962+TakWolf@users.noreply.github.com> Date: Fri, 14 Aug 2020 02:10:34 +0800 Subject: [PATCH] [macos] add NSWindow.hasShadow support (#1637) * [macos] add NSWindow.hasShadow * change log * cargo fmt * Update CHANGELOG.md * Update src/platform_impl/macos/window.rs * Update src/platform/macos.rs * set_has_shadow() with cuter format * adjust code * cargo fmt * changelog --- CHANGELOG.md | 1 + src/platform/macos.rs | 23 +++++++++++++++++++ src/platform_impl/macos/window.rs | 38 ++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae81ae6f..c109c9c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - On Android, `set_fullscreen` is now a no-op instead of a runtime crash. - On iOS and Android, `set_inner_size` is now a no-op instead of a runtime crash. - On Android, fix `ControlFlow::Poll` not polling the Android event queue. +- On macOS, add `NSWindow.hasShadow` support. - **Breaking:** On Web, `set_cursor_position` and `set_cursor_grab` will now always return an error. - **Breaking:** `PixelDelta` scroll events now return a `PhysicalPosition`. diff --git a/src/platform/macos.rs b/src/platform/macos.rs index f07e2f6e..f6f18d44 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -56,6 +56,12 @@ pub trait WindowExtMacOS { /// And allows the user to have a fullscreen window without using another /// space or taking control over the entire monitor. fn set_simple_fullscreen(&self, fullscreen: bool) -> bool; + + /// Returns whether or not the window has shadow. + fn has_shadow(&self) -> bool; + + /// Sets whether or not the window has shadow. + fn set_has_shadow(&self, has_shadow: bool); } impl WindowExtMacOS for Window { @@ -83,6 +89,16 @@ impl WindowExtMacOS for Window { fn set_simple_fullscreen(&self, fullscreen: bool) -> bool { self.window.set_simple_fullscreen(fullscreen) } + + #[inline] + fn has_shadow(&self) -> bool { + self.window.has_shadow() + } + + #[inline] + fn set_has_shadow(&self, has_shadow: bool) { + self.window.set_has_shadow(has_shadow) + } } /// Corresponds to `NSApplicationActivationPolicy`. @@ -131,6 +147,7 @@ pub trait WindowBuilderExtMacOS { /// Build window with `resizeIncrements` property. Values must not be 0. fn with_resize_increments(self, increments: LogicalSize) -> WindowBuilder; fn with_disallow_hidpi(self, disallow_hidpi: bool) -> WindowBuilder; + fn with_has_shadow(self, has_shadow: bool) -> WindowBuilder; } impl WindowBuilderExtMacOS for WindowBuilder { @@ -190,6 +207,12 @@ impl WindowBuilderExtMacOS for WindowBuilder { self.platform_specific.disallow_hidpi = disallow_hidpi; self } + + #[inline] + fn with_has_shadow(mut self, has_shadow: bool) -> WindowBuilder { + self.platform_specific.has_shadow = has_shadow; + self + } } /// Additional methods on `MonitorHandle` that are specific to MacOS. diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 5505b661..175cf733 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -60,7 +60,7 @@ pub fn get_window_id(window_cocoa_id: id) -> Id { Id(window_cocoa_id as *const Object as _) } -#[derive(Clone, Default)] +#[derive(Clone)] pub struct PlatformSpecificWindowBuilderAttributes { pub activation_policy: ActivationPolicy, pub movable_by_window_background: bool, @@ -71,6 +71,25 @@ pub struct PlatformSpecificWindowBuilderAttributes { pub fullsize_content_view: bool, pub resize_increments: Option>, pub disallow_hidpi: bool, + pub has_shadow: bool, +} + +impl Default for PlatformSpecificWindowBuilderAttributes { + #[inline] + fn default() -> Self { + Self { + activation_policy: Default::default(), + movable_by_window_background: false, + titlebar_transparent: false, + title_hidden: false, + titlebar_hidden: false, + titlebar_buttons_hidden: false, + fullsize_content_view: false, + resize_increments: None, + disallow_hidpi: false, + has_shadow: true, + } + } } fn create_app(activation_policy: ActivationPolicy) -> Option { @@ -224,6 +243,10 @@ fn create_window( } } + if !pl_attrs.has_shadow { + ns_window.setHasShadow_(NO); + } + ns_window.center(); ns_window }); @@ -1094,6 +1117,19 @@ impl WindowExtMacOS for UnownedWindow { } } } + + #[inline] + fn has_shadow(&self) -> bool { + unsafe { self.ns_window.hasShadow() == YES } + } + + #[inline] + fn set_has_shadow(&self, has_shadow: bool) { + unsafe { + self.ns_window + .setHasShadow_(if has_shadow { YES } else { NO }) + } + } } impl Drop for UnownedWindow {