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 {
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-09-01 21:09:00 +03:00
|
|
|
case becomeKey(mainWindow: MainWindowComponent)
|
2016-09-01 21:10:40 +03:00
|
|
|
case openQuickly(mainWindow: MainWindowComponent)
|
2016-11-22 01:17:24 +03:00
|
|
|
case changeCwd(mainWindow: MainWindowComponent, cwd: URL)
|
2016-11-22 01:13:38 +03:00
|
|
|
case changeBufferList(mainWindow: MainWindowComponent, buffers: [NeoVimBuffer])
|
2016-12-11 18:14:51 +03:00
|
|
|
case changeFileBrowserSelection(mainWindow: MainWindowComponent, url: URL)
|
2016-10-03 15:58:49 +03:00
|
|
|
case close(mainWindow: MainWindowComponent, mainWindowPrefData: MainWindowPrefData)
|
|
|
|
}
|
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
struct MainWindowPrefData: StandardPrefData {
|
2016-10-03 15:58:49 +03:00
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
fileprivate static let isAllToolsVisible = "is-all-tools-visible"
|
|
|
|
fileprivate static let isToolButtonsVisible = "is-tool-buttons-visible"
|
|
|
|
fileprivate static let toolPrefDatas = "tool-pref-datas"
|
2016-10-03 15:58:49 +03:00
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
static let `default` = MainWindowPrefData(isAllToolsVisible: true,
|
|
|
|
isToolButtonsVisible: true,
|
2016-11-22 21:42:11 +03:00
|
|
|
toolPrefDatas: [
|
|
|
|
ToolPrefData.defaults[.fileBrowser]!,
|
|
|
|
ToolPrefData.defaults[.bufferList]!,
|
2016-12-19 20:23:43 +03:00
|
|
|
ToolPrefData.defaults[.preview]!,
|
2016-11-22 21:42:11 +03:00
|
|
|
])
|
2016-11-19 15:29:18 +03:00
|
|
|
|
|
|
|
var isAllToolsVisible: Bool
|
|
|
|
var isToolButtonsVisible: Bool
|
|
|
|
var toolPrefDatas: [ToolPrefData]
|
|
|
|
|
|
|
|
init(isAllToolsVisible: Bool, isToolButtonsVisible: Bool, toolPrefDatas: [ToolPrefData]) {
|
|
|
|
self.isAllToolsVisible = isAllToolsVisible
|
|
|
|
self.isToolButtonsVisible = isToolButtonsVisible
|
|
|
|
self.toolPrefDatas = toolPrefDatas
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(dict: [String: Any]) {
|
|
|
|
|
|
|
|
guard let isAllToolsVisible = PrefUtils.bool(from: dict, for: MainWindowPrefData.isAllToolsVisible),
|
|
|
|
let isToolButtonsVisible = PrefUtils.bool(from: dict, for: MainWindowPrefData.isToolButtonsVisible),
|
|
|
|
let toolDataDicts = dict[MainWindowPrefData.toolPrefDatas] as? [[String: Any]]
|
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 21:09:00 +03:00
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
// Add default tool pref data for missing identifiers.
|
|
|
|
let toolDatas = toolDataDicts.map { ToolPrefData(dict: $0) }.flatMap { $0 }
|
|
|
|
let missingToolDatas = Set(ToolIdentifier.all)
|
|
|
|
.subtracting(toolDatas.map { $0.identifier })
|
|
|
|
.map { ToolPrefData.defaults[$0] }
|
|
|
|
.flatMap { $0 }
|
2016-09-27 19:02:05 +03:00
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
self.init(isAllToolsVisible: isAllToolsVisible,
|
|
|
|
isToolButtonsVisible: isToolButtonsVisible,
|
|
|
|
toolPrefDatas: [ toolDatas, missingToolDatas ].flatMap { $0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
func dict() -> [String: Any] {
|
|
|
|
return [
|
|
|
|
MainWindowPrefData.isAllToolsVisible: self.isAllToolsVisible,
|
|
|
|
MainWindowPrefData.isToolButtonsVisible: self.isToolButtonsVisible,
|
|
|
|
MainWindowPrefData.toolPrefDatas: self.toolPrefDatas.map { $0.dict() },
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
func toolPrefData(for identifier: ToolIdentifier) -> ToolPrefData {
|
2016-12-19 20:23:43 +03:00
|
|
|
guard let tool = self.toolPrefDatas.first(where: { $0.identifier == identifier }) else {
|
2016-11-19 15:29:18 +03:00
|
|
|
preconditionFailure("[ERROR] No tool for \(identifier) found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tool
|
|
|
|
}
|
2016-09-27 19:02:05 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 01:13:38 +03:00
|
|
|
class MainWindowComponent: WindowComponent,
|
|
|
|
NeoVimViewDelegate,
|
|
|
|
NSWindowDelegate,
|
|
|
|
NSUserInterfaceValidations,
|
|
|
|
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
|
2016-07-27 00:40:20 +03:00
|
|
|
|
2016-10-02 15:11:39 +03:00
|
|
|
fileprivate var _cwd: URL = FileUtils.userHomeUrl
|
2016-09-27 01:17:53 +03:00
|
|
|
|
|
|
|
fileprivate let fontManager = NSFontManager.shared()
|
|
|
|
fileprivate let fileItemService: FileItemService
|
|
|
|
|
|
|
|
fileprivate let workspace: Workspace
|
|
|
|
fileprivate let neoVimView: NeoVimView
|
2016-11-18 20:54:14 +03:00
|
|
|
fileprivate var tools = [ToolIdentifier: WorkspaceToolComponent]()
|
2016-10-02 15:07:12 +03:00
|
|
|
|
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...
|
2016-10-25 20:37:13 +03:00
|
|
|
/**
|
|
|
|
The init() method does not show the window. Call MainWindowComponent.show() to do so.
|
|
|
|
*/
|
2016-09-24 17:31:14 +03:00
|
|
|
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-11-22 21:42:11 +03:00
|
|
|
self.neoVimView.configureForAutoLayout()
|
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-09-07 21:12:18 +03:00
|
|
|
self.fileItemService = fileItemService
|
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
|
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
self.setupTools(with: initialData.mainWindow)
|
2016-10-02 15:07:12 +03:00
|
|
|
|
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
|
2016-10-27 09:21:29 +03:00
|
|
|
self.neoVimView.usesLigatures = initialData.appearance.editorUsesLigatures
|
|
|
|
self.neoVimView.linespacing = initialData.appearance.editorLinespacing
|
2016-11-25 23:17:24 +03:00
|
|
|
let neoVimViewCwd = self.neoVimView.cwd
|
|
|
|
if neoVimViewCwd == cwd {
|
|
|
|
self.fileItemService.monitor(url: cwd)
|
|
|
|
} else {
|
2016-12-13 20:29:42 +03:00
|
|
|
self.neoVimView.cwd = cwd // The above will publish the MainWindowAction.changeCwd action for the file browser.
|
2016-11-25 23:17:24 +03:00
|
|
|
}
|
2016-08-21 18:32:35 +03:00
|
|
|
self.neoVimView.open(urls: urls)
|
2016-07-11 18:56:55 +03:00
|
|
|
|
2016-12-13 20:29:42 +03:00
|
|
|
self._cwd = cwd
|
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
self.addReactions()
|
|
|
|
|
|
|
|
self.window.makeFirstResponder(self.neoVimView)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func setupTools(with mainWindowData: MainWindowPrefData) {
|
2016-12-05 00:03:40 +03:00
|
|
|
// By default the tool buttons are shown and only the file browser tool is shown.
|
|
|
|
let fileBrowserToolData = mainWindowData.toolPrefData(for: .fileBrowser)
|
|
|
|
let bufferListToolData = mainWindowData.toolPrefData(for: .bufferList)
|
2016-12-19 20:23:43 +03:00
|
|
|
let previewToolData = mainWindowData.toolPrefData(for: .preview)
|
2016-12-05 00:03:40 +03:00
|
|
|
|
2016-12-05 20:02:45 +03:00
|
|
|
let fileBrowserData = fileBrowserToolData.toolData as? FileBrowserData ?? FileBrowserData.default
|
2016-12-05 00:03:40 +03:00
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
// FIXME: We do not use [self.sink, source].toMergedObservables. If we do so, then self.sink seems to live as long
|
|
|
|
// as source, i.e. forever. Thus, self (MainWindowComponent) does not get deallocated. Not nice...
|
2016-12-05 00:03:40 +03:00
|
|
|
let fileBrowser = FileBrowserComponent(source: self.sink,
|
|
|
|
fileItemService: self.fileItemService,
|
|
|
|
initialData: fileBrowserData)
|
2016-12-04 22:27:29 +03:00
|
|
|
let fileBrowserConfig = WorkspaceTool.Config(title: "Files",
|
|
|
|
view: fileBrowser,
|
|
|
|
minimumDimension: 100,
|
|
|
|
withInnerToolbar: true,
|
2016-12-05 20:19:46 +03:00
|
|
|
customToolbar: fileBrowser.innerCustomToolbar,
|
|
|
|
customMenuItems: fileBrowser.menuItems)
|
2016-12-04 22:27:29 +03:00
|
|
|
let fileBrowserTool = WorkspaceToolComponent(toolIdentifier: .fileBrowser, config: fileBrowserConfig)
|
2016-11-22 21:42:11 +03:00
|
|
|
self.tools[.fileBrowser] = fileBrowserTool
|
|
|
|
|
2016-11-23 00:34:00 +03:00
|
|
|
let bufferList = BufferListComponent(source: self.sink, fileItemService: self.fileItemService)
|
2016-12-04 22:27:29 +03:00
|
|
|
let bufferListConfig = WorkspaceTool.Config(title: "Buffers",
|
|
|
|
view: bufferList,
|
|
|
|
minimumDimension: 100,
|
2016-12-04 23:19:20 +03:00
|
|
|
withInnerToolbar: true)
|
2016-12-04 22:27:29 +03:00
|
|
|
let bufferListTool = WorkspaceToolComponent(toolIdentifier: .bufferList, config: bufferListConfig)
|
2016-11-22 21:42:11 +03:00
|
|
|
self.tools[.bufferList] = bufferListTool
|
|
|
|
|
2016-12-19 20:23:43 +03:00
|
|
|
let preview = PreviewComponent(source: self.sink)
|
|
|
|
let previewConfig = WorkspaceTool.Config(title: "Preview",
|
|
|
|
view: preview,
|
|
|
|
minimumDimension: 200,
|
|
|
|
withInnerToolbar: true)
|
|
|
|
let previewTool = WorkspaceToolComponent(toolIdentifier: .preview, config: previewConfig)
|
|
|
|
self.tools[.preview] = previewTool
|
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
self.workspace.append(tool: fileBrowserTool, location: fileBrowserToolData.location)
|
2016-11-22 21:42:11 +03:00
|
|
|
self.workspace.append(tool: bufferListTool, location: bufferListToolData.location)
|
2016-12-19 20:23:43 +03:00
|
|
|
self.workspace.append(tool: previewTool, location: previewToolData.location)
|
2016-11-22 21:42:11 +03:00
|
|
|
|
|
|
|
fileBrowserTool.dimension = fileBrowserToolData.dimension
|
|
|
|
bufferListTool.dimension = bufferListToolData.dimension
|
2016-12-19 20:23:43 +03:00
|
|
|
previewTool.dimension = previewToolData.dimension
|
2016-10-03 15:58:49 +03:00
|
|
|
|
|
|
|
if !mainWindowData.isAllToolsVisible {
|
|
|
|
self.toggleAllTools(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mainWindowData.isToolButtonsVisible {
|
|
|
|
self.toggleToolButtons(self)
|
|
|
|
}
|
|
|
|
|
2016-11-19 15:29:18 +03:00
|
|
|
if fileBrowserToolData.isVisible {
|
2016-10-03 16:03:18 +03:00
|
|
|
fileBrowserTool.toggle()
|
2016-10-03 15:58:49 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
if bufferListToolData.isVisible {
|
|
|
|
bufferListTool.toggle()
|
|
|
|
}
|
2016-12-19 20:23:43 +03:00
|
|
|
|
|
|
|
if previewToolData.isVisible {
|
|
|
|
previewTool.toggle()
|
|
|
|
}
|
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-10-03 16:03:18 +03:00
|
|
|
self.window.makeFirstResponder(self.neoVimView)
|
2016-08-25 00:06:39 +03:00
|
|
|
}
|
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() {
|
2016-10-03 10:45:38 +03:00
|
|
|
self.tools.values
|
2016-10-02 15:07:12 +03:00
|
|
|
.map { $0.sink }
|
|
|
|
.toMergedObservables()
|
|
|
|
.subscribe(onNext: { [unowned self] action in
|
|
|
|
switch action {
|
2016-11-12 18:42:10 +03:00
|
|
|
|
2016-10-02 15:07:12 +03:00
|
|
|
case let FileBrowserAction.open(url: url):
|
2016-11-12 18:42:10 +03:00
|
|
|
self.neoVimView.open(urls: [url])
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-11-12 18:42:10 +03:00
|
|
|
case let FileBrowserAction.openInNewTab(url: url):
|
|
|
|
self.neoVimView.openInNewTab(urls: [url])
|
|
|
|
|
|
|
|
case let FileBrowserAction.openInCurrentTab(url: url):
|
|
|
|
self.neoVimView.openInCurrentTab(url: url)
|
|
|
|
|
|
|
|
case let FileBrowserAction.openInHorizontalSplit(url: url):
|
|
|
|
self.neoVimView.openInHorizontalSplit(urls: [url])
|
|
|
|
|
|
|
|
case let FileBrowserAction.openInVerticalSplit(url: url):
|
|
|
|
self.neoVimView.openInVerticalSplit(urls: [url])
|
|
|
|
|
|
|
|
case let FileBrowserAction.setAsWorkingDirectory(url: url):
|
|
|
|
self.neoVimView.cwd = url
|
|
|
|
|
2016-12-07 23:04:47 +03:00
|
|
|
case let FileBrowserAction.scrollToSource(cwd: cwd):
|
2016-12-11 18:14:51 +03:00
|
|
|
guard let curBufUrl = self.neoVimView.currentBuffer()?.url else {
|
2016-12-07 23:04:47 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-11 18:14:51 +03:00
|
|
|
guard curBufUrl.isContained(in: cwd) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.publish(event: MainWindowAction.changeFileBrowserSelection(mainWindow: self, url: curBufUrl))
|
2016-12-07 23:04:47 +03:00
|
|
|
|
2016-11-23 23:46:31 +03:00
|
|
|
case let BufferListAction.open(buffer: buffer):
|
|
|
|
self.neoVimView.select(buffer: buffer)
|
|
|
|
|
2016-10-02 15:07:12 +03:00
|
|
|
default:
|
2016-11-23 23:46:31 +03:00
|
|
|
NSLog("WARN unrecognized action: \(action)")
|
2016-10-02 15:07:12 +03:00
|
|
|
return
|
|
|
|
}
|
2016-11-12 18:42:10 +03:00
|
|
|
|
|
|
|
self.window.makeFirstResponder(self.neoVimView)
|
2016-10-02 15:07:12 +03:00
|
|
|
})
|
|
|
|
.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-12-02 00:53:35 +03:00
|
|
|
let curData = AppearancePrefData(editorFont: self.neoVimView.font,
|
|
|
|
editorLinespacing: self.neoVimView.linespacing,
|
|
|
|
editorUsesLigatures: self.neoVimView.usesLigatures)
|
|
|
|
return appearanceData != curData
|
2016-08-14 16:38:41 +03:00
|
|
|
}
|
2016-11-03 21:57:19 +03:00
|
|
|
.observeOn(MainScheduler.instance)
|
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-10-27 09:21:29 +03:00
|
|
|
self.neoVimView.linespacing = appearance.editorLinespacing
|
2016-12-02 00:53:35 +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-10-03 11:17:30 +03:00
|
|
|
// MARK: - File Menu Item Actions
|
2016-08-11 22:19:03 +03:00
|
|
|
extension MainWindowComponent {
|
2016-11-20 22:46:11 +03:00
|
|
|
|
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-11-05 13:11:03 +03:00
|
|
|
panel.allowsMultipleSelection = 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
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-11-05 17:17:43 +03:00
|
|
|
let urls = panel.urls
|
|
|
|
if self.neoVimView.allBuffers().count == 1 {
|
|
|
|
let isTransient = self.neoVimView.allBuffers().first?.isTransient ?? false
|
|
|
|
if isTransient {
|
|
|
|
self.neoVimView.cwd = FileUtils.commonParent(of: urls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.neoVimView.open(urls: 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-10-12 20:41:18 +03:00
|
|
|
guard let curBuf = self.neoVimView.currentBuffer() else {
|
|
|
|
return
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-11-24 21:46:25 +03:00
|
|
|
if curBuf.url == nil {
|
2016-08-20 20:02:16 +03:00
|
|
|
self.savePanelSheet { self.neoVimView.saveCurrentTab(url: $0) }
|
|
|
|
return
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-08-20 20:02:16 +03:00
|
|
|
self.neoVimView.saveCurrentTab()
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-09-27 01:17:53 +03:00
|
|
|
@IBAction func saveDocumentAs(_ sender: Any?) {
|
2016-10-12 20:41:18 +03:00
|
|
|
if self.neoVimView.currentBuffer() == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-20 20:02:16 +03:00
|
|
|
self.savePanelSheet { url in
|
|
|
|
self.neoVimView.saveCurrentTab(url: url)
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-08-20 20:02:16 +03:00
|
|
|
if self.neoVimView.isCurrentBufferDirty() {
|
|
|
|
self.neoVimView.openInNewTab(urls: [url])
|
|
|
|
} else {
|
|
|
|
self.neoVimView.openInCurrentTab(url: url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
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
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-08-20 20:02:16 +03:00
|
|
|
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-11-20 22:46:11 +03:00
|
|
|
|
2016-08-20 20:02:16 +03:00
|
|
|
alert.runModal()
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
guard let url = panel.url else {
|
2016-08-20 20:02:16 +03:00
|
|
|
showAlert()
|
|
|
|
return
|
|
|
|
}
|
2016-11-20 22:46:11 +03:00
|
|
|
|
2016-08-20 20:02:16 +03:00
|
|
|
action(url)
|
2016-08-12 15:54:37 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 22:19:03 +03:00
|
|
|
}
|
|
|
|
|
2016-10-03 16:03:18 +03:00
|
|
|
// MARK: - Tools Menu Item Actions
|
2016-10-03 11:17:30 +03:00
|
|
|
extension MainWindowComponent {
|
|
|
|
|
|
|
|
@IBAction func toggleAllTools(_ sender: Any?) {
|
|
|
|
self.workspace.toggleAllTools()
|
2016-10-03 16:03:18 +03:00
|
|
|
self.focusNeoVimView(self)
|
2016-10-03 11:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func toggleToolButtons(_ sender: Any?) {
|
|
|
|
self.workspace.toggleToolButtons()
|
|
|
|
}
|
2016-10-03 16:03:18 +03:00
|
|
|
|
|
|
|
@IBAction func toggleFileBrowser(_ sender: Any?) {
|
|
|
|
let fileBrowserTool = self.tools[.fileBrowser]!
|
|
|
|
|
|
|
|
if fileBrowserTool.isSelected {
|
|
|
|
if fileBrowserTool.viewComponent.isFirstResponder {
|
|
|
|
fileBrowserTool.toggle()
|
2016-11-07 01:46:00 +03:00
|
|
|
self.focusNeoVimView(self)
|
2016-10-03 16:03:18 +03:00
|
|
|
} else {
|
|
|
|
fileBrowserTool.viewComponent.beFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileBrowserTool.toggle()
|
|
|
|
fileBrowserTool.viewComponent.beFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func focusNeoVimView(_ sender: Any?) {
|
|
|
|
self.window.makeFirstResponder(self.neoVimView)
|
|
|
|
}
|
2016-10-03 11:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Font Menu Item Actions
|
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-11-19 15:29:18 +03:00
|
|
|
let font = self.fontManager.convert(curFont, toSize: min(curFont.pointSize + 1, NeoVimView.maxFontSize))
|
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-11-19 15:29:18 +03:00
|
|
|
let font = self.fontManager.convert(curFont, toSize: max(curFont.pointSize - 1, NeoVimView.minFontSize))
|
2016-07-28 20:37:39 +03:00
|
|
|
self.neoVimView.font = font
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
// MARK: - NeoVimViewDelegate
|
2016-11-22 01:13:38 +03:00
|
|
|
extension MainWindowComponent {
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-11-20 23:56:57 +03:00
|
|
|
func neoVimStopped() {
|
|
|
|
self.windowController.close()
|
|
|
|
}
|
|
|
|
|
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
|
2016-11-27 12:12:35 +03:00
|
|
|
|
|
|
|
// FIXME: This can still happen...
|
|
|
|
if old == self._cwd {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-07 21:12:18 +03:00
|
|
|
self.fileItemService.unmonitor(url: old)
|
|
|
|
self.fileItemService.monitor(url: self._cwd)
|
2016-09-27 19:02:05 +03:00
|
|
|
|
2016-11-22 01:17:24 +03:00
|
|
|
self.publish(event: MainWindowAction.changeCwd(mainWindow: self, cwd: self._cwd))
|
2016-09-07 21:12:18 +03:00
|
|
|
}
|
2016-11-21 23:47:31 +03:00
|
|
|
|
2016-11-22 01:13:38 +03:00
|
|
|
func bufferListChanged() {
|
2016-11-22 21:42:11 +03:00
|
|
|
let buffers = self.neoVimView.allBuffers()
|
|
|
|
self.publish(event: MainWindowAction.changeBufferList(mainWindow: self, buffers: buffers))
|
2016-11-21 23:47:31 +03:00
|
|
|
}
|
2016-12-05 23:30:25 +03:00
|
|
|
|
2016-12-20 01:57:42 +03:00
|
|
|
func currentBufferChanged() {
|
|
|
|
NSLog("current buffer changed!!!")
|
|
|
|
}
|
|
|
|
|
2016-12-06 00:05:38 +03:00
|
|
|
func ipcBecameInvalid(reason: String) {
|
2016-12-05 23:30:25 +03:00
|
|
|
let alert = NSAlert()
|
|
|
|
alert.addButton(withTitle: "Close")
|
|
|
|
alert.messageText = "Sorry, an error occurred."
|
2016-12-06 00:05:38 +03:00
|
|
|
alert.informativeText = "VimR encountered an error from which it cannot recover. This window will now close.\n"
|
|
|
|
+ reason
|
2016-12-05 23:30:25 +03:00
|
|
|
alert.alertStyle = .critical
|
|
|
|
alert.beginSheetModal(for: self.window) { [weak self] response in
|
|
|
|
self?.windowController.close()
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSWindowDelegate
|
|
|
|
extension MainWindowComponent {
|
2016-11-20 22:46:11 +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-10-03 15:58:49 +03:00
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
let prefData = MainWindowPrefData(isAllToolsVisible: self.workspace.isAllToolsVisible,
|
|
|
|
isToolButtonsVisible: self.workspace.isToolButtonsVisible,
|
|
|
|
toolPrefDatas: self.toolPrefDatas())
|
|
|
|
|
|
|
|
self.publish(event: MainWindowAction.close(mainWindow: self, mainWindowPrefData: prefData))
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func toolPrefDatas() -> [ToolPrefData] {
|
2016-10-03 15:58:49 +03:00
|
|
|
let fileBrowser = self.tools[.fileBrowser]!
|
2016-11-19 15:29:18 +03:00
|
|
|
let fileBrowserData = ToolPrefData(identifier: .fileBrowser,
|
|
|
|
location: fileBrowser.location,
|
|
|
|
isVisible: fileBrowser.isSelected,
|
2016-12-05 00:03:40 +03:00
|
|
|
dimension: fileBrowser.dimension,
|
2016-12-05 20:02:45 +03:00
|
|
|
toolData: fileBrowser.toolData)
|
2016-10-03 15:58:49 +03:00
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
let bufferList = self.tools[.bufferList]!
|
|
|
|
let bufferListData = ToolPrefData(identifier: .bufferList,
|
|
|
|
location: bufferList.location,
|
|
|
|
isVisible: bufferList.isSelected,
|
|
|
|
dimension: bufferList.dimension)
|
|
|
|
|
2016-12-19 20:23:43 +03:00
|
|
|
let preview = self.tools[.preview]!
|
|
|
|
let previewData = ToolPrefData(identifier: .preview,
|
|
|
|
location: preview.location,
|
|
|
|
isVisible: preview.isSelected,
|
|
|
|
dimension: preview.dimension)
|
|
|
|
|
|
|
|
return [ fileBrowserData, bufferListData, previewData ]
|
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-11-20 22:46:11 +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
|
|
|
}
|
2016-10-03 16:03:18 +03:00
|
|
|
|
|
|
|
// MARK: - NSUserInterfaceValidationsProtocol
|
|
|
|
extension MainWindowComponent {
|
|
|
|
|
|
|
|
public func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
|
2016-10-12 20:41:18 +03:00
|
|
|
let canSave = self.neoVimView.currentBuffer() != nil
|
|
|
|
let canSaveAs = canSave
|
|
|
|
let canOpen = canSave
|
|
|
|
let canOpenQuickly = canSave
|
|
|
|
let canFocusNeoVimView = self.window.firstResponder != self.neoVimView
|
|
|
|
|
|
|
|
guard let action = item.action else {
|
2016-10-03 16:03:18 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-10-12 20:41:18 +03:00
|
|
|
switch action {
|
|
|
|
case #selector(focusNeoVimView(_:)):
|
|
|
|
return canFocusNeoVimView
|
|
|
|
|
|
|
|
case #selector(openDocument(_:)):
|
|
|
|
return canOpen
|
|
|
|
|
|
|
|
case #selector(openQuickly(_:)):
|
|
|
|
return canOpenQuickly
|
|
|
|
|
|
|
|
case #selector(saveDocument(_:)):
|
|
|
|
return canSave
|
2016-10-03 16:03:18 +03:00
|
|
|
|
2016-10-12 20:41:18 +03:00
|
|
|
case #selector(saveDocumentAs(_:)):
|
|
|
|
return canSaveAs
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
2016-10-03 16:03:18 +03:00
|
|
|
}
|
|
|
|
}
|