Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Frames a hidden window never asked for are not counted as missed

Țugui Dragoșffc460b83d91
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Hitches.swift b/Packages/PeelCore/Sources/PeelCore/Support/Hitches.swift
index 70df886b..55107eb2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Hitches.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Hitches.swift
@@ -43,6 +43,12 @@ public struct Hitches: Sendable {
shortestPeriod = min(shortestPeriod ?? period, period)
}
+ /// Marks a stretch the display asked for no frames, as for a window out of sight: the next frame starts afresh
+ /// instead of counting the wait as frames missed.
+ public mutating func pause() {
+ lastFrame = nil
+ }
+
public var report: [String] {
let runLoop = String(format: "run loop: %ld turns, longest %.1f ms, %ld over one frame",
turns, longestTurn * 1000, turnsOverAFrame)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HitchesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HitchesTests.swift
index 1491261e..0416ee3f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HitchesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HitchesTests.swift
@@ -54,6 +54,18 @@ struct HitchesTests {
#expect(hitches.report.last == "frames: 3 callbacks, period 8.33 ms, worst gap 16.7 ms, 0 missed")
}
+ /// A window that cannot be seen gets no frames, and the wait is not frames missed: after a pause the next
+ /// frame starts afresh. A gap nothing paused, such as a busy main thread, still counts.
+ @Test func aWindowOutOfSightMissesNothing() {
+ var hitches = Hitches()
+ hitches.frame(at: 0, due: 0.010)
+ hitches.pause()
+ hitches.frame(at: 60, due: 60.010)
+ hitches.frame(at: 60.040, due: 60.050)
+
+ #expect(hitches.report.last == "frames: 3 callbacks, period 10.00 ms, worst gap 40.0 ms, 3 missed")
+ }
+
@Test func aWindowTheDisplayNeverAskedForSaysSo() {
#expect(Hitches().report == ["run loop: 0 turns, longest 0.0 ms, 0 over one frame", "frames: 0 callbacks"])
}
diff --git a/Peel/FrameWatch.swift b/Peel/FrameWatch.swift
index 82e67056..6a42d035 100644
--- a/Peel/FrameWatch.swift
+++ b/Peel/FrameWatch.swift
@@ -18,6 +18,7 @@ final class FrameWatch: NSObject {
private var link: CADisplayLink?
private var observer: CFRunLoopObserver?
private var request: (any DispatchSourceSignal)?
+ private var sight: (any NSObjectProtocol)?
private var hasDrawn = false
private init(file: URL) {
@@ -30,6 +31,13 @@ final class FrameWatch: NSObject {
let link = view.displayLink(target: self, selector: #selector(frame(_:)))
link.add(to: .main, forMode: .common)
self.link = link
+ // The display asks no frames for a window out of sight (minimized, hidden, or covered), which is no hitch.
+ sight = NotificationCenter.default.addObserver(
+ forName: NSWindow.didChangeOcclusionStateNotification, object: view.window, queue: .main
+ ) { [weak self] note in
+ guard let window = note.object as? NSWindow, !window.occlusionState.contains(.visible) else { return }
+ MainActor.assumeIsolated { self?.hitches.pause() }
+ }
guard observer == nil else { return }
watchTheMainThread()
reportWhenAsked()
@@ -38,6 +46,8 @@ final class FrameWatch: NSObject {
fileprivate func stop() {
link?.invalidate()
link = nil
+ sight.map(NotificationCenter.default.removeObserver)
+ sight = nil
}
@objc private func frame(_ link: CADisplayLink) {