cursor visibility

This commit is contained in:
Radek Davidek 2026-01-21 13:28:01 +01:00
parent 3645fdc78b
commit 38aa55c463
9 changed files with 154 additions and 9 deletions

View File

@ -58,6 +58,26 @@ public class MainApp {
}, AWTEvent.KEY_EVENT_MASK);
}
/**
* Helper to set reflective caret color for all text components in a container
*/
public static void applyReflectiveCaretColor(Container container) {
if (container == null) return;
for (Component c : container.getComponents()) {
if (c instanceof javax.swing.text.JTextComponent) {
javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c;
Color bg = tc.getBackground();
if (bg != null) {
double darkness = 1 - (0.299 * bg.getRed() + 0.587 * bg.getGreen() + 0.114 * bg.getBlue()) / 255;
tc.setCaretColor(darkness >= 0.5 ? Color.WHITE : Color.BLACK);
}
}
if (c instanceof Container) {
applyReflectiveCaretColor((Container) c);
}
}
}
private static boolean isInsideJOptionPane(Component c) {
Component parent = c;
while (parent != null) {

View File

@ -382,12 +382,18 @@ public class FileEditor extends JDialog {
if (container == null) return;
container.setBackground(bg);
for (Component c : container.getComponents()) {
if (c instanceof JPanel || c instanceof JScrollPane || c instanceof JViewport) {
if (c instanceof JPanel || c instanceof JScrollPane || c instanceof JViewport || c instanceof JToolBar) {
c.setBackground(bg);
}
if (c instanceof JLabel) {
c.setForeground(dark ? Color.WHITE : Color.BLACK);
}
if (c instanceof javax.swing.text.JTextComponent) {
javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c;
tc.setBackground(bg);
tc.setForeground(dark ? Color.WHITE : Color.BLACK);
tc.setCaretColor(dark ? Color.WHITE : Color.BLACK);
}
if (c instanceof Container) {
applyRecursiveColors((Container) c, bg, dark);
}

View File

@ -83,6 +83,14 @@ public class FilePanelTab extends JPanel {
Component ed = fileTable.getEditorComponent();
if (ed instanceof JTextField) {
JTextField tf = (JTextField) ed;
// Ensure caret is visible
Color bg = tf.getBackground();
if (bg != null) {
double darkness = 1 - (0.299 * bg.getRed() + 0.587 * bg.getGreen() + 0.114 * bg.getBlue()) / 255;
tf.setCaretColor(darkness >= 0.5 ? Color.WHITE : Color.BLACK);
}
String text = tf.getText();
int lastDot = text.lastIndexOf('.');
if (lastDot > 0) { // Found a dot, and it's not the first character
@ -123,6 +131,14 @@ public class FilePanelTab extends JPanel {
Component ed = fileTable.getEditorComponent();
if (ed instanceof JTextField) {
JTextField tf = (JTextField) ed;
// Ensure caret is visible
Color bg = tf.getBackground();
if (bg != null) {
double darkness = 1 - (0.299 * bg.getRed() + 0.587 * bg.getGreen() + 0.114 * bg.getBlue()) / 255;
tf.setCaretColor(darkness >= 0.5 ? Color.WHITE : Color.BLACK);
}
tf.requestFocusInWindow();
tf.selectAll();
}

View File

@ -1,5 +1,6 @@
package cz.kamma.kfmanager.ui;
import cz.kamma.kfmanager.MainApp;
import javax.swing.*;
import java.awt.*;
@ -23,6 +24,7 @@ public class FontChooserDialog extends JDialog {
selectFont(initialFont);
setSize(600, 400);
MainApp.applyReflectiveCaretColor(getContentPane());
setLocationRelativeTo(parent);
}

View File

@ -638,6 +638,7 @@ public class MainWindow extends JFrame {
panel.add(iconPanel);
String title = (existing != null) ? "Edit Toolbar Shortcut" : "Add Toolbar Shortcut";
cz.kamma.kfmanager.MainApp.applyReflectiveCaretColor(panel);
int result = JOptionPane.showConfirmDialog(this, panel, title, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String label = labelField.getText().trim();
@ -861,10 +862,8 @@ public class MainWindow extends JFrame {
Color selColor = config.getSelectionColor();
if (selColor != null) {
tf.setSelectionColor(selColor);
tf.setCaretColor(selColor);
} else {
tf.setCaretColor(dark ? Color.WHITE : Color.BLACK);
}
tf.setCaretColor(dark ? Color.WHITE : Color.BLACK);
}
}
});
@ -875,13 +874,15 @@ public class MainWindow extends JFrame {
if (leftPanel != null) leftPanel.applySelectionColor(sel);
if (rightPanel != null) rightPanel.applySelectionColor(sel);
// Apply selection color to command line editor for cursor and selection
// Apply selection color to command line editor for selection
if (commandLine != null) {
Component ed = commandLine.getEditor().getEditorComponent();
if (ed instanceof JTextField) {
JTextField tf = (JTextField) ed;
tf.setSelectionColor(sel);
tf.setCaretColor(sel);
Color fieldBg = tf.getBackground();
boolean darkField = isDark(fieldBg);
tf.setCaretColor(darkField ? Color.WHITE : Color.BLACK);
tf.setSelectedTextColor(Color.WHITE); // Ensure selected text is readable on selection bg
}
}
@ -927,11 +928,9 @@ public class MainWindow extends JFrame {
javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c;
tc.setBackground(bg);
tc.setForeground(dark ? Color.WHITE : Color.BLACK);
tc.setCaretColor(dark ? Color.WHITE : Color.BLACK);
if (selColor != null) {
tc.setSelectionColor(selColor);
tc.setCaretColor(selColor);
} else {
tc.setCaretColor(dark ? Color.WHITE : Color.BLACK);
}
}
if (c instanceof Container) {

View File

@ -1,5 +1,6 @@
package cz.kamma.kfmanager.ui;
import cz.kamma.kfmanager.MainApp;
import cz.kamma.kfmanager.model.FileItem;
import javax.swing.*;
@ -33,6 +34,8 @@ public class PropertiesDialog extends JDialog {
initComponents();
MainApp.applyReflectiveCaretColor(getContentPane());
pack();
setMinimumSize(new Dimension(500, 550));
setLocationRelativeTo(owner);

View File

@ -58,6 +58,58 @@ public class SearchDialog extends JDialog {
setSize(700, 500);
setLocationRelativeTo(parent);
}
applyAppearance();
}
private void applyAppearance() {
if (config == null) return;
Color bg = config.getBackgroundColor();
if (bg == null) return;
updateComponentBackground(getContentPane(), bg);
}
private void updateComponentBackground(Container container, Color bg) {
if (container == null) return;
container.setBackground(bg);
boolean dark = isDark(bg);
Color selColor = config != null ? config.getSelectionColor() : null;
for (Component c : container.getComponents()) {
if (c instanceof JPanel || c instanceof JScrollPane || c instanceof JViewport || c instanceof JTabbedPane || c instanceof JButton || c instanceof JComboBox) {
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 javax.swing.text.JTextComponent) {
javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c;
tc.setBackground(bg);
tc.setForeground(dark ? Color.WHITE : Color.BLACK);
tc.setCaretColor(dark ? Color.WHITE : Color.BLACK);
if (selColor != null) {
tc.setSelectionColor(selColor);
}
}
if (c instanceof JComboBox) {
JComboBox<?> cb = (JComboBox<?>) c;
Component editor = cb.getEditor().getEditorComponent();
if (editor instanceof javax.swing.text.JTextComponent) {
javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) editor;
tc.setBackground(bg);
tc.setForeground(dark ? Color.WHITE : Color.BLACK);
tc.setCaretColor(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;
}
private void initComponents() {

View File

@ -105,6 +105,8 @@ public class SettingsDialog extends JDialog {
add(split, BorderLayout.CENTER);
applyAppearance();
JPanel btns = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton ok = new JButton("OK");
ok.addActionListener(e -> {
@ -249,6 +251,47 @@ public class SettingsDialog extends JDialog {
});
}
private void applyAppearance() {
if (config == null) return;
Color bg = config.getBackgroundColor();
if (bg == null) return;
updateComponentBackground(getContentPane(), bg);
}
private void updateComponentBackground(Container container, Color bg) {
if (container == null) return;
container.setBackground(bg);
boolean dark = isDark(bg);
Color selColor = config != null ? config.getSelectionColor() : null;
for (Component c : container.getComponents()) {
if (c instanceof JPanel || c instanceof JScrollPane || c instanceof JViewport || c instanceof JTabbedPane || c instanceof JButton || c instanceof JSplitPane || c instanceof JList) {
c.setBackground(bg);
}
if (c instanceof JLabel || c instanceof JCheckBox || c instanceof JRadioButton || c instanceof JButton || c instanceof JList) {
c.setForeground(dark ? Color.WHITE : Color.BLACK);
}
if (c instanceof javax.swing.text.JTextComponent) {
javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c;
tc.setBackground(bg);
tc.setForeground(dark ? Color.WHITE : Color.BLACK);
tc.setCaretColor(dark ? Color.WHITE : Color.BLACK);
if (selColor != null) {
tc.setSelectionColor(selColor);
}
}
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;
}
private JPanel buildAppearancePanel() {
JPanel p = new JPanel(new BorderLayout(8, 8));
JPanel grid = new JPanel(new GridBagLayout());

View File

@ -1,5 +1,6 @@
package cz.kamma.kfmanager.ui;
import cz.kamma.kfmanager.MainApp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
@ -15,6 +16,9 @@ public class WildcardSelectDialog extends JDialog {
public WildcardSelectDialog(Frame parent) {
super(parent, "Vybrat podle masky", true);
initComponents();
MainApp.applyReflectiveCaretColor(getContentPane());
pack();
setLocationRelativeTo(parent);
}