fixed focus on restart app

This commit is contained in:
rdavidek 2026-01-18 11:59:43 +01:00
parent 5454ac5a5c
commit 3b1065bc81
5 changed files with 104 additions and 28 deletions

View File

@ -4,7 +4,6 @@ import cz.kamma.kfmanager.ui.MainWindow;
import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
/**

View File

@ -94,6 +94,14 @@ public class AppConfig {
properties.setProperty("window.maximized", String.valueOf(maximized));
}
public String getActivePanel() {
return properties.getProperty("active.panel", "left");
}
public void setActivePanel(String panel) {
properties.setProperty("active.panel", panel);
}
public String getLeftPanelPath() {
return properties.getProperty("leftPanel.path", System.getProperty("user.home"));
}

View File

@ -21,7 +21,7 @@ public class FilePanel extends JPanel {
public FilePanel(String initialPath) {
initComponents();
addNewTab(initialPath);
addNewTab(initialPath, false);
}
public void setOnDirectoryChangedAll(Runnable callback) {
@ -215,10 +215,14 @@ public class FilePanel extends JPanel {
* Add a new tab with a directory
*/
public void addNewTab(String path) {
addNewTab(path, true);
}
public void addNewTab(String path, boolean requestFocus) {
// Get view mode from current tab
ViewMode currentMode = getViewMode();
FilePanelTab tab = new FilePanelTab(path);
FilePanelTab tab = new FilePanelTab(path, requestFocus);
if (appConfig != null) tab.setAppConfig(appConfig);
// Set callback for updating tab title on directory change
@ -244,12 +248,13 @@ public class FilePanel extends JPanel {
addMouseListenerToComponents(tab);
// Update path field
updatePathField();
updateTabStyles();
// Set focus to the table in the new tab
SwingUtilities.invokeLater(() -> {
if (requestFocus) {
tab.getFileTable().requestFocusInWindow();
}
// Ensure renderers are attached now that the tab is added to the UI
tab.ensureRenderers();
});
@ -259,7 +264,11 @@ public class FilePanel extends JPanel {
* Add a new tab and explicitly set the ViewMode for this tab.
*/
public void addNewTabWithMode(String path, ViewMode mode) {
FilePanelTab tab = new FilePanelTab(path);
addNewTabWithMode(path, mode, true);
}
public void addNewTabWithMode(String path, ViewMode mode, boolean requestFocus) {
FilePanelTab tab = new FilePanelTab(path, requestFocus);
if (appConfig != null) tab.setAppConfig(appConfig);
tab.setOnDirectoryChanged(() -> {
updateTabTitle(tab);
@ -282,7 +291,9 @@ public class FilePanel extends JPanel {
updateTabStyles();
SwingUtilities.invokeLater(() -> {
if (requestFocus) {
tab.getFileTable().requestFocusInWindow();
}
tab.ensureRenderers();
});
}
@ -346,6 +357,10 @@ public class FilePanel extends JPanel {
* Restore the set of tabs according to specified paths and view modes. If the list is empty, nothing happens.
*/
public void restoreTabs(java.util.List<String> paths, java.util.List<String> viewModes, java.util.List<String> focusedItems, int selectedIndex) {
restoreTabs(paths, viewModes, focusedItems, selectedIndex, true);
}
public void restoreTabs(java.util.List<String> paths, java.util.List<String> viewModes, java.util.List<String> focusedItems, int selectedIndex, boolean requestFocus) {
if (paths == null || paths.isEmpty()) return;
tabbedPane.removeAll();
@ -360,13 +375,13 @@ public class FilePanel extends JPanel {
mode = ViewMode.FULL;
}
}
addNewTabWithMode(p, mode);
addNewTabWithMode(p, mode, false);
// Restore focus to the specific item if provided
if (focusedItems != null && i < focusedItems.size() && focusedItems.get(i) != null) {
final String focusName = focusedItems.get(i);
final FilePanelTab currentTab = (FilePanelTab) tabbedPane.getComponentAt(i);
SwingUtilities.invokeLater(() -> currentTab.selectItem(focusName));
SwingUtilities.invokeLater(() -> currentTab.selectItem(focusName, requestFocus));
}
}
@ -639,9 +654,13 @@ public class FilePanel extends JPanel {
}
public void setViewMode(ViewMode mode) {
setViewMode(mode, true);
}
public void setViewMode(ViewMode mode, boolean requestFocus) {
FilePanelTab tab = getCurrentTab();
if (tab != null) {
tab.setViewMode(mode);
tab.setViewMode(mode, requestFocus);
}
}

View File

@ -64,9 +64,13 @@ public class FilePanelTab extends JPanel {
private File currentArchiveSourceFile = null;
public FilePanelTab(String initialPath) {
this(initialPath, true);
}
public FilePanelTab(String initialPath, boolean requestFocus) {
this.currentDirectory = new File(initialPath);
initComponents();
loadDirectory(currentDirectory);
loadDirectory(currentDirectory, true, requestFocus);
}
/** Start inline rename for currently selected item (if single selection). */
@ -542,7 +546,7 @@ public class FilePanelTab extends JPanel {
// Re-select item
if (selectedItemName != null) {
selectItemByName(selectedItemName);
selectItemByName(selectedItemName, false);
}
});
}
@ -1411,6 +1415,10 @@ public class FilePanelTab extends JPanel {
}
private void selectItemByName(String name) {
selectItemByName(name, true);
}
private void selectItemByName(String name, boolean requestFocus) {
if (viewMode == ViewMode.BRIEF) {
for (int i = 0; i < tableModel.items.size(); i++) {
FileItem item = tableModel.items.get(i);
@ -1423,7 +1431,9 @@ public class FilePanelTab extends JPanel {
fileTable.setRowSelectionInterval(row, row);
fileTable.scrollRectToVisible(fileTable.getCellRect(row, column, true));
fileTable.repaint();
if (requestFocus) {
fileTable.requestFocusInWindow();
}
updateStatus();
}
return;
@ -1435,7 +1445,9 @@ public class FilePanelTab extends JPanel {
if (item != null && item.getName().equals(name)) {
fileTable.setRowSelectionInterval(i, i);
fileTable.scrollRectToVisible(fileTable.getCellRect(i, 0, true));
if (requestFocus) {
fileTable.requestFocusInWindow();
}
updateStatus();
return;
}
@ -1448,7 +1460,11 @@ public class FilePanelTab extends JPanel {
* Useful for other UI components to request focusing a specific file.
*/
public void selectItem(String name) {
selectItemByName(name);
selectItemByName(name, true);
}
public void selectItem(String name, boolean requestFocus) {
selectItemByName(name, requestFocus);
}
public void toggleSelectionAndMoveDown() {
@ -1678,6 +1694,10 @@ public class FilePanelTab extends JPanel {
}
public void setViewMode(ViewMode mode) {
setViewMode(mode, true);
}
public void setViewMode(ViewMode mode, boolean requestFocus) {
if (this.viewMode != mode) {
String selectedItemName = null;
int selectedRow = fileTable.getSelectedRow();
@ -1715,12 +1735,14 @@ public class FilePanelTab extends JPanel {
fileTable.repaint();
if (itemToSelect != null) {
selectItemByName(itemToSelect);
selectItemByName(itemToSelect, requestFocus);
} else if (fileTable.getRowCount() > 0) {
fileTable.setRowSelectionInterval(0, 0);
}
if (requestFocus) {
fileTable.requestFocusInWindow();
}
});
}
}

View File

@ -55,9 +55,18 @@ public class MainWindow extends JFrame {
}
});
// After start, set focus and selection to the left panel
// After start, set focus and selection to the active panel
String initialActiveSide = config.getActivePanel();
SwingUtilities.invokeLater(() -> {
leftPanel.getFileTable().requestFocus();
if ("right".equalsIgnoreCase(initialActiveSide)) {
activePanel = rightPanel;
rightPanel.requestFocusOnCurrentTab();
} else {
activePanel = leftPanel;
leftPanel.requestFocusOnCurrentTab();
}
updateActivePanelBorder();
updateCommandLinePrompt();
});
}
@ -94,7 +103,7 @@ public class MainWindow extends JFrame {
// Load and set ViewMode for left panel
try {
ViewMode leftViewMode = ViewMode.valueOf(config.getLeftPanelViewMode());
leftPanel.setViewMode(leftViewMode);
leftPanel.setViewMode(leftViewMode, false);
} catch (IllegalArgumentException e) {
// Default value FULL is already set
}
@ -111,7 +120,7 @@ public class MainWindow extends JFrame {
// Load and set ViewMode for right panel
try {
ViewMode rightViewMode = ViewMode.valueOf(config.getRightPanelViewMode());
rightPanel.setViewMode(rightViewMode);
rightPanel.setViewMode(rightViewMode, false);
} catch (IllegalArgumentException e) {
// Default value FULL is already set
}
@ -121,8 +130,13 @@ public class MainWindow extends JFrame {
add(mainPanel, BorderLayout.CENTER);
// Set left panel as active by default
// Restore active panel from configuration
String savedActive = config.getActivePanel();
if ("right".equalsIgnoreCase(savedActive)) {
activePanel = rightPanel;
} else {
activePanel = leftPanel;
}
updateActivePanelBorder();
updateCommandLinePrompt();
@ -141,7 +155,7 @@ public class MainWindow extends JFrame {
focusedItems.add(config.getLeftPanelTabFocusedItem(i));
}
int sel = config.getLeftPanelSelectedIndex();
leftPanel.restoreTabs(paths, modes, focusedItems, sel);
leftPanel.restoreTabs(paths, modes, focusedItems, sel, false);
}
} catch (Exception ex) {
// ignore and keep default
@ -161,7 +175,7 @@ public class MainWindow extends JFrame {
focusedItems.add(config.getRightPanelTabFocusedItem(i));
}
int sel = config.getRightPanelSelectedIndex();
rightPanel.restoreTabs(paths, modes, focusedItems, sel);
rightPanel.restoreTabs(paths, modes, focusedItems, sel, false);
}
} catch (Exception ex) {
// ignore and keep default
@ -304,6 +318,13 @@ public class MainWindow extends JFrame {
// Menu
createMenuBar();
// Request focus for the active panel
SwingUtilities.invokeLater(() -> {
if (activePanel != null && activePanel.getFileTable() != null) {
activePanel.getFileTable().requestFocusInWindow();
}
});
}
/**
@ -1883,6 +1904,13 @@ public class MainWindow extends JFrame {
// Save window state
config.saveWindowState(this);
// Save active panel
if (activePanel == leftPanel) {
config.setActivePanel("left");
} else if (activePanel == rightPanel) {
config.setActivePanel("right");
}
// Save current panel paths (for backward compatibility)
if (leftPanel.getCurrentDirectory() != null) {
config.setLeftPanelPath(leftPanel.getCurrentDirectory().getAbsolutePath());