fixed root button

This commit is contained in:
Radek Davidek 2026-03-11 09:36:20 +01:00
parent effadc84fe
commit e191561e9c

View File

@ -189,8 +189,7 @@ public class FilePanel extends JPanel {
rootButton.setToolTipText("Computer / Root");
rootButton.addActionListener(e -> {
if (MainApp.CURRENT_OS == MainApp.OS.WINDOWS) {
// On Windows, the "root" is typically C:\
loadDirectory(new File("C:\\"));
loadDirectory(resolveWindowsCurrentDriveRoot());
} else {
loadDirectory(new File("/"));
}
@ -766,6 +765,34 @@ public class FilePanel extends JPanel {
double gb = bytes / 1024.0 / 1024.0 / 1024.0;
return "%.1f".formatted(gb);
}
private File resolveWindowsCurrentDriveRoot() {
// Prefer root of the active tab's current directory, e.g. D:\\ when browsing D:.
File currentDir = getCurrentDirectory();
if (currentDir != null) {
try {
java.nio.file.Path rootPath = currentDir.toPath().toAbsolutePath().normalize().getRoot();
if (rootPath != null) {
return rootPath.toFile();
}
} catch (Exception ignore) {
}
}
// Fallback to selected drive in dropdown.
Object selected = driveCombo != null ? driveCombo.getSelectedItem() : null;
if (selected instanceof File drive) {
return drive;
}
// Final fallback: first available filesystem root.
File[] roots = File.listRoots();
if (roots != null && roots.length > 0) {
return roots[0];
}
return new File("C:\\");
}
/**
* Request focus on the table in the current tab