From 53c1946a9bbb7fdc5ec9f93dffd4f5f915efaecc Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Sun, 17 Dec 2017 22:51:18 +0100 Subject: [PATCH] Use stream for nvim view delegate methods --- NvimView/NvimView/NvimView+Api.swift | 2 +- NvimView/NvimView/NvimView+UiBridge.swift | 32 +++++++++-------------- NvimView/NvimView/NvimView.swift | 9 +++++-- NvimView/NvimView/NvimViewDelegate.swift | 29 ++++++++++---------- VimR/VimR/MainWindow.swift | 24 +++++++++++++++-- 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/NvimView/NvimView/NvimView+Api.swift b/NvimView/NvimView/NvimView+Api.swift index 31ec7dc9..5f43d29e 100644 --- a/NvimView/NvimView/NvimView+Api.swift +++ b/NvimView/NvimView/NvimView+Api.swift @@ -197,7 +197,7 @@ extension NvimView { public func quitNeoVimWithoutSaving() { self.nvim.command(command: "qa!", expectsReturnValue: false) - self.delegate?.neoVimStopped() + self.eventsSubject.onNext(.neoVimStopped) self.waitForNeoVimToQuit() } diff --git a/NvimView/NvimView/NvimView+UiBridge.swift b/NvimView/NvimView/NvimView+UiBridge.swift index da2b828f..ac268276 100644 --- a/NvimView/NvimView/NvimView+UiBridge.swift +++ b/NvimView/NvimView/NvimView+UiBridge.swift @@ -49,7 +49,7 @@ extension NvimView { self.markForRender(cellPosition: self.grid.position) self.grid.goto(position) - self.delegate?.cursor(to: textPosition) + self.eventsSubject.onNext(.cursor(textPosition)) } } @@ -77,7 +77,7 @@ extension NvimView { self.markForRender(region: self.grid.region) // Do not send msgs to agent -> neovim in the delegate method. It causes spinning // when you're opening a file with existing swap file. - self.delegate?.scroll() + self.eventsSubject.onNext(.scroll) } } @@ -176,7 +176,7 @@ extension NvimView { gui.async { self.bridgeLogger.debug(title) - self.delegate?.set(title: title) + self.eventsSubject.onNext(.setTitle(title)) } } @@ -187,7 +187,7 @@ extension NvimView { gui.async { self.waitForNeoVimToQuit() - self.delegate?.neoVimStopped() + self.eventsSubject.onNext(.neoVimStopped) } } @@ -200,7 +200,7 @@ extension NvimView { } if event == .TABENTER { - self.tabChanged() + self.eventsSubject.onNext(.tabChanged) } if event == .BUFWRITEPOST { @@ -221,7 +221,7 @@ extension NvimView { return } - self.delegate?.ipcBecameInvalid(reason: reason) + self.eventsSubject.onNext(.ipcBecameInvalid(reason)) self.bridgeLogger.error("Force-closing due to IPC error.") self.nvim.disconnect() @@ -246,7 +246,7 @@ extension NvimView { self.bridgeLogger.debug(cwd) self._cwd = URL(fileURLWithPath: cwd) - self.cwdChanged() + self.eventsSubject.onNext(.cwdChanged) } } public func colorSchemeChanged(_ values: [NSNumber]) { @@ -255,7 +255,7 @@ extension NvimView { self.bridgeLogger.debug(theme) self.theme = theme - self.delegate?.colorschemeChanged(to: theme) + self.eventsSubject.onNext(.colorschemeChanged(theme)) } } @@ -263,7 +263,7 @@ extension NvimView { gui.async { self.bridgeLogger.debug(dirty) - self.delegate?.set(dirtyStatus: dirty) + self.eventsSubject.onNext(.setDirtyStatus(dirty)) } } @@ -367,7 +367,7 @@ extension NvimView { return buffer } .subscribe(onSuccess: { - self.delegate?.bufferWritten($0) + self.eventsSubject.onNext(.bufferWritten($0)) if #available(OSX 10.12.2, *) { self.updateTouchBarTab() } @@ -379,23 +379,15 @@ extension NvimView { .currentBuffer() .filter { $0.apiBuffer.handle == handle } .subscribe(onSuccess: { - self.delegate?.newCurrentBuffer($0) + self.eventsSubject.onNext(.newCurrentBuffer($0)) if #available(OSX 10.12.2, *) { self.updateTouchBarTab() } }) } - fileprivate func tabChanged() { - self.delegate?.tabChanged() - } - - fileprivate func cwdChanged() { - self.delegate?.cwdChanged() - } - fileprivate func bufferListChanged() { - self.delegate?.bufferListChanged() + self.eventsSubject.onNext(.bufferListChanged) if #available(OSX 10.12.2, *) { self.updateTouchBarCurrentBuffer() } diff --git a/NvimView/NvimView/NvimView.swift b/NvimView/NvimView/NvimView.swift index da5be5e0..a0991046 100644 --- a/NvimView/NvimView/NvimView.swift +++ b/NvimView/NvimView/NvimView.swift @@ -81,7 +81,6 @@ public class NvimView: NSView, public static let maxLinespacing = CGFloat(8) public let uuid = UUID().uuidString - public weak var delegate: NvimViewDelegate? public internal(set) var mode = CursorModeShape.normal @@ -155,6 +154,10 @@ public class NvimView: NSView, public internal(set) var currentPosition = Position.beginning + public var events: Observable { + return self.eventsSubject.asObservable() + } + public init(frame rect: NSRect, config: Config) { self.drawer = TextDrawer(font: self._font) self.uiClient = UiClient(uuid: self.uuid) @@ -257,6 +260,8 @@ public class NvimView: NSView, var nvimApiScheduler = SerialDispatchQueueScheduler(qos: .userInitiated) + let eventsSubject = PublishSubject() + // MARK: - Private - fileprivate var _linespacing = NvimView.defaultLinespacing + private var _linespacing = NvimView.defaultLinespacing } diff --git a/NvimView/NvimView/NvimViewDelegate.swift b/NvimView/NvimView/NvimViewDelegate.swift index 0a750b18..b3f79632 100644 --- a/NvimView/NvimView/NvimViewDelegate.swift +++ b/NvimView/NvimView/NvimViewDelegate.swift @@ -5,23 +5,22 @@ import Cocoa -// See http://stackoverflow.com/a/24104371 for class -public protocol NvimViewDelegate: class { +public enum NvimViewEvent { - func neoVimStopped() - func set(title: String) - func set(dirtyStatus: Bool) - func cwdChanged() - func bufferListChanged() - func tabChanged() - /// Called when the current buffer changes, including when a new one is selected. - func newCurrentBuffer(_ currentBuffer: NvimView.Buffer) - func bufferWritten(_ buffer: NvimView.Buffer) + case neoVimStopped + case setTitle(String) + case setDirtyStatus(Bool) + case cwdChanged + case bufferListChanged + case tabChanged + + case newCurrentBuffer(NvimView.Buffer) + case bufferWritten(NvimView.Buffer) - func colorschemeChanged(to: NvimView.Theme) + case colorschemeChanged(NvimView.Theme) - func ipcBecameInvalid(reason: String) + case ipcBecameInvalid(String) - func scroll() - func cursor(to: Position) + case scroll + case cursor(Position) } diff --git a/VimR/VimR/MainWindow.swift b/VimR/VimR/MainWindow.swift index e239d0e7..c4b16b89 100644 --- a/VimR/VimR/MainWindow.swift +++ b/VimR/VimR/MainWindow.swift @@ -10,7 +10,6 @@ import PureLayout class MainWindow: NSObject, UiComponent, - NvimViewDelegate, NSWindowDelegate, NSUserInterfaceValidations, WorkspaceDelegate { @@ -200,7 +199,6 @@ class MainWindow: NSObject, self.addViews() self.updateNeoVimAppearance() - self.neoVimView.delegate = self self.open(urls: state.urlsToOpen) @@ -215,6 +213,28 @@ class MainWindow: NSObject, }) .disposed(by: self.disposeBag) + self.neoVimView.events + .observeOn(MainScheduler.instance) + .subscribe(onNext: { event in + switch event { + + case .neoVimStopped: self.neoVimStopped() + case .setTitle(let title): self.set(title: title) + case .setDirtyStatus(let dirty): self.set(dirtyStatus: dirty) + case .cwdChanged: self.cwdChanged() + case .bufferListChanged: self.bufferListChanged() + case .tabChanged: self.tabChanged() + case .newCurrentBuffer(let curBuf): self.newCurrentBuffer(curBuf) + case .bufferWritten(let buf): self.bufferWritten(buf) + case .colorschemeChanged(let theme): self.colorschemeChanged(to: theme) + case .ipcBecameInvalid(let reason): self.ipcBecameInvalid(reason: reason) + case .scroll: self.scroll() + case .cursor(let position): self.cursor(to: position) + + } + }) + .disposed(by: self.disposeBag) + source .observeOn(MainScheduler.instance) .subscribe(onNext: { state in