Skip to content

Commit

Permalink
Merge pull request #103 from kishikawakatsumi/newfolder
Browse files Browse the repository at this point in the history
Add "Create new folder" action
  • Loading branch information
kishikawakatsumi authored Aug 5, 2024
2 parents e931cdd + 18b668b commit c66ca18
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<action selector="newDocument:" target="Ady-hI-5gd" id="4Si-XN-c54"/>
</connections>
</menuItem>
<menuItem title="New Folder" keyEquivalent="N" id="O6M-Jf-eUY">
<connections>
<action selector="newFolderAction:" target="Ady-hI-5gd" id="Rg2-z9-hIT"/>
</connections>
</menuItem>
<menuItem title="New Tab" keyEquivalent="t" id="08g-95-MgD">
<connections>
<action selector="newWindowForTab:" target="Voe-Tx-rLC" id="tiD-UI-SVZ"/>
Expand Down
14 changes: 14 additions & 0 deletions Examples/FileBrowser/FileBrowser (macOS)/FilesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ class FilesViewController: NSViewController {
}
}

func createDirectory(in parent: String? = nil) {
Task {
let name = NSLocalizedString("untitled folder", comment: "")
if let parent {
await dirTree.reload(directory: join(parent, name), outlineView)
} else {
try await client.createDirectory(path: join(path, name))
await dirTree.reload(directory: path, outlineView)
}
}
}

