import AppKit import SwiftUI import SheetsKit @MainActor private enum Run { static var started = false } @main struct AccountMenuSmoke: App { @State private var windows = SheetsWindows() var body: some Scene { Window("Smoke Home", id: "home") { Launcher(windows: windows) } .commands { SheetsWindowCommands(windows: windows) } Window("Smoke Document", id: "document") { Text("Document").frame(width: 500, height: 300) } } } private struct Launcher: View { let windows: SheetsWindows @Environment(\.openWindow) private var openWindow var body: some View { Text("Home").frame(width: 400, height: 200).task { guard !Run.started else { return } Run.started = true NSApp.setActivationPolicy(.regular) openWindow(id: "document") try? await Task.sleep(for: .milliseconds(500)) NSApp.windows.first(where: { $0.title == "Smoke Home" })?.close() let document = NSApp.windows.first(where: { $0.title == "Smoke Document" })! NSApp.activate(ignoringOtherApps: true) document.makeKeyAndOrderFront(nil) try? await Task.sleep(for: .milliseconds(500)) let menu = NSApp.mainMenu!.items.first!.submenu! menu.delegate?.menuNeedsUpdate?(menu) menu.update() guard let accounts = menu.items.first(where: { $0.title == sheetsMacString("Accounts") }), accounts.isEnabled, let signOut = menu.items.first(where: { $0.title == sheetsMacString("Sign out") }), !signOut.isEnabled else { fatalError("Account commands missing or unsafe") } menu.performActionForItem(at: menu.index(of: accounts)) try? await Task.sleep(for: .milliseconds(300)) precondition(windows.accountsRequested) precondition(NSApp.windows.contains { $0.title == "Smoke Home" && $0.isVisible }) print("PASS: Accounts and Sign out live in app menu; Accounts reopens closed Home") NSApp.terminate(nil) } } }