Skip to content

Commit

Permalink
Merge pull request #1005 from EdgeJ/file-handling
Browse files Browse the repository at this point in the history
Add basic file handling to the filebrowser
  • Loading branch information
qvacua authored Oct 31, 2023
2 parents 732c490 + 04edf2e commit 555618b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
19 changes: 17 additions & 2 deletions VimR/VimR/Base.lproj/FileBrowserMenu.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="FileOutlineView" customModule="VimR" customModuleProvider="target">
Expand Down Expand Up @@ -45,13 +46,27 @@
<action selector="openInVerticalSplit:" target="-1" id="oxd-lh-ci7"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="pj6-EL-86D"/>
<menuItem title="New File" id="6kx-Fi-Vxq">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="newFile:" target="-1" id="EZe-IK-FHt"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="m5x-pf-2gI"/>
<menuItem title="Set As Working Directory" id="5Ph-01-gFz">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setAsWorkingDirectory:" target="-1" id="tpe-qN-Zn6"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="ofn-GH-Cnm"/>
<menuItem title="Move to Trash" id="tHT-Cg-8xR">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="deleteFile:" target="-1" id="IqF-eg-9H7"/>
</connections>
</menuItem>
</items>
<point key="canvasLocation" x="97" y="-58"/>
</menu>
Expand Down
75 changes: 71 additions & 4 deletions VimR/VimR/FileOutlineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,66 @@ extension FileOutlineView {

self.emit(UuidAction(uuid: self.uuid, action: .open(url: node.url, mode: .verticalSplit)))
}

@IBAction func newFile(_: Any?){
guard let node = self.node(from: self.clickedItem) else { return }

guard node.url.hasDirectoryPath else { return }

let panel = NSSavePanel()
panel.directoryURL = node.url
panel.nameFieldLabel = "Filename"
panel.prompt = "Open"
panel.showsTagField = false
panel.beginSheetModal(for: self.window!) { result in
guard result == .OK else {
return
}

let showAlert: () -> Void = {
let alert = NSAlert()
alert.addButton(withTitle: "OK")
alert.messageText = "Invalid File Name"
alert
.informativeText =
"The file name you have entered cannot be used. Please use a different name."
alert.alertStyle = .warning

alert.runModal()
}

guard let url = panel.url else {
showAlert()
return
}

self.emit(UuidAction(uuid: self.uuid, action: .open(url: url, mode: .newTab)))
}
}

@IBAction func deleteFile(_: Any?) {
guard let node = self.node(from: self.clickedItem) else { return }

let fileManager = FileManager.default

let showAlert: () -> Void = {
let alert = NSAlert()
alert.addButton(withTitle: "OK")
alert.messageText = "Could not move file to Trash"
alert.alertStyle = .warning

alert.runModal()
}

do{
try fileManager.trashItem(at: node.url, resultingItemURL: nil)
} catch {
showAlert()
return
}

self.emit(UuidAction(uuid: self.uuid, action: .refresh))
}

@IBAction func setAsWorkingDirectory(_: Any?) {
guard let node = self.node(from: self.clickedItem) else { return }
Expand Down Expand Up @@ -461,11 +521,18 @@ extension FileOutlineView {
override func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
guard let clickedNode = self.node(from: self.clickedItem) else { return true }

if item
.action ==
#selector(self.setAsWorkingDirectory(_:)) { return clickedNode.url.hasDirectoryPath }
var isValid: Bool

switch(item.action) {
case #selector(self.setAsWorkingDirectory(_:)):
isValid = clickedNode.url.hasDirectoryPath
case #selector(self.newFile(_:)):
isValid = clickedNode.url.hasDirectoryPath
default:
isValid = true
}

return true
return isValid
}
}

Expand Down

0 comments on commit 555618b

Please sign in to comment.