1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-29 12:04:11 +03:00
vimr/MacNeovim/AppDelegate.swift

65 lines
1.6 KiB
Swift
Raw Normal View History

2017-04-21 14:44:25 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NeoVimWindowDelegate {
2017-04-21 14:44:25 +03:00
fileprivate var neoVimWindows = Set<NeoVimWindow>()
}
2017-04-21 14:44:25 +03:00
// MARK: - NSApplicationDelegate
extension AppDelegate {
2017-04-21 14:44:25 +03:00
func applicationOpenUntitledFile(_ sender: NSApplication) -> Bool {
self.newDocument(self)
return true
2017-04-21 14:44:25 +03:00
}
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() }
2017-06-11 16:48:18 +03:00
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() }
2017-06-11 16:48:18 +03:00
return .terminateNow
}
return .terminateCancel
}
}
// MARK: - NeoVimWindow.Delegate
extension AppDelegate {
2017-04-21 14:44:25 +03:00
func neoVimWindowDidClose(neoVimWindow: NeoVimWindow) {
self.neoVimWindows.remove(neoVimWindow)
2017-04-21 14:44:25 +03:00
}
}
2017-04-21 14:44:25 +03:00
// MARK: - IBActions
extension AppDelegate {
2017-04-21 14:44:25 +03:00
@IBAction func newDocument(_: Any?) {
let neoVimWindow = NeoVimWindow(delegate: self)
self.neoVimWindows.insert(neoVimWindow)
2017-04-21 14:44:25 +03:00
neoVimWindow.window.makeKeyAndOrderFront(self)
}
}