2016-07-10 19:47:24 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import PureLayout
|
2016-07-21 20:28:58 +03:00
|
|
|
import RxSwift
|
|
|
|
|
2016-09-01 21:09:00 +03:00
|
|
|
enum MainWindowAction {
|
|
|
|
case becomeKey(mainWindow: MainWindowComponent)
|
2016-09-01 21:10:40 +03:00
|
|
|
case openQuickly(mainWindow: MainWindowComponent)
|
2016-09-01 21:09:00 +03:00
|
|
|
case close(mainWindow: MainWindowComponent)
|
|
|
|
}
|
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
class MainWindowComponent: WindowComponent, NSWindowDelegate, NeoVimViewDelegate {
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-28 20:37:39 +03:00
|
|
|
private let fontManager = NSFontManager.sharedFontManager()
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-28 20:37:39 +03:00
|
|
|
private var defaultEditorFont: NSFont
|
2016-07-31 00:04:20 +03:00
|
|
|
private var usesLigatures: Bool
|
2016-07-27 00:40:20 +03:00
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
var uuid: String {
|
|
|
|
return self.neoVimView.uuid
|
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-09-03 00:35:18 +03:00
|
|
|
var cwd: NSURL {
|
|
|
|
get {
|
|
|
|
return self.neoVimView.cwd
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
|
|
|
self.neoVimView.cwd = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
private let neoVimView = NeoVimView(forAutoLayout: ())
|
2016-08-11 23:37:41 +03:00
|
|
|
|
2016-09-01 21:09:00 +03:00
|
|
|
init(source: Observable<Any>, urls: [NSURL] = [], initialData: PrefData) {
|
2016-07-28 20:37:39 +03:00
|
|
|
self.defaultEditorFont = initialData.appearance.editorFont
|
2016-07-31 00:04:20 +03:00
|
|
|
self.usesLigatures = initialData.appearance.editorUsesLigatures
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
super.init(source: source, nibName: "MainWindow")
|
2016-07-21 20:28:58 +03:00
|
|
|
|
|
|
|
self.window.delegate = self
|
2016-07-17 15:41:53 +03:00
|
|
|
self.neoVimView.delegate = self
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-08-21 18:32:35 +03:00
|
|
|
self.neoVimView.font = self.defaultEditorFont
|
|
|
|
self.neoVimView.usesLigatures = self.usesLigatures
|
|
|
|
self.neoVimView.open(urls: urls)
|
2016-07-11 18:56:55 +03:00
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
self.window.makeFirstResponder(self.neoVimView)
|
2016-08-30 23:25:34 +03:00
|
|
|
self.show()
|
2016-07-27 00:40:20 +03:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:06:39 +03:00
|
|
|
func open(urls urls: [NSURL]) {
|
|
|
|
self.neoVimView.open(urls: urls)
|
|
|
|
}
|
2016-07-11 18:56:55 +03:00
|
|
|
|
2016-07-18 23:44:23 +03:00
|
|
|
func isDirty() -> Bool {
|
|
|
|
return self.neoVimView.hasDirtyDocs()
|
|
|
|
}
|
2016-08-13 00:07:16 +03:00
|
|
|
|
2016-08-20 17:59:30 +03:00
|
|
|
func closeAllNeoVimWindows() {
|
|
|
|
self.neoVimView.closeAllWindows()
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:00:05 +03:00
|
|
|
func closeAllNeoVimWindowsWithoutSaving() {
|
|
|
|
self.neoVimView.closeAllWindowsWithoutSaving()
|
|
|
|
}
|
2016-07-18 23:44:23 +03:00
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
override func addViews() {
|
2016-07-21 20:28:58 +03:00
|
|
|
self.window.contentView?.addSubview(self.neoVimView)
|
|
|
|
self.neoVimView.autoPinEdgesToSuperviewEdges()
|
|
|
|
}
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
override func subscription(source source: Observable<Any>) -> Disposable {
|
|
|
|
return source
|
2016-07-24 21:32:07 +03:00
|
|
|
.filter { $0 is PrefData }
|
2016-07-31 00:04:20 +03:00
|
|
|
.map { ($0 as! PrefData).appearance }
|
2016-08-14 16:38:41 +03:00
|
|
|
.filter { [unowned self] appearanceData in
|
|
|
|
!appearanceData.editorFont.isEqualTo(self.neoVimView.font)
|
|
|
|
|| appearanceData.editorUsesLigatures != self.neoVimView.usesLigatures
|
|
|
|
}
|
2016-07-31 00:04:20 +03:00
|
|
|
.subscribeNext { [unowned self] appearance in
|
|
|
|
self.neoVimView.usesLigatures = appearance.editorUsesLigatures
|
|
|
|
self.neoVimView.font = appearance.editorFont
|
2016-08-30 23:25:34 +03:00
|
|
|
}
|
2016-07-24 21:32:07 +03:00
|
|
|
}
|
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
|
|
|
|
2016-08-12 15:54:37 +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.
|
2016-08-12 15:54:37 +03:00
|
|
|
self.neoVimView.open(urls: panel.URLs)
|
2016-08-20 20:02:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func saveDocument(sender: AnyObject!) {
|
|
|
|
let curBuf = self.neoVimView.currentBuffer()
|
|
|
|
|
|
|
|
if curBuf.fileName == nil {
|
|
|
|
self.savePanelSheet { self.neoVimView.saveCurrentTab(url: $0) }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.neoVimView.saveCurrentTab()
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func saveDocumentAs(sender: AnyObject!) {
|
|
|
|
self.savePanelSheet { url in
|
|
|
|
self.neoVimView.saveCurrentTab(url: url)
|
|
|
|
|
|
|
|
if self.neoVimView.isCurrentBufferDirty() {
|
|
|
|
self.neoVimView.openInNewTab(urls: [url])
|
|
|
|
} else {
|
|
|
|
self.neoVimView.openInCurrentTab(url: url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func savePanelSheet(action action: (NSURL) -> Void) {
|
|
|
|
let panel = NSSavePanel()
|
|
|
|
panel.beginSheetModalForWindow(self.window) { result in
|
|
|
|
guard result == NSFileHandlingPanelOKButton else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let showAlert: () -> Void = {
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.addButtonWithTitle("OK")
|
|
|
|
alert.messageText = "Invalid File Name"
|
|
|
|
alert.informativeText = "The file name you have entered cannot be used. Please use a different name."
|
|
|
|
alert.alertStyle = .WarningAlertStyle
|
|
|
|
|
|
|
|
alert.runModal()
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let url = panel.URL else {
|
|
|
|
showAlert()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard url.path != nil else {
|
|
|
|
showAlert()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
action(url)
|
2016-08-12 15:54:37 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 22:19:03 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:02:47 +03:00
|
|
|
// MARK: - Font Menu Items
|
2016-07-28 20:37:39 +03:00
|
|
|
extension MainWindowComponent {
|
|
|
|
|
2016-09-01 21:10:40 +03:00
|
|
|
@IBAction func openQuickly(sender: AnyObject!) {
|
|
|
|
self.publish(event: MainWindowAction.openQuickly(mainWindow: self))
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:37:39 +03:00
|
|
|
@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))
|
2016-07-28 20:37:39 +03:00
|
|
|
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))
|
2016-07-28 20:37:39 +03:00
|
|
|
self.neoVimView.font = font
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
// MARK: - NeoVimViewDelegate
|
|
|
|
extension MainWindowComponent {
|
|
|
|
|
2016-07-28 23:13:30 +03:00
|
|
|
func setTitle(title: String) {
|
|
|
|
self.window.title = title
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|
2016-07-27 00:40:20 +03:00
|
|
|
|
2016-08-19 00:11:33 +03:00
|
|
|
func setDirtyStatus(dirty: Bool) {
|
|
|
|
self.windowController.setDocumentEdited(dirty)
|
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
|
|
|
|
func neoVimStopped() {
|
|
|
|
self.windowController.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSWindowDelegate
|
|
|
|
extension MainWindowComponent {
|
2016-08-25 00:06:39 +03:00
|
|
|
|
|
|
|
func windowDidBecomeKey(_: NSNotification) {
|
2016-09-01 21:09:00 +03:00
|
|
|
self.publish(event: MainWindowAction.becomeKey(mainWindow: self))
|
2016-08-25 00:06:39 +03:00
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-10 19:47:24 +03:00
|
|
|
func windowWillClose(notification: NSNotification) {
|
2016-09-01 21:09:00 +03:00
|
|
|
self.publish(event: MainWindowAction.close(mainWindow: self))
|
2016-07-10 19:47:24 +03:00
|
|
|
}
|
2016-07-18 21:16:56 +03:00
|
|
|
|
|
|
|
func windowShouldClose(sender: AnyObject) -> Bool {
|
2016-08-13 00:07:16 +03:00
|
|
|
if self.neoVimView.isCurrentBufferDirty() {
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.addButtonWithTitle("Cancel")
|
|
|
|
alert.addButtonWithTitle("Discard and Close")
|
|
|
|
alert.messageText = "The current buffer has unsaved changes!"
|
|
|
|
alert.alertStyle = .WarningAlertStyle
|
|
|
|
alert.beginSheetModalForWindow(self.window) { response in
|
|
|
|
if response == NSAlertSecondButtonReturn {
|
|
|
|
self.neoVimView.closeCurrentTabWithoutSaving()
|
|
|
|
}
|
2016-07-18 21:16:56 +03:00
|
|
|
}
|
2016-08-13 00:07:16 +03:00
|
|
|
|
|
|
|
return false
|
2016-07-18 21:16:56 +03:00
|
|
|
}
|
|
|
|
|
2016-08-13 00:07:16 +03:00
|
|
|
self.neoVimView.closeCurrentTab()
|
2016-07-18 21:16:56 +03:00
|
|
|
return false
|
2016-07-18 21:16:56 +03:00
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|