secretive/Secretive/AppDelegate.swift

101 lines
3.6 KiB
Swift
Raw Normal View History

2020-02-19 03:36:41 +00:00
import Cocoa
import SwiftUI
2020-02-19 04:52:00 +00:00
import SecretKit
2020-02-19 03:36:41 +00:00
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
2020-03-16 01:30:45 +00:00
@IBOutlet var newMenuItem: NSMenuItem!
2020-03-04 07:14:38 +00:00
@IBOutlet var toolbar: NSToolbar!
2020-03-09 03:03:40 +00:00
let storeList: SecretStoreList = {
let list = SecretStoreList()
list.add(store: SecureEnclave.Store())
list.add(store: SmartCard.Store())
return list
2020-03-07 23:04:36 +00:00
}()
2020-03-15 08:11:16 +00:00
let updater = Updater()
let agentStatusChecker = AgentStatusChecker()
2020-02-19 03:36:41 +00:00
func applicationDidFinishLaunching(_ aNotification: Notification) {
let contentView = ContentView(storeList: storeList, updater: updater, agentStatusChecker: agentStatusChecker, runSetupBlock: { self.runSetup(sender: nil) })
2020-02-19 04:52:00 +00:00
// Create the window and set the content view.
2020-02-19 03:36:41 +00:00
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
2020-03-04 07:14:38 +00:00
window.titleVisibility = .hidden
window.toolbar = toolbar
2020-03-15 23:03:20 +00:00
window.isReleasedWhenClosed = false
2020-03-09 03:03:40 +00:00
if storeList.modifiableStore?.isAvailable ?? false {
2020-03-07 23:42:40 +00:00
let plus = NSTitlebarAccessoryViewController()
plus.view = NSButton(image: NSImage(named: NSImage.addTemplateName)!, target: self, action: #selector(add(sender:)))
plus.layoutAttribute = .right
window.addTitlebarAccessoryViewController(plus)
2020-03-16 01:30:45 +00:00
newMenuItem.isEnabled = true
2020-03-07 23:42:40 +00:00
}
2020-03-04 07:14:38 +00:00
runSetupIfNeeded()
2020-02-19 03:36:41 +00:00
}
func applicationDidBecomeActive(_ notification: Notification) {
agentStatusChecker.check()
}
2020-03-15 23:03:20 +00:00
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
guard !flag else { return false }
window.makeKeyAndOrderFront(self)
return true
}
2020-03-04 07:14:38 +00:00
@IBAction func add(sender: AnyObject?) {
var addWindow: NSWindow!
2020-03-09 03:03:40 +00:00
let addView = CreateSecretView(store: storeList.modifiableStore!) {
2020-03-04 07:14:38 +00:00
self.window.endSheet(addWindow)
}
addWindow = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
addWindow.contentView = NSHostingView(rootView: addView)
window.beginSheet(addWindow, completionHandler: nil)
}
@IBAction func runSetup(sender: AnyObject?) {
let setupWindow = NSWindow(
2020-03-22 00:52:51 +00:00
contentRect: NSRect(x: 0, y: 0, width: 00, height: 00),
2020-03-04 07:14:38 +00:00
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
let setupView = SetupView() { success in
self.window.endSheet(setupWindow)
2020-03-15 22:57:11 +00:00
self.agentStatusChecker.check()
2020-03-04 07:14:38 +00:00
}
setupWindow.contentView = NSHostingView(rootView: setupView)
2020-03-22 00:52:51 +00:00
setupWindow.setContentSize(setupWindow.contentView!.fittingSize)
2020-03-04 07:14:38 +00:00
window.beginSheet(setupWindow, completionHandler: nil)
2020-02-19 03:36:41 +00:00
}
2020-03-04 07:14:38 +00:00
}
extension AppDelegate {
func runSetupIfNeeded() {
if !UserDefaults.standard.bool(forKey: Constants.defaultsHasRunSetup) {
UserDefaults.standard.set(true, forKey: Constants.defaultsHasRunSetup)
runSetup(sender: nil)
}
}
2020-02-19 03:36:41 +00:00
}
2020-03-04 07:14:38 +00:00
extension AppDelegate {
enum Constants {
static let defaultsHasRunSetup = "defaultsHasRunSetup"
}
}