EmptyIcon: move scaled cache to parent

This commit is contained in:
Konstantin Bulenkov
2016-03-01 13:45:39 +01:00
parent ebc9ca0820
commit 310ad3453d
2 changed files with 25 additions and 23 deletions
@@ -18,7 +18,6 @@ package com.intellij.util.ui;
import com.intellij.ui.Gray;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
/**
@@ -28,7 +27,6 @@ public class ColorIcon extends EmptyIcon {
private final Color myColor;
private boolean myBorder;
private int myColorSize;
private ColorIcon myScaledCache;
public ColorIcon(int size, int colorSize, @NotNull Color color, final boolean border) {
super(size, size);
@@ -68,20 +66,13 @@ public class ColorIcon extends EmptyIcon {
}
protected int getColorSize() {
return scale == 1f ? myColorSize : (int)(myColorSize * scale);
return scale(myColorSize);
}
@Override
public Icon scale(float scaleFactor) {
if (scale == scaleFactor) {
return this;
}
if (myScaledCache != null && myScaledCache.scale == scaleFactor) {
return myScaledCache;
}
myScaledCache = new ColorIcon(getIconWidth(), myColorSize, myColor, myBorder);
myScaledCache.scale = scaleFactor;
return myScaledCache;
protected EmptyIcon createScaledInstance(float scale) {
return new ColorIcon(getIconWidth(), myColorSize, myColor, myBorder);
}
@Override
@@ -42,6 +42,7 @@ public class EmptyIcon implements Icon, ScalableIcon {
private final int width;
private final int height;
protected float scale = 1f;
private EmptyIcon myScaledCache;
public static Icon create(int size) {
Icon icon = cache.get(size);
@@ -123,17 +124,27 @@ public class EmptyIcon implements Icon, ScalableIcon {
@Override
public Icon scale(float scaleFactor) {
if (scaleFactor != scale) {
EmptyIcon icon;
if (scale != 1f) {
icon = this;
} else {
icon = this instanceof UIResource ? new EmptyIconUIResource(width, height) : new EmptyIcon(width, height);
}
icon.scale = scaleFactor;
return icon;
if (scale == scaleFactor) {
return this;
}
return this;
if (myScaledCache != null && myScaledCache.scale == scaleFactor) {
return myScaledCache;
}
myScaledCache = createScaledInstance(scaleFactor);
myScaledCache.scale = scaleFactor;
return myScaledCache;
}
protected EmptyIcon createScaledInstance(float scale) {
final EmptyIcon icon;
if (scale != 1f) {
icon = this;
} else {
icon = this instanceof UIResource ? new EmptyIconUIResource(width, height) : new EmptyIcon(width, height);
}
return icon;
}
public static class EmptyIconUIResource extends EmptyIcon implements UIResource {