Skip to content

Commit dcd16a5

Browse files
committed
feat(macos): allow setting of blur radius for blurred windows
1 parent 11414b6 commit dcd16a5

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
//! [`Window`]: window::Window
166166
//! [`WindowId`]: window::WindowId
167167
//! [`WindowAttributes`]: window::WindowAttributes
168-
//! [window_new]: window::Window::new
168+
//! [window_new]: window::Window
169169
//! [`create_window`]: event_loop::ActiveEventLoop::create_window
170170
//! [`Window::id()`]: window::Window::id
171171
//! [`WindowEvent`]: event::WindowEvent

src/platform/macos.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ pub trait WindowExtMacOS {
100100

101101
/// Getter for the [`WindowExtMacOS::set_borderless_game`].
102102
fn is_borderless_game(&self) -> bool;
103+
104+
/// Set the blur radius of the window.
105+
///
106+
/// Only applies to a window if it's both [`transparent`] and [`blurred`].
107+
///
108+
/// [`transparent`]: Window::set_transparent
109+
/// [`blurred`]: Window::set_blur
110+
fn set_blur_radius(&self, blur_radius: i64);
111+
112+
/// Getter for the [`WindowExtMacOS::set_blur_radius`].
113+
fn blur_radius(&self) -> i64;
103114
}
104115

105116
impl WindowExtMacOS for Window {
@@ -182,6 +193,16 @@ impl WindowExtMacOS for Window {
182193
fn is_borderless_game(&self) -> bool {
183194
self.window.maybe_wait_on_main(|w| w.is_borderless_game())
184195
}
196+
197+
#[inline]
198+
fn set_blur_radius(&self, blur_radius: i64) {
199+
self.window.maybe_wait_on_main(|w| w.set_blur_radius(blur_radius))
200+
}
201+
202+
#[inline]
203+
fn blur_radius(&self) -> i64 {
204+
self.window.maybe_wait_on_main(|w| w.blur_radius())
205+
}
185206
}
186207

187208
/// Corresponds to `NSApplicationActivationPolicy`.
@@ -234,6 +255,10 @@ pub trait WindowAttributesExtMacOS {
234255
fn with_option_as_alt(self, option_as_alt: OptionAsAlt) -> Self;
235256
/// See [`WindowExtMacOS::set_borderless_game`] for details on what this means if set.
236257
fn with_borderless_game(self, borderless_game: bool) -> Self;
258+
/// Sets the blur radius of the window.
259+
///
260+
/// See [`WindowExtMacOS::set_blur_radius`] for details on what this means if set.
261+
fn with_blur_radius(self, blur_radius: i64) -> Self;
237262
}
238263

239264
impl WindowAttributesExtMacOS for WindowAttributes {
@@ -308,6 +333,12 @@ impl WindowAttributesExtMacOS for WindowAttributes {
308333
self.platform_specific.borderless_game = borderless_game;
309334
self
310335
}
336+
337+
#[inline]
338+
fn with_blur_radius(mut self, blur_radius: i64) -> Self {
339+
self.platform_specific.blur_radius = blur_radius;
340+
self
341+
}
311342
}
312343

313344
pub trait EventLoopBuilderExtMacOS {

src/platform_impl/macos/window_delegate.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct PlatformSpecificWindowAttributes {
5656
pub tabbing_identifier: Option<String>,
5757
pub option_as_alt: OptionAsAlt,
5858
pub borderless_game: bool,
59+
pub blur_radius: i64,
5960
}
6061

6162
impl Default for PlatformSpecificWindowAttributes {
@@ -74,6 +75,7 @@ impl Default for PlatformSpecificWindowAttributes {
7475
tabbing_identifier: None,
7576
option_as_alt: Default::default(),
7677
borderless_game: false,
78+
blur_radius: 80,
7779
}
7880
}
7981
}
@@ -123,6 +125,7 @@ pub(crate) struct State {
123125
is_simple_fullscreen: Cell<bool>,
124126
saved_style: Cell<Option<NSWindowStyleMask>>,
125127
is_borderless_game: Cell<bool>,
128+
blur_radius: Cell<i64>,
126129
}
127130

128131
declare_class!(
@@ -729,6 +732,7 @@ impl WindowDelegate {
729732
is_simple_fullscreen: Cell::new(false),
730733
saved_style: Cell::new(None),
731734
is_borderless_game: Cell::new(attrs.platform_specific.borderless_game),
735+
blur_radius: Cell::new(attrs.platform_specific.blur_radius),
732736
});
733737
let delegate: Retained<WindowDelegate> = unsafe { msg_send_id![super(delegate), init] };
734738

@@ -823,13 +827,10 @@ impl WindowDelegate {
823827

824828
let suggested_size = content_size.to_physical(scale_factor);
825829
let new_inner_size = Arc::new(Mutex::new(suggested_size));
826-
app_delegate.handle_window_event(
827-
window.id(),
828-
WindowEvent::ScaleFactorChanged {
829-
scale_factor,
830-
inner_size_writer: InnerSizeWriter::new(Arc::downgrade(&new_inner_size)),
831-
},
832-
);
830+
app_delegate.handle_window_event(window.id(), WindowEvent::ScaleFactorChanged {
831+
scale_factor,
832+
inner_size_writer: InnerSizeWriter::new(Arc::downgrade(&new_inner_size)),
833+
});
833834
let physical_size = *new_inner_size.lock().unwrap();
834835
drop(new_inner_size);
835836

@@ -887,7 +888,7 @@ impl WindowDelegate {
887888
pub fn set_blur(&self, blur: bool) {
888889
// NOTE: in general we want to specify the blur radius, but the choice of 80
889890
// should be a reasonable default.
890-
let radius = if blur { 80 } else { 0 };
891+
let radius = if blur { self.blur_radius() } else { 0 };
891892
let window_number = unsafe { self.window().windowNumber() };
892893
unsafe {
893894
ffi::CGSSetWindowBackgroundBlurRadius(
@@ -1855,6 +1856,14 @@ impl WindowExtMacOS for WindowDelegate {
18551856
fn is_borderless_game(&self) -> bool {
18561857
self.ivars().is_borderless_game.get()
18571858
}
1859+
1860+
fn set_blur_radius(&self, blur_radius: i64) {
1861+
self.ivars().blur_radius.set(blur_radius);
1862+
}
1863+
1864+
fn blur_radius(&self) -> i64 {
1865+
self.ivars().blur_radius.get()
1866+
}
18581867
}
18591868

18601869
const DEFAULT_STANDARD_FRAME: NSRect =

src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@ impl ActivationToken {
18711871
)]
18721872
///
18731873
#[rustfmt::skip]
1874-
/// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token
1874+
/// [`request_activation_token`]: crate::platform
18751875
pub fn from_raw(token: String) -> Self {
18761876
Self { token }
18771877
}

0 commit comments

Comments
 (0)