1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-22 13:11:55 +03:00
vimr/VimR/MainWindowController.swift

93 lines
2.1 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import PureLayout
2016-07-21 20:28:58 +03:00
import RxSwift
class MainWindowComponent: NSObject, NSWindowDelegate, NeoVimViewDelegate, Component {
private let source: Observable<Any>
private let disposeBag = DisposeBag()
private let subject = PublishSubject<Any>()
var sink: Observable<Any> {
return self.subject.asObservable()
}
private weak var mainWindowManager: MainWindowManager?
private let windowController = NSWindowController(windowNibName: "MainWindow")
private let window: NSWindow
var uuid: String {
return self.neoVimView.uuid
}
2016-07-21 20:28:58 +03:00
private let neoVimView = NeoVimView(forAutoLayout: ())
2016-07-21 20:28:58 +03:00
init(source: Observable<Any>, manager: MainWindowManager) {
self.source = source
self.mainWindowManager = manager
2016-07-21 20:28:58 +03:00
self.window = self.windowController.window!
super.init()
self.window.delegate = self
self.neoVimView.delegate = self
self.addViews()
2016-07-21 20:28:58 +03:00
self.window.makeFirstResponder(self.neoVimView)
self.windowController.showWindow(self)
}
func isDirty() -> Bool {
return self.neoVimView.hasDirtyDocs()
}
2016-07-21 20:28:58 +03:00
private func addViews() {
self.window.contentView?.addSubview(self.neoVimView)
self.neoVimView.autoPinEdgesToSuperviewEdges()
}
}
// MARK: - NeoVimViewDelegate
extension MainWindowComponent {
func setNeoVimTitle(title: String) {
NSLog("\(#function): \(title)")
}
func neoVimStopped() {
self.windowController.close()
}
}
// MARK: - NSWindowDelegate
extension MainWindowComponent {
func windowWillClose(notification: NSNotification) {
self.mainWindowManager?.closeMainWindow(self)
}
func windowShouldClose(sender: AnyObject) -> Bool {
2016-07-21 20:28:58 +03:00
guard self.isDirty() else {
return true
}
let alert = NSAlert()
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Discard and Close")
alert.messageText = "There are unsaved buffers!"
alert.alertStyle = .WarningAlertStyle
2016-07-21 20:28:58 +03:00
alert.beginSheetModalForWindow(self.window) { response in
if response == NSAlertSecondButtonReturn {
2016-07-21 20:28:58 +03:00
self.windowController.close()
}
}
return false
}
2016-07-21 20:28:58 +03:00
}