| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | import SheetsKit |
| 4 | |
| 5 | @MainActor private enum Run { static var started = false } |
| 6 | @main struct AccountMenuSmoke: App { |
| 7 | @State private var windows = SheetsWindows() |
| 8 | var body: some Scene { |
| 9 | Window("Smoke Home", id: "home") { Launcher(windows: windows) } |
| 10 | .commands { SheetsWindowCommands(windows: windows) } |
| 11 | Window("Smoke Document", id: "document") { Text("Document").frame(width: 500, height: 300) } |
| 12 | } |
| 13 | } |
| 14 | private struct Launcher: View { |
| 15 | let windows: SheetsWindows |
| 16 | @Environment(\.openWindow) private var openWindow |
| 17 | var body: some View { |
| 18 | Text("Home").frame(width: 400, height: 200).task { |
| 19 | guard !Run.started else { return } |
| 20 | Run.started = true |
| 21 | NSApp.setActivationPolicy(.regular) |
| 22 | openWindow(id: "document") |
| 23 | try? await Task.sleep(for: .milliseconds(500)) |
| 24 | NSApp.windows.first(where: { $0.title == "Smoke Home" })?.close() |
| 25 | let document = NSApp.windows.first(where: { $0.title == "Smoke Document" })! |
| 26 | NSApp.activate(ignoringOtherApps: true) |
| 27 | document.makeKeyAndOrderFront(nil) |
| 28 | try? await Task.sleep(for: .milliseconds(500)) |
| 29 | let menu = NSApp.mainMenu!.items.first!.submenu! |
| 30 | menu.delegate?.menuNeedsUpdate?(menu) |
| 31 | menu.update() |
| 32 | guard let accounts = menu.items.first(where: { $0.title == sheetsMacString("Accounts") }), accounts.isEnabled, |
| 33 | let signOut = menu.items.first(where: { $0.title == sheetsMacString("Sign out") }), !signOut.isEnabled else { |
| 34 | fatalError("Account commands missing or unsafe") |
| 35 | } |
| 36 | menu.performActionForItem(at: menu.index(of: accounts)) |
| 37 | try? await Task.sleep(for: .milliseconds(300)) |
| 38 | precondition(windows.accountsRequested) |
| 39 | precondition(NSApp.windows.contains { $0.title == "Smoke Home" && $0.isVisible }) |
| 40 | print("PASS: Accounts and Sign out live in app menu; Accounts reopens closed Home") |
| 41 | NSApp.terminate(nil) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |