1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-22 13:11:55 +03:00
vimr/VimR/MainWindowComponent.swift

173 lines
4.5 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import PureLayout
2016-07-21 20:28:58 +03:00
import RxSwift
class MainWindowComponent: NSObject, NSWindowDelegate, NeoVimViewDelegate, Component {
private let source: Observable<Any>
private let disposeBag = DisposeBag()
private let subject = PublishSubject<Any>()
var sink: Observable<Any> {
return self.subject.asObservable()
}
private weak var mainWindowManager: MainWindowManager?
private let fontManager = NSFontManager.sharedFontManager()
2016-07-21 20:28:58 +03:00
private let windowController = NSWindowController(windowNibName: "MainWindow")
private let window: NSWindow
2016-08-12 12:05:32 +03:00
private let urlsToBeOpenedWhenReady: [NSURL]
2016-08-11 23:37:41 +03:00
private var defaultEditorFont: NSFont
private var usesLigatures: Bool
2016-07-27 00:40:20 +03:00
var uuid: String {
return self.neoVimView.uuid
}
2016-07-21 20:28:58 +03:00
private let neoVimView = NeoVimView(forAutoLayout: ())
2016-08-11 23:37:41 +03:00
2016-08-12 12:05:32 +03:00
init(source: Observable<Any>, manager: MainWindowManager, urls: [NSURL] = [], initialData: PrefData) {
2016-07-21 20:28:58 +03:00
self.source = source
self.mainWindowManager = manager
2016-07-21 20:28:58 +03:00
self.window = self.windowController.window!
self.defaultEditorFont = initialData.appearance.editorFont
self.usesLigatures = initialData.appearance.editorUsesLigatures
2016-08-12 12:05:32 +03:00
self.urlsToBeOpenedWhenReady = urls
2016-07-21 20:28:58 +03:00
super.init()
self.window.delegate = self
self.neoVimView.delegate = self
2016-07-24 21:32:07 +03:00
self.addViews()
2016-07-24 21:32:07 +03:00
self.addReactions()
2016-07-21 20:28:58 +03:00
self.window.makeFirstResponder(self.neoVimView)
self.windowController.showWindow(self)
2016-07-27 00:40:20 +03:00
}
deinit {
self.subject.onCompleted()
}
func isDirty() -> Bool {
return self.neoVimView.hasDirtyDocs()
}
func closeAllNeoVimWindowsWithoutSaving() {
self.neoVimView.closeAllWindowsWithoutSaving()
}
2016-07-21 20:28:58 +03:00
private func addViews() {
self.window.contentView?.addSubview(self.neoVimView)
self.neoVimView.autoPinEdgesToSuperviewEdges()
}
2016-07-24 21:32:07 +03:00
private func addReactions() {
self.source
.filter { $0 is PrefData }
.map { ($0 as! PrefData).appearance }
.subscribeNext { [unowned self] appearance in
self.neoVimView.usesLigatures = appearance.editorUsesLigatures
self.neoVimView.font = appearance.editorFont
2016-07-24 21:32:07 +03:00
}
.addDisposableTo(self.disposeBag)
}
2016-07-21 20:28:58 +03:00
}
2016-08-11 22:19:03 +03:00
// MARK: - File Menu Items
extension MainWindowComponent {
@IBAction func newTab(sender: AnyObject!) {
self.neoVimView.newTab()
}
2016-08-11 23:37:41 +03:00
@IBAction func openDocument(sender: AnyObject!) {
2016-08-11 22:19:03 +03:00
let panel = NSOpenPanel()
panel.canChooseDirectories = true
panel.beginSheetModalForWindow(self.window) { result in
guard result == NSFileHandlingPanelOKButton else {
return
}
// The open panel can choose only one file.
self.neoVimView.open(urls: panel.URLs)
}
}
2016-08-11 22:19:03 +03:00
}
2016-08-03 22:02:47 +03:00
// MARK: - Font Menu Items
extension MainWindowComponent {
@IBAction func resetFontSize(sender: AnyObject!) {
self.neoVimView.font = self.defaultEditorFont
}
@IBAction func makeFontBigger(sender: AnyObject!) {
let curFont = self.neoVimView.font
2016-07-28 20:41:55 +03:00
let font = self.fontManager.convertFont(curFont,
toSize: min(curFont.pointSize + 1, PrefStore.maximumEditorFontSize))
self.neoVimView.font = font
}
@IBAction func makeFontSmaller(sender: AnyObject!) {
let curFont = self.neoVimView.font
2016-07-28 20:41:55 +03:00
let font = self.fontManager.convertFont(curFont,
toSize: max(curFont.pointSize - 1, PrefStore.minimumEditorFontSize))
self.neoVimView.font = font
}
}
2016-07-21 20:28:58 +03:00
// MARK: - NeoVimViewDelegate
extension MainWindowComponent {
func setTitle(title: String) {
self.window.title = title
2016-07-21 20:28:58 +03:00
}
2016-07-27 00:40:20 +03:00
func neoVimReady() {
self.neoVimView.font = self.defaultEditorFont
self.neoVimView.usesLigatures = self.usesLigatures
2016-08-11 23:37:41 +03:00
self.neoVimView.open(urls: self.urlsToBeOpenedWhenReady)
2016-07-27 00:40:20 +03:00
}
2016-07-21 20:28:58 +03:00
func neoVimStopped() {
self.windowController.close()
}
}
// MARK: - NSWindowDelegate
extension MainWindowComponent {
func windowWillClose(notification: NSNotification) {
self.mainWindowManager?.closeMainWindow(self)
}
func windowShouldClose(sender: AnyObject) -> Bool {
2016-07-21 20:28:58 +03:00
guard self.isDirty() else {
return true
}
let alert = NSAlert()
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Discard and Close")
alert.messageText = "There are unsaved buffers!"
alert.alertStyle = .WarningAlertStyle
2016-07-21 20:28:58 +03:00
alert.beginSheetModalForWindow(self.window) { response in
if response == NSAlertSecondButtonReturn {
2016-07-21 20:28:58 +03:00
self.windowController.close()
}
}
return false
}
2016-07-21 20:28:58 +03:00
}