Skip to content

Commit

Permalink
NumberingPane: Respect icon width when calculating numbering position
Browse files Browse the repository at this point in the history
This also improves the font choice for numbering panes on windows.
Fixes #251
  • Loading branch information
weisJ committed Jul 31, 2021
1 parent 39dfba2 commit b6d7c68
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package com.github.weisj.darklaf.components.text;

import java.awt.Font;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -45,6 +46,11 @@ public NumberingPane() {
updateUI();
}

@Override
public void setFont(Font font) {
super.setFont(font);
}

@Override
public void updateUI() {
setUI(UIManager.getUI(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class FontDefaultsInitTask implements DefaultsInitTask {
private static final String MAC_OS_CATALINA_FONT_NAME_FALLBACK = "Helvetica Neue";
private static final String MAC_OS_FONT_NAME = ".SF NS Text";
private static final String WINDOWS_10_FONT_NAME = "Segoe UI";
private static final String WINDOWS_10_MONO_FONT_NAME = "Consolas";

@Override
public void run(final Theme currentTheme, final UIDefaults defaults) {
Expand Down Expand Up @@ -182,15 +183,21 @@ private void loadFontProperties(final UIDefaults defaults) {

private void patchOSFonts(final UIDefaults defaults, final Function<Map.Entry<Object, Font>, Font> mapper) {
PropertyLoader.replacePropertyEntriesOfType(Font.class, defaults,
e -> isDefaultFont(e.getValue()), mapper);
e -> isDefaultFont(e.getValue()) || isMonospaceDefault(e.getValue()), mapper);
}

private boolean isDefaultFont(final Font font) {
return Font.DIALOG.equals(font.getFamily());
}

private boolean isMonospaceDefault(final Font font) {
return Font.MONOSPACED.equals(font.getFamily());
}

private Font mapMacOSFont(final Map.Entry<Object, Font> entry) {
Font font = entry.getValue();
if (isMonospaceDefault(font)) return font;

String fontName = SystemInfo.isMacOSCatalina
? MAC_OS_CATALINA_FONT_NAME_FALLBACK
: MAC_OS_FONT_NAME;
Expand All @@ -205,7 +212,10 @@ private Font mapMacOSFont(final Map.Entry<Object, Font> entry) {
private Font mapWindowsFont(final Map.Entry<Object, Font> entry) {
Font font = entry.getValue();
if (!SystemInfo.isWindowsVista) return font;
Font windowsFont = FontUtil.createFont(WINDOWS_10_FONT_NAME, font.getStyle(), font.getSize());
String fontName = isMonospaceDefault(font)
? WINDOWS_10_MONO_FONT_NAME
: WINDOWS_10_FONT_NAME;
Font windowsFont = FontUtil.createFont(fontName, font.getStyle(), font.getSize());
if (font instanceof UIResource) {
windowsFont = new DarkFontUIResource(windowsFont);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.text.*;

import com.github.weisj.darklaf.components.text.IconListener;
Expand Down Expand Up @@ -190,7 +191,7 @@ protected void drawNumbering(final Graphics g, final int startLine, final int en
GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
g.setColor(numberingPane.getForeground());

Font font = getNumberingFont(numberingPane.getTextComponent(), g);
Font font = getNumberingFont(numberingPane.getTextComponent(), g, numberingPane.getFont());
g.setFont(font);
FontMetrics fm = numberingPane.getFontMetrics(font);

Expand All @@ -201,20 +202,23 @@ protected void drawNumbering(final Graphics g, final int startLine, final int en
String numberStr = String.valueOf(i);
Rectangle lineRect = textComponent.modelToView(off);
g.setColor(lineRect.y == yCur ? foregroundHighlight : numberingPane.getForeground());
g.drawString(numberStr, width - OUTER_PAD - fm.stringWidth(numberStr),
g.drawString(numberStr, width - OUTER_PAD - fm.stringWidth(numberStr) - maxIconWidth,
lineRect.y + lineRect.height - descent);
} catch (final BadLocationException ignored) {
}
}
config.restore();
}

private Font getNumberingFont(final JComponent c, final Graphics g) {
Font font = c.getFont();
if (font != null) {
float newSize = (float) font.getSize() - 1;
private Font getNumberingFont(final JComponent c, final Graphics g, final Font f) {
Font baseFont = c.getFont();
Font font = f;
if (font instanceof UIResource) {
int newSize = baseFont.getSize() - 1;
if (newSize > 0) {
font = font.deriveFont(newSize);
font = font.deriveFont((float) newSize);
} else {
font = font.deriveFont(baseFont.getSize2D());
}
} else {
font = g.getFont();
Expand Down Expand Up @@ -355,9 +359,6 @@ public void propertyChange(final PropertyChangeEvent evt) {
((Caret) newCaret).addChangeListener(currentLinePainter);
}
}
} else if (PropertyKey.FONT.equals(key)) {
Font font = textComponent.getFont();
numberingPane.setFont(font.deriveFont(Math.max(font.getSize() - 1, 1.0f)));
} else if (NumberingPane.KEY_EDITOR.equals(key)) {
Object newPane = evt.getNewValue();
if (textComponent != null) {
Expand All @@ -378,8 +379,6 @@ public void propertyChange(final PropertyChangeEvent evt) {
}
textComponent.addPropertyChangeListener(getPropertyChangeListener());
textComponent.getCaret().addChangeListener(getChangeListener());
Font font = textComponent.getFont();
numberingPane.setFont(font.deriveFont(Math.max(font.getSize() - 1, 1.0f)));
oldBackground = textComponent.getBackground();
textComponent.setBackground(UIManager.getColor("NumberingPane.textBackground"));
}
Expand Down

0 comments on commit b6d7c68

Please sign in to comment.