Fixed issue with scaling fonts incorrectly on Swing UIs.

This commit is contained in:
nathan 2017-02-23 01:31:05 +01:00
parent 29186db011
commit ee5f509e76
5 changed files with 0 additions and 78 deletions

View File

@ -30,7 +30,6 @@ import dorkbox.systemTray.Separator;
import dorkbox.systemTray.Status;
import dorkbox.systemTray.SystemTray;
import dorkbox.systemTray.peer.MenuPeer;
import dorkbox.systemTray.util.ImageUtils;
import dorkbox.util.SwingUtil;
// this is a weird composite class, because it must be a Menu, but ALSO a Entry -- so it has both (and duplicate code)
@ -47,10 +46,6 @@ class SwingMenu implements MenuPeer {
if (parent == null) {
TrayPopup trayPopup = new TrayPopup();
// this is before setUI, so that users can customize the font if they want
if (ImageUtils.ENTRY_FONT != null) {
trayPopup.setFont(ImageUtils.ENTRY_FONT);
}
if (SystemTray.SWING_UI != null) {
trayPopup.setUI(SystemTray.SWING_UI.getMenuUI(trayPopup, null));
}
@ -60,8 +55,6 @@ class SwingMenu implements MenuPeer {
JMenu jMenu = new JMenu();
JPopupMenu popupMenu = jMenu.getPopupMenu(); // ensure the popup menu is created
// this is before setUI, so that users can customize the font if they want
jMenu.setFont(ImageUtils.ENTRY_FONT);
if (SystemTray.SWING_UI != null) {
jMenu.setUI(SystemTray.SWING_UI.getItemUI(jMenu, entry));
popupMenu.setUI(SystemTray.SWING_UI.getMenuUI(popupMenu, entry));

View File

@ -44,10 +44,6 @@ class SwingMenuItem implements MenuItemPeer {
SwingMenuItem(final SwingMenu parent, Entry entry) {
this.parent = parent;
// this is before setUI, so that users can customize the font if they want
if (ImageUtils.ENTRY_FONT != null) {
_native.setFont(ImageUtils.ENTRY_FONT);
}
if (SystemTray.SWING_UI != null) {
_native.setUI(SystemTray.SWING_UI.getItemUI(_native, entry));
}

View File

@ -45,10 +45,6 @@ class SwingMenuItemCheckbox implements CheckboxPeer {
SwingMenuItemCheckbox(final SwingMenu parent, final Entry entry) {
this.parent = parent;
// this is before setUI, so that users can customize the font if they want
if (ImageUtils.ENTRY_FONT != null) {
_native.setFont(ImageUtils.ENTRY_FONT);
}
if (SystemTray.SWING_UI != null) {
_native.setUI(SystemTray.SWING_UI.getItemUI(_native, entry));
}

View File

@ -23,7 +23,6 @@ import dorkbox.systemTray.Entry;
import dorkbox.systemTray.Status;
import dorkbox.systemTray.SystemTray;
import dorkbox.systemTray.peer.StatusPeer;
import dorkbox.systemTray.util.ImageUtils;
import dorkbox.util.SwingUtil;
class SwingMenuItemStatus implements StatusPeer {
@ -35,10 +34,6 @@ class SwingMenuItemStatus implements StatusPeer {
SwingMenuItemStatus(final SwingMenu parent, final Entry entry) {
this.parent = parent;
// this is before setUI, so that users can customize the font if they want
if (ImageUtils.ENTRY_FONT != null) {
_native.setFont(ImageUtils.ENTRY_FONT);
}
if (SystemTray.SWING_UI != null) {
_native.setUI(SystemTray.SWING_UI.getItemUI(_native, entry));
}

View File

@ -20,12 +20,9 @@ import static dorkbox.systemTray.jna.windows.Gdi32.LOGPIXELSX;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -43,7 +40,6 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import com.sun.jna.Pointer;
@ -70,10 +66,6 @@ class ImageUtils {
public static volatile int TRAY_SIZE = 0;
public static volatile int ENTRY_SIZE = 0;
// the menu entry font size ALSO must be detected, and it is a little bit tricky to figure this out.
// Only exists (and is necessary) for SWING menus
public static volatile Font ENTRY_FONT = null;
public static
void determineIconSize() {
double trayScalingFactor = 0;
@ -294,63 +286,13 @@ class ImageUtils {
ENTRY_SIZE = SystemTray.DEFAULT_MENU_SIZE;
}
// this must be a JMenuItem component, because that is the component we are setting the font on.
// this is only important to do if we are a swing tray type, which ONLY happens in Windows
if (OS.isWindows()) {
// must be a plain style font
Font font = new JMenuItem().getFont().deriveFont(Font.PLAIN);
if (menuScalingFactor > 1) {
font = ImageUtils.getFontForSpecificHeight(font, ENTRY_SIZE);
if (SystemTray.DEBUG) {
SystemTray.logger.debug("Menu entry font size '{}' found for requested size '{}'", font.getSize(), ENTRY_SIZE);
}
} else if (SystemTray.DEBUG) {
SystemTray.logger.debug("Menu entry font size '{}'. Not scaling for requested size '{}'", font.getSize(), ENTRY_SIZE);
}
ENTRY_FONT = font;
}
if (SystemTray.DEBUG) {
SystemTray.logger.debug("ScalingFactor is '{}', tray icon size is '{}'.", trayScalingFactor, TRAY_SIZE);
SystemTray.logger.debug("ScalingFactor is '{}', tray menu size is '{}'.", menuScalingFactor, ENTRY_SIZE);
}
}
/**
* Gets the correct font (in GENERAL) for a specified pixel height.
* @param font the font we are checking
* @param height the height in pixels we want to get as close as possible to
*
* @return the font (derived from the specified font) that is as close as possible to the requested height
*/
private static
Font getFontForSpecificHeight(final Font font, final int height) {
int size = font.getSize();
Boolean lastAction = null;
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
while (true) {
Font fontCheck = new Font(font.getName(), Font.PLAIN, size);
FontMetrics metrics = g.getFontMetrics(fontCheck);
Rectangle2D rect = metrics.getStringBounds("Tj|", g);
int testHeight = (int) rect.getHeight();
if (testHeight < height && lastAction != Boolean.FALSE) {
size++;
lastAction = Boolean.TRUE;
} else if (testHeight > height && lastAction != Boolean.TRUE) {
size--;
lastAction = Boolean.FALSE;
} else {
// either we are the exact size, or we are ONE font size to big/small (depending on what our initial guess was)
return fontCheck;
}
}
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static