Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 14 additions & 22 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1310,16 +1310,12 @@ private final class WindowTmuxWorkspacePaneOverlayController: NSObject {
private let model = TmuxWorkspacePaneOverlayModel()
private let hostingView: NSHostingView<TmuxWorkspacePaneOverlayView>
private var installConstraints: [NSLayoutConstraint] = []
private var modelVisibilityCancellable: AnyCancellable?

init(window: NSWindow) {
self.window = window
self.hostingView = NSHostingView(
rootView: TmuxWorkspacePaneOverlayView(
unreadRects: [],
flashRect: nil,
flashStartedAt: nil,
flashReason: nil
)
rootView: TmuxWorkspacePaneOverlayView(model: model)
)
super.init()
containerView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -1338,6 +1334,18 @@ private final class WindowTmuxWorkspacePaneOverlayController: NSObject {
hostingView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
hostingView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
])
modelVisibilityCancellable = Publishers.CombineLatest3(
model.$unreadRects,
model.$flashRect,
model.$flashStartedAt
)
.receive(on: RunLoop.main)
.sink { [weak self] unreadRects, flashRect, flashStartedAt in
guard let self else { return }
let hasVisibleContent = !unreadRects.isEmpty || (flashRect != nil && flashStartedAt != nil)
self.containerView.alphaValue = hasVisibleContent ? 1 : 0
self.containerView.isHidden = !hasVisibleContent
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Multiple intermediate CombineLatest3 emissions from sequential @Published mutations

apply() mutates unreadRects, flashRect, flashReason, and flashStartedAt in sequence. Each assignment fires a separate @Published event, so CombineLatest3 emits up to four times before settling on the final state. With receive(on: RunLoop.main), each event is queued as a separate RunLoop callback and the sink runs once per event — meaning containerView.isHidden and containerView.alphaValue can be toggled multiple times within the same logical update.

This is visually harmless today (AppKit defers compositing), but it produces unnecessary AppKit property churn on every overlay update. Coalescing the sink with .removeDuplicates() would eliminate the intermediate states without changing any externally observable behaviour.

_ = ensureInstalled()
}

Expand Down Expand Up @@ -1368,24 +1376,8 @@ private final class WindowTmuxWorkspacePaneOverlayController: NSObject {
guard ensureInstalled() else { return }
if let state {
model.apply(state)
hostingView.rootView = TmuxWorkspacePaneOverlayView(
unreadRects: model.unreadRects,
flashRect: model.flashRect,
flashStartedAt: model.flashStartedAt,
flashReason: model.flashReason
)
containerView.alphaValue = 1
containerView.isHidden = false
} else {
model.clear()
hostingView.rootView = TmuxWorkspacePaneOverlayView(
unreadRects: [],
flashRect: nil,
flashStartedAt: nil,
flashReason: nil
)
containerView.alphaValue = 0
containerView.isHidden = true
}
}
}
Expand Down
47 changes: 43 additions & 4 deletions Sources/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,11 @@ class TabManager: ObservableObject {
/// Static so port ranges don't overlap across multiple windows (each window has its own TabManager).
private static var nextPortOrdinal: Int = 0
private static let initialWorkspaceGitProbeDelays: [TimeInterval] = [0, 0.5, 1.5, 3.0, 6.0, 10.0]
private static let workspaceGitMetadataPollInterval: TimeInterval = 30
private static let selectedWorkspaceGitMetadataPollInterval: TimeInterval = 5
private nonisolated static let workspacePullRequestProbeTimeout: TimeInterval = 5.0
// Background PR metadata is best-effort. Keep it infrequent so idle windows
// don't repeatedly spawn `gh` when nothing is changing.
private static let workspaceGitMetadataPollInterval: TimeInterval = 5 * 60
private static let selectedWorkspaceGitMetadataPollInterval: TimeInterval = 2 * 60
private nonisolated static let workspacePullRequestProbeTimeout: TimeInterval = 2.0
@Published var selectedTabId: UUID? {
willSet {
#if DEBUG
Expand Down Expand Up @@ -761,6 +763,7 @@ class TabManager: ObservableObject {
if let selectedTabId = self.selectedTabId {
self.dismissFocusedPanelNotificationIfActive(tabId: selectedTabId)
}
self.scheduleSelectedWorkspaceGitMetadataRefresh(reason: "selectionChange")
#if DEBUG
let dtMs = self.debugWorkspaceSwitchStartTime > 0
? (CACurrentMediaTime() - self.debugWorkspaceSwitchStartTime) * 1000
Expand Down Expand Up @@ -944,7 +947,7 @@ class TabManager: ObservableObject {
let activeProbeKeys = Set(workspaceGitProbeGenerationByKey.keys)

for workspace in tabs {
for panelId in trackedWorkspaceGitMetadataPollCandidatePanelIds(
for panelId in trackedWorkspaceGitMetadataPeriodicPollCandidatePanelIds(
in: workspace,
activeProbeKeys: activeProbeKeys
) {
Expand Down Expand Up @@ -977,6 +980,19 @@ class TabManager: ObservableObject {
)
}

private func scheduleSelectedWorkspaceGitMetadataRefresh(reason: String) {
guard let workspace = selectedWorkspace,
let focusedPanelId = workspace.focusedPanelId else {
return
}

scheduleWorkspaceGitMetadataRefreshIfPossible(
workspaceId: workspace.id,
panelId: focusedPanelId,
reason: reason
)
}

func refreshTrackedWorkspaceGitMetadataForTesting() {
refreshTrackedWorkspaceGitMetadata()
}
Expand Down Expand Up @@ -1011,6 +1027,29 @@ class TabManager: ObservableObject {
})
}

private func trackedWorkspaceGitMetadataPeriodicPollCandidatePanelIds(
in workspace: Workspace,
activeProbeKeys: Set<WorkspaceGitProbeKey>
) -> Set<UUID> {
var candidatePanelIds = Set(
workspace.panelPullRequests.compactMap { panelId, pullRequest in
pullRequest.status == .open ? panelId : nil
}
)

if candidatePanelIds.isEmpty,
let focusedPanelId = workspace.focusedPanelId,
let pullRequest = workspace.pullRequest,
pullRequest.status == .open {
candidatePanelIds.insert(focusedPanelId)
}

return Set(candidatePanelIds.filter { panelId in
let probeKey = WorkspaceGitProbeKey(workspaceId: workspace.id, panelId: panelId)
return !activeProbeKeys.contains(probeKey)
})
}

private func sweepStaleAgentPIDs() {
for tab in tabs {
var keysToRemove: [String] = []
Expand Down
102 changes: 81 additions & 21 deletions Sources/WorkspaceContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ final class TmuxWorkspacePaneOverlayModel: ObservableObject {

private var lastWorkspaceId: UUID?
private var lastFlashToken: UInt64?
private var flashResetWorkItem: DispatchWorkItem?

var showsAnimatedFlash: Bool {
flashRect != nil && flashStartedAt != nil
}

var hasVisibleContent: Bool {
!unreadRects.isEmpty || showsAnimatedFlash
}

func apply(
_ state: TmuxWorkspacePaneOverlayRenderState,
Expand All @@ -123,58 +132,109 @@ final class TmuxWorkspacePaneOverlayModel: ObservableObject {
if didChangeWorkspace {
lastWorkspaceId = state.workspaceId
lastFlashToken = state.flashToken
cancelFlashReset()
flashStartedAt = nil
return
}

if state.flashRect == nil {
cancelFlashReset()
flashStartedAt = nil
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

if let lastFlashToken,
state.flashToken != lastFlashToken,
state.flashRect != nil {
flashStartedAt = now()
let startedAt = now()
flashStartedAt = startedAt
scheduleFlashReset(workspaceId: state.workspaceId, flashToken: state.flashToken)
}
self.lastFlashToken = state.flashToken
}

func clear() {
cancelFlashReset()
unreadRects = []
flashRect = nil
flashStartedAt = nil
flashReason = nil
lastWorkspaceId = nil
lastFlashToken = nil
}

private func scheduleFlashReset(workspaceId: UUID, flashToken: UInt64) {
cancelFlashReset()

let workItem = DispatchWorkItem { [weak self] in
guard let self else { return }
guard self.lastWorkspaceId == workspaceId,
self.lastFlashToken == flashToken else {
return
}

self.flashStartedAt = nil
}
flashResetWorkItem = workItem
DispatchQueue.main.asyncAfter(
deadline: .now() + FocusFlashPattern.duration,
execute: workItem
)
Comment on lines +195 to +212

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 @MainActor-isolated property access in @Sendable closure

DispatchWorkItem takes a @Sendable closure, so Swift Concurrency's type checker sees access to self.lastWorkspaceId, self.lastFlashToken, and self.flashStartedAt (all @MainActor-isolated) as potential data races. At runtime this is safe because the work item is dispatched to DispatchQueue.main, but the compiler cannot verify the equivalence between DispatchQueue.main and @MainActor in a @Sendable closure without an explicit annotation.

Wrapping the body with MainActor.assumeIsolated documents the intent and avoids warnings under -strict-concurrency=complete:

Suggested change
let workItem = DispatchWorkItem { [weak self] in
guard let self else { return }
guard self.lastWorkspaceId == workspaceId,
self.lastFlashToken == flashToken else {
return
}
self.flashStartedAt = nil
}
flashResetWorkItem = workItem
DispatchQueue.main.asyncAfter(
deadline: .now() + FocusFlashPattern.duration,
execute: workItem
)
let workItem = DispatchWorkItem { [weak self] in
MainActor.assumeIsolated {
guard let self else { return }
guard self.lastWorkspaceId == workspaceId,
self.lastFlashToken == flashToken else {
return
}
self.flashStartedAt = nil
}
}

}

private func cancelFlashReset() {
flashResetWorkItem?.cancel()
flashResetWorkItem = nil
}
}

struct TmuxWorkspacePaneOverlayView: View {
let unreadRects: [CGRect]
let flashRect: CGRect?
let flashStartedAt: Date?
let flashReason: WorkspaceAttentionFlashReason?
@ObservedObject var model: TmuxWorkspacePaneOverlayModel

var body: some View {
TimelineView(.animation) { timeline in
Canvas { context, _ in
for rect in unreadRects {
drawUnreadRing(in: &context, rect: rect)
Group {
if model.showsAnimatedFlash {
TimelineView(.animation) { timeline in
Canvas { context, _ in
renderOverlay(
in: &context,
currentDate: timeline.date
)
}
}

guard let flashRect,
let flashStartedAt else { return }
let elapsed = timeline.date.timeIntervalSince(flashStartedAt)
let opacity = FocusFlashPattern.opacity(at: elapsed)
guard opacity > 0.001 else { return }
drawFlashRing(
in: &context,
rect: flashRect,
opacity: opacity,
reason: flashReason ?? .notificationArrival
)
} else if !model.unreadRects.isEmpty {
Canvas { context, _ in
renderOverlay(in: &context, currentDate: nil)
}
} else {
Color.clear
}
}
.allowsHitTesting(false)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}

private func renderOverlay(
in context: inout GraphicsContext,
currentDate: Date?
) {
for rect in model.unreadRects {
drawUnreadRing(in: &context, rect: rect)
}

guard let flashRect = model.flashRect,
let flashStartedAt = model.flashStartedAt,
let currentDate else { return }
let elapsed = currentDate.timeIntervalSince(flashStartedAt)
let opacity = FocusFlashPattern.opacity(at: elapsed)
guard opacity > 0.001 else { return }
drawFlashRing(
in: &context,
rect: flashRect,
opacity: opacity,
reason: model.flashReason ?? .notificationArrival
)
}

private func drawUnreadRing(in context: inout GraphicsContext, rect: CGRect) {
guard let path = ringPath(for: rect) else { return }
var glowContext = context
Expand Down
Loading