Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 17 additions & 10 deletions src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct PlatformSpecificWindowAttributes {
pub tabbing_identifier: Option<String>,
pub option_as_alt: OptionAsAlt,
pub borderless_game: bool,
pub blur_radius: i64,
}

impl Default for PlatformSpecificWindowAttributes {
Expand All @@ -74,6 +75,7 @@ impl Default for PlatformSpecificWindowAttributes {
tabbing_identifier: None,
option_as_alt: Default::default(),
borderless_game: false,
blur_radius: 80,
}
}
}
Expand Down Expand Up @@ -123,6 +125,7 @@ pub(crate) struct State {
is_simple_fullscreen: Cell<bool>,
saved_style: Cell<Option<NSWindowStyleMask>>,
is_borderless_game: Cell<bool>,
blur_radius: Cell<i64>,
}

declare_class!(
Expand Down Expand Up @@ -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<WindowDelegate> = unsafe { msg_send_id![super(delegate), init] };

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 =
Expand Down