From 265da0714d524c0c8ea0ad0abaa84de0bc299786 Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 18 Mar 2020 20:27:56 +0100 Subject: [PATCH] Made selection highlight invisible when text component doesn't have focus. --- .../github/weisj/darklaf/ui/text/DarkCaret.java | 16 ++++++++++------ .../weisj/darklaf/ui/text/DarkTextFieldUI.java | 9 --------- .../github/weisj/darklaf/ui/text/DarkTextUI.java | 8 ++++++++ .../DarkHighlightPainter.java | 16 ++++++++++++++-- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java index bb87b5e6..759a6dde 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java @@ -28,13 +28,8 @@ import com.github.weisj.darklaf.util.DarkUIUtil; import javax.swing.*; import javax.swing.plaf.TextUI; import javax.swing.plaf.UIResource; -import javax.swing.text.BadLocationException; -import javax.swing.text.DefaultCaret; +import javax.swing.text.*; import javax.swing.text.DefaultHighlighterDark.DarkHighlightPainter; -import javax.swing.text.Highlighter; -import javax.swing.text.JTextComponent; -import javax.swing.text.Position; -import javax.swing.text.Segment; import java.awt.*; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Transferable; @@ -230,6 +225,15 @@ public class DarkCaret extends DefaultCaret implements UIResource { return selectionPainter; } + @Override + public boolean isSelectionVisible() { + return super.isSelectionVisible() && selectionPainter.isEnabled(); + } + + public void setPaintSelectionHighlight(final boolean paintSelectionHighlight) { + selectionPainter.setEnabled(paintSelectionHighlight); + } + public boolean getPasteOnMiddleMouseClick() { return pasteOnMiddleMouseClick; } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java index 8fcf643e..d101e73d 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java @@ -54,13 +54,6 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh protected static Icon clearHover; protected static Icon search; protected static Icon searchWithHistory; - private final FocusListener focusListener = new FocusAdapter() { - public void focusLost(final FocusEvent e) { - if (Boolean.FALSE.equals(getComponent().getClientProperty(KEY_KEEP_SELECTION_ON_FOCUS_LOST))) { - getComponent().select(0, 0); - } - } - }; protected int arcSize; protected int searchArcSize; protected int borderSize; @@ -284,7 +277,6 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh JTextComponent c = getComponent(); c.addMouseListener(mouseListener); c.addMouseMotionListener(mouseMotionListener); - c.addFocusListener(focusListener); c.addKeyListener(keyListener); } @@ -293,7 +285,6 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh JTextComponent c = getComponent(); c.removeMouseListener(mouseListener); c.removeMouseMotionListener(mouseMotionListener); - c.removeFocusListener(focusListener); c.removeKeyListener(keyListener); } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java index 69b55b3a..4acccb30 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java @@ -61,11 +61,19 @@ public abstract class DarkTextUI extends BasicTextUI implements PropertyChangeLi private FocusListener focusListener = new FocusListener() { @Override public void focusGained(final FocusEvent e) { + Caret caret = editor.getCaret(); + if (caret instanceof DarkCaret) { + ((DarkCaret) caret).setPaintSelectionHighlight(true); + } editor.repaint(); } @Override public void focusLost(final FocusEvent e) { + Caret caret = editor.getCaret(); + if (caret instanceof DarkCaret) { + ((DarkCaret) caret).setPaintSelectionHighlight(false); + } editor.repaint(); } }; diff --git a/core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java b/core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java index 06b34e51..77b7e9f0 100644 --- a/core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java +++ b/core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java @@ -59,6 +59,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai private int repaintCount = 0; private int arcSize; private boolean suppressRounded = false; + private boolean enabled; public DarkHighlightPainter() { @@ -81,11 +82,12 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai setPaint(paint); setRoundedEdges(rounded); setAlpha(alpha); + setEnabled(true); arcSize = UIManager.getInt("Highlight.arc"); wrapper = new ColorWrapper(color) { @Override public boolean equals(final Object obj) { - return obj != null; + return obj != null && enabled; } }; } @@ -116,6 +118,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai @Override public void paint(final Graphics g, final int offs0, final int offs1, final Shape bounds, final JTextComponent c) { + if (!enabled) return; Rectangle alloc = bounds.getBounds(); Graphics2D g2d = (Graphics2D) g; GraphicsContext context = new GraphicsContext(g2d); @@ -183,6 +186,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai @Override public Shape paintLayer(final Graphics g, final int offs0, final int offs1, final Shape bounds, final JTextComponent c, final View view) { + if (!enabled) return bounds; color = (Color) view.getAttributes().getAttribute(StyleConstantsEx.SelectedForeground); if (color == null) { color = c.getSelectedTextColor(); @@ -204,7 +208,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai context.restore(); } /* - * To make sure the the right part of the highlight is actually painted we manually repaint + * To make sure the correct part of the highlight is actually painted we manually repaint * after the selection has changed. */ if (dirtyShape != null && (selectionEnd != c.getSelectionEnd() @@ -521,4 +525,12 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai g2d.setPaint(paint); } } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(final boolean enabled) { + this.enabled = enabled; + } }