From 379068de1348cfb77bec71f960330dc2bcdbdb55 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Thu, 9 Feb 2017 00:31:49 +0100 Subject: [PATCH] Use marked positions to distinguish the cases to set the position --- VimR/Context.swift | 11 +++++++---- VimR/MainWindowTransformer.swift | 2 +- VimR/PreviewTool.swift | 19 +++++++++++-------- VimR/PreviewToolTransformer.swift | 31 ++++++++++++++++++++++++++++++- VimR/States.swift | 18 +++++++++++++----- 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/VimR/Context.swift b/VimR/Context.swift index 14dcf9ba..78c32413 100644 --- a/VimR/Context.swift +++ b/VimR/Context.swift @@ -12,14 +12,17 @@ class StateContext { let actionEmitter = Emitter() init(_ initialState: AppState) { - self.appState = initialState - self.stateSource = self.stateSubject.asObservable() - let actionSource = self.actionEmitter.observable - self.appDelegateTransformer = AppDelegateTransformer(baseServerUrl: initialState.baseServerUrl) self.previewTransformer = PreviewTransformer(baseServerUrl: initialState.baseServerUrl) self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0) + self.appState = initialState + + self.stateSource = self.stateSubject.asObservable() + let actionSource = self.actionEmitter.observable/*.do(onNext: { _ in + self.appState.mainWindows.keys.forEach { self.appState.mainWindows[$0]?.resetInstantStates() } + })*/ + Observable .of( actionSource diff --git a/VimR/MainWindowTransformer.swift b/VimR/MainWindowTransformer.swift index f3fc850b..cd75f78b 100644 --- a/VimR/MainWindowTransformer.swift +++ b/VimR/MainWindowTransformer.swift @@ -31,7 +31,7 @@ class MainWindowTransformer: Transformer { state.currentBuffer = buffer case let .scroll(to: position), let .setCursor(to: position): - state.cursorPosition = position + state.preview.editorPosition = Marked(position) case .close: state.isClosed = true diff --git a/VimR/PreviewTool.swift b/VimR/PreviewTool.swift index 36a50948..fbda7654 100644 --- a/VimR/PreviewTool.swift +++ b/VimR/PreviewTool.swift @@ -80,15 +80,18 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate { source .observeOn(MainScheduler.instance) .subscribe(onNext: { [unowned self] state in - self.currentEditorPosition = state.cursorPosition self.automaticForwardMenuItem.boolState = state.previewTool.isForwardSearchAutomatically self.automaticReverseMenuItem.boolState = state.previewTool.isReverseSearchAutomatically self.refreshOnWriteMenuItem.boolState = state.previewTool.isRefreshOnWrite - if state.previewTool.isForwardSearchAutomatically { - self.forwardSearch(position: state.cursorPosition) + if state.previewTool.isForwardSearchAutomatically + && state.preview.editorPosition.mark != self.editorPosition.mark + { + self.forwardSearch(position: state.preview.editorPosition.payload) } + self.editorPosition = state.preview.editorPosition + guard state.preview.updateDate > self.lastUpdateDate else { return } guard let serverUrl = state.preview.server else { return } @@ -100,7 +103,7 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate { self.webviewMessageHandler.source .throttle(0.75, latest: true, scheduler: self.scheduler) .subscribe(onNext: { [unowned self] position in - self.currentPreviewPosition = position + self.previewPosition = position self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: position))) }) .addDisposableTo(self.disposeBag) @@ -129,8 +132,8 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate { fileprivate var isOpen = false fileprivate var lastUpdateDate = Date.distantPast - fileprivate var currentEditorPosition = Position(row: 1, column: 1) - fileprivate var currentPreviewPosition = Position(row: 1, column: 1) + fileprivate var editorPosition = Marked(Position(row: 1, column: 1)) + fileprivate var previewPosition = Position(row: 1, column: 1) fileprivate let userContentController = WKUserContentController() fileprivate let webviewMessageHandler = WebviewMessageHandler() @@ -160,11 +163,11 @@ extension PreviewTool { } func forwardSearchAction(_: Any?) { - self.forwardSearch(position: self.currentEditorPosition) + self.forwardSearch(position: self.editorPosition.payload) } func reverseSearchAction(_: Any?) { - self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.currentPreviewPosition))) + self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.previewPosition))) } func automaticForwardSearchAction(_ sender: NSMenuItem) { diff --git a/VimR/PreviewToolTransformer.swift b/VimR/PreviewToolTransformer.swift index 1f1abba0..0ce78ccb 100644 --- a/VimR/PreviewToolTransformer.swift +++ b/VimR/PreviewToolTransformer.swift @@ -13,6 +13,35 @@ class PreviewToolTransformer: Transformer { typealias Pair = StateActionPair, PreviewTool.Action> func transform(_ source: Observable) -> Observable { - return source + return source.map { pair in + var state = pair.state.payload + + switch pair.action { + + case .refreshNow: + return pair + + case let .reverseSearch(to:position): + return pair + + case .forwardSearch: + return pair + + case let .scroll(to:position): + state.preview.previewPosition = Marked(position) + + case let .setAutomaticReverseSearch(to:value): + return pair + + case let .setAutomaticForwardSearch(to:value): + return pair + + case let .setRefreshOnWrite(to:value): + return pair + + } + + return StateActionPair(state: UuidState(uuid: state.uuid, state: state), action: pair.action) + } } } diff --git a/VimR/States.swift b/VimR/States.swift index 39ca68e5..1f14d373 100644 --- a/VimR/States.swift +++ b/VimR/States.swift @@ -71,6 +71,16 @@ class UuidState: CustomStringConvertible { } } +class Marked { + + let mark = Token() + let payload: T + + init(_ payload: T) { + self.payload = payload + } +} + protocol Morpher { associatedtype In @@ -132,21 +142,20 @@ struct PreviewState { var updateDate = Date.distantPast - var scrollPosition = Position(row: 1, column: 1) + var editorPosition = Marked(Position(row: 1, column: 1)) + var previewPosition = Marked(Position(row: 1, column: 1)) init(status: Status = .none, buffer: URL? = nil, html: URL? = nil, server: URL? = nil, - updateDate: Date = Date.distantPast, - scrollPosition: Position = Position(row: 1, column: 1)) + updateDate: Date = Date.distantPast) { self.status = status self.buffer = buffer self.html = html self.server = server self.updateDate = updateDate - self.scrollPosition = scrollPosition } } @@ -171,7 +180,6 @@ extension MainWindow { var currentBuffer: NeoVimBuffer? var buffers = [NeoVimBuffer]() var cwd = FileUtils.userHomeUrl - var cursorPosition = Position(row: 1, column: 1) var isDirty = false