1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-28 08:13:17 +03:00
vimr/SwiftNeoVim/NeoVimViewUiBridge.swift
Tae Won Ha 07ac09e95f
Make resizing NeoVimView work
- live resizing is not yet activated, but can be easily done
- @autoreleasepool-block for each UI callback and for each key input is
  ugly...
2016-07-05 18:49:19 +02:00

224 lines
5.7 KiB
Swift

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
extension NeoVimView: NeoVimUiBridgeProtocol {
public func neoVimUiIsReady() {
DispatchUtils.gui {
NSLog("\(#function): \(self.frame)")
self.resizeNeoVimUiTo(size: self.frame.size)
}
}
public func resizeToWidth(width: Int32, height: Int32) {
DispatchUtils.gui {
// NSLog("\(#function): \(width):\(height)")
self.grid.resize(Size(width: Int(width), height: Int(height)))
self.needsDisplay = true
}
}
public func clear() {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.clear()
self.needsDisplay = true
}
}
public func eolClear() {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.eolClear()
let origin = self.pointInView(self.grid.putPosition)
let size = CGSize(
width: CGFloat(self.grid.region.right - self.grid.putPosition.column + 1) * self.cellSize.width,
height: self.cellSize.height
)
let rect = CGRect(origin: origin, size: size)
self.setNeedsDisplayInRect(rect)
}
}
public func gotoPosition(position: Position, screenCursor: Position, bufferCursor: Position) {
//if self.inLiveResize { return }
DispatchUtils.gui {
// NSLog("\(#function): \(position), \(screenCursor), \(bufferCursor)")
self.setNeedsDisplay(cellPosition: self.grid.screenCursor) // redraw where the cursor was till now
self.setNeedsDisplay(screenCursor: screenCursor) // draw the new cursor
self.grid.goto(position)
self.grid.moveCursor(screenCursor)
}
}
public func updateMenu() {
}
public func busyStart() {
}
public func busyStop() {
}
public func mouseOn() {
}
public func mouseOff() {
}
public func modeChange(mode: Int32) {
}
public func setScrollRegionToTop(top: Int32, bottom: Int32, left: Int32, right: Int32) {
//if self.inLiveResize { return }
DispatchUtils.gui {
let region = Region(top: Int(top), bottom: Int(bottom), left: Int(left), right: Int(right))
self.grid.setScrollRegion(region)
self.setNeedsDisplay(region: region)
}
}
public func scroll(count: Int32) {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.scroll(Int(count))
self.setNeedsDisplay(region: self.grid.region)
}
}
public func highlightSet(attrs: CellAttributes) {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.attrs = attrs
}
}
public func put(string: String) {
//if self.inLiveResize { return }
DispatchUtils.gui {
let curPos = self.grid.putPosition
// NSLog("\(#function): \(curPos) -> \(string)")
self.grid.put(string)
self.setNeedsDisplay(cellPosition: curPos)
self.setNeedsDisplay(screenCursor: self.grid.screenCursor)
}
}
public func putMarkedText(markedText: String) {
//if self.inLiveResize { return }
DispatchUtils.gui {
NSLog("\(#function): '\(markedText)'")
let curPos = self.grid.putPosition
self.grid.putMarkedText(markedText)
self.setNeedsDisplay(position: curPos)
if markedText.characters.count == 0 {
self.setNeedsDisplay(position: self.grid.previousCellPosition(curPos))
}
self.setNeedsDisplay(screenCursor: self.grid.screenCursor)
}
}
public func unmarkRow(row: Int32, column: Int32) {
//if self.inLiveResize { return }
DispatchUtils.gui {
let position = Position(row: Int(row), column: Int(column))
NSLog("\(#function): \(position)")
self.grid.unmarkCell(position)
self.setNeedsDisplay(position: position)
self.setNeedsDisplay(screenCursor: self.grid.screenCursor)
}
}
public func bell() {
DispatchUtils.gui {
NSBeep()
}
}
public func visualBell() {
}
public func flush() {
// Swift.print("\(self.grid)")
}
public func updateForeground(fg: Int32) {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.foreground = UInt32(bitPattern: fg)
}
}
public func updateBackground(bg: Int32) {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.background = UInt32(bitPattern: bg)
self.layer?.backgroundColor = ColorUtils.colorIgnoringAlpha(self.grid.background).CGColor
}
}
public func updateSpecial(sp: Int32) {
//if self.inLiveResize { return }
DispatchUtils.gui {
self.grid.special = UInt32(bitPattern: sp)
}
}
public func suspend() {
}
public func setTitle(title: String) {
self.delegate?.setTitle(title)
}
public func setIcon(icon: String) {
}
public func stop() {
}
private func setNeedsDisplay(region region: Region) {
self.setNeedsDisplayInRect(self.regionRect(region))
}
private func setNeedsDisplay(cellPosition position: Position) {
self.setNeedsDisplay(position: position)
if self.grid.isCellEmpty(position) {
self.setNeedsDisplay(position: self.grid.previousCellPosition(position))
}
if self.grid.isNextCellEmpty(position) {
self.setNeedsDisplay(position: self.grid.nextCellPosition(position))
}
}
private func setNeedsDisplay(position position: Position) {
self.setNeedsDisplay(row: position.row, column: position.column)
}
private func setNeedsDisplay(row row: Int, column: Int) {
// Swift.print("\(#function): \(row):\(column)")
self.setNeedsDisplayInRect(self.cellRect(row: row, column: column))
}
private func setNeedsDisplay(screenCursor position: Position) {
self.setNeedsDisplay(position: position)
if self.grid.isNextCellEmpty(position) {
self.setNeedsDisplay(position: self.grid.nextCellPosition(position))
}
}
}