// // AppDelegate.swift // MongoView // // Created by Shadowfacts on 1/9/20. // Copyright © 2020 Shadowfacts. All rights reserved. // import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { var windowControllers = [DatabaseWindowController]() var serverConnectWindowController: ServerConnectWindowController? func applicationDidFinishLaunching(_ aNotification: Notification) { let wc = DatabaseWindowController() windowControllers.append(wc) wc.showWindow(nil) } func applicationWillTerminate(_ aNotification: Notification) { } func newWindow(mongoController: MongoController?, collection: DatabaseCollection? = nil, addToTabGroup: Bool = true) { let wc = DatabaseWindowController() wc.mongoController = mongoController wc.initialCollection = collection windowControllers.append(wc) if addToTabGroup, let tabGroup = windowControllers.first(where: { $0.window!.isKeyWindow })?.window?.tabGroup { tabGroup.addWindow(wc.window!) tabGroup.selectedWindow = wc.window! } else { wc.showWindow(nil) } } @IBAction func newTab(_ sender: Any) { let mongoController = windowControllers.first { $0.window!.isKeyWindow }?.mongoController newWindow(mongoController: mongoController) } @IBAction func connectToServer(_ sender: Any) { guard serverConnectWindowController == nil else { return } let wc = ServerConnectWindowController() wc.delegate = self serverConnectWindowController = wc wc.showWindow(nil) } } extension AppDelegate: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { if menuItem.action == #selector(connectToServer(_:)) { return serverConnectWindowController == nil } return true } } extension AppDelegate: ServerConnectViewControllerDelegate { func connectToServer(mongoController: MongoController) { serverConnectWindowController!.close() serverConnectWindowController = nil newWindow(mongoController: mongoController, addToTabGroup: false) } }