Files
openclaw/apps/macos/Sources/OpenClaw/NodeMode/MacNodeChannelStatusStore.swift
Peter Steinberger 99a2434f7b refactor(macos): status menu polish — owned width, quiet header, structured diagnostics, native highlight (#130388)
* fix(macos): stabilize status menu width across hosted and native rows

* fix(macos): format node worker termination status without optionals

* refactor(macos): give status menu width one owner

fittedTitle's hasImage/hasSubmenu/keyEquivalent parameters never changed
the computation - the budget always included image, submenu, and shortcut
chrome - because AppKit aligns those columns across the whole menu, so a
per-item budget is the wrong model. Collapse to one menu-wide budget and
drop the misleading knobs from all 13 call sites.

* refactor(macos): keep the status menu header quiet when healthy

A working system now says nothing: the header renders only operator-
actionable trouble lines (colored text, no status bubbles), the healthy
state is title + toggle + capability strip, and the churn sources that
made the open menu jump are gone - relative check ages, 'health check
running', and the per-tool activity label (the critter icon already
animates work). Session-row kind icons go monochrome; green conveyed
nothing actionable. Healthy title is 'OpenClaw' (or the primary gateway
name with multiple gateways) since the toggle already communicates
active. DashboardGatewayMenuModel.connectionLabel lost its last caller
and is deleted with its test.

* fix(macos): preserve structured node worker diagnostics in status menu

* feat(macos): highlight hosted menu rows from AppKit's selection signal

AppKit paints no selection behind NSMenuItem.view, so hosted session and
device rows had no hover or keyboard highlight. HostedMenuRowView now
draws the native selection material (rounded, emphasized) and flips the
SwiftUI content through the existing menuItemHighlighted environment,
driven by NSMenuDelegate.menu(_:willHighlight:) so pointer and arrow-key
navigation both light up. Submenus get a shared highlight-only delegate;
the controller's root-menu guards keep open/refresh paths unaffected.

* refactor(macos): drop the manual Open Canvas action

Canvas panels are agent-opened; a manual open action was noise. The
Allow Canvas capability tile stays - that is the permission. Removes the
menu action, the dock-menu item, and the now-orphaned
AppNavigationActions.toggleCanvas.

* fix(macos): clear hosted submenu selection when the submenu closes

AppKit sends no willHighlight(nil) on submenu close, so a hosted device
row selected there reopened still lit. The shared highlight delegate now
resets its menu's hosted rows in menuDidClose; regression covers the
close cycle.

* style(macos): apply swiftformat to highlight delegate
2026-08-26 14:49:57 -07:00

80 lines
3.4 KiB
Swift

import Foundation
import Observation
/// Recorded fact for the Mac node channel, written by MacNodeModeCoordinator at
/// the connect boundary. The menu bar reads this instead of inferring node
/// health from gateway node listings, so a node channel that never dials still
/// surfaces its reason to the operator.
enum MacNodeChannelState: Equatable, Sendable {
/// Node mode is paused, stopped, or not configured to run.
case idle
/// The channel connected. A non-nil reason means the node-host worker is
/// unavailable and only native capabilities are advertised.
case connected(workerUnavailableReason: String?, diagnostic: String? = nil)
/// The last connect attempt failed; the coordinator keeps retrying.
case unavailable(reason: String, diagnostic: String? = nil)
var operatorStatusLine: (label: String, diagnostic: String?, isDegraded: Bool)? {
switch self {
case .idle, .connected(workerUnavailableReason: nil, diagnostic: _):
nil
case let .connected(workerUnavailableReason: .some(reason), diagnostic: diagnostic):
("Mac node degraded — \(Self.condense(reason))", Self.excerpt(diagnostic), true)
case let .unavailable(reason, diagnostic):
("Mac node unavailable — \(Self.condense(reason))", Self.excerpt(diagnostic), false)
}
}
/// Menu status lines are single-line; keep the leading reason sentence and
/// bound it so a CLI stack trace cannot flood the menu.
private static func condense(_ reason: String) -> String {
let firstLine = reason
.split(separator: "\n", omittingEmptySubsequences: true)
.first
.map { $0.trimmingCharacters(in: .whitespaces) } ?? reason
return firstLine.count > 220 ? firstLine.prefix(220) + "…" : firstLine
}
/// Preserve complete stderr lines whenever possible so truncation cannot
/// turn a useful CLI diagnostic into another clipped headline fragment.
private static func excerpt(_ diagnostic: String?) -> String? {
guard let diagnostic = diagnostic?.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty else {
return nil
}
let lines = diagnostic.split(separator: "\n", omittingEmptySubsequences: false)
let needsEllipsis = diagnostic.count > 360 || lines.count > 4
let maximumLength = needsEllipsis ? 359 : 360
var retainedLines: [Substring] = []
var retainedLength = 0
for line in lines.prefix(4) {
let length = retainedLength + (retainedLines.isEmpty ? 0 : 1) + line.count
guard length <= maximumLength else { break }
retainedLines.append(line)
retainedLength = length
}
if retainedLines.isEmpty {
let prefix = diagnostic.prefix(maximumLength)
let boundary = prefix.lastIndex(where: \.isWhitespace) ?? prefix.endIndex
return "\(prefix[..<boundary])…"
}
let excerpt = retainedLines.joined(separator: "\n")
return retainedLines.count == lines.count ? excerpt : "\(excerpt)…"
}
}
@MainActor
@Observable
final class MacNodeChannelStatusStore {
static let shared = MacNodeChannelStatusStore()
private(set) var state: MacNodeChannelState = .idle
func record(_ state: MacNodeChannelState) {
guard self.state != state else { return }
self.state = state
}
}