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-27 19:02:05 +03:00
|
|
|
case changeCwd(mainWindow: MainWindowComponent)
|
2016-09-01 21:09:00 +03:00
|
|
|
case close(mainWindow: MainWindowComponent)
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
fileprivate enum Tool {
|
|
|
|
|
|
|
|
case fileBrowser
|
|
|
|
}
|
|
|
|
|
|
|
|
class MainWindowComponent: WindowComponent, NSWindowDelegate, WorkspaceDelegate {
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
fileprivate static let nibName = "MainWindow"
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate var defaultEditorFont: NSFont
|
|
|
|
fileprivate var usesLigatures: Bool
|
2016-07-27 00:40:20 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
fileprivate var _cwd: URL = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
|
|
|
|
|
|
|
|
fileprivate let fontManager = NSFontManager.shared()
|
|
|
|
fileprivate let fileItemService: FileItemService
|
|
|
|
|
|
|
|
fileprivate let workspace: Workspace
|
|
|
|
fileprivate let neoVimView: NeoVimView
|
2016-09-27 19:02:05 +03:00
|
|
|
fileprivate var tools = [Tool: WorkspaceTool]()
|
2016-09-27 01:17:53 +03:00
|
|
|
|
2016-10-02 15:07:12 +03:00
|
|
|
fileprivate var flows = [Flow]()
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
// MARK: - API
|
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-25 18:50:33 +03:00
|
|
|
var cwd: URL {
|
2016-09-03 00:35:18 +03:00
|
|
|
get {
|
2016-09-07 21:12:18 +03:00
|
|
|
self._cwd = self.neoVimView.cwd
|
|
|
|
return self._cwd
|
2016-09-03 00:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
2016-09-07 21:12:18 +03:00
|
|
|
let oldValue = self._cwd
|
|
|
|
if oldValue == newValue {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self._cwd = newValue
|
2016-09-03 00:35:18 +03:00
|
|
|
self.neoVimView.cwd = newValue
|
2016-09-07 21:12:18 +03:00
|
|
|
self.fileItemService.unmonitor(url: oldValue)
|
|
|
|
self.fileItemService.monitor(url: newValue)
|
2016-09-03 00:35:18 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 17:31:14 +03:00
|
|
|
|
|
|
|
// TODO: Consider an option object for cwd, urls, etc...
|
|
|
|
init(source: Observable<Any>,
|
|
|
|
fileItemService: FileItemService,
|
2016-09-25 18:50:33 +03:00
|
|
|
cwd: URL,
|
|
|
|
urls: [URL] = [],
|
2016-09-24 17:31:14 +03:00
|
|
|
initialData: PrefData)
|
|
|
|
{
|
|
|
|
self.neoVimView = NeoVimView(frame: CGRect.zero,
|
2016-09-25 09:55:26 +03:00
|
|
|
config: NeoVimView.Config(useInteractiveZsh: initialData.advanced.useInteractiveZsh))
|
2016-09-24 17:31:14 +03:00
|
|
|
self.neoVimView.translatesAutoresizingMaskIntoConstraints = false
|
2016-09-25 01:48:26 +03:00
|
|
|
|
|
|
|
self.workspace = Workspace(mainView: self.neoVimView)
|
|
|
|
|
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-09-07 21:12:18 +03:00
|
|
|
self.fileItemService = fileItemService
|
|
|
|
self._cwd = cwd
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
super.init(source: source, nibName: MainWindowComponent.nibName)
|
2016-07-21 20:28:58 +03:00
|
|
|
|
|
|
|
self.window.delegate = self
|
2016-09-27 19:02:05 +03:00
|
|
|
self.workspace.delegate = self
|
|
|
|
|
|
|
|
let fileBrowser = FileBrowserComponent(source: self.sink, fileItemService: fileItemService)
|
|
|
|
let fileBrowserTool = WorkspaceTool(title: "Files", view: fileBrowser)
|
|
|
|
self.tools[.fileBrowser] = fileBrowserTool
|
|
|
|
self.workspace.append(tool: fileBrowserTool, location: .left)
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-10-02 15:07:12 +03:00
|
|
|
self.flows.append(fileBrowser)
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
// FIXME: temporarily for dev
|
|
|
|
fileBrowserTool.dimension = 200
|
|
|
|
self.workspace.bars[.left]?.toggle(fileBrowserTool)
|
|
|
|
|
2016-10-02 15:07:12 +03:00
|
|
|
self.addReactions()
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
self.neoVimView.cwd = cwd // This will publish the MainWindowAction.changeCwd action for the file browser.
|
2016-09-07 21:12:18 +03:00
|
|
|
self.neoVimView.delegate = self
|
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-09-07 21:12:18 +03:00
|
|
|
// We don't call self.fileItemService.monitor(url: cwd) here since self.neoVimView.cwd = cwd causes the call
|
|
|
|
// cwdChanged() and in that function we do monitor(...).
|
|
|
|
|
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-09-25 18:50:33 +03:00
|
|
|
func open(urls: [URL]) {
|
2016-08-25 00:06:39 +03:00
|
|
|
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-10-02 15:07:12 +03:00
|
|
|
// MARK: - Private
|
|
|
|
fileprivate func addReactions() {
|
|
|
|
self.flows
|
|
|
|
.map { $0.sink }
|
|
|
|
.toMergedObservables()
|
|
|
|
.subscribe(onNext: { [unowned self] action in
|
|
|
|
switch action {
|
|
|
|
case let FileBrowserAction.open(url: url):
|
|
|
|
self.open(urls: [url])
|
|
|
|
default:
|
|
|
|
NSLog("unrecognized action: \(action)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.addDisposableTo(self.disposeBag)
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
// MARK: - WindowComponent
|
2016-08-30 23:25:34 +03:00
|
|
|
override func addViews() {
|
2016-09-25 01:48:26 +03:00
|
|
|
self.window.contentView?.addSubview(self.workspace)
|
|
|
|
self.workspace.autoPinEdgesToSuperviewEdges()
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|
2016-07-24 21:32:07 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
override func subscription(source: Observable<Any>) -> Disposable {
|
2016-08-30 23:25:34 +03:00
|
|
|
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
|
2016-09-25 18:50:33 +03:00
|
|
|
!appearanceData.editorFont.isEqual(to: self.neoVimView.font)
|
2016-08-14 16:38:41 +03:00
|
|
|
|| appearanceData.editorUsesLigatures != self.neoVimView.usesLigatures
|
|
|
|
}
|
2016-09-25 19:10:07 +03:00
|
|
|
.subscribe(onNext: { [unowned self] appearance in
|
2016-07-31 00:04:20 +03:00
|
|
|
self.neoVimView.usesLigatures = appearance.editorUsesLigatures
|
|
|
|
self.neoVimView.font = appearance.editorFont
|
2016-09-25 19:10:07 +03:00
|
|
|
})
|
2016-07-24 21:32:07 +03:00
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
// MARK: - WorkspaceDelegate
|
|
|
|
extension MainWindowComponent {
|
|
|
|
|
|
|
|
func resizeWillStart(workspace: Workspace) {
|
|
|
|
self.neoVimView.enterResizeMode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func resizeDidEnd(workspace: Workspace) {
|
|
|
|
self.neoVimView.exitResizeMode()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:19:03 +03:00
|
|
|
// MARK: - File Menu Items
|
|
|
|
extension MainWindowComponent {
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func newTab(_ sender: Any?) {
|
2016-08-11 22:19:03 +03:00
|
|
|
self.neoVimView.newTab()
|
|
|
|
}
|
2016-08-11 23:37:41 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func openDocument(_ sender: Any?) {
|
2016-08-11 22:19:03 +03:00
|
|
|
let panel = NSOpenPanel()
|
|
|
|
panel.canChooseDirectories = true
|
2016-09-25 18:50:33 +03:00
|
|
|
panel.beginSheetModal(for: self.window) { result in
|
2016-08-11 22:19:03 +03:00
|
|
|
guard result == NSFileHandlingPanelOKButton else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The open panel can choose only one file.
|
2016-09-25 18:50:33 +03:00
|
|
|
self.neoVimView.open(urls: panel.urls)
|
2016-08-20 20:02:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func openQuickly(_ sender: Any?) {
|
2016-09-07 21:12:18 +03:00
|
|
|
self.publish(event: MainWindowAction.openQuickly(mainWindow: self))
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func saveDocument(_ sender: Any?) {
|
2016-08-20 20:02:16 +03:00
|
|
|
let curBuf = self.neoVimView.currentBuffer()
|
|
|
|
|
|
|
|
if curBuf.fileName == nil {
|
|
|
|
self.savePanelSheet { self.neoVimView.saveCurrentTab(url: $0) }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.neoVimView.saveCurrentTab()
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func saveDocumentAs(_ sender: Any?) {
|
2016-08-20 20:02:16 +03:00
|
|
|
self.savePanelSheet { url in
|
|
|
|
self.neoVimView.saveCurrentTab(url: url)
|
|
|
|
|
|
|
|
if self.neoVimView.isCurrentBufferDirty() {
|
|
|
|
self.neoVimView.openInNewTab(urls: [url])
|
|
|
|
} else {
|
|
|
|
self.neoVimView.openInCurrentTab(url: url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func savePanelSheet(action: @escaping (URL) -> Void) {
|
2016-08-20 20:02:16 +03:00
|
|
|
let panel = NSSavePanel()
|
2016-09-25 18:50:33 +03:00
|
|
|
panel.beginSheetModal(for: self.window) { result in
|
2016-08-20 20:02:16 +03:00
|
|
|
guard result == NSFileHandlingPanelOKButton else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let showAlert: () -> Void = {
|
|
|
|
let alert = NSAlert()
|
2016-09-25 18:50:33 +03:00
|
|
|
alert.addButton(withTitle: "OK")
|
2016-08-20 20:02:16 +03:00
|
|
|
alert.messageText = "Invalid File Name"
|
|
|
|
alert.informativeText = "The file name you have entered cannot be used. Please use a different name."
|
2016-09-25 18:50:33 +03:00
|
|
|
alert.alertStyle = .warning
|
2016-08-20 20:02:16 +03:00
|
|
|
|
|
|
|
alert.runModal()
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
guard let url = panel.url else {
|
2016-08-20 20:02:16 +03:00
|
|
|
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-27 01:17:53 +03:00
|
|
|
@IBAction func resetFontSize(_ sender: Any?) {
|
2016-07-28 20:37:39 +03:00
|
|
|
self.neoVimView.font = self.defaultEditorFont
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func makeFontBigger(_ sender: Any?) {
|
2016-07-28 20:37:39 +03:00
|
|
|
let curFont = self.neoVimView.font
|
2016-09-25 18:50:33 +03:00
|
|
|
let font = self.fontManager.convert(curFont,
|
2016-07-28 20:41:55 +03:00
|
|
|
toSize: min(curFont.pointSize + 1, PrefStore.maximumEditorFontSize))
|
2016-07-28 20:37:39 +03:00
|
|
|
self.neoVimView.font = font
|
|
|
|
}
|
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func makeFontSmaller(_ sender: Any?) {
|
2016-07-28 20:37:39 +03:00
|
|
|
let curFont = self.neoVimView.font
|
2016-09-25 18:50:33 +03:00
|
|
|
let font = self.fontManager.convert(curFont,
|
2016-07-28 20:41:55 +03:00
|
|
|
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
|
2016-09-07 21:12:18 +03:00
|
|
|
extension MainWindowComponent: NeoVimViewDelegate {
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
func set(title: String) {
|
2016-07-28 23:13:30 +03:00
|
|
|
self.window.title = title
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|
2016-07-27 00:40:20 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
func set(dirtyStatus: Bool) {
|
|
|
|
self.windowController.setDocumentEdited(dirtyStatus)
|
2016-08-19 00:11:33 +03:00
|
|
|
}
|
2016-09-07 21:12:18 +03:00
|
|
|
|
|
|
|
func cwdChanged() {
|
|
|
|
let old = self._cwd
|
|
|
|
self._cwd = self.neoVimView.cwd
|
|
|
|
self.fileItemService.unmonitor(url: old)
|
|
|
|
self.fileItemService.monitor(url: self._cwd)
|
2016-09-27 19:02:05 +03:00
|
|
|
|
|
|
|
self.publish(event: MainWindowAction.changeCwd(mainWindow: self))
|
2016-09-07 21:12:18 +03:00
|
|
|
}
|
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
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func windowDidBecomeKey(_: Notification) {
|
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-09-25 18:50:33 +03:00
|
|
|
func windowWillClose(_ notification: Notification) {
|
2016-09-07 21:12:18 +03:00
|
|
|
self.fileItemService.unmonitor(url: self._cwd)
|
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
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func windowShouldClose(_ sender: Any) -> Bool {
|
2016-08-13 00:07:16 +03:00
|
|
|
if self.neoVimView.isCurrentBufferDirty() {
|
|
|
|
let alert = NSAlert()
|
2016-09-25 18:50:33 +03:00
|
|
|
alert.addButton(withTitle: "Cancel")
|
|
|
|
alert.addButton(withTitle: "Discard and Close")
|
2016-08-13 00:07:16 +03:00
|
|
|
alert.messageText = "The current buffer has unsaved changes!"
|
2016-09-25 18:50:33 +03:00
|
|
|
alert.alertStyle = .warning
|
|
|
|
alert.beginSheetModal(for: self.window, completionHandler: { response in
|
2016-08-13 00:07:16 +03:00
|
|
|
if response == NSAlertSecondButtonReturn {
|
|
|
|
self.neoVimView.closeCurrentTabWithoutSaving()
|
|
|
|
}
|
2016-09-25 18:50:33 +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-09-07 21:12:18 +03:00
|
|
|
}
|