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.ui.text.bridge.DarkTextFieldUIBridge;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.FontUtil;
import com.github.weisj.darklaf.util.PropertyUtil;
/** @author Jannis Weis */
@ -142,11 +143,9 @@ public class DarkTextFieldUI extends DarkTextFieldUIBridge implements PropertyCh
Rectangle rect = super.getVisibleEditorRect();
if (rect != null && shrinkHeight) {
FontMetrics fm = SwingUtilities2.getFontMetrics(editor, editor.getFont());
int asc = fm.getMaxAscent();
Insets ins = editor.getInsets();
int height = editor.getHeight() - ins.top - ins.bottom;
rect.y = ins.top + (height - asc) / 2;
rect.y -= fm.getDescent() / 2;
rect.y = ins.top + FontUtil.getCenteredFontPosition(height, fm);
rect.height = fm.getHeight();
}
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 javax.swing.*;
import javax.swing.text.StyleContext;
public class FontUtil {
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));
}
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 NonUIResourceFont(final Font font) {

Loading…
Cancel
Save