fixed background color

This commit is contained in:
rdavidek 2026-01-14 20:50:46 +01:00
parent dcc9a6a418
commit 0fc9a543d5
4 changed files with 124 additions and 9 deletions

View File

@ -505,7 +505,8 @@ public class FilePanel extends JPanel {
} }
public void applyBackgroundColor(Color bg) { public void applyBackgroundColor(Color bg) {
setBackground(bg); if (bg == null) return;
updateComponentBackground(this, bg);
for (int i = 0; i < tabbedPane.getTabCount(); i++) { for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Component c = tabbedPane.getComponentAt(i); Component c = tabbedPane.getComponentAt(i);
if (c instanceof FilePanelTab) { if (c instanceof FilePanelTab) {
@ -514,6 +515,28 @@ public class FilePanel extends JPanel {
} }
} }
private void updateComponentBackground(Container container, Color bg) {
if (container == null) return;
container.setBackground(bg);
boolean dark = isDark(bg);
for (Component c : container.getComponents()) {
if (c instanceof JPanel || c instanceof JToolBar || c instanceof JScrollPane || c instanceof JViewport || c instanceof JTabbedPane) {
c.setBackground(bg);
} else if (c instanceof JLabel || c instanceof JCheckBox || c instanceof JRadioButton) {
c.setForeground(dark ? Color.WHITE : Color.BLACK);
}
if (c instanceof Container) {
updateComponentBackground((Container) c, bg);
}
}
}
private boolean isDark(Color c) {
if (c == null) return false;
double darkness = 1 - (0.299 * c.getRed() + 0.587 * c.getGreen() + 0.114 * c.getBlue()) / 255;
return darkness >= 0.5;
}
public void applySelectionColor(Color sel) { public void applySelectionColor(Color sel) {
for (int i = 0; i < tabbedPane.getTabCount(); i++) { for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Component c = tabbedPane.getComponentAt(i); Component c = tabbedPane.getComponentAt(i);

View File

@ -124,12 +124,34 @@ public class FilePanelTab extends JPanel {
public void applyBackgroundColor(Color bg) { public void applyBackgroundColor(Color bg) {
if (bg == null) return; if (bg == null) return;
setBackground(bg); updateComponentBackground(this, bg);
fileTable.setBackground(bg); fileTable.setBackground(bg);
statusLabel.setBackground(bg); statusLabel.setBackground(bg);
repaint(); repaint();
} }
private void updateComponentBackground(Container container, Color bg) {
if (container == null) return;
container.setBackground(bg);
boolean dark = isDark(bg);
for (Component c : container.getComponents()) {
if (c instanceof JPanel || c instanceof JToolBar || c instanceof JScrollPane || c instanceof JViewport || c instanceof JTabbedPane) {
c.setBackground(bg);
} else if (c instanceof JLabel || c instanceof JCheckBox || c instanceof JRadioButton) {
c.setForeground(dark ? Color.WHITE : Color.BLACK);
}
if (c instanceof Container) {
updateComponentBackground((Container) c, bg);
}
}
}
private boolean isDark(Color c) {
if (c == null) return false;
double darkness = 1 - (0.299 * c.getRed() + 0.587 * c.getGreen() + 0.114 * c.getBlue()) / 255;
return darkness >= 0.5;
}
public void applySelectionColor(Color sel) { public void applySelectionColor(Color sel) {
if (sel == null) return; if (sel == null) return;
this.selectionColor = sel; this.selectionColor = sel;
@ -1239,6 +1261,7 @@ public class FilePanelTab extends JPanel {
Font baseFont = table.getFont(); Font baseFont = table.getFont();
if (baseFont == null) baseFont = getFont(); if (baseFont == null) baseFont = getFont();
int baseStyle = baseFont != null ? baseFont.getStyle() : Font.PLAIN; int baseStyle = baseFont != null ? baseFont.getStyle() : Font.PLAIN;
if (isMarked) { if (isMarked) {
// Ensure marked items are at least bold, preserve italic if present // Ensure marked items are at least bold, preserve italic if present
int newStyle = baseStyle | Font.BOLD; int newStyle = baseStyle | Font.BOLD;
@ -1247,8 +1270,12 @@ public class FilePanelTab extends JPanel {
} else { } else {
// Preserve whatever style the base font has (do not force plain) // Preserve whatever style the base font has (do not force plain)
setFont(baseFont.deriveFont(baseStyle)); setFont(baseFont.deriveFont(baseStyle));
// Use a white foreground color for better contrast on dark backgrounds // Automatically adjust foreground contrast
setForeground(new Color(255, 255, 255)); if (isCurrentCell && table.hasFocus()) {
setForeground(isDark(selectionColor) ? Color.WHITE : Color.BLACK);
} else {
setForeground(isDark(FilePanelTab.this.getBackground()) ? Color.WHITE : Color.BLACK);
}
} }
// Zobrazit ikonu pro názvy souborů // Zobrazit ikonu pro názvy souborů

View File

@ -320,6 +320,7 @@ public class MainWindow extends JFrame {
// Settings menu // Settings menu
JMenu settingsMenu = new JMenu("Settings"); JMenu settingsMenu = new JMenu("Settings");
JMenuItem appearanceItem = new JMenuItem("Appearance..."); JMenuItem appearanceItem = new JMenuItem("Appearance...");
appearanceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.ALT_DOWN_MASK));
appearanceItem.addActionListener(e -> showSettingsDialog()); appearanceItem.addActionListener(e -> showSettingsDialog());
settingsMenu.add(appearanceItem); settingsMenu.add(appearanceItem);
@ -373,7 +374,7 @@ public class MainWindow extends JFrame {
Color bg = config.getBackgroundColor(); Color bg = config.getBackgroundColor();
if (bg != null) { if (bg != null) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
getContentPane().setBackground(bg); updateComponentBackground(getContentPane(), bg);
if (leftPanel != null) leftPanel.applyBackgroundColor(bg); if (leftPanel != null) leftPanel.applyBackgroundColor(bg);
if (rightPanel != null) rightPanel.applyBackgroundColor(bg); if (rightPanel != null) rightPanel.applyBackgroundColor(bg);
}); });
@ -401,6 +402,29 @@ public class MainWindow extends JFrame {
} catch (Exception ignore) {} } catch (Exception ignore) {}
}); });
} }
private void updateComponentBackground(Container container, Color bg) {
if (container == null) return;
container.setBackground(bg);
boolean dark = isDark(bg);
for (Component c : container.getComponents()) {
if (c instanceof JPanel || c instanceof JToolBar || c instanceof JScrollPane || c instanceof JViewport || c instanceof JTabbedPane || c instanceof JButton) {
c.setBackground(bg);
}
if (c instanceof JLabel || c instanceof JCheckBox || c instanceof JRadioButton || c instanceof JButton) {
c.setForeground(dark ? Color.WHITE : Color.BLACK);
}
if (c instanceof Container) {
updateComponentBackground((Container) c, bg);
}
}
}
private boolean isDark(Color c) {
if (c == null) return false;
double darkness = 1 - (0.299 * c.getRed() + 0.587 * c.getGreen() + 0.114 * c.getBlue()) / 255;
return darkness >= 0.5;
}
/** /**
* Setup keyboard shortcuts * Setup keyboard shortcuts
@ -433,6 +457,11 @@ public class MainWindow extends JFrame {
KeyStroke.getKeyStroke(KeyEvent.VK_F9, InputEvent.ALT_DOWN_MASK), KeyStroke.getKeyStroke(KeyEvent.VK_F9, InputEvent.ALT_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW); JComponent.WHEN_IN_FOCUSED_WINDOW);
// Alt+O - Settings
rootPane.registerKeyboardAction(e -> showSettingsDialog(),
KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.ALT_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW);
// F6 - Move // F6 - Move
rootPane.registerKeyboardAction(e -> moveFiles(), rootPane.registerKeyboardAction(e -> moveFiles(),
KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0),
@ -1027,6 +1056,7 @@ public class MainWindow extends JFrame {
"F9 / Shift+F6 - Rename\n" + "F9 / Shift+F6 - Rename\n" +
"TAB - Switch panel\n" + "TAB - Switch panel\n" +
"Ctrl+F - Search\n" + "Ctrl+F - Search\n" +
"Alt+O - Settings\n" +
"Enter - Open directory\n" + "Enter - Open directory\n" +
"Backspace - Parent directory", "Backspace - Parent directory",
"About", "About",

View File

@ -18,6 +18,13 @@ public class SettingsDialog extends JDialog {
private final JPanel cards; private final JPanel cards;
private final CardLayout cardLayout; private final CardLayout cardLayout;
// Original values for cancellation
private final Color originalBg;
private final Color originalSel;
private final Color originalMark;
private final Font originalGlobalFont;
private final Font originalEditorFont;
// Appearance controls // Appearance controls
private JButton appearanceFontBtn; private JButton appearanceFontBtn;
private JButton appearanceBgBtn; private JButton appearanceBgBtn;
@ -35,6 +42,13 @@ public class SettingsDialog extends JDialog {
this.config = config; this.config = config;
this.onChange = onChange; this.onChange = onChange;
// Store original values
this.originalBg = config.getBackgroundColor();
this.originalSel = config.getSelectionColor();
this.originalMark = config.getMarkedColor();
this.originalGlobalFont = config.getGlobalFont();
this.originalEditorFont = config.getEditorFont();
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(700, 420); setSize(700, 420);
setLocationRelativeTo(parent); setLocationRelativeTo(parent);
@ -139,7 +153,19 @@ public class SettingsDialog extends JDialog {
dispose(); dispose();
}); });
JButton cancel = new JButton("Cancel"); JButton cancel = new JButton("Cancel");
cancel.addActionListener(e -> dispose()); cancel.addActionListener(e -> {
// Restore original values
config.setBackgroundColor(originalBg);
config.setSelectionColor(originalSel);
config.setMarkedColor(originalMark);
config.setGlobalFont(originalGlobalFont);
config.setEditorFont(originalEditorFont);
// Notify UI to revert changes
if (onChange != null) onChange.run();
dispose();
});
btns.add(ok); btns.add(ok);
btns.add(cancel); btns.add(cancel);
add(btns, BorderLayout.SOUTH); add(btns, BorderLayout.SOUTH);
@ -164,13 +190,16 @@ public class SettingsDialog extends JDialog {
grid.add(new JLabel("Background color:")); grid.add(new JLabel("Background color:"));
appearanceBgBtn = new JButton(); appearanceBgBtn = new JButton();
appearanceBgBtn.setOpaque(true);
appearanceBgBtn.setContentAreaFilled(false);
appearanceBgBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY));
Color bg = config.getBackgroundColor(); Color bg = config.getBackgroundColor();
appearanceBgBtn.setBackground(bg != null ? bg : UIManager.getColor("Panel.background")); appearanceBgBtn.setBackground(bg != null ? bg : UIManager.getColor("Panel.background"));
appearanceBgBtn.addActionListener(e -> { appearanceBgBtn.addActionListener(e -> {
Color chosen = JColorChooser.showDialog(this, "Choose background color", appearanceBgBtn.getBackground()); Color chosen = JColorChooser.showDialog(this, "Choose background color", appearanceBgBtn.getBackground());
if (chosen != null) { if (chosen != null) {
appearanceBgBtn.setBackground(chosen);
config.setBackgroundColor(chosen); config.setBackgroundColor(chosen);
appearanceBgBtn.setBackground(chosen);
if (onChange != null) onChange.run(); if (onChange != null) onChange.run();
} }
}); });
@ -178,13 +207,16 @@ public class SettingsDialog extends JDialog {
grid.add(new JLabel("Selection color:")); grid.add(new JLabel("Selection color:"));
appearanceSelBtn = new JButton(); appearanceSelBtn = new JButton();
appearanceSelBtn.setOpaque(true);
appearanceSelBtn.setContentAreaFilled(false);
appearanceSelBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY));
Color sel = config.getSelectionColor(); Color sel = config.getSelectionColor();
appearanceSelBtn.setBackground(sel != null ? sel : new Color(184, 207, 229)); appearanceSelBtn.setBackground(sel != null ? sel : new Color(184, 207, 229));
appearanceSelBtn.addActionListener(e -> { appearanceSelBtn.addActionListener(e -> {
Color chosen = JColorChooser.showDialog(this, "Choose selection color", appearanceSelBtn.getBackground()); Color chosen = JColorChooser.showDialog(this, "Choose selection color", appearanceSelBtn.getBackground());
if (chosen != null) { if (chosen != null) {
appearanceSelBtn.setBackground(chosen);
config.setSelectionColor(chosen); config.setSelectionColor(chosen);
appearanceSelBtn.setBackground(chosen);
if (onChange != null) onChange.run(); if (onChange != null) onChange.run();
} }
}); });
@ -192,13 +224,16 @@ public class SettingsDialog extends JDialog {
grid.add(new JLabel("Marked item color:")); grid.add(new JLabel("Marked item color:"));
appearanceMarkBtn = new JButton(); appearanceMarkBtn = new JButton();
appearanceMarkBtn.setOpaque(true);
appearanceMarkBtn.setContentAreaFilled(false);
appearanceMarkBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY));
Color mark = config.getMarkedColor(); Color mark = config.getMarkedColor();
appearanceMarkBtn.setBackground(mark != null ? mark : new Color(204, 153, 0)); appearanceMarkBtn.setBackground(mark != null ? mark : new Color(204, 153, 0));
appearanceMarkBtn.addActionListener(e -> { appearanceMarkBtn.addActionListener(e -> {
Color chosen = JColorChooser.showDialog(this, "Choose marked item color", appearanceMarkBtn.getBackground()); Color chosen = JColorChooser.showDialog(this, "Choose marked item color", appearanceMarkBtn.getBackground());
if (chosen != null) { if (chosen != null) {
appearanceMarkBtn.setBackground(chosen);
config.setMarkedColor(chosen); config.setMarkedColor(chosen);
appearanceMarkBtn.setBackground(chosen);
if (onChange != null) onChange.run(); if (onChange != null) onChange.run();
} }
}); });