native macOS codings agent orchestrator prowl.onev.cat
Something went wrong. Try again.
1.5 kB · 56 lines
Swift
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import SwiftUI
struct NotificationPopoverButton<Label: View>: View { let notifications: [WorktreeTerminalNotification] let onFocusNotification: (WorktreeTerminalNotification) -> Void @ViewBuilder let label: () -> Label @State private var isPresented = false @State private var isHoveringButton = false @State private var isHoveringPopover = false @State private var closeTask: Task<Void, Never>?
var body: some View { Button { isPresented.toggle() } label: { label() } .buttonStyle(.plain) .contentShape(.rect) .help("Unread notifications. Hover to show.") .accessibilityLabel("Unread notifications") .onHover { hovering in isHoveringButton = hovering updatePresentation() } .popover(isPresented: $isPresented) { NotificationPopoverView(notifications: notifications, onFocusNotification: onFocusNotification) .onHover { hovering in isHoveringPopover = hovering updatePresentation() } .onDisappear { isHoveringPopover = false updatePresentation() } } .onDisappear { closeTask?.cancel() } }
private func updatePresentation() { if isHoveringButton || isHoveringPopover { closeTask?.cancel() isPresented = true return } closeTask?.cancel() closeTask = Task { @MainActor in try? await ContinuousClock().sleep(for: .milliseconds(150)) if !Task.isCancelled { isPresented = false } } }}