Skip to content

Commit

Permalink
On macOS, add support for Window::set_blur
Browse files Browse the repository at this point in the history
  • Loading branch information
kchibisov committed Oct 27, 2023
1 parent 12dbbf8 commit 904128e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Unreleased` header.
- On Wayland, use `configure_bounds` to constrain `with_inner_size` when compositor wants users to pick size.
- On Windows, fix deadlock when accessing the state during `Cursor{Enter,Leave}`.
- On macOS, fix deadlock when entering a nested event loop from an event handler.
- On macOS, add support for `Window::set_blur`.

# 0.29.2

Expand Down
3 changes: 3 additions & 0 deletions src/platform_impl/macos/appkit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extern_methods!(
#[method(frame)]
pub(crate) fn frame(&self) -> NSRect;

#[method(windowNumber)]
pub(crate) fn windowNumber(&self) -> NSInteger;

#[method(backingScaleFactor)]
pub(crate) fn backingScaleFactor(&self) -> CGFloat;

Expand Down
9 changes: 9 additions & 0 deletions src/platform_impl/macos/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![allow(dead_code, non_snake_case, non_upper_case_globals)]

use objc2::{ffi::NSInteger, runtime::AnyObject};
use std::ffi::c_void;

use core_foundation::{
Expand Down Expand Up @@ -113,6 +114,14 @@ extern "C" {
pub fn CGDisplayModeCopyPixelEncoding(mode: CGDisplayModeRef) -> CFStringRef;
pub fn CGDisplayModeRetain(mode: CGDisplayModeRef);
pub fn CGDisplayModeRelease(mode: CGDisplayModeRef);

// Wildly used private APIs, even Apple uses them for their Terminal.app.
pub fn CGSMainConnectionID() -> *mut AnyObject;
pub fn CGSSetWindowBackgroundBlurRadius(
connection_id: *mut AnyObject,
window_id: NSInteger,
radius: i64,
) -> i32;
}

mod core_video {
Expand Down
16 changes: 15 additions & 1 deletion src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ use super::appkit::{
NSView, NSWindow, NSWindowButton, NSWindowLevel, NSWindowSharingType, NSWindowStyleMask,
NSWindowTabbingMode, NSWindowTitleVisibility,
};
use super::ffi::CGSMainConnectionID;
use super::ffi::CGSSetWindowBackgroundBlurRadius;

pub(crate) struct Window {
window: MainThreadBound<Id<WinitWindow>>,
Expand Down Expand Up @@ -494,6 +496,10 @@ impl WinitWindow {
this.setBackgroundColor(&NSColor::clear());
}

if attrs.blur {
this.set_blur(attrs.blur);
}

if let Some(dim) = attrs.min_inner_size {
this.set_min_inner_size(Some(dim));
}
Expand Down Expand Up @@ -582,7 +588,15 @@ impl WinitWindow {
self.setOpaque(!transparent)
}

pub fn set_blur(&self, _blur: bool) {}
pub fn set_blur(&self, blur: bool) {
// NOTE: in general we want to specify the blur radius, but the choice of 80
// should be a reasonable default.
let radius = if blur { 80 } else { 0 };
let window_number = self.windowNumber();
unsafe {
CGSSetWindowBackgroundBlurRadius(CGSMainConnectionID(), window_number, radius);
}
}

pub fn set_visible(&self, visible: bool) {
match visible {
Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ impl Window {
///
/// ## Platform-specific
///
/// - **Android / iOS / macOS / X11 / Web / Windows:** Unsupported.
/// - **Android / iOS / X11 / Web / Windows:** Unsupported.
/// - **Wayland:** Only works with org_kde_kwin_blur_manager protocol.
#[inline]
pub fn set_blur(&self, blur: bool) {
Expand Down

0 comments on commit 904128e

Please sign in to comment.