Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic naming for creating new folder #144

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading