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.setToolTipText("Computer / Root");
rootButton.addActionListener(e -> { rootButton.addActionListener(e -> {
if (MainApp.CURRENT_OS == MainApp.OS.WINDOWS) { if (MainApp.CURRENT_OS == MainApp.OS.WINDOWS) {
// On Windows, the "root" is typically C:\ loadDirectory(resolveWindowsCurrentDriveRoot());
loadDirectory(new File("C:\\"));
} else { } else {
loadDirectory(new File("/")); loadDirectory(new File("/"));
} }
@ -767,6 +766,34 @@ public class FilePanel extends JPanel {
return "%.1f".formatted(gb); 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 * Request focus on the table in the current tab
*/ */