1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-29 16:56:40 +03:00
vimr/MacNeovim/AppDelegate.swift
2017-06-11 15:48:18 +02:00

65 lines
1.6 KiB
Swift

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NeoVimWindowDelegate {
fileprivate var neoVimWindows = Set<NeoVimWindow>()
}
// MARK: - NSApplicationDelegate
extension AppDelegate {
func applicationOpenUntitledFile(_ sender: NSApplication) -> Bool {
self.newDocument(self)
return true
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplicationTerminateReply {
if self.neoVimWindows.isEmpty {
return .terminateNow
}
let isDirty = self.neoVimWindows.reduce(false) { $0 ? true : $1.window.isDocumentEdited }
guard isDirty else {
self.neoVimWindows.forEach { $0.closeNeoVimWithoutSaving() }
return .terminateNow
}
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.neoVimWindows.forEach { $0.closeNeoVimWithoutSaving() }
return .terminateNow
}
return .terminateCancel
}
}
// MARK: - NeoVimWindow.Delegate
extension AppDelegate {
func neoVimWindowDidClose(neoVimWindow: NeoVimWindow) {
self.neoVimWindows.remove(neoVimWindow)
}
}
// MARK: - IBActions
extension AppDelegate {
@IBAction func newDocument(_: Any?) {
let neoVimWindow = NeoVimWindow(delegate: self)
self.neoVimWindows.insert(neoVimWindow)
neoVimWindow.window.makeKeyAndOrderFront(self)
}
}