@IBAction func openMenuAction(_ sender: Any?) {
guard let fileNode = outlineView.item(atRow: outlineView.selectedRow) as? FileNode else { return }
openFileNode(fileNode)
Expand Down Expand Up @@ -584,6 +596,8 @@ extension FilesViewController: NSMenuItemValidation {
let selectedRows = outlineView.selectedRowIndexes

switch menuItem.title {
case "New Folder":
return true
case "Open":
guard selectedRows.count == 1 else { return false }
guard let selectedRow = selectedRows.first else { return false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
},
"Connect" : {

},
"New Folder" : {

},
"Queued" : {
"comment" : "(progressBytes) of (totalBytes)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ class NavigationController: NSViewController {
current < history.count - 1
}

func currentViewController() -> NSViewController {
history[current]
func currentViewController() -> NSViewController? {
guard !history.isEmpty else { return nil }
return history[current]
}

private func replace(_ viewController: NSViewController) {
Expand Down
80 changes: 60 additions & 20 deletions Examples/FileBrowser/FileBrowser (macOS)/WindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ class WindowController: NSWindowController {
segmentedControl.setEnabled(navigationController.canGoBack(), forSegment: 0)
segmentedControl.setEnabled(navigationController.canGoForward(), forSegment: 1)

window?.title = navigationController.currentViewController().title ?? ""
window?.title = navigationController.currentViewController()?.title ?? ""
}

@IBAction
private func newFolderAction(_ sender: NSToolbarItem) {
guard let navigationController = navigationController() else { return }
guard let filesViewController = navigationController.topViewController as? FilesViewController else { return }

filesViewController.createDirectory()
}

@objc
Expand Down Expand Up @@ -105,7 +113,7 @@ class WindowController: NSWindowController {
guard let toolbarItems = window?.toolbar?.items else {
return
}
guard let toolbarItem = toolbarItems.first(where: { $0.itemIdentifier == .activitiesToolbarItemIdentifer }) else {
guard let toolbarItem = toolbarItems.first(where: { $0.itemIdentifier == .activitiesToolbarItemIdentifier }) else {
return
}

Expand All @@ -118,13 +126,24 @@ class WindowController: NSWindowController {
}
}

extension WindowController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.title == "New Folder" {
guard let navigationController = navigationController() else { return false }
return navigationController.topViewController is FilesViewController
}
return false
}
}

extension WindowController: NSToolbarDelegate {
func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
[
.navigationToolbarItemIdentifer,
.connectToServerToolbarItemIdentifer,
.activitiesToolbarItemIdentifer,
.searchToolbarItemIdentifer,
.navigationToolbarItemIdentifier,
.newFolderToolbarItemIdentifier,
.connectToServerToolbarItemIdentifier,
.activitiesToolbarItemIdentifier,
.searchToolbarItemIdentifier,
]
}

Expand All @@ -134,13 +153,13 @@ extension WindowController: NSToolbarDelegate {
willBeInsertedIntoToolbar flag: Bool
) -> NSToolbarItem? {
switch itemIdentifier {
case .navigationToolbarItemIdentifer:
case .navigationToolbarItemIdentifier:
let group = NSToolbarItemGroup(itemIdentifier: itemIdentifier)

let back = NSToolbarItem(itemIdentifier: .backToolbarItemIdentifer)
let back = NSToolbarItem(itemIdentifier: .backToolbarItemIdentifier)
back.label = "Back"

let forward = NSToolbarItem(itemIdentifier: .forwardItemIdentifer)
let forward = NSToolbarItem(itemIdentifier: .forwardItemIdentifier)
forward.label = "Forward"

segmentedControl.segmentStyle = .separated
Expand All @@ -165,7 +184,16 @@ extension WindowController: NSToolbarDelegate {
group.view = segmentedControl

return group
case .connectToServerToolbarItemIdentifer:
case .newFolderToolbarItemIdentifier:
let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)

toolbarItem.isBordered = true
toolbarItem.image = NSImage(systemSymbolName: "folder.badge.plus", accessibilityDescription: nil)
toolbarItem.label = NSLocalizedString("New Folder", comment: "")
toolbarItem.action = #selector(newFolderAction(_:))

return toolbarItem
case .connectToServerToolbarItemIdentifier:
let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)

toolbarItem.isBordered = true
Expand All @@ -174,8 +202,7 @@ extension WindowController: NSToolbarDelegate {
toolbarItem.action = #selector(connectToServerAction(_:))

return toolbarItem

case .activitiesToolbarItemIdentifer:
case .activitiesToolbarItemIdentifier:
let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)

toolbarItem.isBordered = true
Expand All @@ -184,7 +211,7 @@ extension WindowController: NSToolbarDelegate {
toolbarItem.action = #selector(activitiesAction(_:))

return toolbarItem
case .searchToolbarItemIdentifer:
case .searchToolbarItemIdentifier:
let toolbarItem = NSSearchToolbarItem(itemIdentifier: itemIdentifier)
toolbarItem.searchField.delegate = self
searchField = toolbarItem.searchField
Expand All @@ -195,6 +222,18 @@ extension WindowController: NSToolbarDelegate {
}
}

extension WindowController: NSToolbarItemValidation {
func validateToolbarItem(_ item: NSToolbarItem) -> Bool {
guard let navigationController = navigationController() else { return false }

if item.itemIdentifier == .newFolderToolbarItemIdentifier {
return navigationController.topViewController is FilesViewController
}

return true
}
}

extension WindowController: NSSearchFieldDelegate {
@objc
func controlTextDidChange(_ notification : Notification){
Expand Down Expand Up @@ -222,11 +261,12 @@ extension WindowControllerUserInfoKey {
}

private extension NSToolbarItem.Identifier {
static let navigationToolbarItemIdentifer = NSToolbarItem.Identifier("NavigationToolbarItem")
static let backToolbarItemIdentifer = NSToolbarItem.Identifier("BackToolbarItem")
static let forwardItemIdentifer = NSToolbarItem.Identifier("ForwardToolbarItem")

static let connectToServerToolbarItemIdentifer = NSToolbarItem.Identifier("ConnectToServerToolbarItem")
static let activitiesToolbarItemIdentifer = NSToolbarItem.Identifier("ActivitiesToolbbarItem")
static let searchToolbarItemIdentifer = NSToolbarItem.Identifier("SearchToolbarItem")
static let navigationToolbarItemIdentifier = NSToolbarItem.Identifier("NavigationToolbarItem")
static let backToolbarItemIdentifier = NSToolbarItem.Identifier("BackToolbarItem")
static let forwardItemIdentifier = NSToolbarItem.Identifier("ForwardToolbarItem")

static let newFolderToolbarItemIdentifier = NSToolbarItem.Identifier("NewFolderToolbarItem")
static let connectToServerToolbarItemIdentifier = NSToolbarItem.Identifier("ConnectToServerToolbarItem")
static let activitiesToolbarItemIdentifier = NSToolbarItem.Identifier("ActivitiesToolbbarItem")
static let searchToolbarItemIdentifier = NSToolbarItem.Identifier("SearchToolbarItem")
}

0 comments on commit c66ca18

Please sign in to comment.