Skip to content

Commit

Permalink
Fixed highlight sometimes not painting right side of rounded corners.
Browse files Browse the repository at this point in the history
Removed default kerning as it resulted in shifting text on windows.
Fixed text shifting when caret es at left most position.
  • Loading branch information
weisJ committed Apr 23, 2020
1 parent 052f40e commit 73bf8ec
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 32 deletions.
31 changes: 22 additions & 9 deletions core/src/main/java/com/github/weisj/darklaf/ui/text/DarkCaret.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,26 @@ public void setAlwaysVisible(final boolean alwaysVisible) {
}

public enum CaretStyle {
VERTICAL_LINE_STYLE,
UNDERLINE_STYLE,
BLOCK_STYLE,
BLOCK_BORDER_STYLE,
THICK_VERTICAL_LINE_STYLE
VERTICAL_LINE_STYLE(1),
UNDERLINE_STYLE(0),
BLOCK_STYLE(0),
BLOCK_BORDER_STYLE(0),
THICK_VERTICAL_LINE_STYLE(1);

private final int width;

CaretStyle(final int width) {
this.width = width;
}

public int getWidth() {
return width;
}
}

@Override
public double getWidth() {
return style.getWidth();
}

/**
Expand Down Expand Up @@ -288,7 +303,7 @@ public void paint(final Graphics g) {
g.fillRect(r.x, r.y, r.width, r.height);
break;
case BLOCK_BORDER_STYLE :
PaintUtil.drawRect(g, r.x, r.y, r.width - 1, r.height, 1);
PaintUtil.drawRect(g, r.x, r.y, r.width, r.height, 1);
break;
case UNDERLINE_STYLE :
if (textAreaBg == null) {
Expand All @@ -299,10 +314,8 @@ public void paint(final Graphics g) {
g.fillRect(r.x, y - 1, r.width, 1);
break;
case THICK_VERTICAL_LINE_STYLE :
g.fillRect(r.x, r.y, 2, r.height);
break;
case VERTICAL_LINE_STYLE :
g.fillRect(r.x, r.y, 1, r.height);
g.fillRect(r.x, r.y, style.getWidth(), r.height);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,33 @@ protected Color getBackground(final JTextComponent c) {
}
}

@Override
public Dimension getPreferredSize(final JComponent c) {
Dimension dim = super.getPreferredSize(c);
if (isEmpty()) {
String defaultText = getDefaultText();
if (!defaultText.isEmpty()) {
Component renderer = getDefaultTextRenderer().getRendererComponent(editor, defaultText);
Dimension prefSize = renderer.getPreferredSize();
Insets i = c.getInsets();
prefSize.width += i.left + i.right;
prefSize.height += i.top + i.bottom;
dim.width = Math.max(dim.width, prefSize.width);
dim.height = Math.max(dim.height, prefSize.height);
}
}
dim.width += getCaretWidth();
return dim;
}

protected int getCaretWidth() {
Caret caret = editor.getCaret();
if (caret instanceof DefaultCaret) {
return (int) ((DefaultCaret) caret).getWidth();
}
return 1;
}

@Override
protected void paintBackground(final Graphics g) {
final Container parent = getRelevantParent(editor);
Expand Down Expand Up @@ -217,19 +244,28 @@ protected void paintSafely(final Graphics g) {
}

protected void paintDefaultText(final Graphics g) {
Document doc = editor.getDocument();
if (doc != null && doc.getLength() == 0) {
Object defaultText = editor.getClientProperty(KEY_DEFAULT_TEXT);
if (defaultText instanceof String) {
if (isEmpty()) {
String defaultText = getDefaultText();
if (!defaultText.isEmpty()) {
Rectangle rect = getVisibleEditorRect();
g.translate(rect.x, rect.y);
Component renderer = getDefaultTextRenderer().getRendererComponent(editor, (String) defaultText);
Component renderer = getDefaultTextRenderer().getRendererComponent(editor, defaultText);
renderer.setBounds(rect);
renderer.paint(g);
}
}
}

protected String getDefaultText() {
Object defaultText = editor.getClientProperty(KEY_DEFAULT_TEXT);
return defaultText instanceof String ? (String) defaultText : "";
}

protected boolean isEmpty() {
Document doc = editor.getDocument();
return doc == null || doc.getLength() == 0;
}

protected DefaultTextRenderer getDefaultTextRenderer() {
if (defaultTextRenderer == null) defaultTextRenderer = createDefaultTextRenderer();
return defaultTextRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ protected Shape paintLayerImpl(final Graphics2D g2d, final int offs0, final int
Rectangle posOffs1 = c.getUI().modelToView(c, offs1, Position.Bias.Backward);
Rectangle posOffs1Forward = c.modelToView(offs1);
Rectangle posOffs1Next = c.modelToView(Math.min(c.getDocument().getLength(), offs1 - 1));
boolean selectionStart = c.getSelectionStart() >= offs0;
boolean selectionEnd = c.getSelectionEnd() <= offs1;
boolean isSelectionStart = c.getSelectionStart() >= offs0;
boolean isSelectionEnd = c.getSelectionEnd() <= offs1;

Insets margin = c.getInsets();

boolean firstLineNotPainted = posStartPrev.y + posStartPrev.height == posStart.y;
boolean lastLineNotPainted = posOffs1Next.y == posEnd.y && posOffs1.y == posOffs1Forward.y;
boolean isToEndOfLine = posOffs1.y < posEnd.y && !lastLineNotPainted;
boolean isToStartOfLine = !selectionEnd && posOffs0.y > posStart.y && (posOffs0.y != posOffs0Prev.y);
boolean isToStartOfLine = !isSelectionEnd && posOffs0.y > posStart.y && (posOffs0.y != posOffs0Prev.y);

Rectangle alloc;
if (offs0 == offs1 && posEnd.y != posStart.y) {
Expand Down Expand Up @@ -280,7 +280,7 @@ protected Shape paintLayerImpl(final Graphics2D g2d, final int offs0, final int
false, false,
isFirstLine, isLastLine, isSecondLastLine, isSecondLine,
firstLineNotPainted, lastLineNotPainted);
} else if (!selectionStart && !selectionEnd) {
} else if (!isSelectionStart && !isSelectionEnd) {
if (DEBUG_COLOR) g2d.setColor(Color.ORANGE);
dirtyShape = paintMiddleSelection(g2d, alloc, c,
isToEndOfLine, isToStartOfLine, isFirstLine, isLastLine,
Expand All @@ -294,9 +294,9 @@ protected Shape paintLayerImpl(final Graphics2D g2d, final int offs0, final int
suppressRounded = true;
rect = new Rectangle(originalX, alloc.y, originalWidth, alloc.height);
}
dirtyShape = paintSelection(g2d, c, rect, selectionStart, selectionEnd);
dirtyShape = paintSelection(g2d, c, rect, isSelectionStart, isSelectionEnd);
suppressRounded = false;
} else if (selectionStart) {
} else if (isSelectionStart) {
dirtyShape = paintSelectionStart(g2d, alloc, c, posStart, posOffs0, endBeforeStart, isSecondLastLine,
isToEndOfLine);
} else {
Expand All @@ -309,7 +309,7 @@ protected Shape paintLayerImpl(final Graphics2D g2d, final int offs0, final int
isToEndOfLine, isToStartOfLine,
isFirstLine, isLastLine,
isSecondLine, isSecondLastLine,
selectionStart, selectionEnd,
isSelectionStart, isSelectionEnd,
posStart, posEnd, dirtyShape.getBounds());
return dirtyShape;
}
Expand Down Expand Up @@ -367,6 +367,7 @@ private Shape paintSelectionStart(final Graphics2D g2d, final Rectangle r,
*/
private Shape paintSelection(final Graphics2D g2d, final JTextComponent c, final Rectangle r,
final boolean selectionStart, final boolean selectionEnd) {
g2d.setClip(r);
if (DEBUG_COLOR) g2d.setColor(Color.BLUE);
if (isRounded(c)) {
paintRoundedLeftRight(g2d, selectionStart, selectionEnd, r);
Expand Down
9 changes: 1 addition & 8 deletions core/src/test/java/ui/text/TextFieldDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,7 @@ public JComponent createComponent() {
}

protected JTextField createTextField() {
return new JTextField("Demo TextField") {
@Override
public Dimension getPreferredSize() {
Dimension dim = super.getPreferredSize();
dim.width = Math.max(dim.width, 100);
return dim;
}
};
return new JTextField("Demo TextField");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package com.github.weisj.darklaf;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.io.InputStream;
import java.text.AttributedCharacterIterator;
Expand Down Expand Up @@ -82,8 +81,7 @@ public final class PropertyLoader {

private static boolean addReferenceInfo;

private static final Map<AttributedCharacterIterator.Attribute, Integer> attributes = Collections.singletonMap(TextAttribute.KERNING,
TextAttribute.KERNING_ON);
private static final Map<AttributedCharacterIterator.Attribute, Integer> attributes = Collections.emptyMap();

public static void setAddReferenceInfo(final boolean addReferenceInfo) {
PropertyLoader.addReferenceInfo = addReferenceInfo;
Expand Down

0 comments on commit 73bf8ec

Please sign in to comment.