import RastrilloApple import SheetsKit import SwiftUI /// Each window owns its navigation and model lifetime. Switching an account in /// one window must never discard another window's unconfirmed edits. struct SheetsAccountWindow: View { @StateObject private var connection = NativeConnection(defaultServer: "https://sheets.oficina.works") #if os(macOS) @Bindable var windows: SheetsWindows @Environment(\.openWindow) private var openWindow #endif var body: some View { Group { if let client = connection.client { #if os(macOS) if let account = connection.activeAccountID { SheetsWorkspace(client: client, accounts: connection.showAccounts, signOut: { guard connection.activeAccountID == account, windows.canSignOut(account) else { return } windows.entries.removeAll { $0.account == account } connection.signOut() }, canSignOut: windows.canSignOut(account), createRequested: $windows.createRequested, accountsRequested: $windows.accountsRequested, openDocument: { document, title in guard connection.activeAccountID == account else { return } let id = windows.reserve(account: account, document: document, title: title, client: client) openWindow(id: "document", value: id) }, updateRecents: { documents in guard connection.activeAccountID == account else { return } windows.updateRecents(documents, account: account, client: client) }) .id(account) } #else SheetsWorkspace(client: client, accounts: connection.showAccounts, signOut: connection.signOut) .id(connection.activeAccountID) #endif } else { NativeAccountsView("Oficina Sheets", systemImage: "tablecells", connection: connection) } }.onOpenURL(perform: connection.handleOpenURL) #if os(macOS) .onChange(of: windows.accountsRequested, initial: true) { _, requested in if requested && connection.client == nil { windows.accountsRequested = false } } .focusedSceneValue(\.sheetsWindowAccount, connection.activeAccountID) .onChange(of: connection.activeAccountID, initial: true) { _, value in windows.activeAccount = value } #endif .tint(.green) #if os(macOS) .frame(minWidth: 760, minHeight: 600) #endif } }