tokenary/Encrypted Ink/Window.swift

63 lines
1.8 KiB
Swift
Raw Normal View History

2021-06-12 19:33:24 +03:00
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Cocoa
struct Window {
2021-06-13 05:45:54 +03:00
static func showNew() -> NSWindowController {
closeAll()
let windowController = new
activate(windowController)
return windowController
}
static func activate(_ windowController: NSWindowController) {
windowController.showWindow(nil)
2021-06-19 00:31:19 +03:00
activateWindow(windowController.window)
2021-06-13 05:45:54 +03:00
}
2021-06-19 00:31:19 +03:00
static func activateWindow(_ window: NSWindow?) {
NSApp.activate(ignoringOtherApps: true)
2021-06-19 00:31:19 +03:00
window?.makeKeyAndOrderFront(nil)
}
static func closeAllAndActivateBrowser() {
closeAll()
activateBrowser()
}
static func closeAll(updateStatusBarItem: Bool = true) {
2021-06-12 19:33:24 +03:00
NSApplication.shared.windows.forEach { $0.close() }
if updateStatusBarItem {
Agent.shared.setupStatusBarItem()
}
2021-06-12 19:33:24 +03:00
}
2021-06-16 20:09:30 +03:00
static func activateBrowser() {
// TODO: support more browsers
for bundleId in ["com.apple.Safari", "com.google.Chrome"] {
2021-06-16 20:53:31 +03:00
if let browser = NSRunningApplication.runningApplications(withBundleIdentifier: bundleId).first {
2021-06-16 20:09:30 +03:00
browser.activate(options: .activateIgnoringOtherApps)
return
}
2021-06-12 19:33:24 +03:00
}
}
2021-06-13 05:45:54 +03:00
static var current: NSWindowController? {
return NSApplication.shared.windows.first?.windowController
}
static var new: NSWindowController {
return NSStoryboard.main.instantiateController(withIdentifier: "initial") as! NSWindowController
2021-06-13 05:45:54 +03:00
}
}
extension NSStoryboard {
static let main = NSStoryboard(name: "Main", bundle: nil)
}
func instantiate<ViewController: NSViewController>(_ type: ViewController.Type) -> ViewController {
return NSStoryboard.main.instantiateController(withIdentifier: String(describing: type)) as! ViewController
2021-06-12 19:33:24 +03:00
}