From bee1e0a6b993e3bac25685eefeb50a8e489f29d7 Mon Sep 17 00:00:00 2001 From: vito Date: Mon, 20 Nov 2023 16:44:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0jira=E4=BB=BB=E5=8A=A1=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=B4=A8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fine/theme/icon/IconManager.java | 45 ++++++++++++++++--- .../com/fine/theme/icon/IconResource.java | 6 +++ .../java/com/fine/theme/icon/IconSet.java | 2 - .../java/com/fine/theme/icon/IconSource.java | 11 +++++ .../java/com/fine/theme/icon/LazyIcon.java | 2 +- .../java/com/fine/theme/icon/svg/SvgIcon.java | 18 +++++--- .../fine/theme/icon/svg/SvgTranscoder.java | 2 +- .../com/fine/theme/utils/FineUIUtils.java | 21 ++++++--- 8 files changed, 83 insertions(+), 24 deletions(-) diff --git a/designer-base/src/main/java/com/fine/theme/icon/IconManager.java b/designer-base/src/main/java/com/fine/theme/icon/IconManager.java index 9f4600609..3f9f6c7f7 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/IconManager.java +++ b/designer-base/src/main/java/com/fine/theme/icon/IconManager.java @@ -24,11 +24,19 @@ import java.util.HashMap; public class IconManager { public static boolean initialized = false; - public static ArrayList iconSets = new ArrayList<>(2);; - public static HashMap> cache = new HashMap<>(60);; + public static ArrayList iconSets = new ArrayList<>(2); + ; + public static HashMap> cache = new HashMap<>(60); + ; public static HashMap> disableCache = new HashMap<>(60); + /** + * 获取图标集 + * + * @param id 图标集ID + * @return 图标集 + */ public static IconSet getSet(String id) { for (IconSet set : iconSets) { if (set.getId().equals(id)) { @@ -38,16 +46,28 @@ public class IconManager { throw new IconException("[IconManager] Can not find icon set by id: " + id); } + /** + * 添加图标集 + * + * @param set 图标集 + */ public static void addSet(@NotNull IconSet set) { if (!iconSets.contains(set)) { iconSets.add(set); // 清理可能来自其他图集相同名称图标 clearIconSetCache(set); } else { - throw new IconException("[IconManager] IconSet by id:" + set.getId() + "is added!"); + throw new IconException("[IconManager] IconSet by id:" + set.getId() + "is added!"); } } + /** + * 根据图标ID获取图标 + * + * @param id 图标ID + * @param 图标类型 + * @return 图标 + */ @NotNull public static I getIcon(String id) { Icon icon = findIcon(id); @@ -57,6 +77,13 @@ public class IconManager { return (I) icon; } + /** + * 获取灰化图标 + * + * @param id 图标ID + * @param 图标类型 + * @return 图标 + */ @NotNull public static I getDisableIcon(String id) { Icon icon = findDisableIcon(id); @@ -67,7 +94,7 @@ public class IconManager { } @Nullable - public static I findDisableIcon(String id) { + private static I findDisableIcon(String id) { final WeakReference reference = disableCache.get(id); I icon = reference != null ? (I) reference.get() : null; if (icon == null) { @@ -83,7 +110,7 @@ public class IconManager { } @Nullable - public static I findIcon(String id) { + private static I findIcon(String id) { final WeakReference reference = cache.get(id); I icon = reference != null ? (I) reference.get() : null; if (icon == null) { @@ -98,15 +125,21 @@ public class IconManager { return icon; } + /** + * 清理所有缓存 + */ public static void clearCache() { cache.clear(); } + /** + * 清理图标缓存 + */ public static void clearIconCache(String id) { cache.remove(id); } - public static void clearIconSetCache(@NotNull IconSet set) { + private static void clearIconSetCache(@NotNull IconSet set) { for (String id : set.getIds()) { cache.remove(id); } diff --git a/designer-base/src/main/java/com/fine/theme/icon/IconResource.java b/designer-base/src/main/java/com/fine/theme/icon/IconResource.java index d5375c745..f654a6cf8 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/IconResource.java +++ b/designer-base/src/main/java/com/fine/theme/icon/IconResource.java @@ -12,6 +12,12 @@ import java.io.InputStream; * Created on 2023/11/6 */ public interface IconResource { + + /** + * 获取输入资源流 + * + * @return 资源流 + */ @Nullable InputStream getInputStream(); } diff --git a/designer-base/src/main/java/com/fine/theme/icon/IconSet.java b/designer-base/src/main/java/com/fine/theme/icon/IconSet.java index 5f14f697e..f5f92d6ba 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/IconSet.java +++ b/designer-base/src/main/java/com/fine/theme/icon/IconSet.java @@ -47,7 +47,6 @@ public interface IconSet extends Identifiable { * * @param id id * @return Icon - * todo 实现 iconset,实现 iconsource */ @Nullable Icon findIcon(@NotNull String id); @@ -58,7 +57,6 @@ public interface IconSet extends Identifiable { * * @param id id * @return Icon - * todo 实现 iconset,实现 iconsource */ @Nullable Icon findDisableIcon(@NotNull String id); diff --git a/designer-base/src/main/java/com/fine/theme/icon/IconSource.java b/designer-base/src/main/java/com/fine/theme/icon/IconSource.java index 7d6234afe..ed2d1c6b7 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/IconSource.java +++ b/designer-base/src/main/java/com/fine/theme/icon/IconSource.java @@ -16,10 +16,21 @@ import java.io.Serializable; */ @Immutable public interface IconSource extends Identifiable, DisabledIcon, Cloneable, Serializable { + + /** + * 获取图标资源 + * + * @return 图标资源 + */ @NotNull IconResource getResource(); + /** + * 加载图标 + * + * @return 图标 + */ @NotNull I loadIcon(); } diff --git a/designer-base/src/main/java/com/fine/theme/icon/LazyIcon.java b/designer-base/src/main/java/com/fine/theme/icon/LazyIcon.java index 3e72dab96..aa77042fd 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/LazyIcon.java +++ b/designer-base/src/main/java/com/fine/theme/icon/LazyIcon.java @@ -17,7 +17,7 @@ import java.awt.Graphics; @Immutable public class LazyIcon implements Identifiable, DisabledIcon, Icon { @NotNull - private String id; + private final String id; public LazyIcon(@NotNull final String id) { this.id = id; diff --git a/designer-base/src/main/java/com/fine/theme/icon/svg/SvgIcon.java b/designer-base/src/main/java/com/fine/theme/icon/svg/SvgIcon.java index 9887a7eb1..1d81ed150 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/svg/SvgIcon.java +++ b/designer-base/src/main/java/com/fine/theme/icon/svg/SvgIcon.java @@ -18,7 +18,7 @@ import java.awt.image.BufferedImage; import static com.fine.theme.utils.FineUIScale.scale; import static com.fine.theme.utils.FineUIScale.unscale; import static com.fine.theme.utils.FineUIUtils.RETINA_SCALE_FACTOR; -import static com.fine.theme.utils.FineUIUtils.isRetina; +import static com.fine.theme.utils.FineUIUtils.getRetina; /** * svg图标 @@ -62,9 +62,7 @@ public class SvgIcon implements DisabledIcon, Icon { */ @Override public void paintIcon(Component c, Graphics g, int x, int y) { - if (cache == null - || cache.getWidth() != scaleRetina(size.width) - || cache.getHeight() != scaleRetina(size.height)) { + if (isCacheValid()) { if (cache != null) { cache.flush(); } @@ -75,7 +73,7 @@ public class SvgIcon implements DisabledIcon, Icon { cache = toImage(scale); } } - if (isRetina()) { + if (getRetina()) { // 高清绘制的原理:scale(1/2,1/2)的原理是坐标减半,底层是矩阵进行坐标变换,意思是坐标减半进行绘制, // 这样就可以将两倍图绘制到一倍的大小,如果这时候设备支持Retina绘制(四个像素模拟一个像素), // 正好就可以将4个像素利用起来,每个像素点都有不同的颜色,而不像之前只能是四个共用一个颜色。因此图像 @@ -88,14 +86,20 @@ public class SvgIcon implements DisabledIcon, Icon { } } + private boolean isCacheValid() { + return cache == null + || cache.getWidth() != scaleRetina(size.width) + || cache.getHeight() != scaleRetina(size.height); + } + private static int scaleRetina(int size) { - return isRetina() + return getRetina() ? size * RETINA_SCALE_FACTOR : size; } private static Dimension scaleRetina(Dimension dimension) { - return isRetina() + return getRetina() ? new Dimension(dimension.width * RETINA_SCALE_FACTOR, dimension.height * RETINA_SCALE_FACTOR) : dimension; } diff --git a/designer-base/src/main/java/com/fine/theme/icon/svg/SvgTranscoder.java b/designer-base/src/main/java/com/fine/theme/icon/svg/SvgTranscoder.java index ca4a6132a..abb55e1c7 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/svg/SvgTranscoder.java +++ b/designer-base/src/main/java/com/fine/theme/icon/svg/SvgTranscoder.java @@ -47,7 +47,7 @@ public class SvgTranscoder extends ImageTranscoder { } /** - * 灰度控件底图 + * 灰化底图 */ private BufferedImage createGrayImage(int width, int height) { return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); diff --git a/designer-base/src/main/java/com/fine/theme/utils/FineUIUtils.java b/designer-base/src/main/java/com/fine/theme/utils/FineUIUtils.java index 3af6be8e5..b4b6c9a30 100644 --- a/designer-base/src/main/java/com/fine/theme/utils/FineUIUtils.java +++ b/designer-base/src/main/java/com/fine/theme/utils/FineUIUtils.java @@ -24,10 +24,11 @@ public class FineUIUtils { * 判断是否支持retina,制作一些特殊效果,如HIDPI图片绘制。 * retina 是一种特殊的效果,使用4个像素点模拟一个像素点, * 因此在其他操作系统上,即使是高分屏也不具备retina的效果。 + * 甚至还有劣化的效果以及更差的性能。 * * @since 2023.11.16 */ - private static final AtomicClearableLazyValue retina = AtomicClearableLazyValue.create(() -> { + private static final AtomicClearableLazyValue RETINA = AtomicClearableLazyValue.create(() -> { // 经过测试win11,ubuntu等,没有retina效果 if (!OperatingSystem.isMacos()) { return false; @@ -39,7 +40,7 @@ public class FineUIUtils { Field field = device.getClass().getDeclaredField("scale"); field.setAccessible(true); Object scale = field.get(device); - if (scale instanceof Integer && (Integer) scale == 2) { + if (scale instanceof Integer && (Integer) scale == RETINA_SCALE_FACTOR) { return true; } } catch (Exception ignored) { @@ -47,13 +48,20 @@ public class FineUIUtils { return false; }); - - public static boolean isRetina() { - return retina.getValue(); + /** + * 是否支持 retina + * + * @return 是否支持 retina + */ + public static boolean getRetina() { + return RETINA.getValue(); } + /** + * 放弃 retina 判断结果,用于清理或者切换环境 + */ public static void clearRetina() { - retina.drop(); + RETINA.drop(); } /** @@ -63,7 +71,6 @@ public class FineUIUtils { * @param defaultKey 颜色后备key * @return 颜色 */ - public static Color getUIColor(String key, String defaultKey) { Color color = UIManager.getColor(key); return (color != null) ? color : UIManager.getColor(defaultKey);