1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 11:35:35 +03:00
vimr/SwiftNeoVim/NeoVimView+Resize.swift

70 lines
1.8 KiB
Swift

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
extension NeoVimView {
override public func setFrameSize(_ newSize: NSSize) {
super.setFrameSize(newSize)
// initial resizing is done when grid has data
guard self.grid.hasData else {
return
}
if self.inLiveResize || self.currentlyResizing {
// TODO: Turn off live resizing for now.
// self.resizeNeoVimUi(to: newSize)
return
}
// There can be cases where the frame is resized not by live resizing,
// eg when the window is resized by window management tools.
// Thus, we make sure that the resize call is made when this happens.
self.resizeNeoVimUi(to: newSize)
}
override public func viewDidEndLiveResize() {
super.viewDidEndLiveResize()
self.resizeNeoVimUi(to: self.bounds.size)
}
func discreteSize(size: CGSize) -> Size {
return Size(width: Int(floor(size.width / self.cellSize.width)),
height: Int(floor(size.height / self.cellSize.height)))
}
func resizeNeoVimUi(to size: CGSize) {
self.currentEmoji = self.randomEmoji()
let discreteSize = self.discreteSize(size: size)
if discreteSize == self.grid.size {
return
}
self.xOffset = floor((size.width - self.cellSize.width * CGFloat(discreteSize.width)) / 2)
self.yOffset = floor((size.height - self.cellSize.height * CGFloat(discreteSize.height)) / 2)
self.agent.resize(toWidth: Int32(discreteSize.width), height: Int32(discreteSize.height))
}
fileprivate func randomEmoji() -> String {
let idx = Int(arc4random_uniform(UInt32(emojis.count)))
guard let scalar = UnicodeScalar(emojis[idx]) else {
return "😎"
}
return String(scalar)
}
}
fileprivate let emojis: [UInt32] = [
0x1F600...0x1F64F,
0x1F910...0x1F918,
0x1F980...0x1F984,
0x1F9C0...0x1F9C0
].flatMap { $0 }