Browse Source

Made selection highlight invisible when text component doesn't have focus.

pull/97/head
weisj 5 years ago
parent
commit
265da0714d
  1. 16
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java
  2. 9
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java
  3. 8
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextUI.java
  4. 16
      core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java

16
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.*;
import javax.swing.plaf.TextUI; import javax.swing.plaf.TextUI;
import javax.swing.plaf.UIResource; import javax.swing.plaf.UIResource;
import javax.swing.text.BadLocationException; import javax.swing.text.*;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighterDark.DarkHighlightPainter; 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.*;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
@ -230,6 +225,15 @@ public class DarkCaret extends DefaultCaret implements UIResource {
return selectionPainter; return selectionPainter;
} }
@Override
public boolean isSelectionVisible() {
return super.isSelectionVisible() && selectionPainter.isEnabled();
}
public void setPaintSelectionHighlight(final boolean paintSelectionHighlight) {
selectionPainter.setEnabled(paintSelectionHighlight);
}
public boolean getPasteOnMiddleMouseClick() { public boolean getPasteOnMiddleMouseClick() {
return pasteOnMiddleMouseClick; return pasteOnMiddleMouseClick;
} }

9
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 clearHover;
protected static Icon search; protected static Icon search;
protected static Icon searchWithHistory; 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 arcSize;
protected int searchArcSize; protected int searchArcSize;
protected int borderSize; protected int borderSize;
@ -284,7 +277,6 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh
JTextComponent c = getComponent(); JTextComponent c = getComponent();
c.addMouseListener(mouseListener); c.addMouseListener(mouseListener);
c.addMouseMotionListener(mouseMotionListener); c.addMouseMotionListener(mouseMotionListener);
c.addFocusListener(focusListener);
c.addKeyListener(keyListener); c.addKeyListener(keyListener);
} }
@ -293,7 +285,6 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh
JTextComponent c = getComponent(); JTextComponent c = getComponent();
c.removeMouseListener(mouseListener); c.removeMouseListener(mouseListener);
c.removeMouseMotionListener(mouseMotionListener); c.removeMouseMotionListener(mouseMotionListener);
c.removeFocusListener(focusListener);
c.removeKeyListener(keyListener); c.removeKeyListener(keyListener);
} }

8
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() { private FocusListener focusListener = new FocusListener() {
@Override @Override
public void focusGained(final FocusEvent e) { public void focusGained(final FocusEvent e) {
Caret caret = editor.getCaret();
if (caret instanceof DarkCaret) {
((DarkCaret) caret).setPaintSelectionHighlight(true);
}
editor.repaint(); editor.repaint();
} }
@Override @Override
public void focusLost(final FocusEvent e) { public void focusLost(final FocusEvent e) {
Caret caret = editor.getCaret();
if (caret instanceof DarkCaret) {
((DarkCaret) caret).setPaintSelectionHighlight(false);
}
editor.repaint(); editor.repaint();
} }
}; };

16
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 repaintCount = 0;
private int arcSize; private int arcSize;
private boolean suppressRounded = false; private boolean suppressRounded = false;
private boolean enabled;
public DarkHighlightPainter() { public DarkHighlightPainter() {
@ -81,11 +82,12 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai
setPaint(paint); setPaint(paint);
setRoundedEdges(rounded); setRoundedEdges(rounded);
setAlpha(alpha); setAlpha(alpha);
setEnabled(true);
arcSize = UIManager.getInt("Highlight.arc"); arcSize = UIManager.getInt("Highlight.arc");
wrapper = new ColorWrapper(color) { wrapper = new ColorWrapper(color) {
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
return obj != null; return obj != null && enabled;
} }
}; };
} }
@ -116,6 +118,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai
@Override @Override
public void paint(final Graphics g, final int offs0, final int offs1, final Shape bounds, public void paint(final Graphics g, final int offs0, final int offs1, final Shape bounds,
final JTextComponent c) { final JTextComponent c) {
if (!enabled) return;
Rectangle alloc = bounds.getBounds(); Rectangle alloc = bounds.getBounds();
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
GraphicsContext context = new GraphicsContext(g2d); GraphicsContext context = new GraphicsContext(g2d);
@ -183,6 +186,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai
@Override @Override
public Shape paintLayer(final Graphics g, final int offs0, final int offs1, public Shape paintLayer(final Graphics g, final int offs0, final int offs1,
final Shape bounds, final JTextComponent c, final View view) { final Shape bounds, final JTextComponent c, final View view) {
if (!enabled) return bounds;
color = (Color) view.getAttributes().getAttribute(StyleConstantsEx.SelectedForeground); color = (Color) view.getAttributes().getAttribute(StyleConstantsEx.SelectedForeground);
if (color == null) { if (color == null) {
color = c.getSelectedTextColor(); color = c.getSelectedTextColor();
@ -204,7 +208,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai
context.restore(); 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. * after the selection has changed.
*/ */
if (dirtyShape != null && (selectionEnd != c.getSelectionEnd() if (dirtyShape != null && (selectionEnd != c.getSelectionEnd()
@ -521,4 +525,12 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai
g2d.setPaint(paint); g2d.setPaint(paint);
} }
} }
public boolean isEnabled() {
return enabled;
}
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
} }

Loading…
Cancel
Save