2016-07-10 19:47:24 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
2016-07-21 20:28:58 +03:00
|
|
|
import RxSwift
|
2016-07-10 19:47:24 +03:00
|
|
|
|
2016-08-12 19:00:05 +03:00
|
|
|
enum MainWindowEvent {
|
|
|
|
case allWindowsClosed
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
class MainWindowManager {
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-07-26 23:51:05 +03:00
|
|
|
private let source: Observable<Any>
|
2016-07-27 00:40:20 +03:00
|
|
|
private let disposeBag = DisposeBag()
|
|
|
|
|
2016-08-12 19:00:05 +03:00
|
|
|
private let subject = PublishSubject<Any>()
|
|
|
|
var sink: Observable<Any> {
|
|
|
|
return self.subject.asObservable()
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
private var mainWindowComponents = [String:MainWindowComponent]()
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-07-27 00:40:20 +03:00
|
|
|
private var data: PrefData
|
|
|
|
|
|
|
|
init(source: Observable<Any>, initialData: PrefData) {
|
2016-07-26 23:51:05 +03:00
|
|
|
self.source = source
|
2016-07-27 00:40:20 +03:00
|
|
|
self.data = initialData
|
|
|
|
|
|
|
|
self.addReactions()
|
2016-07-24 21:32:07 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:05:32 +03:00
|
|
|
func newMainWindow(urls urls: [NSURL] = []) -> MainWindowComponent {
|
|
|
|
let mainWindowComponent = MainWindowComponent(source: self.source,
|
|
|
|
manager: self,
|
|
|
|
urls: urls,
|
|
|
|
initialData: self.data)
|
2016-07-21 20:28:58 +03:00
|
|
|
self.mainWindowComponents[mainWindowComponent.uuid] = mainWindowComponent
|
2016-08-11 23:37:41 +03:00
|
|
|
return mainWindowComponent
|
2016-07-10 19:47:24 +03:00
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
func closeMainWindow(mainWindowComponent: MainWindowComponent) {
|
|
|
|
self.mainWindowComponents.removeValueForKey(mainWindowComponent.uuid)
|
2016-08-12 19:00:05 +03:00
|
|
|
|
|
|
|
if self.mainWindowComponents.isEmpty {
|
|
|
|
self.subject.onNext(MainWindowEvent.allWindowsClosed)
|
|
|
|
}
|
2016-07-10 19:47:24 +03:00
|
|
|
}
|
2016-07-18 23:44:23 +03:00
|
|
|
|
|
|
|
func hasDirtyWindows() -> Bool {
|
2016-07-21 20:28:58 +03:00
|
|
|
return self.mainWindowComponents.values.reduce(false) { $0 ? true : $1.isDirty() }
|
2016-07-18 23:44:23 +03:00
|
|
|
}
|
2016-08-12 19:00:05 +03:00
|
|
|
|
|
|
|
func closeAllWindowsWithoutSaving() {
|
|
|
|
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindowsWithoutSaving() }
|
|
|
|
}
|
2016-07-27 00:40:20 +03:00
|
|
|
|
|
|
|
private func addReactions() {
|
|
|
|
self.source
|
|
|
|
.filter { $0 is PrefData }
|
|
|
|
.map { $0 as! PrefData }
|
|
|
|
.subscribeNext { [unowned self] prefData in
|
|
|
|
self.data = prefData
|
|
|
|
}
|
|
|
|
.addDisposableTo(self.disposeBag)
|
|
|
|
}
|
2016-07-10 19:47:24 +03:00
|
|
|
}
|