Skip to content

Commit

Permalink
Revert removal of shell.moveItemToTrash… (#1125)
Browse files Browse the repository at this point in the history
* Revert removal of `shell.moveItemToTrash`…

…since it doesn’t work in Electron 12 on Windows.

(Seems to work fine in Electron 30, so we'll just wait until the upgrade to migrate this usage.)

* Revert spec
  • Loading branch information
savetheclocktower authored Nov 5, 2024
1 parent 970f83c commit 6e2674c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 36 deletions.
47 changes: 14 additions & 33 deletions packages/tree-view/lib/tree-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ class TreeView {
return dialog.attach();
}

async removeSelectedEntries() {
removeSelectedEntries() {
let activePath = this.getActivePath();
let selectedPaths, selectedEntries;
if (this.hasFocus()) {
Expand All @@ -896,25 +896,13 @@ class TreeView {
}
}

atom.confirm({
return atom.confirm({
message: `Are you sure you want to delete the selected ${selectedPaths.length > 1 ? 'items' : 'item'}?`,
detailedMessage: `You are deleting:\n${selectedPaths.join('\n')}`,
buttons: ['Move to Trash', 'Cancel']
}, async (response) => {
}, (response) => {
if (response === 0) { // Move to Trash
let failedDeletions = [];
let deletionPromises = [];

// Since this goes async, all entries that correspond to paths we're
// about to delete will soon detach from the tree. So we should figure
// out ahead of time which element we're going to select when we're
// done.
let newSelectedEntry;
let firstSelectedEntry = selectedEntries[0];
if (firstSelectedEntry) {
newSelectedEntry = firstSelectedEntry.closest('.directory:not(.selected)');
}

for (let selectedPath of selectedPaths) {
// Don't delete entries which no longer exist. This can happen, for
// example, when
Expand All @@ -925,24 +913,17 @@ class TreeView {
// but the parent folder is deleted first.
if (!fs.existsSync(selectedPath)) continue;

let meta = { pathToDelete: selectedPath };

this.emitter.emit('will-delete-entry', meta);
this.emitter.emit('will-delete-entry', { pathToDelete: selectedPath });

let promise = shell.trashItem(selectedPath).then(() => {
this.emitter.emit('entry-deleted', meta);
}).catch(() => {
this.emitter.emit('delete-entry-failed', meta);
// TODO: `shell.trashItem` is the favored way to do this.
if (shell.moveItemToTrash(selectedPath)) {
this.emitter.emit('entry-deleted', { pathToDelete: selectedPath });
} else {
this.emitter.emit('delete-entry-failed', { pathToDelete: selectedPath });
failedDeletions.push(selectedPath);
}).finally(() => {
repoForPath(selectedPath)?.getPathStatus(selectedPath);
});

deletionPromises.push(promise);
}
repoForPath(selectedPath)?.getPathStatus(selectedPath);
}

await Promise.allSettled(deletionPromises);

if (failedDeletions.length > 0) {
atom.notifications.addError(
this.formatTrashFailureMessage(failedDeletions),
Expand All @@ -953,9 +934,9 @@ class TreeView {
}
);
}

if (newSelectedEntry) {
this.selectEntry(newSelectedEntry);
let firstSelectedEntry = selectedEntries[0];
if (firstSelectedEntry) {
this.selectEntry(firstSelectedEntry.closest('.directory:not(.selected)'));
}

if (atom.config.get('tree-view.squashDirectoryNames')) {
Expand Down
4 changes: 1 addition & 3 deletions packages/tree-view/spec/tree-view-package-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3042,9 +3042,7 @@ describe("TreeView", function () {
fileView.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}));
treeView.focus();

spyOn(shell, 'trashItem').andCallFake(() => {
return Promise.reject(false);
})
spyOn(shell, 'moveItemToTrash').andReturn(false);

spyOn(atom, 'confirm').andCallFake((options, callback) => callback(0));

Expand Down

0 comments on commit 6e2674c

Please sign in to comment.