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

Remove some crashing

This commit is contained in:
Tae Won Ha 2016-06-14 23:50:25 +02:00
parent 355844ad01
commit 79e39b8350
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 30 additions and 27 deletions

View File

@ -52,17 +52,18 @@ public class NeoVimView: NSView {
public var delegate: NeoVimViewDelegate?
private static let qDispatchMainQueue = dispatch_get_main_queue()
private let qDispatchMainQueue = dispatch_get_main_queue()
private let qLineGap = CGFloat(2)
private var foregroundColor = Int32(bitPattern: UInt32(0xFF000000))
private var backgroundColor = Int32(bitPattern: UInt32(0xFFFFFFFF))
private var font = NSFont(name: "Menlo", size: 14)!
private var font = NSFont(name: "Menlo", size: 13)!
private let xpc: NeoVimXpc
private let drawer = TextDrawer()
private var cellSize: CGSize = CGSizeMake(0, 0)
private let grid = Grid()
private var rowFragmentsToDraw: [RowFragment] = []
@ -73,7 +74,7 @@ public class NeoVimView: NSView {
// hard-code some stuff
let attrs = [ NSFontAttributeName: self.font ]
let width = ceil(" ".sizeWithAttributes(attrs).width)
let height = ceil(self.font.ascender - self.font.descender + self.font.leading);
let height = ceil(self.font.ascender - self.font.descender + self.font.leading) + qLineGap
self.cellSize = CGSizeMake(width, height)
}
@ -82,7 +83,6 @@ public class NeoVimView: NSView {
}
override public func drawRect(dirtyRect: NSRect) {
// Swift.print("$$$ DRAWING: \(dirtyRect)")
let context = NSGraphicsContext.currentContext()!.CGContext
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
@ -90,22 +90,30 @@ public class NeoVimView: NSView {
self.rowFragmentsToDraw.forEach { rowFrag in
let string = self.grid.cells[rowFrag.row][rowFrag.range].reduce("") { $0 + $1.string }
let positions = rowFrag.range
// filter out the put(0, 0)s (after a wide character)
.filter { self.grid.cells[rowFrag.row][$0].string.characters.count > 0 }
.map { self.originOnView(rowFrag.row, column: $0) }
let positionsPtr: UnsafeMutablePointer<CGPoint> = UnsafeMutablePointer(positions)
ColorUtils.colorFromCode(self.backgroundColor).set()
let backgroundRect = CGRect(x: positions[0].x, y: positions[0].y,
width: positions.last!.x + self.cellSize.width, height: self.cellSize.height)
NSRectFill(backgroundRect)
ColorUtils.colorFromCode(self.foregroundColor).set()
let glyphPositions = positions.map { CGPoint(x: $0.x, y: $0.y + qLineGap) }
self.drawer.drawString(
string, positions: positionsPtr,
string, positions: UnsafeMutablePointer(glyphPositions),
font: self.font, foreground: self.foregroundColor, background: self.backgroundColor,
context: context
)
positionsPtr.destroy()
NSColor.redColor().set()
positions.forEach { NSRectFill(CGRect(origin: $0, size: CGSize(width: 1, height: 1))) }
}
self.rowFragmentsToDraw = []
// Swift.print("$$$ DRAWN")
}
private func originOnView(row: Int, column: Int) -> CGPoint {
@ -116,7 +124,7 @@ public class NeoVimView: NSView {
}
private func gui(call: () -> Void) {
dispatch_async(NeoVimView.qDispatchMainQueue, call)
dispatch_async(qDispatchMainQueue, call)
}
required public init?(coder: NSCoder) {
@ -157,7 +165,9 @@ extension NeoVimView: NeoVimUiBridgeProtocol {
width: CGFloat(self.grid.region.right - self.grid.position.column + 1) * self.cellSize.width,
height: self.cellSize.height
)
self.setNeedsDisplayInRect(CGRect(origin: origin, size: size))
let rect = CGRect(origin: origin, size: size)
Swift.print("### eol clear: \(rect)")
self.setNeedsDisplayInRect(rect)
}
}
@ -216,9 +226,8 @@ extension NeoVimView: NeoVimUiBridgeProtocol {
self.addToRowFragmentsToDraw(curPos)
self.setNeedsDisplayInRect(
CGRect(origin: self.originOnView(curPos.row, column: curPos.column), size: self.cellSize)
)
let rect = CGRect(origin: self.originOnView(curPos.row, column: curPos.column), size: self.cellSize)
self.setNeedsDisplayInRect(rect)
}
}

View File

@ -19,10 +19,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NeoVimViewDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.neoVim = NeoVim()
self.neoVim.view.delegate = self
self.neoVim.view.setFrameSize(CGSizeMake(100.0, 100.0))
self.neoVim.view.setFrameOrigin(CGPointMake(0, 0))
window.contentView?.addSubview(self.neoVim.view)
window.makeFirstResponder(self.neoVim.view)
self.neoVim.view.setFrameSize(CGSize(width: 100.0, height: 100.0))
self.neoVim.view.setFrameOrigin(CGPoint(x: 0, y: 0))
self.window.contentView?.addSubview(self.neoVim.view)
self.window.makeFirstResponder(self.neoVim.view)
// neoVim.vimInput("i")
// neoVim.vimInput("\u{1F914}")
@ -30,17 +32,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NeoVimViewDelegate {
// neoVim.vimInput("č")
// neoVim.vimInput("")
// neoVim.vimInput("a")
// neoVim.vimInput("")
// neoVim.vimInput("z")
// neoVim.vimInput("\u{001B}")
// neoVim.vimInput("12")
// neoVim.vimInput("r")
// neoVim.vimInput("")
// neoVim.vimInput("\u{001B}")
// neoVim.vimInput("Z")
// neoVim.vimInput("i")
// for i in 0...9 {
// neoVim.vimInput("\(i)")
// }
}
func resizeToSize(size: CGSize) {