Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(desktop): add macOS liquid glass teardown and SPI guards
Richie McIlroyc997c64a0b6c
diff --git a/apps/desktop/src-tauri/src/platform/macos/mod.rs b/apps/desktop/src-tauri/src/platform/macos/mod.rsindex b37f75155..b5eb71e68 100644--- a/apps/desktop/src-tauri/src/platform/macos/mod.rs+++ b/apps/desktop/src-tauri/src/platform/macos/mod.rs@@ -54,77 +54,82 @@ pub fn apply_squircle_corners(window: &tauri::WebviewWindow, radius: f64) {}}-pub fn apply_liquid_glass_background(- window: &tauri::Window,- enabled: bool,- radius: f64,-) -> Result<bool, String> {- use cocoa::{- base::{id, nil},- foundation::{NSRect, NSString},- };- use objc::{class, msg_send, runtime::Class, sel, sel_impl};+const TAURI_VIBRANCY_VIEW_TAG: isize = 91376254;+const LIQUID_GLASS_IDENTIFIER: &str = "so.cap.liquid-glass-background";- const TAURI_VIBRANCY_VIEW_TAG: isize = 91376254;+unsafe fn remove_tagged_subview(container: cocoa::base::id, tag: isize) {+ use objc::{msg_send, sel, sel_impl};- unsafe fn remove_tagged_subview(container: id, tag: isize) {- unsafe {- let responds_to_view_with_tag: bool =- msg_send![container, respondsToSelector: sel!(viewWithTag:)];- if !responds_to_view_with_tag {- return;- }+ unsafe {+ let responds_to_view_with_tag: bool =+ msg_send![container, respondsToSelector: sel!(viewWithTag:)];+ if !responds_to_view_with_tag {+ return;+ }- let view: id = msg_send![container, viewWithTag: tag];- if view != nil {- let _: () = msg_send![view, removeFromSuperview];- }+ let view: cocoa::base::id = msg_send![container, viewWithTag: tag];+ if view != cocoa::base::nil {+ let _: () = msg_send![view, removeFromSuperview];}}+}- unsafe fn remove_liquid_glass_subviews(- container: id,- identifier: id,- glass_class: *const Class,- ) {- unsafe {- let subviews: id = msg_send![container, subviews];- if subviews == nil {- return;- }+unsafe fn remove_liquid_glass_subviews(+ container: cocoa::base::id,+ identifier: cocoa::base::id,+ glass_class: *const objc::runtime::Class,+) {+ use cocoa::base::{id, nil};+ use objc::{msg_send, sel, sel_impl};- let count: usize = msg_send![subviews, count];- for index in (0..count).rev() {- let view: id = msg_send![subviews, objectAtIndex: index];- if view == nil {- continue;- }+ unsafe {+ let subviews: id = msg_send![container, subviews];+ if subviews == nil {+ return;+ }- let is_glass_view: bool = msg_send![view, isKindOfClass: glass_class];- if is_glass_view {- let _: () = msg_send![view, removeFromSuperview];- continue;- }+ let count: usize = msg_send![subviews, count];+ for index in (0..count).rev() {+ let view: id = msg_send![subviews, objectAtIndex: index];+ if view == nil {+ continue;+ }- let responds_to_identifier: bool =- msg_send![view, respondsToSelector: sel!(identifier)];- if !responds_to_identifier {- continue;- }+ let is_glass_view: bool = msg_send![view, isKindOfClass: glass_class];+ if is_glass_view {+ let _: () = msg_send![view, removeFromSuperview];+ continue;+ }- let view_identifier: id = msg_send![view, identifier];- if view_identifier == nil {- continue;- }+ let responds_to_identifier: bool =+ msg_send![view, respondsToSelector: sel!(identifier)];+ if !responds_to_identifier {+ continue;+ }- let matches_identifier: bool =- msg_send![view_identifier, isEqualToString: identifier];- if matches_identifier {- let _: () = msg_send![view, removeFromSuperview];- }+ let view_identifier: id = msg_send![view, identifier];+ if view_identifier == nil {+ continue;+ }++ let matches_identifier: bool = msg_send![view_identifier, isEqualToString: identifier];+ if matches_identifier {+ let _: () = msg_send![view, removeFromSuperview];}}}+}++pub fn apply_liquid_glass_background(+ window: &tauri::Window,+ enabled: bool,+ radius: f64,+) -> Result<bool, String> {+ use cocoa::{+ base::{id, nil},+ foundation::{NSRect, NSString},+ };+ use objc::{class, msg_send, runtime::Class, sel, sel_impl};unsafe {let ns_window = window@@ -138,25 +143,30 @@ pub fn apply_liquid_glass_background(}let Some(glass_class) = Class::get("NSGlassEffectView") else {+ // No NSGlassEffectView (pre-macOS-26) — the caller uses vibrancy.+ if enabled {+ crate::crash_sentinel::set_liquid_glass_outcome("unsupported");+ }return Ok(false);};let glass_class = glass_class as *const Class;- let glass_identifier = NSString::alloc(nil).init_str("so.cap.liquid-glass-background");-- remove_liquid_glass_subviews(content_view, glass_identifier, glass_class);+ let glass_identifier = NSString::alloc(nil).init_str(LIQUID_GLASS_IDENTIFIER);if !enabled {+ teardown_liquid_glass_ns(ns_window);return Ok(false);}- // Clip the content view itself to the same continuous (squircle) curve- // we apply to the NSGlassEffectView below. Without this, WebKit's CSS- // border-radius on `.cap-window-shell` and the glass view's Core- // Animation corner are rasterised by different engines and don't align- // pixel-perfectly, producing the visible inner "second border" inside- // the rounded corner. Setting cornerRadius + masksToBounds on the- // contentView's layer (plus the continuous curve) gives us one- // authoritative mask that clips both the WKWebView and the glass view.+ remove_liquid_glass_subviews(content_view, glass_identifier, glass_class);++ // Clip the content view itself to the same continuous (squircle) curve we apply+ // to the NSGlassEffectView below. Without this, WebKit's CSS border-radius on+ // `.cap-window-shell` and the window's Core Animation corner are rasterised by+ // different engines and don't align pixel-perfectly, producing the visible inner+ // "second border" inside the rounded corner. This is plain Core Animation (no+ // private SPI / no WindowServer risk), so we apply it up front — it must clip+ // BOTH the glass view and the vibrancy fallback, otherwise the artifact returns+ // on the fallback path (most visibly on the larger-radius settings window).let _: () = msg_send![content_view, setWantsLayer: true];let content_layer: id = msg_send![content_view, layer];if content_layer != nil {@@ -166,29 +176,12 @@ pub fn apply_liquid_glass_background(let _: () = msg_send![content_layer, setCornerCurve: continuous];}- remove_tagged_subview(content_view, TAURI_VIBRANCY_VIEW_TAG);-- let clear_color: id = msg_send![class!(NSColor), clearColor];- let _: () = msg_send![ns_window, setOpaque: false];- let _: () = msg_send![ns_window, setBackgroundColor: clear_color];-- // Stop AppKit from marking the window "occluded" when another app becomes- // frontmost. The OS treats a window-without-key as occluded for power-saving- // purposes and freezes the contentView's rendering loop — which means the- // NSGlassEffectView's backdrop sampling pauses on the last frame, so the- // glass shows whatever happened to be behind us at the moment we lost focus- // (Safari, etc.) instead of reflecting the live backdrop change. There's no- // public API for this; probe the private setters used across NSWindow and- // NSPanel SPI variants, plus the same SPI on the embedded WKWebView since- // its pause is what actually freezes the contentView's render loop.- disable_window_occlusion_detection(ns_window);- disable_webview_occlusion_detection(content_view);-let bounds: NSRect = msg_send![content_view, bounds];let glass_view: id = msg_send![glass_class, alloc];let glass_view: id = msg_send![glass_view, initWithFrame: bounds];if glass_view == nil {+ crate::crash_sentinel::set_liquid_glass_outcome("fallback");return Ok(false);}@@ -212,7 +205,44 @@ pub fn apply_liquid_glass_background(// Liquid Glass. NSGlassEffectView is private SPI introduced in macOS 26, so we// probe multiple state knobs (`setState:`, `setActive:`) instead of assuming a// single inheritance path.- force_glass_view_always_active(glass_view);+ //+ // macOS 26.3 shipped an NSGlassEffectView that responds to neither selector,+ // so the pin silently fails. We MUST NOT proceed to disable occlusion+ // detection in that state: an occlusion-suppressed window whose private glass+ // view is not pinned active leaves WindowServer unable to ever quiesce the+ // surface, which wedges the compositor when the window is hidden/closed and+ // takes down the whole login session. When we can't pin the material, abandon+ // the private SPI entirely and let the caller fall back to NSVisualEffectView+ // vibrancy (Ok(false)).+ if !force_glass_view_always_active(glass_view) {+ // Never entered the view hierarchy; balance the alloc and bail. The+ // content-layer squircle clip applied above is kept (plain Core Animation,+ // not a WindowServer/occlusion mutation) so the vibrancy fallback still gets+ // the aligned rounded corner.+ let _: () = msg_send![glass_view, release];+ crate::crash_sentinel::set_liquid_glass_outcome("fallback");+ return Ok(false);+ }++ remove_tagged_subview(content_view, TAURI_VIBRANCY_VIEW_TAG);++ let clear_color: id = msg_send![class!(NSColor), clearColor];+ let _: () = msg_send![ns_window, setOpaque: false];+ let _: () = msg_send![ns_window, setBackgroundColor: clear_color];++ // Stop AppKit from marking the window "occluded" when another app becomes+ // frontmost. The OS treats a window-without-key as occluded for power-saving+ // purposes and freezes the contentView's rendering loop — which means the+ // NSGlassEffectView's backdrop sampling pauses on the last frame, so the+ // glass shows whatever happened to be behind us at the moment we lost focus+ // (Safari, etc.) instead of reflecting the live backdrop change. There's no+ // public API for this; probe the private setters used across NSWindow and+ // NSPanel SPI variants, plus the same SPI on the embedded WKWebView since+ // its pause is what actually freezes the contentView's render loop. Only+ // reached once the glass view pinned active (see above); reversed by+ // teardown_liquid_glass_ns before the window is hidden or the process exits.+ disable_window_occlusion_detection(ns_window);+ disable_webview_occlusion_detection(content_view);let _: () = msg_send![glass_view, setAutoresizingMask: 18usize];let _: () = msg_send![@@ -227,6 +257,7 @@ pub fn apply_liquid_glass_background(// is what actually sticks.force_glass_view_always_active(glass_view);+ crate::crash_sentinel::set_liquid_glass_outcome("applied");Ok(true)}}@@ -369,7 +400,12 @@ unsafe fn disable_webview_occlusion_detection(content_view: cocoa::base::id) {}}-unsafe fn force_glass_view_always_active(glass_view: cocoa::base::id) {+/// Pin the glass material to its always-active representation. Returns whether the+/// view actually responded to a known pinning selector. A `false` return means this+/// macOS build's NSGlassEffectView SPI differs from what we understand (notably 26.3,+/// where it responds to neither `setState:` nor `setActive:`) and the material must+/// NOT be relied upon — see `apply_liquid_glass_background`.+unsafe fn force_glass_view_always_active(glass_view: cocoa::base::id) -> bool {use cocoa::base::id;use objc::{msg_send, sel, sel_impl};@@ -393,8 +429,9 @@ unsafe fn force_glass_view_always_active(glass_view: cocoa::base::id) {tracing::warn!(target: "cap_desktop_lib::liquid_glass","NSGlassEffectView responds to neither setState: nor setActive: — \- cannot pin material to always-active"+ cannot pin material to always-active; falling back to vibrancy");+ return false;}// If the glass view internally wraps an NSVisualEffectView (rather than@@ -416,9 +453,180 @@ unsafe fn force_glass_view_always_active(glass_view: cocoa::base::id) {}}}++ true+ }+}++unsafe fn enable_window_occlusion_detection(ns_window: cocoa::base::id) {+ use objc::{msg_send, sel, sel_impl};++ // Mirror disable_window_occlusion_detection: re-enable via whichever private SPI+ // variant this AppKit build responds to, so the OS can quiesce the surface again.+ unsafe {+ let responds: bool = msg_send![+ ns_window,+ respondsToSelector: sel!(_setWindowOcclusionDetectionEnabled:)+ ];+ if responds {+ let _: () = msg_send![ns_window, _setWindowOcclusionDetectionEnabled: true];+ return;+ }++ let responds: bool = msg_send![+ ns_window,+ respondsToSelector: sel!(setWindowOcclusionDetectionEnabled:)+ ];+ if responds {+ let _: () = msg_send![ns_window, setWindowOcclusionDetectionEnabled: true];+ return;+ }++ let responds: bool = msg_send![+ ns_window,+ respondsToSelector: sel!(_setOcclusionDetectionEnabled:)+ ];+ if responds {+ let _: () = msg_send![ns_window, _setOcclusionDetectionEnabled: true];+ return;+ }++ let responds: bool = msg_send![+ ns_window,+ respondsToSelector: sel!(setOcclusionDetectionEnabled:)+ ];+ if responds {+ let _: () = msg_send![ns_window, setOcclusionDetectionEnabled: true];+ }+ }+}++unsafe fn enable_webview_occlusion_detection(content_view: cocoa::base::id) {+ use cocoa::base::id;+ use objc::{msg_send, runtime::Class, sel, sel_impl};++ // Re-enumerate the live WKWebView subview (its identity/index can change between+ // apply and teardown) and restore occlusion detection, mirroring the disable path.+ unsafe {+ let Some(wkwebview_class) = Class::get("WKWebView") else {+ return;+ };+ let wkwebview_class = wkwebview_class as *const Class;++ let subviews: id = msg_send![content_view, subviews];+ if subviews == cocoa::base::nil {+ return;+ }++ let count: usize = msg_send![subviews, count];+ for index in 0..count {+ let subview: id = msg_send![subviews, objectAtIndex: index];+ if subview == cocoa::base::nil {+ continue;+ }++ let is_webview: bool = msg_send![subview, isKindOfClass: wkwebview_class];+ if !is_webview {+ continue;+ }++ let responds: bool = msg_send![+ subview,+ respondsToSelector: sel!(_setWebViewWindowOcclusionDetectionEnabled:)+ ];+ if responds {+ let _: () = msg_send![subview, _setWebViewWindowOcclusionDetectionEnabled: true];+ return;+ }++ let responds: bool = msg_send![+ subview,+ respondsToSelector: sel!(_setWindowOcclusionDetectionEnabled:)+ ];+ if responds {+ let _: () = msg_send![subview, _setWindowOcclusionDetectionEnabled: true];+ return;+ }++ return;+ }+ }+}++/// Reverse every WindowServer-visible mutation `apply_liquid_glass_background` makes:+/// remove the private NSGlassEffectView (the load-bearing step — it synchronously+/// detaches the private compositor relationship), then restore window/WKWebView+/// occlusion detection and window opacity. MUST run on the AppKit main thread.+unsafe fn teardown_liquid_glass_ns(ns_window: cocoa::base::id) {+ use cocoa::{+ base::{id, nil},+ foundation::NSString,+ };+ use objc::{msg_send, runtime::Class, sel, sel_impl};++ unsafe {+ let content_view: id = msg_send![ns_window, contentView];+ if content_view == nil {+ return;+ }++ if let Some(glass_class) = Class::get("NSGlassEffectView") {+ let glass_identifier = NSString::alloc(nil).init_str(LIQUID_GLASS_IDENTIFIER);+ remove_liquid_glass_subviews(+ content_view,+ glass_identifier,+ glass_class as *const Class,+ );+ }++ enable_window_occlusion_detection(ns_window);+ enable_webview_occlusion_detection(content_view);+ let _: () = msg_send![ns_window, setOpaque: true];}}+/// Tear down liquid glass on every webview window. MUST be called on the AppKit main+/// thread (e.g. from the `RunEvent::Exit` handler, which runs on main). Returns the+/// number of windows processed.+pub fn teardown_all_liquid_glass_on_main(app: &tauri::AppHandle) -> usize {+ use tauri::Manager;++ let mut count = 0usize;+ for (_label, window) in app.webview_windows() {+ if let Ok(ns_window) = window.ns_window() {+ unsafe {+ teardown_liquid_glass_ns(ns_window as cocoa::base::id);+ }+ count += 1;+ }+ }+ count+}++/// Dispatch liquid-glass teardown onto the main thread and await it. Safe to call+/// from a tokio worker during exit: the AppKit run loop is still pumping because the+/// macOS terminate handler returned NSTerminateCancel before spawning the async exit.+pub async fn teardown_all_liquid_glass(app: &tauri::AppHandle) -> Result<(), String> {+ let app_for_closure = app.clone();+ let (tx, rx) = tokio::sync::oneshot::channel();++ app.run_on_main_thread(move || {+ let count = teardown_all_liquid_glass_on_main(&app_for_closure);+ let _ = tx.send(count);+ })+ .map_err(|error| error.to_string())?;++ let count = rx+ .await+ .map_err(|_| "liquid glass teardown task was cancelled".to_string())?;+ tracing::info!(+ target: "cap_desktop_lib::liquid_glass",+ windows = count,+ "Tore down liquid glass before exit"+ );+ Ok(())+}+// pub fn get_ns_window_number(ns_window: *mut c_void) -> isize {// let ns_window = ns_window as *const objc2_app_kit::NSWindow;