diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 424c9d6213..d0e40edc90 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -100,6 +100,17 @@ pub trait WindowExtMacOS { /// Getter for the [`WindowExtMacOS::set_borderless_game`]. fn is_borderless_game(&self) -> bool; + + /// Set the blur radius of the window. + /// + /// Only applies to a window if it's both [`transparent`] and [`blurred`]. + /// + /// [`transparent`]: Window::set_transparent + /// [`blurred`]: Window::set_blur + fn set_blur_radius(&self, blur_radius: i64); + + /// Getter for the [`WindowExtMacOS::set_blur_radius`]. + fn blur_radius(&self) -> i64; } impl WindowExtMacOS for Window { @@ -182,6 +193,16 @@ impl WindowExtMacOS for Window { fn is_borderless_game(&self) -> bool { self.window.maybe_wait_on_main(|w| w.is_borderless_game()) } + + #[inline] + fn set_blur_radius(&self, blur_radius: i64) { + self.window.maybe_wait_on_main(|w| w.set_blur_radius(blur_radius)) + } + + #[inline] + fn blur_radius(&self) -> i64 { + self.window.maybe_wait_on_main(|w| w.blur_radius()) + } } /// Corresponds to `NSApplicationActivationPolicy`. @@ -234,6 +255,10 @@ pub trait WindowAttributesExtMacOS { fn with_option_as_alt(self, option_as_alt: OptionAsAlt) -> Self; /// See [`WindowExtMacOS::set_borderless_game`] for details on what this means if set. fn with_borderless_game(self, borderless_game: bool) -> Self; + /// Sets the blur radius of the window. + /// + /// See [`WindowExtMacOS::set_blur_radius`] for details on what this means if set. + fn with_blur_radius(self, blur_radius: i64) -> Self; } impl WindowAttributesExtMacOS for WindowAttributes { @@ -308,6 +333,12 @@ impl WindowAttributesExtMacOS for WindowAttributes { self.platform_specific.borderless_game = borderless_game; self } + + #[inline] + fn with_blur_radius(mut self, blur_radius: i64) -> Self { + self.platform_specific.blur_radius = blur_radius; + self + } } pub trait EventLoopBuilderExtMacOS { diff --git a/src/platform_impl/macos/window_delegate.rs b/src/platform_impl/macos/window_delegate.rs index 3c53ee90bd..8768155a67 100644 --- a/src/platform_impl/macos/window_delegate.rs +++ b/src/platform_impl/macos/window_delegate.rs @@ -56,6 +56,7 @@ pub struct PlatformSpecificWindowAttributes { pub tabbing_identifier: Option, pub option_as_alt: OptionAsAlt, pub borderless_game: bool, + pub blur_radius: i64, } impl Default for PlatformSpecificWindowAttributes { @@ -74,6 +75,7 @@ impl Default for PlatformSpecificWindowAttributes { tabbing_identifier: None, option_as_alt: Default::default(), borderless_game: false, + blur_radius: 80, } } } @@ -123,6 +125,7 @@ pub(crate) struct State { is_simple_fullscreen: Cell, saved_style: Cell>, is_borderless_game: Cell, + blur_radius: Cell, } declare_class!( @@ -729,6 +732,7 @@ impl WindowDelegate { is_simple_fullscreen: Cell::new(false), saved_style: Cell::new(None), is_borderless_game: Cell::new(attrs.platform_specific.borderless_game), + blur_radius: Cell::new(attrs.platform_specific.blur_radius), }); let delegate: Retained = unsafe { msg_send_id![super(delegate), init] }; @@ -823,13 +827,10 @@ impl WindowDelegate { let suggested_size = content_size.to_physical(scale_factor); let new_inner_size = Arc::new(Mutex::new(suggested_size)); - app_delegate.handle_window_event( - window.id(), - WindowEvent::ScaleFactorChanged { - scale_factor, - inner_size_writer: InnerSizeWriter::new(Arc::downgrade(&new_inner_size)), - }, - ); + app_delegate.handle_window_event(window.id(), WindowEvent::ScaleFactorChanged { + scale_factor, + inner_size_writer: InnerSizeWriter::new(Arc::downgrade(&new_inner_size)), + }); let physical_size = *new_inner_size.lock().unwrap(); drop(new_inner_size); @@ -885,9 +886,7 @@ impl WindowDelegate { } 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 radius = if blur { self.blur_radius() } else { 0 }; let window_number = unsafe { self.window().windowNumber() }; unsafe { ffi::CGSSetWindowBackgroundBlurRadius( @@ -1855,6 +1854,14 @@ impl WindowExtMacOS for WindowDelegate { fn is_borderless_game(&self) -> bool { self.ivars().is_borderless_game.get() } + + fn set_blur_radius(&self, blur_radius: i64) { + self.ivars().blur_radius.set(blur_radius); + } + + fn blur_radius(&self) -> i64 { + self.ivars().blur_radius.get() + } } const DEFAULT_STANDARD_FRAME: NSRect =