Browse Source

Extract text alignment into separate method.

pull/222/head
weisj 4 years ago
parent
commit
0ef5a677c0
  1. 5
      core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java
  2. 9
      utils/src/main/java/com/github/weisj/darklaf/util/FontUtil.java

5
core/src/main/java/com/github/weisj/darklaf/ui/text/DarkTextFieldUI.java

@ -40,6 +40,7 @@ import com.github.weisj.darklaf.listener.MouseMovementListener;
import com.github.weisj.darklaf.listener.PopupMenuAdapter; import com.github.weisj.darklaf.listener.PopupMenuAdapter;
import com.github.weisj.darklaf.ui.text.bridge.DarkTextFieldUIBridge; import com.github.weisj.darklaf.ui.text.bridge.DarkTextFieldUIBridge;
import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.FontUtil;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
/** @author Jannis Weis */ /** @author Jannis Weis */
@ -142,11 +143,9 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh
Rectangle rect = super.getVisibleEditorRect(); Rectangle rect = super.getVisibleEditorRect();
if (rect != null && shrinkHeight) { if (rect != null && shrinkHeight) {
FontMetrics fm = SwingUtilities2.getFontMetrics(editor, editor.getFont()); FontMetrics fm = SwingUtilities2.getFontMetrics(editor, editor.getFont());
int asc = fm.getMaxAscent();
Insets ins = editor.getInsets(); Insets ins = editor.getInsets();
int height = editor.getHeight() - ins.top - ins.bottom; int height = editor.getHeight() - ins.top - ins.bottom;
rect.y = ins.top + (height - asc) / 2; rect.y = ins.top + FontUtil.getCenteredFontPosition(height, fm);
rect.y -= fm.getDescent() / 2;
rect.height = fm.getHeight(); rect.height = fm.getHeight();
} }
adjustTextRect(getComponent(), rect); adjustTextRect(getComponent(), rect);

9
utils/src/main/java/com/github/weisj/darklaf/util/FontUtil.java

@ -23,8 +23,10 @@ package com.github.weisj.darklaf.util;
import java.awt.*; import java.awt.*;
import javax.swing.*;
import javax.swing.text.StyleContext; import javax.swing.text.StyleContext;
public class FontUtil { public class FontUtil {
public static Font createFont(final String family, final int style, final int size) { public static Font createFont(final String family, final int style, final int size) {
@ -33,6 +35,13 @@ public class FontUtil {
return new NonUIResourceFont(StyleContext.getDefaultStyleContext().getFont(family, style, size)); return new NonUIResourceFont(StyleContext.getDefaultStyleContext().getFont(family, style, size));
} }
public static int getCenteredFontPosition(final int height, final FontMetrics fm) {
int asc = fm.getMaxAscent();
int y = (height - asc) / 2;
y -= fm.getDescent() / 2;
return y;
}
public static class NonUIResourceFont extends Font { public static class NonUIResourceFont extends Font {
public NonUIResourceFont(final Font font) { public NonUIResourceFont(final Font font) {

Loading…
Cancel
Save