From 2a7c279bdd81dacf6f53aa2eb742661c83c00c6f Mon Sep 17 00:00:00 2001 From: dnafication <6381587+dnafication@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:59:35 +0000 Subject: [PATCH 1/3] Initial plan From 82644072f341872bb199f3b618082e31ccfce736 Mon Sep 17 00:00:00 2001 From: dnafication <6381587+dnafication@users.noreply.github.com> Date: Mon, 23 Nov 2026 02:01:38 +0000 Subject: [PATCH 2/3] Defer display recovery during sleep transitions Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- GoDark/ContentView.swift | 44 ++++++++++++++++++++++++++++++++++++++++ docs/DEVELOPMENT.md | 6 ++++++ 2 files changed, 50 insertions(+) diff --git a/GoDark/ContentView.swift b/GoDark/ContentView.swift index 9aa3b33..049c08b 100644 --- a/GoDark/ContentView.swift +++ b/GoDark/ContentView.swift @@ -94,6 +94,20 @@ final class DisplayManager { errorMessage = DisplayError.coreGraphics(callbackResult).localizedDescription } + let workspaceNotifications = NSWorkspace.shared.notificationCenter + for (name, reason, isAsleep) in [ + (NSWorkspace.willSleepNotification, SleepReason.system, true), + (NSWorkspace.didWakeNotification, SleepReason.system, false), + (NSWorkspace.screensDidSleepNotification, SleepReason.screens, true), + (NSWorkspace.screensDidWakeNotification, SleepReason.screens, false) + ] { + workspaceNotifications.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in + MainActor.assumeIsolated { + self?.sleepChanged(reason, isAsleep: isAsleep) + } + } + } + Task { [weak self] in // Fires on hot-plug and after our own reconfigurations. for await _ in NotificationCenter.default.notifications( @@ -140,6 +154,35 @@ final class DisplayManager { /// Keeps App Nap from throttling the watchdog while the window is hidden. private var watchdogActivity: NSObjectProtocol? + private enum SleepReason: Hashable { + case system, screens + } + + private var sleepingReasons: Set = [] + private var wakeTask: Task? + + private func sleepChanged(_ reason: SleepReason, isAsleep: Bool) { + wakeTask?.cancel() + wakeTask = nil + if isAsleep { + sleepingReasons.insert(reason) + } else { + sleepingReasons.remove(reason) + guard sleepingReasons.isEmpty else { return } + // WindowServer can briefly report missing displays while it rebuilds the configuration. + wakeTask = Task { [weak self] in + do { + try await Task.sleep(for: .seconds(5)) + } catch { + return + } + guard let self else { return } + wakeTask = nil + refresh() + } + } + } + func refresh() { restoreBuiltInIfStranded() @@ -183,6 +226,7 @@ final class DisplayManager { /// monitor goes away, macOS inserts an active placeholder display (seen as a 1920×1080 display /// with a new ID), so the Mac never looks screenless. private func restoreBuiltInIfStranded() { + guard sleepingReasons.isEmpty, wakeTask == nil else { return } if let id = Self.displayIDs(.all).first(where: { CGDisplayIsBuiltin($0) != 0 }) { builtInID = id } diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index a478232..b1a032a 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -75,3 +75,9 @@ defaults read com.dnafication.GoDark ## Testing There's no test target. Display behaviour can only be checked on real hardware: toggle displays, and plug and unplug monitors while one is off. + +To check sleep recovery, turn off the built-in display with an external monitor connected, let +the displays and the Mac sleep, then wake them. The built-in display should stay off without a +CoreGraphics error. Also unplug the external monitor while asleep and wake the Mac: after the +display configuration settles, the built-in display should turn back on. Check ordinary +hot-unplug with the Mac awake as well. From 9e13a85250b72751eb13d47a1584f2c78b998e2e Mon Sep 17 00:00:00 2001 From: dnafication <6381587+dnafication@users.noreply.github.com> Date: Mon, 23 Nov 2026 02:02:55 +0000 Subject: [PATCH 3/3] Confirm display loss before restoring built-in screen Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- GoDark/ContentView.swift | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/GoDark/ContentView.swift b/GoDark/ContentView.swift index 049c08b..9fc09a2 100644 --- a/GoDark/ContentView.swift +++ b/GoDark/ContentView.swift @@ -160,10 +160,13 @@ final class DisplayManager { private var sleepingReasons: Set = [] private var wakeTask: Task? + private var displayLossTask: Task? + private var displayLossConfirmed = false private func sleepChanged(_ reason: SleepReason, isAsleep: Bool) { wakeTask?.cancel() wakeTask = nil + resetDisplayLoss() if isAsleep { sleepingReasons.insert(reason) } else { @@ -235,24 +238,51 @@ final class DisplayManager { let active = Set(Self.displayIDs(.active)) guard !active.contains(builtIn) else { externalsWhileBuiltInOff = nil + resetDisplayLoss() return } - guard let externals = externalsWhileBuiltInOff else { + if externalsWhileBuiltInOff == nil { // First time we see the built-in display off: remember which monitors are in use. externalsWhileBuiltInOff = active Self.log.notice("Built-in display \(builtIn, privacy: .public) is off; watching displays \(active.sorted(), privacy: .public)") - if active.isEmpty { enableBuiltInDisplay(builtIn) } - return } + guard let externals = externalsWhileBuiltInOff else { return } let remaining = externals.intersection(active) - guard remaining.isEmpty else { return } + guard remaining.isEmpty else { + resetDisplayLoss() + return + } + + guard displayLossConfirmed else { + // Screen-sleep notification can arrive after the display reconfiguration callback. + if displayLossTask == nil { + displayLossTask = Task { [weak self] in + do { + try await Task.sleep(for: .seconds(2)) + } catch { + return + } + guard let self else { return } + displayLossTask = nil + displayLossConfirmed = true + refresh() + } + } + return + } Self.log.notice("Displays \(externals.sorted(), privacy: .public) are gone (active: \(active.sorted(), privacy: .public)); turning built-in display \(builtIn, privacy: .public) back on") enableBuiltInDisplay(builtIn) } + private func resetDisplayLoss() { + displayLossTask?.cancel() + displayLossTask = nil + displayLossConfirmed = false + } + /// Runs `refresh()` every couple of seconds while the built-in display is off. private func updateWatchdog(builtInIsOff: Bool) { if builtInIsOff, watchdogTask == nil {