Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "1.0.1"
version = "1.0.2"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.SwingUtilities;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static void moveDirectoryToTrashOrDeleteRecursively(File directoryToDelet
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.MOVE_TO_TRASH)) {
logger.debug("Attempting to move {} to trash", directoryToDelete);
if (!desktop.moveToTrash(directoryToDelete)) {
if (!moveToTrash(desktop, directoryToDelete)) {
logger.debug("Failed to move {} to trash. Deleting it", directoryToDelete);
deleteDirectoryRecursively(directoryToDelete);
}
Expand Down Expand Up @@ -127,6 +128,25 @@ public static boolean isFileParentOfAnotherFile(File possibleParent, File possib
return false;
}

private static boolean moveToTrash(Desktop desktop, File fileToDelete) {
if (SwingUtilities.isEventDispatchThread() || !isWindows()) {
// It seems safe to call move to trash from any thread on macOS and Linux
// We can't use the EDT on macOS because of https://bugs.openjdk.org/browse/JDK-8087465
return desktop.moveToTrash(fileToDelete);
} else {
// EXCEPTION_ACCESS_VIOLATION associated with moveToTrash reported on Windows 11.
// https://github.com/qupath/qupath/issues/1738
// Could not be replicated (but we didn't have Windows 11...); taking a guess that this might help.
try {
SwingUtilities.invokeAndWait(() -> moveToTrash(desktop, fileToDelete));
} catch (Exception e) {
logger.warn("Cannot move file {} to trash", fileToDelete, e);
return false;
}
return !fileToDelete.exists();
}
}

private static void deleteDirectoryRecursively(File directoryToBeDeleted) throws IOException {
logger.debug("Deleting children of {}", directoryToBeDeleted);
File[] childFiles = directoryToBeDeleted.listFiles();
Expand All @@ -139,4 +159,8 @@ private static void deleteDirectoryRecursively(File directoryToBeDeleted) throws
logger.debug("Deleting {}", directoryToBeDeleted);
Files.deleteIfExists(directoryToBeDeleted.toPath());
}

private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}
Loading