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; package com.github.weisj.darklaf.components.text;
import java.awt.Font;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -45,6 +46,11 @@ public class NumberingPane extends JComponent {
updateUI(); updateUI();
} }
@Override
public void setFont(Font font) {
super.setFont(font);
}
@Override @Override
public void updateUI() { public void updateUI() {
setUI(UIManager.getUI(this)); 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_CATALINA_FONT_NAME_FALLBACK = "Helvetica Neue";
private static final String MAC_OS_FONT_NAME = ".SF NS Text"; 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_FONT_NAME = "Segoe UI";
private static final String WINDOWS_10_MONO_FONT_NAME = "Consolas";
@Override @Override
public void run(final Theme currentTheme, final UIDefaults defaults) { 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) { private void patchOSFonts(final UIDefaults defaults, final Function<Map.Entry<Object, Font>, Font> mapper) {
PropertyLoader.replacePropertyEntriesOfType(Font.class, defaults, PropertyLoader.replacePropertyEntriesOfType(Font.class, defaults,
e -> isDefaultFont(e.getValue()), mapper); e -> isDefaultFont(e.getValue()) || isMonospaceDefault(e.getValue()), mapper);
} }
private boolean isDefaultFont(final Font font) { private boolean isDefaultFont(final Font font) {
return Font.DIALOG.equals(font.getFamily()); 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) { private Font mapMacOSFont(final Map.Entry<Object, Font> entry) {
Font font = entry.getValue(); Font font = entry.getValue();
if (isMonospaceDefault(font)) return font;
String fontName = SystemInfo.isMacOSCatalina String fontName = SystemInfo.isMacOSCatalina
? MAC_OS_CATALINA_FONT_NAME_FALLBACK ? MAC_OS_CATALINA_FONT_NAME_FALLBACK
: MAC_OS_FONT_NAME; : MAC_OS_FONT_NAME;
@ -205,7 +212,10 @@ public class FontDefaultsInitTask implements DefaultsInitTask {
private Font mapWindowsFont(final Map.Entry<Object, Font> entry) { private Font mapWindowsFont(final Map.Entry<Object, Font> entry) {
Font font = entry.getValue(); Font font = entry.getValue();
if (!SystemInfo.isWindowsVista) return font; 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) { if (font instanceof UIResource) {
windowsFont = new DarkFontUIResource(windowsFont); 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.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.text.*; import javax.swing.text.*;
import com.github.weisj.darklaf.components.text.IconListener; import com.github.weisj.darklaf.components.text.IconListener;
@ -190,7 +191,7 @@ public class DarkNumberingPaneUI extends ComponentUI {
GraphicsContext config = GraphicsUtil.setupAntialiasing(g); GraphicsContext config = GraphicsUtil.setupAntialiasing(g);
g.setColor(numberingPane.getForeground()); g.setColor(numberingPane.getForeground());
Font font = getNumberingFont(numberingPane.getTextComponent(), g); Font font = getNumberingFont(numberingPane.getTextComponent(), g, numberingPane.getFont());
g.setFont(font); g.setFont(font);
FontMetrics fm = numberingPane.getFontMetrics(font); FontMetrics fm = numberingPane.getFontMetrics(font);
@ -201,7 +202,7 @@ public class DarkNumberingPaneUI extends ComponentUI {
String numberStr = String.valueOf(i); String numberStr = String.valueOf(i);
Rectangle lineRect = textComponent.modelToView(off); Rectangle lineRect = textComponent.modelToView(off);
g.setColor(lineRect.y == yCur ? foregroundHighlight : numberingPane.getForeground()); 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); lineRect.y + lineRect.height - descent);
} catch (final BadLocationException ignored) { } catch (final BadLocationException ignored) {
} }
@ -209,12 +210,15 @@ public class DarkNumberingPaneUI extends ComponentUI {
config.restore(); config.restore();
} }
private Font getNumberingFont(final JComponent c, final Graphics g) { private Font getNumberingFont(final JComponent c, final Graphics g, final Font f) {
Font font = c.getFont(); Font baseFont = c.getFont();
if (font != null) { Font font = f;
float newSize = (float) font.getSize() - 1; if (font instanceof UIResource) {
int newSize = baseFont.getSize() - 1;
if (newSize > 0) { if (newSize > 0) {
font = font.deriveFont(newSize); font = font.deriveFont((float) newSize);
} else {
font = font.deriveFont(baseFont.getSize2D());
} }
} else { } else {
font = g.getFont(); font = g.getFont();
@ -355,9 +359,6 @@ public class DarkNumberingPaneUI extends ComponentUI {
((Caret) newCaret).addChangeListener(currentLinePainter); ((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)) { } else if (NumberingPane.KEY_EDITOR.equals(key)) {
Object newPane = evt.getNewValue(); Object newPane = evt.getNewValue();
if (textComponent != null) { if (textComponent != null) {
@ -378,8 +379,6 @@ public class DarkNumberingPaneUI extends ComponentUI {
} }
textComponent.addPropertyChangeListener(getPropertyChangeListener()); textComponent.addPropertyChangeListener(getPropertyChangeListener());
textComponent.getCaret().addChangeListener(getChangeListener()); textComponent.getCaret().addChangeListener(getChangeListener());
Font font = textComponent.getFont();
numberingPane.setFont(font.deriveFont(Math.max(font.getSize() - 1, 1.0f)));
oldBackground = textComponent.getBackground(); oldBackground = textComponent.getBackground();
textComponent.setBackground(UIManager.getColor("NumberingPane.textBackground")); textComponent.setBackground(UIManager.getColor("NumberingPane.textBackground"));
} }

Loading…
Cancel
Save