From b435e57eb9b603431f99805407af9da0c54c4fd3 Mon Sep 17 00:00:00 2001 From: onevcat Date: Wed, 2 Dec 2026 00:42:14 +0900 Subject: [PATCH] Animate the indeterminate progress bar with Core Animation The SwiftUI phase animator updated the view graph on the main thread for each display frame while an agent worked. Core Animation moves the strip in the render server, and a 30 fps limit reduces the WindowServer cost. --- .../GhosttyIndeterminateProgressBar.swift | 132 ++++++++++++++++++ .../Views/GhosttySurfaceProgressBar.swift | 16 +-- ...GhosttyIndeterminateProgressBarTests.swift | 61 ++++++++ 3 files changed, 195 insertions(+), 14 deletions(-) create mode 100644 App/Sources/Features/Terminal/Views/GhosttyIndeterminateProgressBar.swift create mode 100644 App/Tests/GhosttyIndeterminateProgressBarTests.swift diff --git a/App/Sources/Features/Terminal/Views/GhosttyIndeterminateProgressBar.swift b/App/Sources/Features/Terminal/Views/GhosttyIndeterminateProgressBar.swift new file mode 100644 index 000000000..2aca57fe3 --- /dev/null +++ b/App/Sources/Features/Terminal/Views/GhosttyIndeterminateProgressBar.swift @@ -0,0 +1,132 @@ +import AppKit +import SwiftUI + +/// The sliding strip for a progress report without a value. +/// +/// Core Animation moves the strip in the render server. A SwiftUI animation here updates the +/// view graph on the main thread for each display frame while an agent works. +struct GhosttyIndeterminateProgressBar: NSViewRepresentable { + let color: NSColor + @Environment(\.accessibilityReduceMotion) private var reduceMotion + + func makeNSView(context: Context) -> GhosttyIndeterminateProgressBarView { + let view = GhosttyIndeterminateProgressBarView() + view.update(color: color, reduceMotion: reduceMotion) + return view + } + + func updateNSView(_ nsView: GhosttyIndeterminateProgressBarView, context: Context) { + nsView.update(color: color, reduceMotion: reduceMotion) + } +} + +@MainActor +final class GhosttyIndeterminateProgressBarView: NSView { + private static let animationKey = "ghostty-indeterminate-progress-slide" + private static let segmentFraction: CGFloat = 0.25 + private static let trackAlpha: CGFloat = 0.3 + private static let sweepDuration: TimeInterval = 1.2 + /// WindowServer composites the window again for each animation frame, so its cost follows + /// the frame rate and not the size of the strip. + static let frameRateRange = CAFrameRateRange(minimum: 24, maximum: 30, preferred: 30) + + private let segmentLayer = CALayer() + private var currentColor = NSColor.controlAccentColor + private var currentReduceMotion = false + private var animationStartTime: CFTimeInterval? + private var animatedWidth: CGFloat? + + override init(frame frameRect: NSRect) { + super.init(frame: frameRect) + wantsLayer = true + layer?.masksToBounds = true + segmentLayer.anchorPoint = .zero + layer?.addSublayer(segmentLayer) + applyColors() + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + var slideAnimation: CABasicAnimation? { + segmentLayer.animation(forKey: Self.animationKey) as? CABasicAnimation + } + + var segmentFrame: CGRect { + segmentLayer.frame + } + + func update(color: NSColor, reduceMotion: Bool) { + if currentColor != color { + currentColor = color + applyColors() + } + guard currentReduceMotion != reduceMotion else { return } + currentReduceMotion = reduceMotion + updateSegment() + } + + override func layout() { + super.layout() + updateSegment() + } + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + // AppKit can drop layer animations when it moves the view between windows. + animatedWidth = nil + updateSegment() + } + + override func viewDidChangeEffectiveAppearance() { + super.viewDidChangeEffectiveAppearance() + applyColors() + } + + private func updateSegment() { + CATransaction.begin() + CATransaction.setDisableActions(true) + defer { CATransaction.commit() } + + let width = bounds.width + let segmentWidth = currentReduceMotion ? width : width * Self.segmentFraction + segmentLayer.bounds = CGRect(x: 0, y: 0, width: segmentWidth, height: bounds.height) + segmentLayer.position = .zero + + guard width > 0, !currentReduceMotion else { + segmentLayer.removeAnimation(forKey: Self.animationKey) + animationStartTime = nil + animatedWidth = nil + return + } + guard animatedWidth != width || slideAnimation == nil else { return } + animatedWidth = width + + // A fixed start time keeps the phase when a resize replaces the animation. + let startTime = animationStartTime ?? segmentLayer.convertTime(CACurrentMediaTime(), from: nil) + animationStartTime = startTime + let animation = CABasicAnimation(keyPath: "position.x") + animation.fromValue = 0 + animation.toValue = width - segmentWidth + animation.beginTime = startTime + animation.duration = Self.sweepDuration + animation.autoreverses = true + animation.repeatCount = .infinity + animation.isRemovedOnCompletion = false + animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) + animation.preferredFrameRateRange = Self.frameRateRange + segmentLayer.add(animation, forKey: Self.animationKey) + } + + private func applyColors() { + CATransaction.begin() + CATransaction.setDisableActions(true) + effectiveAppearance.performAsCurrentDrawingAppearance { + layer?.backgroundColor = currentColor.withAlphaComponent(Self.trackAlpha).cgColor + segmentLayer.backgroundColor = currentColor.cgColor + } + CATransaction.commit() + } +} diff --git a/App/Sources/Features/Terminal/Views/GhosttySurfaceProgressBar.swift b/App/Sources/Features/Terminal/Views/GhosttySurfaceProgressBar.swift index 8c0c15814..6dcad41b1 100644 --- a/App/Sources/Features/Terminal/Views/GhosttySurfaceProgressBar.swift +++ b/App/Sources/Features/Terminal/Views/GhosttySurfaceProgressBar.swift @@ -1,3 +1,4 @@ +import AppKit import GhosttyKit import SwiftUI @@ -41,20 +42,7 @@ struct GhosttySurfaceProgressBar: View { .fill(color) .scaleEffect(x: CGFloat(progress) / 100, y: 1, anchor: .leading) } else { - GeometryReader { geometry in - ZStack(alignment: .leading) { - Rectangle() - .fill(color.opacity(0.3)) - Rectangle() - .fill(color) - .frame(width: geometry.size.width * 0.25, height: geometry.size.height) - .phaseAnimator([false, true]) { content, moved in - content.offset(x: moved ? geometry.size.width * 0.75 : 0) - } animation: { _ in - .easeInOut(duration: 1.2) - } - } - } + GhosttyIndeterminateProgressBar(color: NSColor(color)) } } .frame(height: 2) diff --git a/App/Tests/GhosttyIndeterminateProgressBarTests.swift b/App/Tests/GhosttyIndeterminateProgressBarTests.swift new file mode 100644 index 000000000..41dadc32c --- /dev/null +++ b/App/Tests/GhosttyIndeterminateProgressBarTests.swift @@ -0,0 +1,61 @@ +import AppKit +import Testing + +@testable import Prowl + +@MainActor +struct GhosttyIndeterminateProgressBarTests { + private func makeBar(width: CGFloat) -> GhosttyIndeterminateProgressBarView { + let bar = GhosttyIndeterminateProgressBarView(frame: CGRect(x: 0, y: 0, width: width, height: 2)) + bar.layout() + return bar + } + + @Test func segmentSlidesAcrossTheRemainingWidth() throws { + let bar = makeBar(width: 400) + + let animation = try #require(bar.slideAnimation) + #expect(bar.segmentFrame == CGRect(x: 0, y: 0, width: 100, height: 2)) + #expect(animation.fromValue as? CGFloat == 0) + #expect(animation.toValue as? CGFloat == 300) + #expect(animation.repeatCount == .infinity) + #expect(animation.preferredFrameRateRange == GhosttyIndeterminateProgressBarView.frameRateRange) + } + + @Test func resizeUpdatesTheTravelAndKeepsThePhase() throws { + let bar = makeBar(width: 400) + let start = try #require(bar.slideAnimation).beginTime + + bar.setFrameSize(CGSize(width: 800, height: 2)) + bar.layout() + + let animation = try #require(bar.slideAnimation) + #expect(animation.toValue as? CGFloat == 600) + #expect(animation.beginTime == start) + } + + @Test func layoutWithTheSameWidthKeepsTheAnimation() throws { + let bar = makeBar(width: 400) + let animation = try #require(bar.slideAnimation) + + bar.layout() + + #expect(bar.slideAnimation === animation) + } + + @Test func reduceMotionShowsAStaticFullWidthBar() { + let bar = makeBar(width: 400) + + bar.update(color: .controlAccentColor, reduceMotion: true) + #expect(bar.slideAnimation == nil) + #expect(bar.segmentFrame.width == 400) + + bar.update(color: .controlAccentColor, reduceMotion: false) + #expect(bar.slideAnimation != nil) + #expect(bar.segmentFrame.width == 100) + } + + @Test func emptyWidthHasNoAnimation() { + #expect(makeBar(width: 0).slideAnimation == nil) + } +}