Browse Source

无jira任务 代码质量

newui
vito 8 months ago
parent
commit
bee1e0a6b9
  1. 45
      designer-base/src/main/java/com/fine/theme/icon/IconManager.java
  2. 6
      designer-base/src/main/java/com/fine/theme/icon/IconResource.java
  3. 2
      designer-base/src/main/java/com/fine/theme/icon/IconSet.java
  4. 11
      designer-base/src/main/java/com/fine/theme/icon/IconSource.java
  5. 2
      designer-base/src/main/java/com/fine/theme/icon/LazyIcon.java
  6. 18
      designer-base/src/main/java/com/fine/theme/icon/svg/SvgIcon.java
  7. 2
      designer-base/src/main/java/com/fine/theme/icon/svg/SvgTranscoder.java
  8. 21
      designer-base/src/main/java/com/fine/theme/utils/FineUIUtils.java

45
designer-base/src/main/java/com/fine/theme/icon/IconManager.java

@ -24,11 +24,19 @@ import java.util.HashMap;
public class IconManager { public class IconManager {
public static boolean initialized = false; public static boolean initialized = false;
public static ArrayList<IconSet> iconSets = new ArrayList<>(2);; public static ArrayList<IconSet> iconSets = new ArrayList<>(2);
public static HashMap<String, WeakReference<Icon>> cache = new HashMap<>(60);; ;
public static HashMap<String, WeakReference<Icon>> cache = new HashMap<>(60);
;
public static HashMap<String, WeakReference<Icon>> disableCache = new HashMap<>(60); public static HashMap<String, WeakReference<Icon>> disableCache = new HashMap<>(60);
/**
* 获取图标集
*
* @param id 图标集ID
* @return 图标集
*/
public static IconSet getSet(String id) { public static IconSet getSet(String id) {
for (IconSet set : iconSets) { for (IconSet set : iconSets) {
if (set.getId().equals(id)) { if (set.getId().equals(id)) {
@ -38,16 +46,28 @@ public class IconManager {
throw new IconException("[IconManager] Can not find icon set by id: " + id); throw new IconException("[IconManager] Can not find icon set by id: " + id);
} }
/**
* 添加图标集
*
* @param set 图标集
*/
public static void addSet(@NotNull IconSet set) { public static void addSet(@NotNull IconSet set) {
if (!iconSets.contains(set)) { if (!iconSets.contains(set)) {
iconSets.add(set); iconSets.add(set);
// 清理可能来自其他图集相同名称图标 // 清理可能来自其他图集相同名称图标
clearIconSetCache(set); clearIconSetCache(set);
} else { } 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 <I> 图标类型
* @return 图标
*/
@NotNull @NotNull
public static <I extends Icon> I getIcon(String id) { public static <I extends Icon> I getIcon(String id) {
Icon icon = findIcon(id); Icon icon = findIcon(id);
@ -57,6 +77,13 @@ public class IconManager {
return (I) icon; return (I) icon;
} }
/**
* 获取灰化图标
*
* @param id 图标ID
* @param <I> 图标类型
* @return 图标
*/
@NotNull @NotNull
public static <I extends Icon> I getDisableIcon(String id) { public static <I extends Icon> I getDisableIcon(String id) {
Icon icon = findDisableIcon(id); Icon icon = findDisableIcon(id);
@ -67,7 +94,7 @@ public class IconManager {
} }
@Nullable @Nullable
public static <I extends Icon> I findDisableIcon(String id) { private static <I extends Icon> I findDisableIcon(String id) {
final WeakReference<Icon> reference = disableCache.get(id); final WeakReference<Icon> reference = disableCache.get(id);
I icon = reference != null ? (I) reference.get() : null; I icon = reference != null ? (I) reference.get() : null;
if (icon == null) { if (icon == null) {
@ -83,7 +110,7 @@ public class IconManager {
} }
@Nullable @Nullable
public static <I extends Icon> I findIcon(String id) { private static <I extends Icon> I findIcon(String id) {
final WeakReference<Icon> reference = cache.get(id); final WeakReference<Icon> reference = cache.get(id);
I icon = reference != null ? (I) reference.get() : null; I icon = reference != null ? (I) reference.get() : null;
if (icon == null) { if (icon == null) {
@ -98,15 +125,21 @@ public class IconManager {
return icon; return icon;
} }
/**
* 清理所有缓存
*/
public static void clearCache() { public static void clearCache() {
cache.clear(); cache.clear();
} }
/**
* 清理图标缓存
*/
public static void clearIconCache(String id) { public static void clearIconCache(String id) {
cache.remove(id); cache.remove(id);
} }
public static void clearIconSetCache(@NotNull IconSet set) { private static void clearIconSetCache(@NotNull IconSet set) {
for (String id : set.getIds()) { for (String id : set.getIds()) {
cache.remove(id); cache.remove(id);
} }

6
designer-base/src/main/java/com/fine/theme/icon/IconResource.java

@ -12,6 +12,12 @@ import java.io.InputStream;
* Created on 2023/11/6 * Created on 2023/11/6
*/ */
public interface IconResource { public interface IconResource {
/**
* 获取输入资源流
*
* @return 资源流
*/
@Nullable @Nullable
InputStream getInputStream(); InputStream getInputStream();
} }

2
designer-base/src/main/java/com/fine/theme/icon/IconSet.java

@ -47,7 +47,6 @@ public interface IconSet extends Identifiable {
* *
* @param id id * @param id id
* @return Icon * @return Icon
* todo 实现 iconset实现 iconsource
*/ */
@Nullable @Nullable
Icon findIcon(@NotNull String id); Icon findIcon(@NotNull String id);
@ -58,7 +57,6 @@ public interface IconSet extends Identifiable {
* *
* @param id id * @param id id
* @return Icon * @return Icon
* todo 实现 iconset实现 iconsource
*/ */
@Nullable @Nullable
Icon findDisableIcon(@NotNull String id); Icon findDisableIcon(@NotNull String id);

11
designer-base/src/main/java/com/fine/theme/icon/IconSource.java

@ -16,10 +16,21 @@ import java.io.Serializable;
*/ */
@Immutable @Immutable
public interface IconSource<I extends Icon> extends Identifiable, DisabledIcon, Cloneable, Serializable { public interface IconSource<I extends Icon> extends Identifiable, DisabledIcon, Cloneable, Serializable {
/**
* 获取图标资源
*
* @return 图标资源
*/
@NotNull @NotNull
IconResource getResource(); IconResource getResource();
/**
* 加载图标
*
* @return 图标
*/
@NotNull @NotNull
I loadIcon(); I loadIcon();
} }

2
designer-base/src/main/java/com/fine/theme/icon/LazyIcon.java

@ -17,7 +17,7 @@ import java.awt.Graphics;
@Immutable @Immutable
public class LazyIcon implements Identifiable, DisabledIcon, Icon { public class LazyIcon implements Identifiable, DisabledIcon, Icon {
@NotNull @NotNull
private String id; private final String id;
public LazyIcon(@NotNull final String id) { public LazyIcon(@NotNull final String id) {
this.id = id; this.id = id;

18
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.scale;
import static com.fine.theme.utils.FineUIScale.unscale; 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.RETINA_SCALE_FACTOR;
import static com.fine.theme.utils.FineUIUtils.isRetina; import static com.fine.theme.utils.FineUIUtils.getRetina;
/** /**
* svg图标 * svg图标
@ -62,9 +62,7 @@ public class SvgIcon implements DisabledIcon, Icon {
*/ */
@Override @Override
public void paintIcon(Component c, Graphics g, int x, int y) { public void paintIcon(Component c, Graphics g, int x, int y) {
if (cache == null if (isCacheValid()) {
|| cache.getWidth() != scaleRetina(size.width)
|| cache.getHeight() != scaleRetina(size.height)) {
if (cache != null) { if (cache != null) {
cache.flush(); cache.flush();
} }
@ -75,7 +73,7 @@ public class SvgIcon implements DisabledIcon, Icon {
cache = toImage(scale); cache = toImage(scale);
} }
} }
if (isRetina()) { if (getRetina()) {
// 高清绘制的原理:scale(1/2,1/2)的原理是坐标减半,底层是矩阵进行坐标变换,意思是坐标减半进行绘制, // 高清绘制的原理:scale(1/2,1/2)的原理是坐标减半,底层是矩阵进行坐标变换,意思是坐标减半进行绘制,
// 这样就可以将两倍图绘制到一倍的大小,如果这时候设备支持Retina绘制(四个像素模拟一个像素), // 这样就可以将两倍图绘制到一倍的大小,如果这时候设备支持Retina绘制(四个像素模拟一个像素),
// 正好就可以将4个像素利用起来,每个像素点都有不同的颜色,而不像之前只能是四个共用一个颜色。因此图像 // 正好就可以将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) { private static int scaleRetina(int size) {
return isRetina() return getRetina()
? size * RETINA_SCALE_FACTOR ? size * RETINA_SCALE_FACTOR
: size; : size;
} }
private static Dimension scaleRetina(Dimension dimension) { private static Dimension scaleRetina(Dimension dimension) {
return isRetina() return getRetina()
? new Dimension(dimension.width * RETINA_SCALE_FACTOR, dimension.height * RETINA_SCALE_FACTOR) ? new Dimension(dimension.width * RETINA_SCALE_FACTOR, dimension.height * RETINA_SCALE_FACTOR)
: dimension; : dimension;
} }

2
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) { private BufferedImage createGrayImage(int width, int height) {
return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

21
designer-base/src/main/java/com/fine/theme/utils/FineUIUtils.java

@ -24,10 +24,11 @@ public class FineUIUtils {
* 判断是否支持retina制作一些特殊效果如HIDPI图片绘制 * 判断是否支持retina制作一些特殊效果如HIDPI图片绘制
* retina 是一种特殊的效果使用4个像素点模拟一个像素点 * retina 是一种特殊的效果使用4个像素点模拟一个像素点
* 因此在其他操作系统上即使是高分屏也不具备retina的效果 * 因此在其他操作系统上即使是高分屏也不具备retina的效果
* 甚至还有劣化的效果以及更差的性能
* *
* @since 2023.11.16 * @since 2023.11.16
*/ */
private static final AtomicClearableLazyValue<Boolean> retina = AtomicClearableLazyValue.create(() -> { private static final AtomicClearableLazyValue<Boolean> RETINA = AtomicClearableLazyValue.create(() -> {
// 经过测试win11,ubuntu等,没有retina效果 // 经过测试win11,ubuntu等,没有retina效果
if (!OperatingSystem.isMacos()) { if (!OperatingSystem.isMacos()) {
return false; return false;
@ -39,7 +40,7 @@ public class FineUIUtils {
Field field = device.getClass().getDeclaredField("scale"); Field field = device.getClass().getDeclaredField("scale");
field.setAccessible(true); field.setAccessible(true);
Object scale = field.get(device); Object scale = field.get(device);
if (scale instanceof Integer && (Integer) scale == 2) { if (scale instanceof Integer && (Integer) scale == RETINA_SCALE_FACTOR) {
return true; return true;
} }
} catch (Exception ignored) { } catch (Exception ignored) {
@ -47,13 +48,20 @@ public class FineUIUtils {
return false; return false;
}); });
/**
public static boolean isRetina() { * 是否支持 retina
return retina.getValue(); *
* @return 是否支持 retina
*/
public static boolean getRetina() {
return RETINA.getValue();
} }
/**
* 放弃 retina 判断结果用于清理或者切换环境
*/
public static void clearRetina() { public static void clearRetina() {
retina.drop(); RETINA.drop();
} }
/** /**
@ -63,7 +71,6 @@ public class FineUIUtils {
* @param defaultKey 颜色后备key * @param defaultKey 颜色后备key
* @return 颜色 * @return 颜色
*/ */
public static Color getUIColor(String key, String defaultKey) { public static Color getUIColor(String key, String defaultKey) {
Color color = UIManager.getColor(key); Color color = UIManager.getColor(key);
return (color != null) ? color : UIManager.getColor(defaultKey); return (color != null) ? color : UIManager.getColor(defaultKey);

Loading…
Cancel
Save