macOS: Feature-gate CGSSetWindowBackgroundBlurRadius

Behind the Cargo feature `private-apple-apis`.
This commit is contained in:
Mads Marquart
2026-03-24 15:18:49 +01:00
parent 5a74bf0aab
commit 12e2d24e10
7 changed files with 30 additions and 24 deletions

View File

@@ -9,6 +9,7 @@ rust-version.workspace = true
version.workspace = true
[features]
private-apple-apis = []
serde = ["dep:serde", "bitflags/serde", "smol_str/serde", "dpi/serde"]
[dependencies]

View File

@@ -4,8 +4,6 @@
use std::ffi::c_void;
use objc2::ffi::NSInteger;
use objc2::runtime::AnyObject;
use objc2_core_foundation::{CFString, CFUUID, cf_type};
use objc2_core_graphics::CGDirectDisplayID;
@@ -28,17 +26,6 @@ unsafe extern "C" {
pub fn CGDisplayGetDisplayIDFromUUID(uuid: &CFUUID) -> CGDirectDisplayID;
}
#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
// Wildly used private APIs; 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;
}
#[repr(transparent)]
pub struct TISInputSource(std::ffi::c_void);

View File

@@ -55,7 +55,6 @@ use winit_core::window::{
use super::app_state::AppState;
use super::cursor::{CustomCursor, cursor_from_icon};
use super::ffi;
use super::monitor::{self, MonitorHandle, flip_window_screen_coordinates, get_display_id};
use super::util::cgerr;
use super::view::WinitView;
@@ -973,17 +972,30 @@ 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 window_number = self.window().windowNumber();
unsafe {
ffi::CGSSetWindowBackgroundBlurRadius(
ffi::CGSMainConnectionID(),
window_number,
radius,
);
#[cfg(feature = "private-apple-apis")]
{
#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
// Wildly used private APIs; Apple uses them for their Terminal.app.
pub fn CGSMainConnectionID() -> *mut objc2::runtime::AnyObject;
pub fn CGSSetWindowBackgroundBlurRadius(
connection_id: *mut objc2::runtime::AnyObject,
window_id: objc2_foundation::NSInteger,
radius: i64,
) -> i32;
}
// 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.window().windowNumber();
unsafe {
CGSSetWindowBackgroundBlurRadius(CGSMainConnectionID(), window_number, radius)
};
}
// TODO: Implement blur using public methods somehow?
let _ = blur;
}
pub fn set_visible(&self, visible: bool) {

View File

@@ -879,6 +879,7 @@ pub trait Window: AsAny + Send + Sync + fmt::Debug {
///
/// ## Platform-specific
///
/// - **macOS**: Must enable the `private-apple-apis` Cargo feature.
/// - **Android / iOS / X11 / Web / Windows:** Unsupported.
/// - **Wayland:** Only works with org_kde_kwin_blur_manager protocol.
fn set_blur(&self, blur: bool);

View File

@@ -44,6 +44,7 @@ default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
android-game-activity = ["winit-android/game-activity"]
android-native-activity = ["winit-android/native-activity"]
mint = ["dpi/mint"]
private-apple-apis = ["winit-appkit/private-apple-apis"]
serde = [
"dep:serde",
"cursor-icon/serde",

View File

@@ -53,6 +53,7 @@ changelog entry.
- Updated `windows-sys` to `v0.61`.
- On older macOS versions (tested up to 12.7.6), applications now receive mouse movement events for unfocused windows, matching the behavior on other platforms.
- On macOS, using the private API `CGSSetWindowBackgroundBlurRadius` for `Window::set_blur` is now disabled by default. It can be re-enabled using the Cargo feature `private-apple-apis`.
### Fixed

View File

@@ -195,6 +195,9 @@
//! * `rwh_06`: Implement `raw-window-handle v0.6` traits.
//! * `serde`: Enables serialization/deserialization of certain types with [Serde](https://crates.io/crates/serde).
//! * `mint`: Enables mint (math interoperability standard types) conversions.
//! * `private-apple-apis`: Enables private APIs whose usage might cause rejections from the App
//! Store. Currently enables the use of `CGSSetWindowBackgroundBlurRadius`, commonly used for
//! terminal emulators.
//!
//! See the [`platform`] module for documentation on platform-specific cargo
//! features.