Skip to content

Commit

Permalink
Merge pull request #144 from kishikawakatsumi/newfolder
Browse files Browse the repository at this point in the history
Automatic naming for creating new folder
  • Loading branch information
kishikawakatsumi authored Sep 19, 2024
2 parents 1a2683c + d662a9e commit bf18a9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ class DirectoryStructure {
viewTree.nodes.count
}

func rootNodes() -> [FileNode] {
viewTree.rootNodes()
}

func parent(of node: FileNode) -> FileNode? {
viewTree.parent(of: node)
}
Expand Down
30 changes: 21 additions & 9 deletions Examples/FileBrowser/FileBrowser (macOS)/FilesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,29 @@ class FilesViewController: NSViewController {
}
}

func createDirectory(in parent: String? = nil) {
Task {
let name = NSLocalizedString("untitled folder", comment: "")
if let parent {
await dirTree.reload(directory: parent, outlineView)
func createNewFolder() {
let filenames = dirTree
.rootNodes()
.map { $0.name }

let filename = {
let baseName = NSLocalizedString("untitled folder", comment: "")
if !filenames.contains(baseName) {
return baseName
} else {
try await client.createDirectory(path: join(path, name))
await dirTree.reload(directory: path, outlineView)
var i = 2
while true {
let name = "\(baseName) \(i)"
if !filenames.contains(name) {
return name
}
i += 1
}
}
}()
Task {
try await client.createDirectory(path: join(path, filename))
await dirTree.reload(directory: path, outlineView)
}
}

Expand Down Expand Up @@ -616,8 +630,6 @@ extension FilesViewController: NSMenuItemValidation {
let targetRows = outlineView.targetRows()

switch menuItem.action {
case Selector({ "newFolderAction(_:)" }()):
return true
case #selector(openMenuAction(_:)):
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 @@ -179,7 +179,7 @@ class WindowController: NSWindowController {
guard let navigationController = navigationController() else { return }
guard let filesViewController = navigationController.topViewController as? FilesViewController else { return }

filesViewController.createDirectory()
filesViewController.createNewFolder()
}

@objc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"location" : "https://github.com/kishikawakatsumi/SMBClient.git",
"state" : {
"branch" : "main",
"revision" : "5a3ce1eafede6ab8cd98ad7fde01a571ccf81bce"
"revision" : "1a2683c12ff2a050f500db293ce0378bdb056411"
}
}
],
Expand Down

0 comments on commit bf18a9a

Please sign in to comment.