1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-26 07:13:24 +03:00
vimr/VimR/MainWindowManager.swift

124 lines
3.5 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 MainWindowManagerAction {
case allWindowsClosed
}
class MainWindowManager: StandardFlow {
2016-07-24 21:32:07 +03:00
2016-09-25 18:50:33 +03:00
fileprivate var mainWindowComponents = [String:MainWindowComponent]()
fileprivate weak var keyMainWindow: MainWindowComponent?
2016-07-24 21:32:07 +03:00
2016-09-25 18:50:33 +03:00
fileprivate let fileItemService: FileItemService
fileprivate var data: PrefData
2016-07-27 00:40:20 +03:00
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
}
2016-10-02 13:28:47 +03:00
func newMainWindow(urls: [URL] = [], cwd: URL = FileUtils.userHomeUrl) -> MainWindowComponent {
let mainWindowComponent = MainWindowComponent(source: self.source,
fileItemService: self.fileItemService,
cwd: cwd,
urls: urls,
initialData: self.data)
2016-07-21 20:28:58 +03:00
self.mainWindowComponents[mainWindowComponent.uuid] = mainWindowComponent
mainWindowComponent.sink
.filter { $0 is MainWindowAction }
.map { $0 as! MainWindowAction }
2016-09-25 19:10:07 +03:00
.subscribe(onNext: { [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:
self.publish(event: action)
2016-09-07 21:12:18 +03:00
case let .close(mainWindow, mainWindowPrefData):
self.close(mainWindow, prefData: mainWindowPrefData)
2016-09-27 19:02:05 +03:00
default:
break;
}
2016-09-25 19:10:07 +03:00
})
.addDisposableTo(self.disposeBag)
mainWindowComponent.show()
2016-08-11 23:37:41 +03:00
return mainWindowComponent
}
func close(_ mainWindowComponent: MainWindowComponent, prefData: MainWindowPrefData) {
// Save the tools settings of the last closed main window.
// TODO: Think about a better time to save this.
self.publish(event: prefData)
2016-08-25 00:06:39 +03:00
if self.keyMainWindow === mainWindowComponent {
self.keyMainWindow = nil
}
2016-09-25 18:50:33 +03:00
self.mainWindowComponents.removeValue(forKey: mainWindowComponent.uuid)
if self.mainWindowComponents.isEmpty {
self.publish(event: MainWindowManagerAction.allWindowsClosed)
}
}
func hasDirtyWindows() -> Bool {
2016-07-21 20:28:58 +03:00
return self.mainWindowComponents.values.reduce(false) { $0 ? true : $1.isDirty() }
}
2016-10-02 13:28:47 +03:00
func openInKeyMainWindow(urls:[URL] = [], cwd: URL = FileUtils.userHomeUrl) {
if self.mainWindowComponents.isEmpty {
2016-09-25 19:10:07 +03:00
_ = self.newMainWindow(urls: urls, cwd: cwd)
2016-08-25 00:06:39 +03:00
return
}
2016-08-25 00:06:39 +03:00
guard let keyMainWindow = self.keyMainWindow else {
2016-09-25 19:10:07 +03:00
_ = self.newMainWindow(urls: urls, cwd: cwd)
2016-08-25 00:06:39 +03:00
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-25 18:50:33 +03:00
fileprivate 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
}
2016-09-25 18:50:33 +03:00
override func subscription(source: Observable<Any>) -> Disposable {
return source
2016-07-27 00:40:20 +03:00
.filter { $0 is PrefData }
.map { $0 as! PrefData }
2016-09-25 19:10:07 +03:00
.subscribe(onNext: { [unowned self] prefData in
2016-07-27 00:40:20 +03:00
self.data = prefData
2016-09-25 19:10:07 +03:00
})
2016-07-27 00:40:20 +03:00
}
}