disk selection fix

This commit is contained in:
Radek Davidek 2026-01-20 18:48:13 +01:00
parent a3f97f9369
commit 7cbff72d38

View File

@ -19,6 +19,7 @@ public class FilePanel extends JPanel {
private JLabel driveInfoLabel;
private cz.kamma.kfmanager.config.AppConfig appConfig;
private Runnable onDirectoryChangedAll;
private boolean ignoreComboActions = false;
public FilePanel(String initialPath) {
initComponents();
@ -112,12 +113,22 @@ public class FilePanel extends JPanel {
driveInfoLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
driveInfoLabel.setBorder(BorderFactory.createEmptyBorder(0,6,0,6));
// Require explicit confirmation (Enter) to load selected drive.
// Selection changes (mouse/typing) only update the selected item; loading occurs on Enter.
driveCombo.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0), "confirmDrive");
driveCombo.getActionMap().put("confirmDrive", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// Interaction behavior:
// - Mouse click in dropdown: Refresh immediately.
// - Keyboard navigation (arrows): Update info label only, refresh only on Enter.
driveCombo.addActionListener(e -> {
if (ignoreComboActions) return;
// Determine if we should trigger directory load.
// We want to skip arrow keys and only react to Mouse or Enter.
java.awt.AWTEvent currentEvent = java.awt.EventQueue.getCurrentEvent();
if (currentEvent instanceof java.awt.event.KeyEvent) {
java.awt.event.KeyEvent ke = (java.awt.event.KeyEvent) currentEvent;
if (ke.getKeyCode() != java.awt.event.KeyEvent.VK_ENTER) {
return;
}
}
Object selObj = driveCombo.getSelectedItem();
if (selObj instanceof File) {
File sel = (File) selObj;
@ -129,7 +140,6 @@ public class FilePanel extends JPanel {
});
}
}
}
});
// Compose a small left panel with the combo and info label
JPanel leftPanel = new JPanel();
@ -413,6 +423,8 @@ public class FilePanel extends JPanel {
}
private void populateDrives() {
ignoreComboActions = true;
try {
driveCombo.removeAllItems();
java.util.Set<File> driveSet = new java.util.LinkedHashSet<>();
@ -472,6 +484,9 @@ public class FilePanel extends JPanel {
// Update info for currently selected drive
updateDriveInfoFromSelection();
} finally {
ignoreComboActions = false;
}
// Update info label on selection changes (visual selection only)
driveCombo.addItemListener(ev -> {
@ -577,6 +592,7 @@ public class FilePanel extends JPanel {
FilePanelTab currentTab = getCurrentTab();
if (currentTab != null) {
// Update drive combo selection to match current directory
ignoreComboActions = true;
try {
File cur = currentTab.getCurrentDirectory();
if (cur != null) {
@ -585,7 +601,10 @@ public class FilePanel extends JPanel {
// also update info label to reflect current tab's root
updateDriveInfo(root);
}
} catch (Exception ignore) {}
} catch (Exception ignore) {
} finally {
ignoreComboActions = false;
}
}
}