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-09-03 11:17:22 +03:00
|
|
|
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]()
|
2016-09-01 21:09:00 +03:00
|
|
|
private weak var keyMainWindow: 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) {
|
|
|
|
self.data = initialData
|
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
super.init(source: source)
|
2016-07-24 21:32:07 +03:00
|
|
|
}
|
|
|
|
|
2016-09-03 11:17:22 +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-01 21:09:00 +03:00
|
|
|
source: self.source, urls: urls, initialData: self.data
|
2016-08-25 00:06:39 +03:00
|
|
|
)
|
2016-09-03 00:35:18 +03:00
|
|
|
mainWindowComponent.cwd = cwd
|
2016-07-21 20:28:58 +03:00
|
|
|
self.mainWindowComponents[mainWindowComponent.uuid] = mainWindowComponent
|
2016-09-01 21:09:00 +03:00
|
|
|
|
|
|
|
mainWindowComponent.sink
|
|
|
|
.filter { $0 is MainWindowAction }
|
|
|
|
.map { $0 as! MainWindowAction }
|
|
|
|
.subscribeNext { [unowned self] action in
|
|
|
|
switch action {
|
|
|
|
case let .becomeKey(mainWindow):
|
|
|
|
self.setKeyWindow(mainWindow)
|
2016-09-01 21:10:40 +03:00
|
|
|
case .openQuickly:
|
|
|
|
self.publish(event: action)
|
2016-09-01 21:09:00 +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-10 19:47:24 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2016-08-12 19:00:05 +03:00
|
|
|
|
2016-09-01 21:09:00 +03:00
|
|
|
self.mainWindowComponents.removeValueForKey(mainWindowComponent.uuid)
|
|
|
|
|
2016-08-12 19:00:05 +03:00
|
|
|
if self.mainWindowComponents.isEmpty {
|
2016-08-30 23:25:34 +03:00
|
|
|
self.publish(event: MainWindowEvent.allWindowsClosed)
|
2016-08-12 19:00:05 +03:00
|
|
|
}
|
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
|
|
|
|
2016-09-03 11:17:22 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setKeyWindow(mainWindow: MainWindowComponent?) {
|
|
|
|
self.keyMainWindow = mainWindow
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:00:05 +03:00
|
|
|
func closeAllWindowsWithoutSaving() {
|
|
|
|
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindowsWithoutSaving() }
|
|
|
|
}
|
2016-07-27 00:40:20 +03:00
|
|
|
|
2016-08-20 17:59:30 +03:00
|
|
|
/// Assumes that no window is dirty.
|
|
|
|
func closeAllWindows() {
|
|
|
|
self.mainWindowComponents.values.forEach { $0.closeAllNeoVimWindows() }
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasMainWindow() -> Bool {
|
|
|
|
return !self.mainWindowComponents.isEmpty
|
|
|
|
}
|
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
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-08-30 23:25:34 +03:00
|
|
|
}
|
2016-07-27 00:40:20 +03:00
|
|
|
}
|
2016-07-10 19:47:24 +03:00
|
|
|
}
|