1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 06:43:24 +03:00

Use stream for nvim view delegate methods

This commit is contained in:
Tae Won Ha 2017-12-17 22:51:18 +01:00
parent bdabf83b38
commit 53c1946a9b
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
5 changed files with 56 additions and 40 deletions

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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<NvimViewEvent> {
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<NvimViewEvent>()
// MARK: - Private
fileprivate var _linespacing = NvimView.defaultLinespacing
private var _linespacing = NvimView.defaultLinespacing
}

View File

@ -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)
}

View File

@ -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