2017-05-27 16:13:22 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
extension NeoVimView {
|
|
|
|
|
|
|
|
override public func setFrameSize(_ newSize: NSSize) {
|
|
|
|
super.setFrameSize(newSize)
|
|
|
|
|
2017-06-12 20:28:28 +03:00
|
|
|
if self.isInitialResize {
|
|
|
|
self.isInitialResize = false
|
2017-06-12 22:49:16 +03:00
|
|
|
self.launchNeoVim(self.discreteSize(size: newSize))
|
2017-06-12 20:28:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-27 16:13:22 +03:00
|
|
|
if self.inLiveResize || self.currentlyResizing {
|
|
|
|
// TODO: Turn off live resizing for now.
|
|
|
|
// self.resizeNeoVimUi(to: newSize)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-27 17:41:28 +03:00
|
|
|
// 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.
|
2017-05-27 16:13:22 +03:00
|
|
|
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))
|
|
|
|
}
|
2017-05-27 19:26:46 +03:00
|
|
|
|
2017-06-12 20:28:28 +03:00
|
|
|
fileprivate func launchNeoVim(_ size: Size) {
|
|
|
|
self.logger.info("=== Starting neovim...")
|
|
|
|
let noErrorDuringInitialization = self.agent.runLocalServerAndNeoVim(withWidth: size.width, height: size.height)
|
|
|
|
|
|
|
|
self.agent.vimCommand("set mouse=a")
|
2017-06-12 22:49:16 +03:00
|
|
|
self.agent.vimCommand("set title")
|
|
|
|
self.agent.vimCommand("set termguicolors")
|
2017-06-12 20:28:28 +03:00
|
|
|
|
|
|
|
if noErrorDuringInitialization == false {
|
|
|
|
self.logger.fault("There was an error launching neovim.")
|
|
|
|
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.alertStyle = .warning
|
|
|
|
alert.messageText = "Error during initialization"
|
|
|
|
alert.informativeText = "There was an error during the initialization of NeoVim. " +
|
|
|
|
"Use :messages to view the error messages."
|
|
|
|
alert.runModal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:26:46 +03:00
|
|
|
fileprivate func randomEmoji() -> String {
|
2017-05-27 22:57:59 +03:00
|
|
|
let idx = Int(arc4random_uniform(UInt32(emojis.count)))
|
|
|
|
guard let scalar = UnicodeScalar(emojis[idx]) else {
|
2017-05-27 19:26:46 +03:00
|
|
|
return "😎"
|
|
|
|
}
|
|
|
|
|
|
|
|
return String(scalar)
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 22:57:59 +03:00
|
|
|
|
|
|
|
fileprivate let emojis: [UInt32] = [
|
|
|
|
0x1F600...0x1F64F,
|
|
|
|
0x1F910...0x1F918,
|
|
|
|
0x1F980...0x1F984,
|
|
|
|
0x1F9C0...0x1F9C0
|
|
|
|
].flatMap { $0 }
|