cmux/Sources/Update/UpdateTitlebarAccessory.swift
Lawrence Chen 50f0dd334d
Fix frozen terminals after split churn (#12)
* Fix blank terminal after split operations and add visual tests

## Blank Terminal Fix
- Add `needsRefreshAfterWindowChange` flag in GhosttyTerminalView
- Force terminal refresh when view is added to window, even if size unchanged
- Add `ghostty_surface_refresh()` call in attachToView for same-view reattachment
- Add debug logging for surface attachment lifecycle (DEBUG builds only)

## Bonsplit Migration
- Add bonsplit as local Swift package (vendor/bonsplit submodule)
- Replace custom SplitTree with BonsplitController
- Add Panel protocol with TerminalPanel and BrowserPanel implementations
- Add SidebarTab as main tab container with BonsplitController
- Remove old Splits/ directory (SplitTree, SplitView, TerminalSplitTreeView)

## Visual Screenshot Tests
- Add test_visual_screenshots.py for automated visual regression testing
- Uses in-app screenshot API (CGWindowListCreateImage) - no screen recording needed
- Generates HTML report with before/after comparisons
- Tests: splits, browser panels, focus switching, close operations, rapid cycles
- Includes annotation fields for easy feedback

## Browser Shortcut (⌘⇧B)
- Add keyboard shortcut to open browser panel in current pane
- Add openBrowser() method to TabManager
- Add shortcut configuration in KeyboardShortcutSettings

## Screenshot Command
- Add 'screenshot' command to TerminalController for in-app window capture
- Returns OK with screenshot ID and path

## Other
- Add tests/visual_output/ and tests/visual_report.html to .gitignore

* Add browser title subscription and set tab height to 30px

- Subscribe to BrowserPanel.$pageTitle changes to update bonsplit tabs
- Update tab titles in real-time as page navigation occurs
- Clean up subscriptions when panels are removed
- Set bonsplit tab bar and tab height to 30px (in submodule)

* Fix socket API regressions in list_surfaces, list_bonsplit_tabs, focus_pane

- list_surfaces: Remove [terminal]/[browser] suffix to keep UUID-only format
  that clients and tests expect for parsing
- list_bonsplit_tabs --pane: Properly look up pane by UUID instead of
  creating a new PaneID (requires bonsplit PaneID.id to be public)
- focus_pane: Accept both UUID strings and integer indices as documented

* Fix browser panel stability and keyboard shortcuts

- Prevent WKWebView focus lifecycle crashes during split/view reshuffles
- Match bracket shortcuts via keyCode (Cmd+Shift+[ / ], Cmd+Ctrl+[ / ])
- Support Ghostty config goto_split:* keybinds when WebView is focused
- Add focus_webview/is_webview_focused socket commands and regression tests
- Rename SidebarTab to Workspace and update docs

* Make ctrl+enter keybind test skippable

Skip when the Ghostty keybind isn't configured or when osascript can't send keystrokes (no Accessibility permission), so VM runs stay green.

* Auto-focus browser omnibar when blank

When a browser surface is focused but no URL is loaded yet, focus the address bar instead of the WKWebView.

* Stabilize socket surface indexing

* Focus browser omnibar escape; add webview keybind UI tests

- Escape in omnibar now returns focus to WKWebView\n- Add UI tests for Cmd+Ctrl+H pane navigation with WebKit focused (including Ghostty config)\n- Avoid flaky element screenshots in UpdatePillUITests on the UTM VM

* Fix browser drag-to-split blanks and socket parsing

* Fix webview-focused shortcuts and stabilize browser splits

- Match ctrl/shift shortcuts by keyCode where needed (Ctrl+H, bracket keys)
- Load Ghostty goto_split triggers reliably and refresh on config load
- Add debug socket helpers: set_shortcut + simulate_shortcut for tests
- Convert browser goto_split/keybind tests to socket-based injection (no osascript)
- Bump bonsplit for drag-to-split fixes

* Fix split layout collapse and harden socket pane APIs

* Stabilize OSC 99 notification test timing

* Fix terminal focus routing after split reparent

* Support simulate_shortcut enter for focus routing test

* Stabilize terminal focus routing test

* Fix frozen new terminal tabs after many splits

* Fix frozen new terminal tabs after splits

* Fix terminal freeze on launch/new tabs

* Update ghostty submodule

* Fix terminal focus/render stalls after split churn

* Fix nested split collapsing existing pane

* Fix nested split collapse + stabilize new-surface focus

* Update bonsplit submodule

* Fix SIGINT test flake

* Remove bonsplit tab-switch crossfade

* Remove PROJECTS.md

* Remove bonsplit tab selection animation

* Ignore generated test reports

* Middle click closes tab

* Revert unintended .gitignore change

* Fix build after main merge

* Revert "Fix build after main merge"

This reverts commit 16bf9816d0856b5385d52f886aa5eb50f3c9d9a4.

* Revert "Merge remote-tracking branch 'origin/main' into fix/blank-terminal-and-visual-tests"

This reverts commit 7c20fb53fd71fea7a19a3673f2dd73e5f0c783c4, reversing
changes made to 0aff107d787bc9d8bbc28220090b4ca7af72e040.

* Remove tab close fade animation

* Use terminal.fill icon

* Make terminal tab icon smaller

* Match browser globe tab icon size

* Bonsplit: tab min width 48 and tighter close button

* Bonsplit: smaller tab title font

* Show unread notification badge in bonsplit tabs and improve UI polish

Sync unread notification state to bonsplit tab badges (blue dot).
Improve EmptyPanelView with Terminal/Browser buttons and shortcut hints.
Add tooltips to close tab button and search overlay buttons.

* Fix reload.sh single-instance safety check on macOS

Replace GNU-only `ps -o etimes=` with portable `ps -o etime=` and
parse the dd-hh:mm:ss format manually for macOS compatibility.

* Centralize keyboard shortcut definitions into Action enum

Replace per-shortcut boilerplate with a single Action enum that holds
the label, defaults key, and default binding for each shortcut. All
call sites now use shortcut(for:). Settings UI is data-driven via
ForEach(Action.allCases). Titlebar tooltips update dynamically when
shortcuts are changed. Remove duplicate .keyboardShortcut() modifiers
from menu items that are already handled by the event monitor.

* Fix WKWebView consuming app menu shortcuts and close panel confirmation

Add CmuxWebView subclass that routes key equivalents through the main
menu before WebKit, so Cmd+N/Cmd+W/tab switching work when a browser
pane is focused. Fix Cmd+W close-panel path: bypass Bonsplit delegate
gating after the user confirms the running-process dialog by tracking
forceCloseTabIds. Add unit tests (CmuxWebViewKeyEquivalentTests) and
UI test scaffolding (MenuKeyEquivalentRoutingUITests) with a new
cmux-unit Xcode scheme.

* Update CLAUDE.md and PROJECTS.md with recent changes

CLAUDE.md: enforce --tag for reload commands, add cleanup safety rules.
PROJECTS.md: log notification badge, reload.sh fix, Cmd+W fix, WebView
key equiv fix, and centralized shortcuts work.

* Keep selection index stable on close

* Add concepts page documenting terminology hierarchy

New docs page explaining Window > Workspace > Pane > Surface > Panel
hierarchy with aligned ASCII diagram. Updated tabs.mdx and splits.mdx
to use consistent terminology (workspace instead of tab, surface
instead of panel) and corrected outdated CLI command references.

* Update bonsplit submodule

* WIP: improve split close stability and UI regressions

* Close terminal panel on child exit; hide terminal dirty dot

* Fix split close/focus regressions and stabilize UI tests

* Add unread Dock/Cmd+Tab badge with settings toggle

* Fix browser-surface shortcuts and Cmd+L browser opening

* Snapshot current workspace state before regression fixes

* Update bonsplit submodule snapshot

* Stabilize split-close regression capture and sidebar resize assertions

* Change default Show Notifications shortcut from Cmd+Shift+I to Cmd+I

* Fix update check readiness race, enable release update logging, and improve checking spinner

* Restore terminal file drop, fix browser omnibar click focus, and add panel workspace ID mutation for surface moves

* Add Cmd+digit workspace hints, titlebar shortcut pills, sidebar drag-reorder, and workspace placement settings

* Add v2 browser automation API, surface move/reorder commands, and short-handle ref system to TerminalController

* Add CLI browser command surface, --id-format flag, and move/reorder commands

* Extend test clients with move/reorder APIs, ref-handle support, and increased timeouts

* Harden test runner scripts with deterministic builds, retry logic, and robust socket readiness

* Stabilize existing test suites with focus-wait helpers, increased timeouts, and API shape updates

* Add terminal file drop e2e regression test

* Add v2 browser API, CLI ref resolution, and surface move/reorder test suites

* Add unit tests for shortcut hints, workspace reorder, drop planner, and update UI test stabilization

* Add cmux-debug-windows skill with snapshot script and agent config

* Update project docs: mark browser parity and move/reorder phases complete, add parallel agent workflow guidelines

* Update bonsplit submodule: re-entrant setPosition guard, tab shortcut hints, and moveTab/reorderTab API

* Add browser agent UX improvements: snapshot refs, placement reuse, diagnostics, and skill docs

- Upgrade browser.snapshot to emit accessibility tree text with element refs (eN)
- Add right-sibling pane reuse policy for browser.open_split placement
- Add rich not_found diagnostics with retry logic for selector actions
- Support --snapshot-after for post-action verification on mutating commands
- Allow browser fill with empty text for clearing inputs
- Default CLI --id-format to refs-first (UUIDs opt-in via --id-format uuids|both)
- Format legacy new-pane/new-surface output with short surface refs
- Add skills/cmuxterm-browser/ and skills/cmuxterm/ end-user skill docs
- Add regression tests for placement policy, snapshot refs, diagnostics, and ID defaults

* Update bonsplit submodule: keep raster favicons in color when inactive
2026-02-13 16:45:31 -08:00

1142 lines
42 KiB
Swift

import AppKit
import Combine
import SwiftUI
final class NonDraggableHostingView<Content: View>: NSHostingView<Content> {
override var mouseDownCanMoveWindow: Bool { false }
}
private struct TitlebarAccessoryView: View {
@ObservedObject var model: UpdateViewModel
var body: some View {
#if DEBUG
UpdatePill(model: model)
.padding(.trailing, 8)
#else
EmptyView()
#endif
}
}
enum TitlebarControlsStyle: Int, CaseIterable, Identifiable {
case classic
case compact
case roomy
case pillGroup
case softButtons
var id: Int { rawValue }
var menuTitle: String {
switch self {
case .classic:
return "Classic"
case .compact:
return "Compact"
case .roomy:
return "Roomy"
case .pillGroup:
return "Pill Group"
case .softButtons:
return "Soft Buttons"
}
}
var config: TitlebarControlsStyleConfig {
switch self {
case .classic:
return TitlebarControlsStyleConfig(
spacing: 10,
iconSize: 15,
buttonSize: 24,
badgeSize: 14,
badgeOffset: CGSize(width: 2, height: -2),
groupBackground: false,
groupPadding: EdgeInsets(),
buttonBackground: false,
buttonCornerRadius: 8,
hoverBackground: false
)
case .compact:
return TitlebarControlsStyleConfig(
spacing: 6,
iconSize: 13,
buttonSize: 20,
badgeSize: 12,
badgeOffset: CGSize(width: 1, height: -1),
groupBackground: false,
groupPadding: EdgeInsets(),
buttonBackground: false,
buttonCornerRadius: 6,
hoverBackground: false
)
case .roomy:
return TitlebarControlsStyleConfig(
spacing: 14,
iconSize: 16,
buttonSize: 28,
badgeSize: 16,
badgeOffset: CGSize(width: 3, height: -3),
groupBackground: false,
groupPadding: EdgeInsets(),
buttonBackground: false,
buttonCornerRadius: 10,
hoverBackground: false
)
case .pillGroup:
return TitlebarControlsStyleConfig(
spacing: 8,
iconSize: 14,
buttonSize: 24,
badgeSize: 14,
badgeOffset: CGSize(width: 2, height: -2),
groupBackground: false,
groupPadding: EdgeInsets(top: 1, leading: 4, bottom: 1, trailing: 4),
buttonBackground: false,
buttonCornerRadius: 8,
hoverBackground: true
)
case .softButtons:
return TitlebarControlsStyleConfig(
spacing: 8,
iconSize: 15,
buttonSize: 26,
badgeSize: 14,
badgeOffset: CGSize(width: 2, height: -2),
groupBackground: false,
groupPadding: EdgeInsets(),
buttonBackground: true,
buttonCornerRadius: 8,
hoverBackground: false
)
}
}
}
struct TitlebarControlsStyleConfig {
let spacing: CGFloat
let iconSize: CGFloat
let buttonSize: CGFloat
let badgeSize: CGFloat
let badgeOffset: CGSize
let groupBackground: Bool
let groupPadding: EdgeInsets
let buttonBackground: Bool
let buttonCornerRadius: CGFloat
let hoverBackground: Bool
}
final class TitlebarControlsViewModel: ObservableObject {
weak var notificationsAnchorView: NSView?
}
private struct NotificationsAnchorView: NSViewRepresentable {
let onResolve: (NSView) -> Void
func makeNSView(context: Context) -> NSView {
let view = AnchorNSView()
view.onLayout = { [weak view] in
guard let view else { return }
onResolve(view)
}
return view
}
func updateNSView(_ nsView: NSView, context: Context) {}
}
private final class AnchorNSView: NSView {
var onLayout: (() -> Void)?
override func layout() {
super.layout()
onLayout?()
}
}
struct ShortcutHintLanePlanner {
static func assignLanes(for intervals: [ClosedRange<CGFloat>], minSpacing: CGFloat = 4) -> [Int] {
guard !intervals.isEmpty else { return [] }
var laneMaxX: [CGFloat] = []
var lanes: [Int] = []
lanes.reserveCapacity(intervals.count)
for interval in intervals {
var lane = 0
while lane < laneMaxX.count {
let requiredMinX = laneMaxX[lane] + minSpacing
if interval.lowerBound >= requiredMinX {
break
}
lane += 1
}
if lane == laneMaxX.count {
laneMaxX.append(interval.upperBound)
} else {
laneMaxX[lane] = max(laneMaxX[lane], interval.upperBound)
}
lanes.append(lane)
}
return lanes
}
}
struct ShortcutHintHorizontalPlanner {
static func assignRightEdges(for intervals: [ClosedRange<CGFloat>], minSpacing: CGFloat = 6) -> [CGFloat] {
guard !intervals.isEmpty else { return [] }
var assignedRightEdges = Array(repeating: CGFloat.zero, count: intervals.count)
var nextMaxRight = CGFloat.greatestFiniteMagnitude
for index in stride(from: intervals.count - 1, through: 0, by: -1) {
let interval = intervals[index]
let width = interval.upperBound - interval.lowerBound
let preferredRightEdge = interval.upperBound
let adjustedRightEdge = min(preferredRightEdge, nextMaxRight)
assignedRightEdges[index] = adjustedRightEdge
nextMaxRight = adjustedRightEdge - width - minSpacing
}
return assignedRightEdges
}
}
private struct TitlebarControlButton<Content: View>: View {
let config: TitlebarControlsStyleConfig
let action: () -> Void
@ViewBuilder let content: () -> Content
@State private var isHovering = false
var body: some View {
Button(action: action) {
content()
.frame(width: config.buttonSize, height: config.buttonSize)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.frame(width: config.buttonSize, height: config.buttonSize)
.contentShape(Rectangle())
.background(hoverBackground)
.onHover { isHovering = $0 }
}
@ViewBuilder
private var hoverBackground: some View {
if config.hoverBackground && isHovering {
RoundedRectangle(cornerRadius: config.buttonCornerRadius, style: .continuous)
.fill(Color.primary.opacity(0.08))
}
}
}
private struct TitlebarControlsView: View {
@ObservedObject var notificationStore: TerminalNotificationStore
@ObservedObject var viewModel: TitlebarControlsViewModel
let onToggleSidebar: () -> Void
let onToggleNotifications: () -> Void
let onNewTab: () -> Void
@AppStorage("titlebarControlsStyle") private var styleRawValue = TitlebarControlsStyle.classic.rawValue
@AppStorage(ShortcutHintDebugSettings.titlebarHintXKey) private var titlebarShortcutHintXOffset = ShortcutHintDebugSettings.defaultTitlebarHintX
@AppStorage(ShortcutHintDebugSettings.titlebarHintYKey) private var titlebarShortcutHintYOffset = ShortcutHintDebugSettings.defaultTitlebarHintY
@AppStorage(ShortcutHintDebugSettings.alwaysShowHintsKey) private var alwaysShowShortcutHints = ShortcutHintDebugSettings.defaultAlwaysShowHints
@State private var shortcutRefreshTick = 0
@StateObject private var commandKeyMonitor = TitlebarCommandKeyMonitor()
private let titlebarHintRightSafetyShift: CGFloat = 10
private let titlebarHintBaseXShift: CGFloat = -10
private let titlebarHintBaseYShift: CGFloat = 1
private enum HintSlot: Int, CaseIterable {
case toggleSidebar
case showNotifications
case newTab
var action: KeyboardShortcutSettings.Action {
switch self {
case .toggleSidebar:
return .toggleSidebar
case .showNotifications:
return .showNotifications
case .newTab:
return .newTab
}
}
}
private struct TitlebarHintLayoutItem: Identifiable {
let action: KeyboardShortcutSettings.Action
let shortcut: StoredShortcut
let width: CGFloat
let leftEdge: CGFloat
var id: String { action.rawValue }
}
private var shouldShowTitlebarShortcutHints: Bool {
alwaysShowShortcutHints || commandKeyMonitor.isCommandPressed
}
var body: some View {
// Force the `.help(...)` tooltips to re-evaluate when shortcuts are changed in settings.
// (The titlebar controls don't otherwise re-render on UserDefaults changes.)
let _ = shortcutRefreshTick
let style = TitlebarControlsStyle(rawValue: styleRawValue) ?? .classic
let config = style.config
controlsGroup(config: config)
.padding(.leading, 4)
.padding(.trailing, titlebarHintTrailingInset)
.onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in
shortcutRefreshTick &+= 1
}
.onAppear {
commandKeyMonitor.start()
}
.onDisappear {
commandKeyMonitor.stop()
}
}
private var titlebarHintTrailingInset: CGFloat {
// Keep room for blur + shadow so the rightmost hint never clips.
max(0, ShortcutHintDebugSettings.clamped(titlebarShortcutHintXOffset)) + titlebarHintRightSafetyShift + 8
}
private func titlebarHintVerticalBaseOffset(for config: TitlebarControlsStyleConfig) -> CGFloat {
max(8, config.buttonSize * 0.4)
}
@ViewBuilder
private func controlsGroup(config: TitlebarControlsStyleConfig) -> some View {
let hintLayoutItems = titlebarHintLayoutItems(config: config)
let content = HStack(spacing: config.spacing) {
TitlebarControlButton(config: config, action: onToggleSidebar) {
iconLabel(systemName: "sidebar.left", config: config)
}
.accessibilityIdentifier("titlebarControl.toggleSidebar")
.accessibilityLabel("Toggle Sidebar")
.help(KeyboardShortcutSettings.Action.toggleSidebar.tooltip("Show or hide the sidebar"))
TitlebarControlButton(config: config, action: onToggleNotifications) {
ZStack(alignment: .topTrailing) {
iconLabel(systemName: "bell", config: config)
if notificationStore.unreadCount > 0 {
Text("\(min(notificationStore.unreadCount, 99))")
.font(.system(size: max(8, config.badgeSize - 5), weight: .semibold))
.foregroundColor(.white)
.frame(width: config.badgeSize, height: config.badgeSize)
.background(
Circle().fill(Color.accentColor)
)
.offset(x: config.badgeOffset.width, y: config.badgeOffset.height)
}
}
.frame(width: config.buttonSize, height: config.buttonSize)
}
.accessibilityIdentifier("titlebarControl.showNotifications")
.overlay(NotificationsAnchorView { viewModel.notificationsAnchorView = $0 }.allowsHitTesting(false))
.accessibilityLabel("Notifications")
.help(KeyboardShortcutSettings.Action.showNotifications.tooltip("Show notifications"))
TitlebarControlButton(config: config, action: onNewTab) {
iconLabel(systemName: "plus", config: config)
}
.accessibilityIdentifier("titlebarControl.newTab")
.accessibilityLabel("New Workspace")
.help(KeyboardShortcutSettings.Action.newTab.tooltip("New workspace"))
}
let paddedContent = content.padding(config.groupPadding)
if config.groupBackground {
paddedContent
.background(
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color(nsColor: .controlBackgroundColor))
)
.overlay(
RoundedRectangle(cornerRadius: 12, style: .continuous)
.stroke(Color(nsColor: .separatorColor).opacity(0.6), lineWidth: 1)
)
.overlay(alignment: .topLeading) {
titlebarShortcutHintOverlay(items: hintLayoutItems, config: config)
}
} else {
paddedContent
.overlay(alignment: .topLeading) {
titlebarShortcutHintOverlay(items: hintLayoutItems, config: config)
}
}
}
private func titlebarHintLayoutItems(config: TitlebarControlsStyleConfig) -> [TitlebarHintLayoutItem] {
let xOffset = CGFloat(ShortcutHintDebugSettings.clamped(titlebarShortcutHintXOffset))
let intervals = titlebarHintIntervals(config: config, xOffset: xOffset)
guard !intervals.isEmpty else { return [] }
// Keep all titlebar hints on the same Y lane and resolve overlaps by shifting left.
let minimumSpacing: CGFloat = 6
let assignedRightEdges = ShortcutHintHorizontalPlanner.assignRightEdges(
for: intervals.map { $0.interval },
minSpacing: minimumSpacing
)
var items: [TitlebarHintLayoutItem] = []
items.reserveCapacity(intervals.count)
for (index, item) in intervals.enumerated() {
let rightEdge = assignedRightEdges[index]
items.append(
TitlebarHintLayoutItem(
action: item.action,
shortcut: item.shortcut,
width: item.width,
leftEdge: rightEdge - item.width
)
)
}
return items
}
private func titlebarHintIntervals(
config: TitlebarControlsStyleConfig,
xOffset: CGFloat
) -> [(action: KeyboardShortcutSettings.Action, shortcut: StoredShortcut, width: CGFloat, interval: ClosedRange<CGFloat>)] {
guard shouldShowTitlebarShortcutHints else { return [] }
return HintSlot.allCases.compactMap { slot in
let shortcut = KeyboardShortcutSettings.shortcut(for: slot.action)
guard shortcut.command else { return nil }
let width = titlebarHintWidth(for: shortcut, config: config)
let rightEdge = config.groupPadding.leading
+ titlebarButtonRightEdge(for: slot, config: config)
+ xOffset
+ titlebarHintRightSafetyShift
+ titlebarHintBaseXShift
return (slot.action, shortcut, width, (rightEdge - width)...rightEdge)
}
}
private func titlebarHintWidth(for shortcut: StoredShortcut, config: TitlebarControlsStyleConfig) -> CGFloat {
let font = NSFont.systemFont(ofSize: max(8, config.iconSize - 4), weight: .semibold)
let textWidth = (shortcut.displayString as NSString).size(withAttributes: [.font: font]).width
return ceil(textWidth) + 12
}
private func titlebarButtonRightEdge(for slot: HintSlot, config: TitlebarControlsStyleConfig) -> CGFloat {
let index = CGFloat(slot.rawValue)
return (index + 1) * config.buttonSize + index * config.spacing
}
@ViewBuilder
private func titlebarShortcutHintOverlay(
items: [TitlebarHintLayoutItem],
config: TitlebarControlsStyleConfig
) -> some View {
let yOffset = config.groupPadding.top
+ titlebarHintVerticalBaseOffset(for: config)
+ titlebarHintBaseYShift
+ ShortcutHintDebugSettings.clamped(titlebarShortcutHintYOffset)
ZStack(alignment: .topLeading) {
ForEach(items) { item in
titlebarShortcutHintPill(shortcut: item.shortcut, config: config)
.accessibilityIdentifier("titlebarShortcutHint.\(item.action.rawValue)")
.frame(width: item.width, alignment: .leading)
.offset(x: item.leftEdge, y: yOffset)
}
}
.animation(.easeInOut(duration: 0.14), value: shouldShowTitlebarShortcutHints)
.transition(.opacity)
.allowsHitTesting(false)
}
private func titlebarShortcutHintPill(
shortcut: StoredShortcut,
config: TitlebarControlsStyleConfig
) -> some View {
Text(shortcut.displayString)
.font(.system(size: max(8, config.iconSize - 5), weight: .semibold, design: .rounded))
.monospacedDigit()
.lineLimit(1)
.fixedSize(horizontal: true, vertical: false)
.foregroundColor(.primary)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.frame(minHeight: max(14, config.iconSize + 1))
.background(ShortcutHintPillBackground())
}
@ViewBuilder
private func iconLabel(systemName: String, config: TitlebarControlsStyleConfig) -> some View {
let icon = Image(systemName: systemName)
.font(.system(size: config.iconSize, weight: .semibold))
.frame(width: config.buttonSize, height: config.buttonSize)
if config.buttonBackground {
icon
.background(
RoundedRectangle(cornerRadius: config.buttonCornerRadius)
.fill(Color(nsColor: .controlBackgroundColor).opacity(0.7))
)
} else {
icon
}
}
}
@MainActor
private final class TitlebarCommandKeyMonitor: ObservableObject {
@Published private(set) var isCommandPressed = false
private var flagsMonitor: Any?
private var keyDownMonitor: Any?
private var resignObserver: NSObjectProtocol?
private var pendingShowWorkItem: DispatchWorkItem?
func start() {
guard flagsMonitor == nil else {
update(from: NSEvent.modifierFlags)
return
}
flagsMonitor = NSEvent.addLocalMonitorForEvents(matching: .flagsChanged) { [weak self] event in
self?.update(from: event.modifierFlags)
return event
}
keyDownMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
self?.cancelPendingHintShow(resetVisible: true)
return event
}
resignObserver = NotificationCenter.default.addObserver(
forName: NSApplication.didResignActiveNotification,
object: nil,
queue: .main
) { [weak self] _ in
Task { @MainActor [weak self] in
self?.cancelPendingHintShow(resetVisible: true)
}
}
update(from: NSEvent.modifierFlags)
}
func stop() {
if let flagsMonitor {
NSEvent.removeMonitor(flagsMonitor)
self.flagsMonitor = nil
}
if let keyDownMonitor {
NSEvent.removeMonitor(keyDownMonitor)
self.keyDownMonitor = nil
}
if let resignObserver {
NotificationCenter.default.removeObserver(resignObserver)
self.resignObserver = nil
}
cancelPendingHintShow(resetVisible: true)
}
private func update(from modifierFlags: NSEvent.ModifierFlags) {
guard SidebarCommandHintPolicy.shouldShowHints(for: modifierFlags) else {
cancelPendingHintShow(resetVisible: true)
return
}
queueHintShow()
}
private func queueHintShow() {
guard !isCommandPressed else { return }
guard pendingShowWorkItem == nil else { return }
let workItem = DispatchWorkItem { [weak self] in
guard let self else { return }
self.pendingShowWorkItem = nil
guard SidebarCommandHintPolicy.shouldShowHints(for: NSEvent.modifierFlags) else { return }
self.isCommandPressed = true
}
pendingShowWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + SidebarCommandHintPolicy.intentionalHoldDelay, execute: workItem)
}
private func cancelPendingHintShow(resetVisible: Bool) {
pendingShowWorkItem?.cancel()
pendingShowWorkItem = nil
if resetVisible {
isCommandPressed = false
}
}
}
final class TitlebarControlsAccessoryViewController: NSTitlebarAccessoryViewController, NSPopoverDelegate {
private let hostingView: NonDraggableHostingView<TitlebarControlsView>
private let containerView = NSView()
private let notificationStore: TerminalNotificationStore
private lazy var notificationsPopover: NSPopover = makeNotificationsPopover()
private var pendingSizeUpdate = false
private let viewModel = TitlebarControlsViewModel()
private var userDefaultsObserver: NSObjectProtocol?
var popoverIsShownForTesting: Bool { notificationsPopover.isShown }
init(notificationStore: TerminalNotificationStore) {
self.notificationStore = notificationStore
let toggleSidebar = { _ = AppDelegate.shared?.sidebarState?.toggle() }
let toggleNotifications: () -> Void = { _ = AppDelegate.shared?.toggleNotificationsPopover(animated: true) }
let newTab = { _ = AppDelegate.shared?.tabManager?.addTab() }
hostingView = NonDraggableHostingView(
rootView: TitlebarControlsView(
notificationStore: notificationStore,
viewModel: viewModel,
onToggleSidebar: toggleSidebar,
onToggleNotifications: toggleNotifications,
onNewTab: newTab
)
)
super.init(nibName: nil, bundle: nil)
view = containerView
containerView.translatesAutoresizingMaskIntoConstraints = true
hostingView.translatesAutoresizingMaskIntoConstraints = true
hostingView.autoresizingMask = [.width, .height]
containerView.addSubview(hostingView)
userDefaultsObserver = NotificationCenter.default.addObserver(
forName: UserDefaults.didChangeNotification,
object: nil,
queue: .main
) { [weak self] _ in
self?.scheduleSizeUpdate()
}
scheduleSizeUpdate()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
if let userDefaultsObserver {
NotificationCenter.default.removeObserver(userDefaultsObserver)
}
}
override func viewDidAppear() {
super.viewDidAppear()
scheduleSizeUpdate()
}
override func viewDidLayout() {
super.viewDidLayout()
scheduleSizeUpdate()
}
private func scheduleSizeUpdate() {
guard !pendingSizeUpdate else { return }
pendingSizeUpdate = true
DispatchQueue.main.async { [weak self] in
self?.pendingSizeUpdate = false
self?.updateSize()
}
}
private func updateSize() {
hostingView.invalidateIntrinsicContentSize()
hostingView.layoutSubtreeIfNeeded()
let contentSize = hostingView.fittingSize
let titlebarHeight = view.window.map { window in
window.frame.height - window.contentLayoutRect.height
} ?? contentSize.height
let containerHeight = max(contentSize.height, titlebarHeight)
let yOffset = max(0, (containerHeight - contentSize.height) / 2.0)
preferredContentSize = NSSize(width: contentSize.width, height: containerHeight)
containerView.frame = NSRect(x: 0, y: 0, width: contentSize.width, height: containerHeight)
hostingView.frame = NSRect(x: 0, y: yOffset, width: contentSize.width, height: contentSize.height)
}
func toggleNotificationsPopover(animated: Bool = true) {
if notificationsPopover.isShown {
notificationsPopover.performClose(nil)
return
}
// Recreate content view each time to avoid stale observers when popover is hidden
let hostingController = NSHostingController(
rootView: NotificationsPopoverView(
notificationStore: notificationStore,
onDismiss: { [weak notificationsPopover] in
notificationsPopover?.performClose(nil)
}
)
)
hostingController.view.wantsLayer = true
hostingController.view.layer?.backgroundColor = .clear
notificationsPopover.contentViewController = hostingController
guard let window = view.window ?? hostingView.window ?? NSApp.keyWindow,
let contentView = window.contentView else {
return
}
// Force layout to ensure geometry is current.
contentView.layoutSubtreeIfNeeded()
if let anchorView = viewModel.notificationsAnchorView, anchorView.window != nil {
anchorView.superview?.layoutSubtreeIfNeeded()
let anchorRect = anchorView.convert(anchorView.bounds, to: contentView)
if !anchorRect.isEmpty {
notificationsPopover.animates = animated
notificationsPopover.show(relativeTo: anchorRect, of: contentView, preferredEdge: .maxY)
return
}
}
// Fallback: position near top-left of the window content.
let bounds = contentView.bounds
let anchorRect = NSRect(x: 12, y: bounds.maxY - 8, width: 1, height: 1)
notificationsPopover.animates = animated
notificationsPopover.show(relativeTo: anchorRect, of: contentView, preferredEdge: .maxY)
}
func dismissNotificationsPopover() {
if notificationsPopover.isShown {
notificationsPopover.performClose(nil)
}
}
private func makeNotificationsPopover() -> NSPopover {
let popover = NSPopover()
popover.behavior = .semitransient
popover.animates = true
popover.delegate = self
// Content view controller is set dynamically in toggleNotificationsPopover
return popover
}
// MARK: - NSPopoverDelegate
func popoverDidClose(_ notification: Notification) {
// Clear the content view controller to stop SwiftUI observers when popover is hidden
notificationsPopover.contentViewController = nil
}
}
private struct NotificationsPopoverView: View {
@ObservedObject var notificationStore: TerminalNotificationStore
let onDismiss: () -> Void
var body: some View {
VStack(spacing: 0) {
HStack {
Text("Notifications")
.font(.headline)
Spacer()
if !notificationStore.notifications.isEmpty {
Button("Clear All") {
notificationStore.clearAll()
}
.buttonStyle(.bordered)
}
}
.padding(.horizontal, 12)
.padding(.vertical, 10)
Divider()
if notificationStore.notifications.isEmpty {
VStack(spacing: 8) {
Image(systemName: "bell.slash")
.font(.system(size: 28))
.foregroundColor(.secondary)
Text("No notifications yet")
.font(.headline)
Text("Desktop notifications will appear here.")
.font(.subheadline)
.foregroundColor(.secondary)
}
.frame(minWidth: 420, idealWidth: 520, maxWidth: 640, minHeight: 180)
} else {
ScrollView {
LazyVStack(spacing: 8) {
ForEach(notificationStore.notifications) { notification in
NotificationPopoverRow(
notification: notification,
tabTitle: tabTitle(for: notification.tabId),
onOpen: { open(notification) },
onClear: { notificationStore.remove(id: notification.id) }
)
}
}
.padding(12)
}
.frame(minWidth: 420, idealWidth: 520, maxWidth: 640, minHeight: 320, maxHeight: 480)
}
}
.background(Color(nsColor: .windowBackgroundColor))
}
private func tabTitle(for tabId: UUID) -> String? {
AppDelegate.shared?.tabTitle(for: tabId)
}
private func open(_ notification: TerminalNotification) {
// SwiftUI action closures are not guaranteed to run on the main actor.
// Ensure window focus + tab selection happens on the main thread.
DispatchQueue.main.async {
_ = AppDelegate.shared?.openNotification(
tabId: notification.tabId,
surfaceId: notification.surfaceId,
notificationId: notification.id
)
onDismiss()
}
}
}
private struct NotificationPopoverRow: View {
let notification: TerminalNotification
let tabTitle: String?
let onOpen: () -> Void
let onClear: () -> Void
var body: some View {
HStack(alignment: .top, spacing: 10) {
Button(action: onOpen) {
HStack(alignment: .top, spacing: 10) {
Circle()
.fill(notification.isRead ? Color.clear : Color.accentColor)
.frame(width: 8, height: 8)
.overlay(
Circle()
.stroke(Color.accentColor.opacity(notification.isRead ? 0.2 : 1), lineWidth: 1)
)
.padding(.top, 6)
VStack(alignment: .leading, spacing: 4) {
HStack {
Text(notification.title)
.font(.headline)
.foregroundColor(.primary)
Spacer()
Text(notification.createdAt.formatted(date: .omitted, time: .shortened))
.font(.caption)
.foregroundColor(.secondary)
}
if !notification.body.isEmpty {
Text(notification.body)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(3)
}
if let tabTitle {
Text(tabTitle)
.font(.caption)
.foregroundColor(.secondary)
}
}
Spacer(minLength: 0)
}
.padding(.trailing, 6)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityIdentifier("NotificationPopoverRow.\(notification.id.uuidString)")
// XCUITest's `.click()` is not always reliable for SwiftUI `Button`s hosted in an `NSPopover`.
// Provide an explicit accessibility action so AXPress always routes to `onOpen`.
.accessibilityAction { onOpen() }
Button(action: onClear) {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
}
.buttonStyle(.plain)
}
.padding(10)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color(nsColor: .controlBackgroundColor))
)
}
}
final class UpdateAccessoryViewController: NSTitlebarAccessoryViewController {
private let hostingView: NonDraggableHostingView<TitlebarAccessoryView>
private let containerView = NSView()
private var stateCancellable: AnyCancellable?
private var pendingSizeUpdate = false
init(model: UpdateViewModel) {
hostingView = NonDraggableHostingView(rootView: TitlebarAccessoryView(model: model))
super.init(nibName: nil, bundle: nil)
view = containerView
containerView.translatesAutoresizingMaskIntoConstraints = true
hostingView.translatesAutoresizingMaskIntoConstraints = true
hostingView.autoresizingMask = [.width, .height]
containerView.addSubview(hostingView)
stateCancellable = model.$state
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
self?.scheduleSizeUpdate()
}
scheduleSizeUpdate()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidAppear() {
super.viewDidAppear()
scheduleSizeUpdate()
}
override func viewDidLayout() {
super.viewDidLayout()
scheduleSizeUpdate()
}
private func scheduleSizeUpdate() {
guard !pendingSizeUpdate else { return }
pendingSizeUpdate = true
DispatchQueue.main.async { [weak self] in
self?.pendingSizeUpdate = false
self?.updateSize()
}
}
private func updateSize() {
hostingView.invalidateIntrinsicContentSize()
hostingView.layoutSubtreeIfNeeded()
let pillSize = hostingView.fittingSize
let titlebarHeight = view.window.map { window in
window.frame.height - window.contentLayoutRect.height
} ?? pillSize.height
let containerHeight = max(pillSize.height, titlebarHeight)
let yOffset = max(0, (containerHeight - pillSize.height) / 2.0)
preferredContentSize = NSSize(width: pillSize.width, height: containerHeight)
containerView.frame = NSRect(x: 0, y: 0, width: pillSize.width, height: containerHeight)
hostingView.frame = NSRect(x: 0, y: yOffset, width: pillSize.width, height: pillSize.height)
}
}
final class UpdateTitlebarAccessoryController {
private weak var updateViewModel: UpdateViewModel?
private var didStart = false
private let attachedWindows = NSHashTable<NSWindow>.weakObjects()
private var observers: [NSObjectProtocol] = []
private var pendingAttachRetries: [ObjectIdentifier: Int] = [:]
private var startupScanWorkItems: [DispatchWorkItem] = []
private let controlsIdentifier = NSUserInterfaceItemIdentifier("cmux.titlebarControls")
private let controlsControllers = NSHashTable<TitlebarControlsAccessoryViewController>.weakObjects()
init(viewModel: UpdateViewModel) {
self.updateViewModel = viewModel
}
deinit {
for observer in observers {
NotificationCenter.default.removeObserver(observer)
}
}
func start() {
guard !didStart else { return }
didStart = true
attachToExistingWindows()
installObservers()
scheduleStartupWindowScans()
}
func attach(to window: NSWindow) {
attachIfNeeded(to: window)
}
private func installObservers() {
let center = NotificationCenter.default
observers.append(center.addObserver(
forName: NSWindow.didBecomeMainNotification,
object: nil,
queue: .main
) { [weak self] notification in
guard let window = notification.object as? NSWindow else { return }
self?.attachIfNeeded(to: window)
})
observers.append(center.addObserver(
forName: NSWindow.didBecomeKeyNotification,
object: nil,
queue: .main
) { [weak self] notification in
guard let window = notification.object as? NSWindow else { return }
self?.attachIfNeeded(to: window)
})
// We intentionally do not rely on "window became visible" notifications here:
// AppKit does not provide a stable cross-SDK API for this. Startup scans handle this case.
}
private func attachToExistingWindows() {
for window in NSApp.windows {
attachIfNeeded(to: window)
}
}
private func scheduleStartupWindowScans() {
// We want to be robust to SwiftUI/AppKit timing and to XCTest automation. Scanning
// NSApp.windows briefly at startup is cheap and ensures accessories are attached even
// if key/main/visible notifications are missed.
let delays: [TimeInterval] = [0.05, 0.15, 0.3, 0.6, 1.0, 2.0, 3.0]
for delay in delays {
let item = DispatchWorkItem { [weak self] in
self?.attachToExistingWindows()
#if DEBUG
let env = ProcessInfo.processInfo.environment
if env["CMUX_UI_TEST_MODE"] == "1" {
let ids = NSApp.windows.map { $0.identifier?.rawValue ?? "<nil>" }
let delayText = String(format: "%.2f", delay)
UpdateLogStore.shared.append("startup window scan (delay=\(delayText)) count=\(NSApp.windows.count) ids=\(ids.joined(separator: ","))")
}
#endif
}
startupScanWorkItems.append(item)
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: item)
}
}
private func attachIfNeeded(to window: NSWindow) {
guard !attachedWindows.contains(window) else { return }
guard !isSettingsWindow(window) else { return }
// Window identifiers are assigned by SwiftUI via WindowAccessor, which can run
// after didBecomeKey/didBecomeMain notifications. Retry briefly to avoid missing
// attaching accessories (notably in UI tests).
if !isMainTerminalWindow(window) {
let key = ObjectIdentifier(window)
let attempts = pendingAttachRetries[key, default: 0]
if attempts < 40 {
pendingAttachRetries[key] = attempts + 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self, weak window] in
guard let self, let window else { return }
self.attachIfNeeded(to: window)
}
} else {
pendingAttachRetries.removeValue(forKey: key)
}
return
}
pendingAttachRetries.removeValue(forKey: ObjectIdentifier(window))
if !window.titlebarAccessoryViewControllers.contains(where: { $0.view.identifier == controlsIdentifier }) {
let controls = TitlebarControlsAccessoryViewController(
notificationStore: TerminalNotificationStore.shared
)
controls.layoutAttribute = .left
controls.view.identifier = controlsIdentifier
window.addTitlebarAccessoryViewController(controls)
controlsControllers.add(controls)
}
attachedWindows.add(window)
#if DEBUG
let env = ProcessInfo.processInfo.environment
if env["CMUX_UI_TEST_MODE"] == "1" {
let ident = window.identifier?.rawValue ?? "<nil>"
UpdateLogStore.shared.append("attached titlebar accessories to window id=\(ident)")
}
#endif
}
private func isSettingsWindow(_ window: NSWindow) -> Bool {
if window.identifier?.rawValue == "cmux.settings" {
return true
}
return window.title == "Settings"
}
private func isMainTerminalWindow(_ window: NSWindow) -> Bool {
guard let raw = window.identifier?.rawValue else { return false }
return raw == "cmux.main" || raw.hasPrefix("cmux.main.")
}
private func preferredNotificationsController(
from controllers: [TitlebarControlsAccessoryViewController],
preferShownPopover: Bool
) -> TitlebarControlsAccessoryViewController? {
if let keyWindow = NSApp.keyWindow,
let match = controllers.first(where: { $0.view.window === keyWindow }) {
return match
}
if let keyMain = NSApp.windows.first(where: { $0.isKeyWindow && isMainTerminalWindow($0) }),
let match = controllers.first(where: { $0.view.window === keyMain }) {
return match
}
if preferShownPopover,
let shown = controllers.first(where: { $0.popoverIsShownForTesting }) {
return shown
}
return controllers.first
}
func toggleNotificationsPopover(animated: Bool = true) {
let controllers = controlsControllers.allObjects
guard !controllers.isEmpty else { return }
let target = preferredNotificationsController(from: controllers, preferShownPopover: true)
for controller in controllers {
if controller !== target {
controller.dismissNotificationsPopover()
}
}
target?.toggleNotificationsPopover(animated: animated)
}
func isNotificationsPopoverShown() -> Bool {
controlsControllers.allObjects.contains(where: { $0.popoverIsShownForTesting })
}
@discardableResult
func dismissNotificationsPopoverIfShown() -> Bool {
let controllers = controlsControllers.allObjects
var dismissed = false
for controller in controllers where controller.popoverIsShownForTesting {
controller.dismissNotificationsPopover()
dismissed = true
}
return dismissed
}
func showNotificationsPopover(animated: Bool = true) {
let controllers = controlsControllers.allObjects
guard !controllers.isEmpty else { return }
let target = preferredNotificationsController(from: controllers, preferShownPopover: false)
for controller in controllers {
if controller !== target {
controller.dismissNotificationsPopover()
}
}
guard let target else { return }
if target.popoverIsShownForTesting {
return
}
target.toggleNotificationsPopover(animated: animated)
}
}