diff --git a/VimR/VimR/AppDelegate.swift b/VimR/VimR/AppDelegate.swift index 67ebf78c..8d997eb1 100644 --- a/VimR/VimR/AppDelegate.swift +++ b/VimR/VimR/AppDelegate.swift @@ -64,6 +64,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele self.useSnapshot = appState.useSnapshotUpdate self.setSparkleUrl(self.useSnapshot) } + + if appState.quit { + NSApp.terminate(self) + } }) .disposed(by: self.disposeBag) diff --git a/VimR/VimR/Context.swift b/VimR/VimR/Context.swift index b9032b20..f7e0e3fe 100644 --- a/VimR/VimR/Context.swift +++ b/VimR/VimR/Context.swift @@ -25,6 +25,7 @@ class Context { let previewService = PreviewService() let httpService: HttpServerService = HttpServerService(port: baseServerUrl.port!) + let uiRootReducer = UiRootReducer() // AppState Observable @@ -33,7 +34,7 @@ class Context { .reduce(by: AppDelegateReducer(baseServerUrl: baseServerUrl).reduce) .filterMapPair(), self.actionSourceForAppState() - .reduce(by: UiRootReducer().reduce) + .reduce(by: uiRootReducer.reduceMainWindow) .reduce(by: openQuicklyReducer.reduceMainWindow) .filter { $0.modified } .apply(self.prefService.applyMainWindow) @@ -43,6 +44,9 @@ class Context { .filterMapPair(), self.actionSourceForAppState() .reduce(by: openQuicklyReducer.reduceOpenQuicklyWindow) + .filterMapPair(), + self.actionSourceForAppState() + .reduce(by: uiRootReducer.reduceUiRoot) .filterMapPair() ) .merge() diff --git a/VimR/VimR/States.swift b/VimR/VimR/States.swift index c07080b1..adf5ef9d 100644 --- a/VimR/VimR/States.swift +++ b/VimR/VimR/States.swift @@ -33,6 +33,8 @@ struct AppState: SerializableState { var openQuickly = OpenQuicklyWindow.State.default + var quit = false + init() { } diff --git a/VimR/VimR/UiRoot.swift b/VimR/VimR/UiRoot.swift index 0d246675..3cf53e23 100644 --- a/VimR/VimR/UiRoot.swift +++ b/VimR/VimR/UiRoot.swift @@ -10,9 +10,15 @@ class UiRoot: UiComponent { typealias StateType = AppState + enum Action { + + case quit + } + required init(source: Observable, emitter: ActionEmitter, state: StateType) { self.source = source self.emitter = emitter + self.emit = emitter.typedEmit() self.fileMonitor = FileMonitor(source: source, emitter: emitter, state: state) self.openQuicklyWindow = OpenQuicklyWindow(source: source, emitter: emitter, state: state) @@ -50,7 +56,7 @@ class UiRoot: UiComponent { case .doNothing: return case .hide: NSApp.hide(self) - case .quit: NSApp.terminate(self) + case .quit: self.emit(.quit) } }) @@ -66,6 +72,7 @@ class UiRoot: UiComponent { private let source: Observable private let emitter: ActionEmitter + private let emit: (Action) -> Void private let disposeBag = DisposeBag() private let fileMonitor: FileMonitor diff --git a/VimR/VimR/UiRootReducer.swift b/VimR/VimR/UiRootReducer.swift index a0e2b898..94d6e8f9 100644 --- a/VimR/VimR/UiRootReducer.swift +++ b/VimR/VimR/UiRootReducer.swift @@ -7,9 +7,23 @@ import Foundation class UiRootReducer { - typealias Pair = StateActionPair> + typealias UiRootPair = StateActionPair + typealias MainWindowPair = StateActionPair> - func reduce(_ pair: Pair) -> Pair { + func reduceUiRoot(_ pair: UiRootPair) -> UiRootPair { + var appState = pair.state + + switch pair.action { + + case .quit: + appState.quit = true + + } + + return StateActionPair(state: appState, action: pair.action) + } + + func reduceMainWindow(_ pair: MainWindowPair) -> MainWindowPair { var appState = pair.state let uuid = pair.action.uuid