import AppKit import SwiftUI /// The S2a header spike (spec 2026-11-22 §1, "Spike checklist"), run on the /// real document window: Docs' own title area at the toolbar's leading edge, /// the system title hidden but still set. Each item prints PASS, FAIL or /// UNVERIFIED with what was measured; the run never stops on a FAIL, so the /// whole checklist is reported at once. `DOCS_SMOKE_SPIKE=1` runs it. @MainActor enum HeaderSpike { static func run(_ window: NSWindow, title: String) async { print("SPIKE: macOS \(ProcessInfo.processInfo.operatingSystemVersionString)") // 0. One title: the window's is the document's, hidden, and no // principal (centred) item draws it again. report("window title is the document title", window.title == title, "title=\(window.title)") report("system title hidden", window.titleVisibility == .hidden, "titleVisibility=\(window.titleVisibility.rawValue)") let toolbar = window.toolbar report("no centred (.principal) item", toolbar?.centeredItemIdentifiers.isEmpty ?? true, "centred=\(toolbar?.centeredItemIdentifiers.map(\.rawValue) ?? [])") let titles = staticTexts(in: window, matching: title) // In process, SwiftUI builds the toolbar's accessibility tree only for // a running assistive technology: zero found means "not measured". if titles.isEmpty { print("SPIKE UNVERIFIED: exactly one visible title — no accessibility tree in process; see the window capture") } else { report("exactly one visible title", titles.count == 1, "static texts reading the title: \(titles.count)") } // 6. VoiceOver: the window's name is the title; the title area reads // the title, then the subtitle. report("VoiceOver window name", window.accessibilityTitle() == title, "accessibilityTitle=\(window.accessibilityTitle() ?? "nil")") let order = toolbarTexts(window) print("SPIKE: toolbar accessibility texts in order: \(order)") if let at = order.firstIndex(of: title) { report("VoiceOver reads title then subtitle", order.count > at + 1 && order[at + 1].contains("·") || (order.count > at + 1 && !order[at + 1].isEmpty), "after the title: \(order.count > at + 1 ? order[at + 1] : "nothing")") } else { print("SPIKE UNVERIFIED: VoiceOver reads title then subtitle — no accessibility tree in process (needs VoiceOver on a real Mac)") } // 5. Window tabs: the tab shows the document title. report("window tab title", window.tab.title == title, "tab.title=\(window.tab.title)") // 3. Overflow at narrow widths: the title area stays; trailing // controls go into the » menu. let original = window.frame for width in [1120.0, 760, 560, 420] { window.setContentSize(NSSize(width: width, height: 700)) try? await Task.sleep(for: .milliseconds(400)) let all = toolbar?.items.map(\.itemIdentifier.rawValue) ?? [] let visible = toolbar?.visibleItems?.map(\.itemIdentifier.rawValue) ?? [] let titleShown = window.toolbar?.visibleItems?.first?.itemIdentifier == window.toolbar?.items.first?.itemIdentifier print("SPIKE: width \(Int(width)): items=\(all) visible=\(visible) titleShown=\(titleShown)") } window.setFrame(original, display: true) try? await Task.sleep(for: .milliseconds(400)) // 2. Double-click on the title area renames. if let frame = titleFrame(window, title) { let point = NSPoint(x: frame.midX, y: frame.midY) click(window, at: point, count: 2) try? await Task.sleep(for: .milliseconds(400)) let field = editableField(window.contentView?.superview) let editing = (window.firstResponder as? NSTextView)?.isFieldEditor == true report("the rename field has the keyboard focus", editing, "firstResponder=\(String(describing: window.firstResponder.map { type(of: $0) }))") report("double-click on the title renames", field != nil, "editable field: \(field.map { String(describing: type(of: $0)) } ?? "none")") // Leave the rename as it was: Esc. // Esc leaves the rename as it was (onExitCommand). if field != nil, let esc = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: window.windowNumber, context: nil, characters: "\u{1b}", charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53) { window.sendEvent(esc) } try? await Task.sleep(for: .milliseconds(400)) report("Esc leaves the rename", editableField(window.contentView?.superview) == nil, "field after Esc: \(editableField(window.contentView?.superview) == nil ? "gone" : "still there")") dump(window.toolbar?.items.first?.view, depth: 0) print("SPIKE: in-process NSAccessibility exposes SwiftUI toolbar text only while an assistive technology is running, so VoiceOver's reading order is not measured here") // 1. Drag by the title area: a mouse-down there may move the window. let hit = window.contentView?.superview?.hitTest(window.contentView!.superview!.convert(point, from: nil)) var chain: [String] = [] var view = hit var movable = false while let v = view { chain.append("\(type(of: v)):\(v.mouseDownCanMoveWindow)"); if v.mouseDownCanMoveWindow { movable = true }; view = v.superview } print("SPIKE: hit chain at the title: \(chain.prefix(6))") report("title area can drag the window (mouseDownCanMoveWindow on the hit view)", hit?.mouseDownCanMoveWindow == true, "hit=\(hit.map { String(describing: type(of: $0)) } ?? "nil")") // A real drag, posted as events: AppKit may run the drag through the // window server, which reads the real cursor, so a non-move here is // UNVERIFIED rather than FAIL. let start = window.frame.origin let screenPoint = window.convertPoint(toScreen: point) func post(_ type: NSEvent.EventType, _ dx: CGFloat) { let p = window.convertPoint(fromScreen: NSPoint(x: screenPoint.x + dx, y: screenPoint.y)) if let event = NSEvent.mouseEvent(with: type, location: p, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: window.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: 1) { NSApp.postEvent(event, atStart: false) } } post(.leftMouseDown, 0); for dx in stride(from: 10.0, through: 80, by: 10) { post(.leftMouseDragged, dx) }; post(.leftMouseUp, 80) try? await Task.sleep(for: .milliseconds(800)) print("SPIKE \(window.frame.origin != start ? "PASS" : "UNVERIFIED"): posted drag on the title moves the window — \(start) -> \(window.frame.origin)") if window.frame.origin != start { window.setFrameOrigin(start) } _ = movable } else { report("title area found", false, "no accessibility frame for the title") } // 2b. Double-click on empty toolbar space still zooms or minimises. let action = UserDefaults.standard.string(forKey: "AppleActionOnDoubleClick") ?? "Maximize" if let frame = titleFrame(window, title) { let empty = NSPoint(x: (frame.maxX + window.frame.width * 0.55) / 2, y: frame.midY) let before = window.frame click(window, at: empty, count: 2) try? await Task.sleep(for: .milliseconds(900)) let changed = window.frame != before || window.isMiniaturized report("double-click on empty toolbar (\(action))", changed || action == "None", "frame \(before) -> \(window.frame), miniaturised=\(window.isMiniaturized)") if window.isMiniaturized { window.deminiaturize(nil); try? await Task.sleep(for: .milliseconds(900)) } if window.frame != before { window.setFrame(before, display: true) } } // 4. Full screen: the title area and controls stay in the toolbar. window.toggleFullScreen(nil) try? await Task.sleep(for: .seconds(2)) let full = window.styleMask.contains(.fullScreen) let titleInFull = window.toolbar?.visibleItems?.first?.view?.window != nil report("full screen keeps the title area", full && titleInFull && window.title == title, "fullScreen=\(full) titleShown=\(titleInFull)") if full { window.toggleFullScreen(nil); try? await Task.sleep(for: .seconds(2)) } } static func dump(_ view: NSView?, depth: Int) { guard let view, depth < 12 else { return } let ax = AX(object: view) print("SPIKE AX \(String(repeating: " ", count: depth))\(type(of: view)) role=\(ax.role?.rawValue ?? "-") text=\(ax.text) children=\(ax.children.count)") for child in ax.children where !(child.object is NSView) { print("SPIKE AX \(String(repeating: " ", count: depth + 1))[element] role=\(child.role?.rawValue ?? "-") text=\(child.text) children=\(child.children.map { ($0.role?.rawValue ?? "-") + ":" + $0.text })") } for sub in view.subviews { dump(sub, depth: depth + 1) } } static func report(_ item: String, _ passed: Bool, _ detail: String) { print("SPIKE \(passed ? "PASS" : "FAIL"): \(item) — \(detail)") } struct AX { let object: AnyObject var role: NSAccessibility.Role? { object.accessibilityRole?() } var text: String { if let element = object as? NSObject & NSAccessibilityProtocol, let value = element.accessibilityValue() as? String, !value.isEmpty { return value } return (object as? NSObject & NSAccessibilityProtocol)?.accessibilityLabel() ?? object.accessibilityLabel?() ?? "" } var frame: NSRect { object.accessibilityFrame?() ?? .zero } var children: [AX] { (object.accessibilityChildren?() ?? []).map { AX(object: $0 as AnyObject) } } var hidden: Bool { (object as? NSView)?.isHiddenOrHasHiddenAncestor ?? false } } static func all(_ window: NSWindow) -> [AX] { var found: [AX] = [] func walk(_ element: AX, depth: Int) { guard depth < 40 else { return } found.append(element) for child in element.children { walk(child, depth: depth + 1) } } walk(AX(object: window), depth: 0) // SwiftUI's toolbar items are their own hosting views, outside the // content view's tree: walk them too. for item in window.toolbar?.items ?? [] { if let view = item.view { walk(AX(object: view), depth: 0) } } return found } /// Every accessibility static text in the window that reads `text`. static func staticTexts(in window: NSWindow, matching text: String) -> [AX] { all(window).filter { $0.role == .staticText && $0.text == text && !$0.hidden } } /// The static texts in the toolbar's band, in accessibility order. static func toolbarTexts(_ window: NSWindow) -> [String] { let top = window.frame.maxY - 60 return all(window).filter { $0.role == .staticText && $0.frame.minY >= top && !$0.text.isEmpty }.map(\.text) } /// The title's frame in window coordinates: the accessibility element's, /// or the upper half of the leading (navigation) toolbar item's view. static func titleFrame(_ window: NSWindow, _ title: String) -> NSRect? { if let element = staticTexts(in: window, matching: title).first, element.frame != .zero { return window.convertFromScreen(element.frame) } guard let view = window.toolbar?.items.first?.view, view.window != nil else { return nil } let frame = view.convert(view.bounds, to: nil) return NSRect(x: frame.minX, y: frame.midY, width: min(frame.width, 160), height: frame.height / 2) } static func click(_ window: NSWindow, at point: NSPoint, count: Int) { for n in 1...count { for type in [NSEvent.EventType.leftMouseDown, .leftMouseUp] { if let event = NSEvent.mouseEvent(with: type, location: point, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: window.windowNumber, context: nil, eventNumber: 0, clickCount: n, pressure: 1) { window.sendEvent(event) } } } } static func editableField(_ view: NSView?) -> NSTextField? { guard let view else { return nil } if let field = view as? NSTextField, field.isEditable, !field.isHiddenOrHasHiddenAncestor { return field } return view.subviews.lazy.compactMap(editableField).first } }