Skip to content

Commit b134f55

Browse files
authored
fix: distinguish between Move to Trash and Delete Immediately when right clicking a file or folder. (#1694)
1 parent e69a433 commit b134f55

2 files changed

Lines changed: 71 additions & 11 deletions

File tree

CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFileManager+FileManagement.swift

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,73 @@ extension CEWorkspaceFileManager {
8888
)
8989
}
9090

91-
/// This function deletes the item or folder from the current project
91+
/// This function deletes the item or folder from the current project by moving to Trash
92+
/// - Parameters:
93+
/// - file: The file or folder to delete
94+
/// - Authors: Paul Ebose
95+
public func trash(file: CEWorkspaceFile) {
96+
let message: String
97+
98+
if !file.isFolder || file.isEmptyFolder { // if its a file or an empty folder, call it by its name
99+
message = file.name
100+
} else {
101+
let fileCount = fileManager.enumerator(atPath: file.url.path)?.allObjects.count
102+
message = "the \((fileCount ?? 0) + 1) selected items"
103+
}
104+
105+
let moveFileToTrashAlert = NSAlert()
106+
moveFileToTrashAlert.messageText = "Do you want to move \(message) to Trash?"
107+
moveFileToTrashAlert.informativeText = "This operation cannot be undone."
108+
moveFileToTrashAlert.alertStyle = .critical
109+
moveFileToTrashAlert.addButton(withTitle: "Move to Trash")
110+
moveFileToTrashAlert.buttons.last?.hasDestructiveAction = true
111+
moveFileToTrashAlert.addButton(withTitle: "Cancel")
112+
113+
if moveFileToTrashAlert.runModal() == .alertFirstButtonReturn { // "Move to Trash" button
114+
if fileManager.fileExists(atPath: file.url.path) {
115+
do {
116+
try fileManager.trashItem(at: file.url, resultingItemURL: nil)
117+
} catch {
118+
print(error.localizedDescription)
119+
}
120+
}
121+
}
122+
}
123+
124+
/// This function deletes the item or folder from the current project by erasing immediately.
92125
/// - Parameters:
93126
/// - file: The file to delete
94127
/// - confirmDelete: True to present an alert to confirm the delete.
95-
/// - Authors: Mattijs Eikelenboom, KaiTheRedNinja. *Moved from 7c27b1e*
128+
/// - Authors: Mattijs Eikelenboom, KaiTheRedNinja., Paul Ebose *Moved from 7c27b1e*
96129
public func delete(file: CEWorkspaceFile, confirmDelete: Bool = true) {
97130
// This function also has to account for how the
98131
// - file system can change outside of the editor
99-
let deleteConfirmation = NSAlert()
100-
let message: String
132+
let messageText: String
133+
let informativeText: String
134+
101135
if !file.isFolder || file.isEmptyFolder { // if its a file or an empty folder, call it by its name
102-
message = file.name
136+
messageText = "Are you sure you want to delete \"\(file.name)\"?"
137+
informativeText = "This item will be deleted immediately. You can't undo this action."
103138
} else {
104139
let childrenCount = try? fileManager.contentsOfDirectory(
105140
at: file.url,
106141
includingPropertiesForKeys: nil
107142
).count
108-
message = "the \((childrenCount ?? 0) + 1) selected items"
143+
144+
if let childrenCount {
145+
messageText = "Are you sure you want to delete the \((childrenCount) + 1) selected items?"
146+
informativeText = "\(childrenCount) items will be deleted immediately. You can't undo this action."
147+
} else {
148+
messageText = "Are you sure you want to delete the selected items?"
149+
informativeText = "Selected items will be deleted immediately. You can't undo this action."
150+
}
109151
}
110-
deleteConfirmation.messageText = "Do you want to move \(message) to the Trash?"
111-
deleteConfirmation.informativeText = "This operation cannot be undone"
152+
153+
let deleteConfirmation = NSAlert()
154+
deleteConfirmation.messageText = messageText
155+
deleteConfirmation.informativeText = informativeText
112156
deleteConfirmation.alertStyle = .critical
113-
deleteConfirmation.addButton(withTitle: "Move to Trash")
157+
deleteConfirmation.addButton(withTitle: "Delete")
114158
deleteConfirmation.buttons.last?.hasDestructiveAction = true
115159
deleteConfirmation.addButton(withTitle: "Cancel")
116160
if !confirmDelete || deleteConfirmation.runModal() == .alertFirstButtonReturn { // "Delete" button

CodeEdit/Features/NavigatorArea/ProjectNavigator/OutlineView/ProjectNavigatorMenu.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ final class ProjectNavigatorMenu: NSMenu {
5959
let newFolder = menuItem("New Folder", action: #selector(newFolder))
6060

6161
let rename = menuItem("Rename", action: #selector(renameFile))
62-
let delete = menuItem("Delete", action:
62+
63+
let trash = menuItem("Move to Trash", action:
64+
item.url != workspace?.workspaceFileManager?.folderUrl
65+
? #selector(trash) : nil)
66+
67+
// trash has to be the previous menu item for delete.isAlternate to work correctly
68+
let delete = menuItem("Delete Immediately...", action:
6369
item.url != workspace?.workspaceFileManager?.folderUrl
6470
? #selector(delete) : nil)
71+
delete.keyEquivalentModifierMask = .option
72+
delete.isAlternate = true
6573

6674
let duplicate = menuItem("Duplicate \(item.isFolder ? "Folder" : "File")", action: #selector(duplicate))
6775

@@ -87,6 +95,7 @@ final class ProjectNavigatorMenu: NSMenu {
8795
newFolder,
8896
NSMenuItem.separator(),
8997
rename,
98+
trash,
9099
delete,
91100
duplicate,
92101
NSMenuItem.separator(),
@@ -229,7 +238,14 @@ final class ProjectNavigatorMenu: NSMenu {
229238
outlineView.window?.makeFirstResponder(cell.textField)
230239
}
231240

232-
/// Action that deletes the item.
241+
/// Action that moves the item to trash.
242+
@objc
243+
private func trash() {
244+
guard let item else { return }
245+
workspace?.workspaceFileManager?.trash(file: item)
246+
}
247+
248+
/// Action that deletes the item immediately.
233249
@objc
234250
private func delete() {
235251
guard let item else { return }

0 commit comments

Comments
 (0)