1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 02:54:31 +03:00
vimr/VimR/AppDelegate.swift

144 lines
3.9 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-06-03 23:13:59 +03:00
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
2016-06-03 23:13:59 +03:00
@IBOutlet var debugMenu: NSMenuItem!
2016-07-26 23:51:05 +03:00
private let disposeBag = DisposeBag()
private let changeSubject = PublishSubject<Any>()
private let changeSink: Observable<Any>
2016-07-27 00:40:20 +03:00
private let actionSubject = PublishSubject<Any>()
2016-07-26 23:51:05 +03:00
private let actionSink: Observable<Any>
private let prefStore: PrefStore
2016-07-24 21:32:07 +03:00
private let mainWindowManager: MainWindowManager
2016-07-26 23:51:05 +03:00
private let prefWindowComponent: PrefWindowComponent
2016-08-12 12:05:32 +03:00
private var quitWhenAllWindowsAreClosed = false
private var launching = true
2016-07-26 22:42:30 +03:00
override init() {
2016-07-27 00:40:20 +03:00
self.actionSink = self.actionSubject.asObservable()
2016-07-26 23:51:05 +03:00
self.changeSink = self.changeSubject.asObservable()
self.prefStore = PrefStore(source: self.actionSink)
2016-07-27 00:40:20 +03:00
self.prefWindowComponent = PrefWindowComponent(source: self.changeSink, initialData: self.prefStore.data)
self.mainWindowManager = MainWindowManager(source: self.changeSink, initialData: self.prefStore.data)
2016-07-26 22:42:30 +03:00
super.init()
self.mainWindowManager.sink
.filter { $0 is MainWindowEvent }
.map { $0 as! MainWindowEvent }
.filter { $0 == MainWindowEvent.allWindowsClosed }
.subscribeNext { [unowned self] mainWindowEvent in
if self.quitWhenAllWindowsAreClosed {
NSApp.stop(self)
}
2016-08-12 20:39:13 +03:00
}
.addDisposableTo(self.disposeBag)
2016-07-26 23:51:05 +03:00
[ self.prefStore ]
.map { $0.sink }
.toMergedObservables()
2016-07-26 23:51:05 +03:00
.subscribe(self.changeSubject)
.addDisposableTo(self.disposeBag)
2016-07-27 00:40:20 +03:00
[ self.prefWindowComponent ]
.map { $0.sink }
.toMergedObservables()
2016-07-27 00:40:20 +03:00
.subscribe(self.actionSubject)
.addDisposableTo(self.disposeBag)
2016-07-26 22:42:30 +03:00
}
}
// MARK: - NSApplicationDelegate
extension AppDelegate {
func applicationWillFinishLaunching(_: NSNotification) {
self.launching = true
}
func applicationDidFinishLaunching(_: NSNotification) {
// let testView = InputTestView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
// self.window.contentView?.addSubview(testView)
// self.window.makeFirstResponder(testView)
self.launching = false
#if DEBUG
self.debugMenu.hidden = false
#endif
}
func applicationOpenUntitledFile(sender: NSApplication) -> Bool {
self.newDocument(self)
NSLog("\(#function)")
return true
}
func applicationShouldTerminate(sender: NSApplication) -> NSApplicationTerminateReply {
if self.mainWindowManager.hasDirtyWindows() {
let alert = NSAlert()
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Discard and Quit")
alert.messageText = "There are windows with unsaved buffers!"
alert.alertStyle = .WarningAlertStyle
if alert.runModal() == NSAlertSecondButtonReturn {
self.mainWindowManager.closeAllWindowsWithoutSaving()
self.quitWhenAllWindowsAreClosed = true
return .TerminateCancel
}
return .TerminateCancel
}
return .TerminateNow
}
// For drag & dropping files on the App icon.
func application(sender: NSApplication, openFiles filenames: [String]) {
2016-08-12 12:05:32 +03:00
let urls = filenames.map { NSURL(fileURLWithPath: $0) }
self.mainWindowManager.newMainWindow(urls: urls)
sender.replyToOpenOrPrint(.Success)
}
2016-06-03 23:13:59 +03:00
}
2016-08-09 23:18:46 +03:00
// MARK: - IBActions
extension AppDelegate {
@IBAction func showPrefWindow(sender: AnyObject!) {
self.prefWindowComponent.show()
}
2016-08-09 23:18:46 +03:00
@IBAction func newDocument(sender: AnyObject!) {
self.mainWindowManager.newMainWindow()
}
2016-08-12 16:02:24 +03:00
// Invoked when no main window is open.
@IBAction func openDocument(sender: AnyObject!) {
2016-08-11 23:37:41 +03:00
let panel = NSOpenPanel()
panel.canChooseDirectories = true
panel.beginWithCompletionHandler { result in
guard result == NSFileHandlingPanelOKButton else {
return
}
2016-08-12 12:05:32 +03:00
self.mainWindowManager.newMainWindow(urls: panel.URLs)
2016-08-11 23:37:41 +03:00
}
}
2016-08-09 23:18:46 +03:00
}