From 3e2b4ee5a7c7642d7e3585235af7ba404eb5ebe6 Mon Sep 17 00:00:00 2001 From: Daisuke Murase Date: Thu, 7 May 2026 13:25:34 -0700 Subject: [PATCH] fix: guard against nil CGDisplayCreateUUIDFromDisplayID in detectFullscreenDisplays CGDisplayCreateUUIDFromDisplayID is imported as Unmanaged! (IUO) in Swift because the C header has no nullability annotation. It returns NULL for display ID 0 (kCGNullDirectDisplay), which the NSScreen.displayID extension can produce as a fallback during display state transitions. Calling .takeRetainedValue() on the nil IUO crashes with "Unexpectedly found nil while implicitly unwrapping an Optional value". The previous fix guarded CFUUIDCreateString instead, which was one line too late. Add guard for displayID != 0 and guard let on the IUO return value so invalid displays are skipped cleanly. --- app/Sources/BarViewModel.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Sources/BarViewModel.swift b/app/Sources/BarViewModel.swift index 717ba68..26a7a93 100644 --- a/app/Sources/BarViewModel.swift +++ b/app/Sources/BarViewModel.swift @@ -206,7 +206,9 @@ final class BarViewModel: StateChangeHandler, @unchecked Sendable { for screen in NSScreen.screens { let displayID = screen.displayID - let uuid = CGDisplayCreateUUIDFromDisplayID(displayID).takeRetainedValue() + guard displayID != 0, + let uuidUnmanaged = CGDisplayCreateUUIDFromDisplayID(displayID) else { continue } + let uuid = uuidUnmanaged.takeRetainedValue() guard let uuidString = CFUUIDCreateString(nil, uuid) else { continue } let spaceID = SLSManagedDisplayGetCurrentSpace(cid, uuidString)