1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-27 15:53:31 +03:00
vimr/SwiftNeoVim/NeoVimView.swift
2016-06-06 21:50:35 +02:00

75 lines
2.1 KiB
Swift

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import RxSwift
public class NeoVimView: NSView {
public var delegate: NeoVimViewDelegate?
private let disposeBag = DisposeBag()
private var foregroundColor = NSColor.blackColor()
private var backgroundColor = NSColor.whiteColor()
private var font = NSFont(name: "Menlo", size: 12)!
private let xpc: NeoVimXpc
private var cellSize: CGSize {
return self.font.boundingRectForFont.size
}
private var lineGap: CGFloat = 2.0
init(frame rect: NSRect = CGRectZero, uiEventObservable: Observable<NeoVim.UiEvent>, xpc: NeoVimXpc) {
self.xpc = xpc
super.init(frame: rect)
uiEventObservable.subscribe(
onNext: { event in
switch event {
case .Resize(let size):
Swift.print("### resize: \(size)")
let rectSize = CGSizeMake(
CGFloat(size.columns) * self.cellSize.width,
CGFloat(size.rows) * self.cellSize.height + self.lineGap * (CGFloat(size.rows - 1))
)
dispatch_async(dispatch_get_main_queue()) {
self.delegate?.resizeToSize(rectSize)
}
case .MoveCursor(let position):
Swift.print("### \(position)")
case .Put(let string):
dispatch_async(dispatch_get_main_queue()) {
NSLog("### putting: %@", string.stringByReplacingOccurrencesOfString(" ", withString: ""))
}
}
}, onError: { error in
Swift.print(error)
}, onCompleted: {
Swift.print("ui event observable completed")
}, onDisposed: {
Swift.print("ui event observable disposed")
}).addDisposableTo(self.disposeBag)
}
override public func keyDown(theEvent: NSEvent) {
self.xpc.vimInput(theEvent.charactersIgnoringModifiers!)
}
override public func drawRect(dirtyRect: NSRect) {
self.backgroundColor.setFill()
NSRectFill(dirtyRect)
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}