1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-28 08:13:17 +03:00
vimr/VimR/MainWindowManager.swift

69 lines
1.8 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
2016-07-21 20:28:58 +03:00
import RxSwift
enum MainWindowEvent {
case allWindowsClosed
}
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()
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-21 20:28:58 +03:00
func closeMainWindow(mainWindowComponent: MainWindowComponent) {
self.mainWindowComponents.removeValueForKey(mainWindowComponent.uuid)
if self.mainWindowComponents.isEmpty {
self.subject.onNext(MainWindowEvent.allWindowsClosed)
}
}
func hasDirtyWindows() -> Bool {
2016-07-21 20:28:58 +03:00
return self.mainWindowComponents.values.reduce(false) { $0 ? true : $1.isDirty() }
}
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)
}
}