1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +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() { public func quitNeoVimWithoutSaving() {
self.nvim.command(command: "qa!", expectsReturnValue: false) self.nvim.command(command: "qa!", expectsReturnValue: false)
self.delegate?.neoVimStopped() self.eventsSubject.onNext(.neoVimStopped)
self.waitForNeoVimToQuit() self.waitForNeoVimToQuit()
} }

View File

@ -49,7 +49,7 @@ extension NvimView {
self.markForRender(cellPosition: self.grid.position) self.markForRender(cellPosition: self.grid.position)
self.grid.goto(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) self.markForRender(region: self.grid.region)
// Do not send msgs to agent -> neovim in the delegate method. It causes spinning // Do not send msgs to agent -> neovim in the delegate method. It causes spinning
// when you're opening a file with existing swap file. // 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 { gui.async {
self.bridgeLogger.debug(title) self.bridgeLogger.debug(title)
self.delegate?.set(title: title) self.eventsSubject.onNext(.setTitle(title))
} }
} }
@ -187,7 +187,7 @@ extension NvimView {
gui.async { gui.async {
self.waitForNeoVimToQuit() self.waitForNeoVimToQuit()
self.delegate?.neoVimStopped() self.eventsSubject.onNext(.neoVimStopped)
} }
} }
@ -200,7 +200,7 @@ extension NvimView {
} }
if event == .TABENTER { if event == .TABENTER {
self.tabChanged() self.eventsSubject.onNext(.tabChanged)
} }
if event == .BUFWRITEPOST { if event == .BUFWRITEPOST {
@ -221,7 +221,7 @@ extension NvimView {
return return
} }
self.delegate?.ipcBecameInvalid(reason: reason) self.eventsSubject.onNext(.ipcBecameInvalid(reason))
self.bridgeLogger.error("Force-closing due to IPC error.") self.bridgeLogger.error("Force-closing due to IPC error.")
self.nvim.disconnect() self.nvim.disconnect()
@ -246,7 +246,7 @@ extension NvimView {
self.bridgeLogger.debug(cwd) self.bridgeLogger.debug(cwd)
self._cwd = URL(fileURLWithPath: cwd) self._cwd = URL(fileURLWithPath: cwd)
self.cwdChanged() self.eventsSubject.onNext(.cwdChanged)
} }
} }
public func colorSchemeChanged(_ values: [NSNumber]) { public func colorSchemeChanged(_ values: [NSNumber]) {
@ -255,7 +255,7 @@ extension NvimView {
self.bridgeLogger.debug(theme) self.bridgeLogger.debug(theme)
self.theme = theme self.theme = theme
self.delegate?.colorschemeChanged(to: theme) self.eventsSubject.onNext(.colorschemeChanged(theme))
} }
} }
@ -263,7 +263,7 @@ extension NvimView {
gui.async { gui.async {
self.bridgeLogger.debug(dirty) self.bridgeLogger.debug(dirty)
self.delegate?.set(dirtyStatus: dirty) self.eventsSubject.onNext(.setDirtyStatus(dirty))
} }
} }
@ -367,7 +367,7 @@ extension NvimView {
return buffer return buffer
} }
.subscribe(onSuccess: { .subscribe(onSuccess: {
self.delegate?.bufferWritten($0) self.eventsSubject.onNext(.bufferWritten($0))
if #available(OSX 10.12.2, *) { if #available(OSX 10.12.2, *) {
self.updateTouchBarTab() self.updateTouchBarTab()
} }
@ -379,23 +379,15 @@ extension NvimView {
.currentBuffer() .currentBuffer()
.filter { $0.apiBuffer.handle == handle } .filter { $0.apiBuffer.handle == handle }
.subscribe(onSuccess: { .subscribe(onSuccess: {
self.delegate?.newCurrentBuffer($0) self.eventsSubject.onNext(.newCurrentBuffer($0))
if #available(OSX 10.12.2, *) { if #available(OSX 10.12.2, *) {
self.updateTouchBarTab() self.updateTouchBarTab()
} }
}) })
} }
fileprivate func tabChanged() {
self.delegate?.tabChanged()
}
fileprivate func cwdChanged() {
self.delegate?.cwdChanged()
}
fileprivate func bufferListChanged() { fileprivate func bufferListChanged() {
self.delegate?.bufferListChanged() self.eventsSubject.onNext(.bufferListChanged)
if #available(OSX 10.12.2, *) { if #available(OSX 10.12.2, *) {
self.updateTouchBarCurrentBuffer() self.updateTouchBarCurrentBuffer()
} }

View File

@ -81,7 +81,6 @@ public class NvimView: NSView,
public static let maxLinespacing = CGFloat(8) public static let maxLinespacing = CGFloat(8)
public let uuid = UUID().uuidString public let uuid = UUID().uuidString
public weak var delegate: NvimViewDelegate?
public internal(set) var mode = CursorModeShape.normal public internal(set) var mode = CursorModeShape.normal
@ -155,6 +154,10 @@ public class NvimView: NSView,
public internal(set) var currentPosition = Position.beginning public internal(set) var currentPosition = Position.beginning
public var events: Observable<NvimViewEvent> {
return self.eventsSubject.asObservable()
}
public init(frame rect: NSRect, config: Config) { public init(frame rect: NSRect, config: Config) {
self.drawer = TextDrawer(font: self._font) self.drawer = TextDrawer(font: self._font)
self.uiClient = UiClient(uuid: self.uuid) self.uiClient = UiClient(uuid: self.uuid)
@ -257,6 +260,8 @@ public class NvimView: NSView,
var nvimApiScheduler = SerialDispatchQueueScheduler(qos: .userInitiated) var nvimApiScheduler = SerialDispatchQueueScheduler(qos: .userInitiated)
let eventsSubject = PublishSubject<NvimViewEvent>()
// MARK: - Private // MARK: - Private
fileprivate var _linespacing = NvimView.defaultLinespacing private var _linespacing = NvimView.defaultLinespacing
} }

View File

@ -5,23 +5,22 @@
import Cocoa import Cocoa
// See http://stackoverflow.com/a/24104371 for class public enum NvimViewEvent {
public protocol NvimViewDelegate: class {
func neoVimStopped() case neoVimStopped
func set(title: String) case setTitle(String)
func set(dirtyStatus: Bool) case setDirtyStatus(Bool)
func cwdChanged() case cwdChanged
func bufferListChanged() case bufferListChanged
func tabChanged() case tabChanged
/// Called when the current buffer changes, including when a new one is selected.
func newCurrentBuffer(_ currentBuffer: NvimView.Buffer) case newCurrentBuffer(NvimView.Buffer)
func bufferWritten(_ buffer: NvimView.Buffer) case bufferWritten(NvimView.Buffer)
func colorschemeChanged(to: NvimView.Theme) case colorschemeChanged(NvimView.Theme)
func ipcBecameInvalid(reason: String) case ipcBecameInvalid(String)
func scroll() case scroll
func cursor(to: Position) case cursor(Position)
} }

View File

@ -10,7 +10,6 @@ import PureLayout
class MainWindow: NSObject, class MainWindow: NSObject,
UiComponent, UiComponent,
NvimViewDelegate,
NSWindowDelegate, NSWindowDelegate,
NSUserInterfaceValidations, NSUserInterfaceValidations,
WorkspaceDelegate { WorkspaceDelegate {
@ -200,7 +199,6 @@ class MainWindow: NSObject,
self.addViews() self.addViews()
self.updateNeoVimAppearance() self.updateNeoVimAppearance()
self.neoVimView.delegate = self
self.open(urls: state.urlsToOpen) self.open(urls: state.urlsToOpen)
@ -215,6 +213,28 @@ class MainWindow: NSObject,
}) })
.disposed(by: self.disposeBag) .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 source
.observeOn(MainScheduler.instance) .observeOn(MainScheduler.instance)
.subscribe(onNext: { state in .subscribe(onNext: { state in