Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AppKit
import GhosttyKit
import SwiftUI

Expand Down Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions App/Tests/GhosttyIndeterminateProgressBarTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading