Browse Source

NumberingPane: Respect icon width when calculating numbering position

This also improves the font choice for numbering panes on windows.
Fixes #251
pull/276/head v2.7.1-rc1
weisj 3 years ago
parent
commit
b6d7c68f3a
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 6
      core/src/main/java/com/github/weisj/darklaf/components/text/NumberingPane.java
  2. 14
      core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java
  3. 23
      core/src/main/java/com/github/weisj/darklaf/ui/numberingpane/DarkNumberingPaneUI.java

6
core/src/main/java/com/github/weisj/darklaf/components/text/NumberingPane.java

@ -21,6 +21,7 @@
*/
package com.github.weisj.darklaf.components.text;
import java.awt.Font;
import java.util.*;
import java.util.stream.Collectors;
@ -45,6 +46,11 @@ public class NumberingPane extends JComponent {
updateUI();
}
@Override
public void setFont(Font font) {
super.setFont(font);
}
@Override
public void updateUI() {
setUI(UIManager.getUI(this));

14
core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java

@ -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) {
@ -182,15 +183,21 @@ public class FontDefaultsInitTask implements DefaultsInitTask {
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;
@ -205,7 +212,10 @@ public class FontDefaultsInitTask implements DefaultsInitTask {
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);
}

23
core/src/main/java/com/github/weisj/darklaf/ui/numberingpane/DarkNumberingPaneUI.java

@ -36,6 +36,7 @@ import javax.swing.*;
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;
@ -190,7 +191,7 @@ public class DarkNumberingPaneUI extends ComponentUI {
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);
@ -201,7 +202,7 @@ public class DarkNumberingPaneUI extends ComponentUI {
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) {
}
@ -209,12 +210,15 @@ public class DarkNumberingPaneUI extends ComponentUI {
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();
@ -355,9 +359,6 @@ public class DarkNumberingPaneUI extends ComponentUI {
((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) {
@ -378,8 +379,6 @@ public class DarkNumberingPaneUI extends ComponentUI {
}
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"));
}

Loading…
Cancel
Save