1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00
vimr/VimR/AppDelegate.swift

254 lines
7.3 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
2016-06-03 23:13:59 +03:00
import Cocoa
import RxSwift
import PureLayout
2016-10-15 11:05:11 +03:00
import Sparkle
2016-06-03 23:13:59 +03:00
/// Keep the rawValues in sync with Action in the `vimr` Python script.
2017-02-26 13:00:52 +03:00
fileprivate enum VimRUrlAction: String {
case activate = "activate"
case open = "open"
2016-08-21 15:02:20 +03:00
case newWindow = "open-in-new-window"
case separateWindows = "open-in-separate-windows"
}
fileprivate let filePrefix = "file="
fileprivate let cwdPrefix = "cwd="
2016-06-03 23:13:59 +03:00
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
2016-06-03 23:13:59 +03:00
enum Action {
case newMainWindow(urls: [URL], cwd: URL)
case openInKeyWindow(urls: [URL], cwd: URL)
2017-02-28 00:45:26 +03:00
case preferences
case quitWithoutSaving
case quit
}
2016-09-25 19:29:42 +03:00
@IBOutlet var debugMenu: NSMenuItem?
2016-10-15 11:05:11 +03:00
@IBOutlet var updater: SUUpdater?
2016-09-25 19:29:42 +03:00
2016-07-26 22:42:30 +03:00
override init() {
2017-03-01 18:59:23 +03:00
let stateDict = UserDefaults.standard.value(forKey: PrefService.compatibleVersion) as? [String: Any] ?? [:]
let initialAppState = AppState(dict: stateDict) ?? AppState.default
2017-02-28 12:43:45 +03:00
self.stateContext = Context(initialAppState)
2017-02-06 20:57:50 +03:00
2017-02-27 23:17:40 +03:00
self.openNewMainWindowOnLaunch = initialAppState.openNewMainWindowOnLaunch
self.openNewMainWindowOnReactivation = initialAppState.openNewMainWindowOnReactivation
2017-02-28 13:10:04 +03:00
self.useSnapshot = initialAppState.useSnapshotUpdate
2017-02-27 23:17:40 +03:00
2017-01-22 16:22:05 +03:00
let source = self.stateContext.stateSource
2017-03-01 18:59:23 +03:00
self.uiRoot = UiRoot(source: source, emitter: self.stateContext.actionEmitter, state: initialAppState)
2016-07-26 22:42:30 +03:00
super.init()
2016-09-01 21:10:40 +03:00
source
2017-02-28 13:10:04 +03:00
.observeOn(MainScheduler.instance)
.subscribe(onNext: { appState in
self.hasMainWindows = !appState.mainWindows.isEmpty
self.hasDirtyWindows = appState.mainWindows.values.reduce(false) { $1.isDirty ? true : $0 }
2017-02-28 00:45:26 +03:00
self.openNewMainWindowOnLaunch = appState.openNewMainWindowOnLaunch
self.openNewMainWindowOnReactivation = appState.openNewMainWindowOnReactivation
2017-02-28 13:10:04 +03:00
if self.useSnapshot != appState.useSnapshotUpdate {
self.useSnapshot = appState.useSnapshotUpdate
2017-02-28 13:10:04 +03:00
self.setSparkleUrl(self.useSnapshot)
}
})
.addDisposableTo(self.disposeBag)
2016-07-26 22:42:30 +03:00
}
2016-10-15 11:05:11 +03:00
2017-02-28 12:43:45 +03:00
fileprivate let stateContext: Context
fileprivate let uiRoot: UiRoot
fileprivate var hasDirtyWindows = false
fileprivate var hasMainWindows = false
2017-02-28 13:10:04 +03:00
fileprivate var openNewMainWindowOnLaunch: Bool
fileprivate var openNewMainWindowOnReactivation: Bool
fileprivate var useSnapshot: Bool
2017-02-27 23:17:40 +03:00
fileprivate let disposeBag = DisposeBag()
fileprivate var quitWhenAllWindowsAreClosed = false
fileprivate var launching = true
fileprivate func setSparkleUrl(_ snapshot: Bool) {
if snapshot {
self.updater?.feedURL = URL(
string: "https://raw.githubusercontent.com/qvacua/vimr/develop/appcast_snapshot.xml"
)
} else {
self.updater?.feedURL = URL(
string: "https://raw.githubusercontent.com/qvacua/vimr/master/appcast.xml"
)
}
}
}
// MARK: - NSApplicationDelegate
extension AppDelegate {
2016-09-25 18:50:33 +03:00
func applicationWillFinishLaunching(_: Notification) {
self.launching = true
2016-09-25 18:50:33 +03:00
let appleEventManager = NSAppleEventManager.shared()
appleEventManager.setEventHandler(self,
2016-09-25 19:29:42 +03:00
andSelector: #selector(AppDelegate.handle(getUrlEvent:replyEvent:)),
forEventClass: UInt32(kInternetEventClass),
andEventID: UInt32(kAEGetURL))
}
2016-09-25 18:50:33 +03:00
func applicationDidFinishLaunching(_: Notification) {
self.launching = false
#if DEBUG
self.debugMenu?.isHidden = false
#endif
}
2016-09-25 18:50:33 +03:00
func applicationOpenUntitledFile(_ sender: NSApplication) -> Bool {
2017-02-27 23:17:40 +03:00
if self.launching {
if self.openNewMainWindowOnLaunch {
self.newDocument(self)
return true
}
} else {
if self.openNewMainWindowOnReactivation {
self.newDocument(self)
return true
}
}
return false
}
2016-09-25 18:50:33 +03:00
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplicationTerminateReply {
if self.hasDirtyWindows {
let alert = NSAlert()
alert.addButton(withTitle: "Cancel")
alert.addButton(withTitle: "Discard and Quit")
alert.messageText = "There are windows with unsaved buffers!"
alert.alertStyle = .warning
if alert.runModal() == NSAlertSecondButtonReturn {
self.quitWhenAllWindowsAreClosed = true
self.stateContext.actionEmitter.emit(AppDelegate.Action.quitWithoutSaving)
}
return .terminateCancel
}
if self.hasMainWindows {
self.quitWhenAllWindowsAreClosed = true
self.stateContext.actionEmitter.emit(AppDelegate.Action.quit)
return .terminateCancel
}
// There are no open main window, then just quit.
2016-09-25 18:50:33 +03:00
return .terminateNow
}
// For drag & dropping files on the App icon.
2016-09-25 18:50:33 +03:00
func application(_ sender: NSApplication, openFiles filenames: [String]) {
let urls = filenames.map { URL(fileURLWithPath: $0) }
self.stateContext.actionEmitter.emit(Action.newMainWindow(urls: urls, cwd: FileUtils.userHomeUrl))
2016-09-25 19:29:42 +03:00
2016-09-25 18:50:33 +03:00
sender.reply(toOpenOrPrint: .success)
}
2016-06-03 23:13:59 +03:00
}
2016-08-09 23:18:46 +03:00
2016-08-20 23:35:31 +03:00
// MARK: - AppleScript
extension AppDelegate {
2016-09-25 19:29:42 +03:00
func handle(getUrlEvent event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
2016-09-25 18:50:33 +03:00
guard let urlString = event.paramDescriptor(forKeyword: UInt32(keyDirectObject))?.stringValue else {
return
}
2016-09-25 18:50:33 +03:00
guard let url = URL(string: urlString) else {
2016-08-20 23:35:31 +03:00
return
}
guard url.scheme == "vimr" else {
2016-08-20 23:35:31 +03:00
return
}
guard let rawAction = url.host else {
return
}
guard let action = VimRUrlAction(rawValue: rawAction) else {
return
}
2016-09-25 18:50:33 +03:00
let queryParams = url.query?.components(separatedBy: "&")
2016-08-25 00:06:39 +03:00
let urls = queryParams?
.filter { $0.hasPrefix(filePrefix) }
.flatMap { $0.without(prefix: filePrefix).removingPercentEncoding }
.map { URL(fileURLWithPath: $0) } ?? []
2016-08-25 00:06:39 +03:00
let cwd = queryParams?
.filter { $0.hasPrefix(cwdPrefix) }
.flatMap { $0.without(prefix: cwdPrefix).removingPercentEncoding }
.map { URL(fileURLWithPath: $0) }
.first ?? FileUtils.userHomeUrl
2016-08-21 15:02:20 +03:00
switch action {
2016-08-25 00:06:39 +03:00
case .activate, .newWindow:
self.stateContext.actionEmitter.emit(Action.newMainWindow(urls: urls, cwd: cwd))
2016-08-25 00:06:39 +03:00
case .open:
self.stateContext.actionEmitter.emit(Action.openInKeyWindow(urls: urls, cwd: cwd))
2016-08-21 15:02:20 +03:00
case .separateWindows:
urls.forEach { self.stateContext.actionEmitter.emit(Action.newMainWindow(urls: [$0], cwd: cwd)) }
2016-08-21 15:02:20 +03:00
}
2016-08-20 23:35:31 +03:00
}
}
2016-08-09 23:18:46 +03:00
// MARK: - IBActions
extension AppDelegate {
@IBAction func newDocument(_ sender: Any?) {
self.stateContext.actionEmitter.emit(Action.newMainWindow(urls: [], cwd: FileUtils.userHomeUrl))
}
@IBAction func openInNewWindow(_ sender: Any?) {
self.openDocument(sender)
}
@IBAction func showPrefWindow(_ sender: Any?) {
2017-02-28 00:45:26 +03:00
self.stateContext.actionEmitter.emit(Action.preferences)
}
2016-08-12 16:02:24 +03:00
// Invoked when no main window is open.
@IBAction func openDocument(_: Any?) {
2016-08-11 23:37:41 +03:00
let panel = NSOpenPanel()
panel.canChooseDirectories = true
panel.allowsMultipleSelection = true
2016-09-25 18:50:33 +03:00
panel.begin { result in
2016-08-11 23:37:41 +03:00
guard result == NSFileHandlingPanelOKButton else {
return
}
let urls = panel.urls
let commonParentUrl = FileUtils.commonParent(of: urls)
self.stateContext.actionEmitter.emit(Action.newMainWindow(urls: urls, cwd: commonParentUrl))
2016-08-11 23:37:41 +03:00
}
}
2016-08-09 23:18:46 +03:00
}