2016-07-10 19:47:24 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2016-07-10 19:47:24 +03:00
|
|
|
|
|
|
|
var uuid: String {
|
|
|
|
return self.neoVimView.uuid
|
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
private let neoVimView = NeoVimView(forAutoLayout: ())
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
init(source: Observable<Any>, manager: MainWindowManager) {
|
|
|
|
self.source = source
|
2016-07-10 19:47:24 +03:00
|
|
|
self.mainWindowManager = manager
|
2016-07-21 20:28:58 +03:00
|
|
|
self.window = self.windowController.window!
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
self.window.delegate = self
|
2016-07-17 15:41:53 +03:00
|
|
|
self.neoVimView.delegate = self
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
self.addViews()
|
2016-07-24 21:32:07 +03:00
|
|
|
self.addReactions()
|
2016-07-11 18:56:55 +03:00
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
self.window.makeFirstResponder(self.neoVimView)
|
|
|
|
self.windowController.showWindow(self)
|
2016-07-10 19:47:24 +03:00
|
|
|
}
|
2016-07-11 18:56:55 +03:00
|
|
|
|
2016-07-18 23:44:23 +03:00
|
|
|
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()
|
|
|
|
}
|
2016-07-24 21:32:07 +03:00
|
|
|
|
|
|
|
private func addReactions() {
|
|
|
|
self.source
|
|
|
|
.filter { $0 is PrefData }
|
|
|
|
.map { ($0 as! PrefData).appearance.editorFont }
|
|
|
|
.subscribeNext { [unowned self] font in
|
|
|
|
self.neoVimView.setFont(font)
|
|
|
|
}
|
|
|
|
.addDisposableTo(self.disposeBag)
|
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NeoVimViewDelegate
|
|
|
|
extension MainWindowComponent {
|
|
|
|
|
|
|
|
func setNeoVimTitle(title: String) {
|
|
|
|
NSLog("\(#function): \(title)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func neoVimStopped() {
|
|
|
|
self.windowController.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSWindowDelegate
|
|
|
|
extension MainWindowComponent {
|
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
func windowWillClose(notification: NSNotification) {
|
|
|
|
self.mainWindowManager?.closeMainWindow(self)
|
|
|
|
}
|
2016-07-18 21:16:56 +03:00
|
|
|
|
|
|
|
func windowShouldClose(sender: AnyObject) -> Bool {
|
2016-07-21 20:28:58 +03:00
|
|
|
guard self.isDirty() else {
|
2016-07-18 21:16:56 +03:00
|
|
|
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
|
2016-07-18 21:16:56 +03:00
|
|
|
if response == NSAlertSecondButtonReturn {
|
2016-07-21 20:28:58 +03:00
|
|
|
self.windowController.close()
|
2016-07-18 21:16:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2016-07-18 21:16:56 +03:00
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|