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) {
setBackground(bg);
if (bg == null) return;
updateComponentBackground(this, bg);
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Component c = tabbedPane.getComponentAt(i);
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) {
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Component c = tabbedPane.getComponentAt(i);

View File

@ -124,12 +124,34 @@ public class FilePanelTab extends JPanel {
public void applyBackgroundColor(Color bg) {
if (bg == null) return;
setBackground(bg);
updateComponentBackground(this, bg);
fileTable.setBackground(bg);
statusLabel.setBackground(bg);
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) {
if (sel == null) return;
this.selectionColor = sel;
@ -1239,6 +1261,7 @@ public class FilePanelTab extends JPanel {
Font baseFont = table.getFont();
if (baseFont == null) baseFont = getFont();
int baseStyle = baseFont != null ? baseFont.getStyle() : Font.PLAIN;
if (isMarked) {
// Ensure marked items are at least bold, preserve italic if present
int newStyle = baseStyle | Font.BOLD;
@ -1247,8 +1270,12 @@ public class FilePanelTab extends JPanel {
} else {
// Preserve whatever style the base font has (do not force plain)
setFont(baseFont.deriveFont(baseStyle));
// Use a white foreground color for better contrast on dark backgrounds
setForeground(new Color(255, 255, 255));
// Automatically adjust foreground contrast
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ů

View File

@ -320,6 +320,7 @@ public class MainWindow extends JFrame {
// Settings menu
JMenu settingsMenu = new JMenu("Settings");
JMenuItem appearanceItem = new JMenuItem("Appearance...");
appearanceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.ALT_DOWN_MASK));
appearanceItem.addActionListener(e -> showSettingsDialog());
settingsMenu.add(appearanceItem);
@ -373,7 +374,7 @@ public class MainWindow extends JFrame {
Color bg = config.getBackgroundColor();
if (bg != null) {
SwingUtilities.invokeLater(() -> {
getContentPane().setBackground(bg);
updateComponentBackground(getContentPane(), bg);
if (leftPanel != null) leftPanel.applyBackgroundColor(bg);
if (rightPanel != null) rightPanel.applyBackgroundColor(bg);
});
@ -402,6 +403,29 @@ public class MainWindow extends JFrame {
});
}
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
*/
@ -433,6 +457,11 @@ public class MainWindow extends JFrame {
KeyStroke.getKeyStroke(KeyEvent.VK_F9, InputEvent.ALT_DOWN_MASK),
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
rootPane.registerKeyboardAction(e -> moveFiles(),
KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0),
@ -1027,6 +1056,7 @@ public class MainWindow extends JFrame {
"F9 / Shift+F6 - Rename\n" +
"TAB - Switch panel\n" +
"Ctrl+F - Search\n" +
"Alt+O - Settings\n" +
"Enter - Open directory\n" +
"Backspace - Parent directory",
"About",

View File

@ -18,6 +18,13 @@ public class SettingsDialog extends JDialog {
private final JPanel cards;
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
private JButton appearanceFontBtn;
private JButton appearanceBgBtn;
@ -35,6 +42,13 @@ public class SettingsDialog extends JDialog {
this.config = config;
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);
setSize(700, 420);
setLocationRelativeTo(parent);
@ -139,7 +153,19 @@ public class SettingsDialog extends JDialog {
dispose();
});
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(cancel);
add(btns, BorderLayout.SOUTH);
@ -164,13 +190,16 @@ public class SettingsDialog extends JDialog {
grid.add(new JLabel("Background color:"));
appearanceBgBtn = new JButton();
appearanceBgBtn.setOpaque(true);
appearanceBgBtn.setContentAreaFilled(false);
appearanceBgBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY));
Color bg = config.getBackgroundColor();
appearanceBgBtn.setBackground(bg != null ? bg : UIManager.getColor("Panel.background"));
appearanceBgBtn.addActionListener(e -> {
Color chosen = JColorChooser.showDialog(this, "Choose background color", appearanceBgBtn.getBackground());
if (chosen != null) {
appearanceBgBtn.setBackground(chosen);
config.setBackgroundColor(chosen);
appearanceBgBtn.setBackground(chosen);
if (onChange != null) onChange.run();
}
});
@ -178,13 +207,16 @@ public class SettingsDialog extends JDialog {
grid.add(new JLabel("Selection color:"));
appearanceSelBtn = new JButton();
appearanceSelBtn.setOpaque(true);
appearanceSelBtn.setContentAreaFilled(false);
appearanceSelBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY));
Color sel = config.getSelectionColor();
appearanceSelBtn.setBackground(sel != null ? sel : new Color(184, 207, 229));
appearanceSelBtn.addActionListener(e -> {
Color chosen = JColorChooser.showDialog(this, "Choose selection color", appearanceSelBtn.getBackground());
if (chosen != null) {
appearanceSelBtn.setBackground(chosen);
config.setSelectionColor(chosen);
appearanceSelBtn.setBackground(chosen);
if (onChange != null) onChange.run();
}
});
@ -192,13 +224,16 @@ public class SettingsDialog extends JDialog {
grid.add(new JLabel("Marked item color:"));
appearanceMarkBtn = new JButton();
appearanceMarkBtn.setOpaque(true);
appearanceMarkBtn.setContentAreaFilled(false);
appearanceMarkBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY));
Color mark = config.getMarkedColor();
appearanceMarkBtn.setBackground(mark != null ? mark : new Color(204, 153, 0));
appearanceMarkBtn.addActionListener(e -> {
Color chosen = JColorChooser.showDialog(this, "Choose marked item color", appearanceMarkBtn.getBackground());
if (chosen != null) {
appearanceMarkBtn.setBackground(chosen);
config.setMarkedColor(chosen);
appearanceMarkBtn.setBackground(chosen);
if (onChange != null) onChange.run();
}
});