Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): stop native traffic lights flashing on the macOS main window at launch (#3280)

Richie McIlroy39ed1c077ff5
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index 9fdde84f4..47e80217b 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -591,6 +591,14 @@ fn main() {
})
.expect("failed to start device enumeration");
+ // Before the launch thumbnail sweep can reach ReplayKit, which would
+ // otherwise add the standard titlebar buttons next to the hand-drawn
+ // close light (`platform::lock_borderless_style`).
+ #[cfg(target_os = "macos")]
+ if let Some(native) = &native_main {
+ platform::lock_borderless_style(native);
+ }
+
// The native window material: `NSGlassEffectView` on macOS 26+,
// `NSVisualEffectView` vibrancy below that -- what
// `applyMacOSWindowMaterial("panel")` does in the Tauri app. Nothing
diff --git a/apps/desktop-gpui/src/platform.rs b/apps/desktop-gpui/src/platform.rs
index 70368f3bf..cfc9b6e6d 100644
--- a/apps/desktop-gpui/src/platform.rs
+++ b/apps/desktop-gpui/src/platform.rs
@@ -1106,22 +1106,106 @@ mod mac {
/// `titlebar: None`). During the recording flow -- after the hidden main
/// window's `orderOut:` and the in-process ScreenCaptureKit start -- macOS
/// 26 adds `NSMiniaturizableWindowMask` to the hidden window on its own
- /// (no app or gpui code writes the mask; verified with a breakpoint on
- /// `setStyleMask:` and a mask poll), which materializes all three standard
- /// titlebar buttons on top of the hand-drawn lights. Setting the mask back
- /// tears the buttons down. Same borrow rule as [`show_native`]: call from
- /// a task, never inside a gpui update.
+ /// (no app or gpui code writes the mask), which materializes all three
+ /// standard titlebar buttons on top of the hand-drawn lights. Setting the
+ /// mask back tears the buttons down. [`lock_borderless_style`] refuses
+ /// the writes that go through `setStyleMask:`; this stays as the backstop
+ /// for any that don't. Same borrow rule as [`show_native`]: call from a
+ /// task, never inside a gpui update.
pub fn restore_borderless_style(native: &NativeWindow) {
use objc2::msg_send;
- const TITLED: usize = 1 << 0;
- const FULL_SIZE_CONTENT_VIEW: usize = 1 << 15;
- let want = TITLED | FULL_SIZE_CONTENT_VIEW;
unsafe {
let mask: usize = msg_send![&*native.0, styleMask];
- if mask != want {
+ if mask != BORDERLESS_STYLE_MASK {
tracing::info!(mask, "clearing foreign style-mask bits on the main window");
- let _: () = msg_send![&*native.0, setStyleMask: want];
+ let _: () = msg_send![&*native.0, setStyleMask: BORDERLESS_STYLE_MASK];
+ }
+ }
+ }
+
+ /// `NSTitledWindowMask | NSFullSizeContentViewWindowMask`: what gpui's
+ /// `titlebar: None` creates the main window with.
+ const BORDERLESS_STYLE_MASK: usize = (1 << 0) | (1 << 15);
+
+ static STYLE_MASK_LOCK_KEY: u8 = 0;
+ static SET_STYLE_MASK_SUPER: std::sync::OnceLock<(usize, usize)> = std::sync::OnceLock::new();
+
+ /// Pin the main window to its buttonless style mask by refusing every
+ /// later `setStyleMask:` that asks for something else.
+ ///
+ /// The thumbnail sweep's in-process ScreenCaptureKit capture at launch
+ /// makes ReplayKit run `-[RPScreenRecorder setWindowRotationLocked:]` on
+ /// the main queue, and that calls `setStyleMask:` on every app window to
+ /// add `NSResizableWindowMask` (`0x8001` -> `0x8009`, caught with a
+ /// `setStyleMask:` override logging `callStackSymbols`). AppKit then
+ /// materializes all three standard titlebar buttons next to the
+ /// hand-drawn close light until [`restore_borderless_style`] runs at the
+ /// end of the sweep. Rejecting the write here means the buttons are never
+ /// built.
+ ///
+ /// The override goes on the window's class, so it sees every
+ /// `GPUIPanel`; windows without the lock pass straight through to the
+ /// inherited implementation.
+ pub fn lock_borderless_style(native: &NativeWindow) {
+ use objc2::{ffi, msg_send, runtime::AnyClass};
+
+ unsafe extern "C" fn guarded_set_style_mask(this: *mut AnyObject, sel: Sel, mask: usize) {
+ let Some(&(_, imp)) = SET_STYLE_MASK_SUPER.get() else {
+ return;
+ };
+ let locked = unsafe {
+ ffi::objc_getAssociatedObject(this.cast(), (&raw const STYLE_MASK_LOCK_KEY).cast())
+ };
+ let mask = if locked.is_null() || mask == BORDERLESS_STYLE_MASK {
+ mask
+ } else {
+ tracing::info!(mask, "kept the main window's borderless style mask");
+ BORDERLESS_STYLE_MASK
+ };
+ let imp: unsafe extern "C" fn(*mut AnyObject, Sel, usize) =
+ unsafe { std::mem::transmute(imp) };
+ unsafe { imp(this, sel, mask) };
+ }
+
+ unsafe {
+ let class: *const AnyClass = msg_send![&*native.0, class];
+ let sel = objc2::sel!(setStyleMask:);
+ let &(installed_on, _) = SET_STYLE_MASK_SUPER.get_or_init(|| {
+ let superclass = ffi::class_getSuperclass(class.cast());
+ let Some(imp) = ffi::method_getImplementation(ffi::class_getInstanceMethod(
+ superclass,
+ sel.as_ptr(),
+ )) else {
+ tracing::warn!(
+ "no inherited setStyleMask: for the main window style-mask lock"
+ );
+ return (0, 0);
+ };
+ let added = ffi::class_addMethod(
+ class.cast_mut().cast(),
+ sel.as_ptr(),
+ Some(std::mem::transmute::<
+ unsafe extern "C" fn(*mut AnyObject, Sel, usize),
+ unsafe extern "C" fn(),
+ >(guarded_set_style_mask)),
+ c"v@:Q".as_ptr(),
+ );
+ if !objc2::runtime::Bool::from_raw(added).as_bool() {
+ tracing::warn!("could not install the main window style-mask lock");
+ return (0, 0);
+ }
+ (class as usize, imp as usize)
+ });
+ if installed_on != class as usize {
+ return;
}
+ let marker: *mut AnyObject = msg_send![objc2::class!(NSNull), null];
+ ffi::objc_setAssociatedObject(
+ (&*native.0 as *const NSWindow).cast_mut().cast(),
+ (&raw const STYLE_MASK_LOCK_KEY).cast(),
+ marker.cast(),
+ ffi::OBJC_ASSOCIATION_RETAIN_NONATOMIC,
+ );
}
}