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

114 lines
3.1 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: StandardFlow {
2016-08-25 00:06:39 +03:00
static private let userHomeUrl = NSURL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
2016-07-24 21:32:07 +03:00
2016-07-21 20:28:58 +03:00
private var mainWindowComponents = [String:MainWindowComponent]()
private weak var keyMainWindow: MainWindowComponent?
2016-07-24 21:32:07 +03:00
2016-09-07 21:12:18 +03:00
private let fileItemService: FileItemService
2016-07-27 00:40:20 +03:00
private var data: PrefData
2016-09-07 21:12:18 +03:00
init(source: Observable<Any>, fileItemService: FileItemService, initialData: PrefData) {
self.fileItemService = fileItemService
2016-07-27 00:40:20 +03:00
self.data = initialData
super.init(source: source)
2016-07-24 21:32:07 +03:00
}
func newMainWindow(urls urls: [NSURL] = [], cwd: NSURL = MainWindowManager.userHomeUrl) -> MainWindowComponent {
2016-08-25 00:06:39 +03:00
let mainWindowComponent = MainWindowComponent(
2016-09-07 21:12:18 +03:00
source: self.source, fileItemService: self.fileItemService, cwd: cwd, urls: urls, initialData: self.data
2016-08-25 00:06:39 +03:00
)
2016-07-21 20:28:58 +03:00
self.mainWindowComponents[mainWindowComponent.uuid] = mainWindowComponent
mainWindowComponent.sink
.filter { $0 is MainWindowAction }
.map { $0 as! MainWindowAction }
.subscribeNext { [unowned self] action in
switch action {
case let .becomeKey(mainWindow):
2016-09-07 21:12:18 +03:00
self.set(keyMainWindow: mainWindow)
2016-09-01 21:10:40 +03:00
case .openQuickly:
2016-09-07 21:12:18 +03:00
Swift.print(action)
2016-09-01 21:10:40 +03:00
self.publish(event: action)
2016-09-07 21:12:18 +03:00
case let .close(mainWindow):
self.closeMainWindow(mainWindow)
}
}
.addDisposableTo(self.disposeBag)
2016-08-25 00:06:39 +03:00
2016-08-11 23:37:41 +03:00
return mainWindowComponent
}
2016-07-21 20:28:58 +03:00
func closeMainWindow(mainWindowComponent: MainWindowComponent) {
2016-08-25 00:06:39 +03:00
if self.keyMainWindow === mainWindowComponent {
self.keyMainWindow = nil
}
self.mainWindowComponents.removeValueForKey(mainWindowComponent.uuid)
if self.mainWindowComponents.isEmpty {
self.publish(event: MainWindowEvent.allWindowsClosed)
}
}
func hasDirtyWindows() -> Bool {
2016-07-21 20:28:58 +03:00
return self.mainWindowComponents.values.reduce(false) { $0 ? true : $1.isDirty() }
}
func openInKeyMainWindow(urls urls:[NSURL] = [], cwd: NSURL = MainWindowManager.userHomeUrl) {
2016-08-25 00:06:39 +03:00
guard !self.mainWindowComponents.isEmpty else {
self.newMainWindow(urls: urls, cwd: cwd)
return
}
guard let keyMainWindow = self.keyMainWindow else {
self.newMainWindow(urls: urls, cwd: cwd)
return
}
2016-09-03 00:35:18 +03:00
keyMainWindow.cwd = cwd
2016-08-25 00:06:39 +03:00
keyMainWindow.open(urls: urls)
}
2016-09-07 21:12:18 +03:00
private func set(keyMainWindow mainWindow: MainWindowComponent?) {
2016-08-25 00:06:39 +03:00
self.keyMainWindow = mainWindow
}
func closeAllWindowsWithoutSaving() {
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindowsWithoutSaving() }
}
2016-07-27 00:40:20 +03:00
/// Assumes that no window is dirty.
func closeAllWindows() {
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindows() }
}
func hasMainWindow() -> Bool {
return !self.mainWindowComponents.isEmpty
}
override func subscription(source source: Observable<Any>) -> Disposable {
return source
2016-07-27 00:40:20 +03:00
.filter { $0 is PrefData }
.map { $0 as! PrefData }
.subscribeNext { [unowned self] prefData in
self.data = prefData
}
2016-07-27 00:40:20 +03:00
}
}