1+ import Cocoa
12import Combine
23import Defaults
34import Lowtech
@@ -91,6 +92,7 @@ private let appHangStateQueue = DispatchQueue(label: "com.lowtechguys.Clop.appHa
9192@MainActor private var appHangTimer : DispatchSourceTimer ?
9293private var lastMainThreadCheckin : TimeInterval = 0
9394private var appHangTriggered = false
95+ private var sleeping = false
9496
9597@MainActor func configureAppHangDetection( ) {
9698 guard appHangTimer == nil else {
@@ -102,7 +104,7 @@ private var appHangTriggered = false
102104 appHangTriggered = false
103105 }
104106
105- let timer = DispatchSource . makeTimerSource ( queue: DispatchQueue . global ( qos: . background ) )
107+ let timer = DispatchSource . makeTimerSource ( queue: DispatchQueue . global ( qos: . userInteractive ) )
106108 timer. schedule (
107109 deadline: . now( ) + APP_HANG_CHECK_INTERVAL,
108110 repeating: APP_HANG_CHECK_INTERVAL,
@@ -113,7 +115,7 @@ private var appHangTriggered = false
113115 var shouldTrigger = false
114116
115117 appHangStateQueue. sync {
116- if !appHangTriggered, now - lastMainThreadCheckin > APP_HANG_DETECTION_INTERVAL {
118+ if !appHangTriggered, !sleeping , now - lastMainThreadCheckin > APP_HANG_DETECTION_INTERVAL {
117119 appHangTriggered = true
118120 shouldTrigger = true
119121 }
@@ -131,6 +133,23 @@ private var appHangTriggered = false
131133 }
132134 appHangTimer = timer
133135 timer. resume ( )
136+
137+ let nc = NSWorkspace . shared. notificationCenter
138+ nc. addObserver ( forName: NSWorkspace . willSleepNotification, object: nil , queue: nil ) { _ in
139+ appHangStateQueue. sync {
140+ lastMainThreadCheckin = Date ( ) . timeIntervalSince1970
141+ sleeping = true
142+ appHangTriggered = false
143+ }
144+ }
145+ nc. addObserver ( forName: NSWorkspace . didWakeNotification, object: nil , queue: nil ) { _ in
146+ let now = Date ( ) . timeIntervalSince1970
147+ appHangStateQueue. sync {
148+ lastMainThreadCheckin = now
149+ sleeping = false
150+ appHangTriggered = false
151+ }
152+ }
134153}
135154
136155private func mainThreadStack( from sampleOutput: String ) -> String {
0 commit comments