From 98d876b00c9fe94c2b1f95f42d9205c938f847e4 Mon Sep 17 00:00:00 2001 From: Anton Tarasov Date: Mon, 31 Oct 2016 17:06:26 +0300 Subject: [PATCH] [followup] Add/refactor JBUI.JBIcon/ScalableJBIcon/ValidatingScalableJBIcon --- .../src/com/intellij/ui/LayeredIcon.java | 8 ++++---- .../core-api/src/com/intellij/ui/RowIcon.java | 1 - .../lookup/DefaultLookupItemRenderer.java | 8 ++++---- .../src/com/intellij/ui/SizedIcon.java | 18 ++---------------- .../util/src/com/intellij/util/ArrayUtil.java | 4 ++++ .../util/src/com/intellij/util/ui/JBUI.java | 18 ++++++++++++------ 6 files changed, 26 insertions(+), 31 deletions(-) diff --git a/platform/core-api/src/com/intellij/ui/LayeredIcon.java b/platform/core-api/src/com/intellij/ui/LayeredIcon.java index cebd0f799dbe..36bee943eb19 100644 --- a/platform/core-api/src/com/intellij/ui/LayeredIcon.java +++ b/platform/core-api/src/com/intellij/ui/LayeredIcon.java @@ -81,7 +81,6 @@ public class LayeredIcon extends JBUI.AuxScalableJBIcon { } for (Icon icon : myIcons) { if (icon != null && !(icon instanceof ScalableIcon)) { - setScale(1f); return myScaledIcons = myIcons; } } @@ -221,13 +220,14 @@ public class LayeredIcon extends JBUI.AuxScalableJBIcon { @Override public void paintIcon(Component c, Graphics g, int x, int y) { if (updateJBUIScale()) updateSize(); - int i = 0; - for (Icon icon : myScaledIcons()) { + Icon[] icons = myScaledIcons(); + if (icons == null) return; + for (int i = 0; i < icons.length; i++) { + Icon icon = icons[i]; if (icon == null || myDisabledLayers[i]) continue; int xOffset = x + scaleVal(myXShift + myHShifts[i], Scale.ARBITRARY); int yOffset = y + scaleVal(myYShift + myVShifts[i], Scale.ARBITRARY); icon.paintIcon(c, g, xOffset, yOffset); - i++; } } diff --git a/platform/core-api/src/com/intellij/ui/RowIcon.java b/platform/core-api/src/com/intellij/ui/RowIcon.java index 701aa254a9df..84dc600a661a 100644 --- a/platform/core-api/src/com/intellij/ui/RowIcon.java +++ b/platform/core-api/src/com/intellij/ui/RowIcon.java @@ -78,7 +78,6 @@ public class RowIcon extends JBUI.AuxScalableJBIcon { } for (Icon icon : myIcons) { if (icon != null && !(icon instanceof ScalableIcon)) { - setScale(1f); return myScaledIcons = myIcons; } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java index 68f32ec40cdb..a67e30db2481 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.lookup; import com.intellij.openapi.util.Iconable; +import com.intellij.openapi.util.ScalableIcon; import com.intellij.psi.PsiElement; import com.intellij.psi.meta.PsiMetaData; import com.intellij.psi.util.PsiUtilCore; @@ -48,10 +49,9 @@ public class DefaultLookupItemRenderer extends LookupElementRenderer @Nullable public static Icon getRawIcon(final LookupElement item, boolean real) { Icon icon = _getRawIcon(item, real); - // align SAMPLE_ICON.getIconHeight() with the 'icon' scale - int sampleIconHeight = ScalableJBIcon.scaleVal(icon, SAMPLE_ICON.getIconHeight(), Scale.ARBITRARY); - if (icon != null && icon.getIconHeight() > sampleIconHeight) { - return new SizedIcon(icon, icon.getIconWidth(), sampleIconHeight, true); + if (icon instanceof ScalableIcon) icon = ((ScalableIcon)icon).scale(1f); + if (icon != null && icon.getIconHeight() > SAMPLE_ICON.getIconHeight()) { + return new SizedIcon(icon, icon.getIconWidth(), SAMPLE_ICON.getIconHeight()); } return icon; } diff --git a/platform/platform-api/src/com/intellij/ui/SizedIcon.java b/platform/platform-api/src/com/intellij/ui/SizedIcon.java index d2d718f74a63..84ae42630955 100644 --- a/platform/platform-api/src/com/intellij/ui/SizedIcon.java +++ b/platform/platform-api/src/com/intellij/ui/SizedIcon.java @@ -31,23 +31,9 @@ public class SizedIcon extends JBUI.CachingScalableJBIcon { private Icon myScaledDelegate; public SizedIcon(Icon delegate, int width, int height) { - this(delegate, width, height, false); - } - - /** - * @param delegate the delegate icon - * @param width this icon width - * @param height this icon height - * @param inDelegateScale whether to align this icon scale with the delegate icon scale - */ - public SizedIcon(Icon delegate, int width, int height, boolean inDelegateScale) { - float scale = 1f; - if (inDelegateScale && delegate instanceof ScalableIcon) { - setScale(scale = ((ScalableIcon)delegate).getScale()); - } myScaledDelegate = myDelegate = delegate; - myWidth = (int)(width / scale); - myHeight = (int)(height / scale); + myWidth = width; + myHeight = height; } protected SizedIcon(SizedIcon icon) { diff --git a/platform/util/src/com/intellij/util/ArrayUtil.java b/platform/util/src/com/intellij/util/ArrayUtil.java index b5b5cd24d4eb..2366dd381213 100644 --- a/platform/util/src/com/intellij/util/ArrayUtil.java +++ b/platform/util/src/com/intellij/util/ArrayUtil.java @@ -908,18 +908,22 @@ public class ArrayUtil extends ArrayUtilRt { } @Nullable + @Contract("null -> null; !null -> !null") + @SuppressWarnings("unchecked") public static T[] copyOf(@Nullable T[] original) { if (original == null) return null; return (T[]) Arrays.copyOf(original, original.length, original.getClass()); } @Nullable + @Contract("null -> null; !null -> !null") public static boolean[] copyOf(@Nullable boolean[] original) { if (original == null) return null; return Arrays.copyOf(original, original.length); } @Nullable + @Contract("null -> null; !null -> !null") public static int[] copyOf(@Nullable int[] original) { if (original == null) return null; return Arrays.copyOf(original, original.length); diff --git a/platform/util/src/com/intellij/util/ui/JBUI.java b/platform/util/src/com/intellij/util/ui/JBUI.java index 2ebb1da1e8be..e74015ee826f 100644 --- a/platform/util/src/com/intellij/util/ui/JBUI.java +++ b/platform/util/src/com/intellij/util/ui/JBUI.java @@ -291,7 +291,7 @@ public class JBUI { * @author tav */ public static abstract class JBIcon implements Icon { - private float myInitialJBUIScale = scale(1f); + private float myInitialJBUIScale = currentJBUIScale(); protected JBIcon() {} @@ -299,11 +299,17 @@ public class JBUI { myInitialJBUIScale = icon.myInitialJBUIScale; } + static float currentJBUIScale() { + // We don't JBUI-scale images on Retina, see comments in ImageLoader.loadFromUrl(..) + // So, make icons JBUI-scale conformant. + return UIUtil.isRetina() ? 1f : scale(1f); + } + /** * @return the scale factor aligning the icon size metrics to conform to up-to-date JBUI.scale */ private float getAligningScale() { - return scale(1f) / myInitialJBUIScale; + return currentJBUIScale() / myInitialJBUIScale; } /** @@ -317,7 +323,7 @@ public class JBUI { * Sets the icon size metrics to {@code preScaled} */ protected void setJBUIPreScaled(boolean preScaled) { - myInitialJBUIScale = preScaled ? scale(1f) : 1f; + myInitialJBUIScale = preScaled ? currentJBUIScale() : 1f; } /** @@ -499,7 +505,7 @@ public class JBUI { * @author tav */ public static abstract class AuxScalableJBIcon extends CachingScalableJBIcon implements AuxJBUIScale { - private float myCachedJBUIScale = JBUI.scale(1f); + private float myCachedJBUIScale = currentJBUIScale(); protected AuxScalableJBIcon() {} @@ -510,7 +516,7 @@ public class JBUI { @Override public boolean updateJBUIScale() { if (needUpdateJBUIScale()) { - myCachedJBUIScale = JBUI.scale(1f); + myCachedJBUIScale = currentJBUIScale(); return true; } return false; @@ -518,7 +524,7 @@ public class JBUI { @Override public boolean needUpdateJBUIScale() { - return myCachedJBUIScale != JBUI.scale(1f); + return myCachedJBUIScale != currentJBUIScale(); } } }