From aa66ea3a3536cc108532a80a7d257ebc91584b8e Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Tue, 29 Mar 2016 14:55:00 +0200 Subject: [PATCH] size method should rescale dimension objects --- .../util/src/com/intellij/util/ui/JBUI.java | 90 +++++++++++++------ 1 file changed, 61 insertions(+), 29 deletions(-) diff --git a/platform/util/src/com/intellij/util/ui/JBUI.java b/platform/util/src/com/intellij/util/ui/JBUI.java index 6139cfdca0f2..25413e0c1966 100644 --- a/platform/util/src/com/intellij/util/ui/JBUI.java +++ b/platform/util/src/com/intellij/util/ui/JBUI.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.CompoundBorder; +import javax.swing.plaf.UIResource; import java.awt.*; /** @@ -36,35 +37,61 @@ import java.awt.*; public class JBUI { private static final Logger LOG = Logger.getInstance("#com.intellij.util.ui.JBUI"); - private static float scaleFactor = 1.0f; + private static float SCALE_FACTOR = calculateScaleFactor(); - static { - calculateScaleFactor(); - } - - private static void calculateScaleFactor() { + private static float calculateScaleFactor() { if (SystemInfo.isMac) { LOG.info("UI scale factor: 1.0"); - scaleFactor = 1.0f; - return; + return 1.0f; } if (SystemProperties.has("hidpi") && !SystemProperties.is("hidpi")) { LOG.info("UI scale factor: 1.0"); - scaleFactor = 1.0f; - return; + return 1.0f; } - UIUtil.initSystemFontData(); - Pair fdata = UIUtil.getSystemFontData(); + float s = 1f; - int size; - if (fdata != null) { - size = fdata.getSecond(); - } else { + // On Linux: rely on DPI + if (SystemInfo.isLinux) { + final int dpi = getSystemDPI(); + if (dpi < 120) s = 1f; + else if (dpi < 144) s = 1.25f; + else if (dpi < 168) s = 1.5f; + else if (dpi < 192) s = 1.75f; + else s = 2f; + + LOG.info("UI scale factor: " + s); + return s; + } + + int size = -1; + + // On Windows: rely on default system font + if (SystemInfo.isWindows) { + UIUtil.initSystemFontData(); + Pair fdata = UIUtil.getSystemFontData(); + if (fdata != null) size = fdata.getSecond(); + } + if (size == -1) { size = Fonts.label().getSize(); } - setScaleFactor(size/UIUtil.DEF_SYSTEM_FONT_SIZE); + if (size <= 13) s = 1.0f; + else if (size <= 16) s = 1.25f; + else if (size <= 18) s = 1.5f; + else if (size < 24) s = 1.75f; + else s = 2.0f; + + LOG.info("UI scale factor: " + s); + return s; + } + + private static int getSystemDPI() { + try { + return Toolkit.getDefaultToolkit().getScreenResolution(); + } catch (HeadlessException e) { + return 96; + } } public static void setScaleFactor(float scale) { @@ -82,22 +109,19 @@ public class JBUI { //Default UI font size for Unity and Gnome is 15. Scaling factor 1.25f works badly on Linux scale = 1f; } - if (scaleFactor == scale) { - return; - } - LOG.info("UI scale factor: " + scale); + LOG.info("UI scale factor changed: " + scale); - scaleFactor = scale; + SCALE_FACTOR = scale; IconLoader.setScale(scale); } public static int scale(int i) { - return Math.round(scaleFactor * i); + return Math.round(SCALE_FACTOR * i); } public static int scaleFontSize(int fontSize) { - if (scaleFactor == 1.25f) return (int)(fontSize * 1.34f); - if (scaleFactor == 1.75f) return (int)(fontSize * 1.67f); + if (SCALE_FACTOR == 1.25f) return (int)(fontSize * 1.34f); + if (SCALE_FACTOR == 1.75f) return (int)(fontSize * 1.67f); return scale(fontSize); } @@ -110,7 +134,15 @@ public class JBUI { } public static JBDimension size(Dimension size) { - return size instanceof JBDimension ? ((JBDimension)size) : new JBDimension(size.width, size.height); + if (size instanceof JBDimension) { + final JBDimension jbSize = (JBDimension)size; + if (jbSize.originalScale == scale(1f)) { + return jbSize; + } + final JBDimension newSize = new JBDimension((int)(jbSize.width / jbSize.originalScale), (int)(jbSize.height / jbSize.originalScale)); + return size instanceof UIResource ? newSize.asUIResource() : newSize; + } + return new JBDimension(size.width, size.height); } public static JBInsets insets(int top, int left, int bottom, int right) { @@ -154,7 +186,7 @@ public class JBUI { } public static float scale(float f) { - return f * scaleFactor; + return f * SCALE_FACTOR; } public static JBInsets insets(Insets insets) { @@ -162,7 +194,7 @@ public class JBUI { } public static boolean isHiDPI() { - return scaleFactor > 1.0f; + return SCALE_FACTOR > 1.0f; } public static class Fonts {