Browse Source

REPORT-127437 feat:调整代码,增加 mapSet

fbp/research
lemon 8 months ago
parent
commit
c62885a704
  1. 54
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconManager.java
  2. 25
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginJsonIconSet.java
  3. 4
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginLazyIcon.java
  4. 54
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginMapIconSet.java
  5. 4
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineDarkLaf.java
  6. 25
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLaf.java
  7. 2
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLightLaf.java

54
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconManager.java

@ -8,6 +8,7 @@ import com.fine.theme.icon.LazyIcon;
import com.fr.base.extension.FileExtension; import com.fr.base.extension.FileExtension;
import com.fr.general.IOUtils; import com.fr.general.IOUtils;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.nx.app.web.out.widget.utils.CollectionUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -15,7 +16,9 @@ import javax.swing.Icon;
import java.awt.Dimension; import java.awt.Dimension;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* 管理插件图标集 * 管理插件图标集
@ -28,8 +31,8 @@ public class PluginIconManager {
public static final String ICON_DISABLE_SUFFIX = "_disable"; public static final String ICON_DISABLE_SUFFIX = "_disable";
public static final Dimension DEFAULT_DIMENSION = new Dimension(16, 16); public static final Dimension DEFAULT_DIMENSION = new Dimension(16, 16);
private static final Map<String, IconSet> SOURCE_ICON_MAPS = new HashMap<>(2); private static final Map<String, List<IconSet>> SOURCE_ICON_MAPS = new ConcurrentHashMap<>(2);
private static final HashMap<String, HashMap<String, WeakReference<Icon>>> CACHE = new HashMap<>(); private static final Map<String, HashMap<String, WeakReference<Icon>>> CACHE = new ConcurrentHashMap<>();
/** /**
@ -39,10 +42,12 @@ public class PluginIconManager {
* @return 图标集 * @return 图标集
*/ */
public static IconSet getSet(String source, String id) { public static IconSet getSet(String source, String id) {
IconSet set = SOURCE_ICON_MAPS.get(source); List<IconSet> sets = SOURCE_ICON_MAPS.get(source);
if (set != null && set.getId().equals(id)) { for (IconSet set : sets) {
return set; if (set != null && set.getId().equals(id)) {
return set;
}
} }
throw new IconException("[PluginIconManager] Can not find icon set by id: " + id); throw new IconException("[PluginIconManager] Can not find icon set by id: " + id);
} }
@ -51,25 +56,26 @@ public class PluginIconManager {
* 添加图标集 * 添加图标集
* *
* @param source 插件 * @param source 插件
* @param set 图标集 * @param sets 图标集
*/ */
public static void addSet(@NotNull String source, @NotNull IconSet set) { public static void addSet(@NotNull String source, @NotNull List<IconSet> sets) {
if (SOURCE_ICON_MAPS.containsKey(source)) { if (SOURCE_ICON_MAPS.containsKey(source)) {
FineLoggerFactory.getLogger().warn("[PluginIconManager] plugin:{} icon set already exists: " + source); FineLoggerFactory.getLogger().warn("[PluginIconManager] plugin:{} icon set already exists: " + source);
} }
SOURCE_ICON_MAPS.put(source, set); SOURCE_ICON_MAPS.put(source, sets);
clearCacheBySource(source); clearCacheBySource(source);
} }
/** /**
* 更新指定插件图标集 * 更新指定插件图标集
* *
* @param source 插件 * @param source 插件
* @param set 图标集 * @param sets 图标集
*/ */
public static void updateSet(@NotNull String source, @NotNull IconSet set) { public static void updateSet(@NotNull String source, @NotNull List<IconSet> sets) {
SOURCE_ICON_MAPS.put(source, set); removeSet(source);
clearCacheBySource(source); addSet(source, sets);
} }
/** /**
@ -120,15 +126,17 @@ public class PluginIconManager {
final WeakReference<Icon> reference = sourceCache.get(cacheKey); final WeakReference<Icon> reference = sourceCache.get(cacheKey);
I icon = reference != null ? (I) reference.get() : null; I icon = reference != null ? (I) reference.get() : null;
if (icon == null) { if (icon == null) {
IconSet set = SOURCE_ICON_MAPS.get(source); List<IconSet> sets = SOURCE_ICON_MAPS.get(source);
if (set == null) { if (CollectionUtils.isEmpty(sets)) {
return icon; return icon;
} }
Icon f = set.findIcon(id, dimension, type); for (IconSet set : sets) {
if (f != null) { Icon f = set.findIcon(id, dimension, type);
icon = (I) f; if (f != null) {
sourceCache.put(cacheKey, new WeakReference<>(icon)); icon = (I) f;
CACHE.put(source, sourceCache); sourceCache.put(cacheKey, new WeakReference<>(icon));
CACHE.put(source, sourceCache);
}
} }
} }
return icon; return icon;
@ -178,9 +186,11 @@ public class PluginIconManager {
* @return 是否存在 * @return 是否存在
*/ */
public static boolean existIcon(String id, String source) { public static boolean existIcon(String id, String source) {
IconSet set = SOURCE_ICON_MAPS.get(source); List<IconSet> sets = SOURCE_ICON_MAPS.get(source);
if (set != null && set.getIds().contains(id)) { for (IconSet set : sets) {
return true; if (set != null && set.getIds().contains(id)) {
return true;
}
} }
return false; return false;
} }

25
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconSet.java → designer-base/src/main/java/com/fine/theme/icon/plugin/PluginJsonIconSet.java

@ -18,20 +18,17 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
/** /**
* 插件图标集 * 插件 json 图标集
* *
* @author lemon * @author lemon
* @since * @since
* Created on 2024/08/20 * Created on 2024/08/20
*/ */
public class PluginIconSet extends AbstractIconSet { public class PluginJsonIconSet extends AbstractIconSet {
private String base; private String base;
public PluginJsonIconSet(UrlIconResource resource) {
public PluginIconSet(UrlIconResource resource, Map<String, String> iconId2Path) {
addIconWithMap(iconId2Path);
if (resource.getPath() == null) { if (resource.getPath() == null) {
return; return;
} }
@ -105,20 +102,6 @@ public class PluginIconSet extends AbstractIconSet {
} }
} }
/**
* 根据 map 注册图标
* @param iconId2Path key: id, value: icon path
*/
public void addIconWithMap(Map<String, String> iconId2Path) {
for (Map.Entry<String, String> entry: iconId2Path.entrySet()) {
if (PluginIconManager.isSvgIcon(entry.getValue())) {
addIcon(new SvgIconSource(entry.getKey(), entry.getValue()));
} else if (PluginIconManager.isImageIcon(entry.getValue())) {
addIcon(new ImageIconSource(entry.getKey(), entry.getValue()));
}
}
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {
@ -127,7 +110,7 @@ public class PluginIconSet extends AbstractIconSet {
if (o == null || getClass() != o.getClass()) { if (o == null || getClass() != o.getClass()) {
return false; return false;
} }
PluginIconSet that = (PluginIconSet) o; PluginJsonIconSet that = (PluginJsonIconSet) o;
return Objects.equals(name, that.name); return Objects.equals(name, that.name);
} }

4
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginLazyIcon.java

@ -127,11 +127,11 @@ public class PluginLazyIcon implements Identifiable, DisabledIcon, WhiteIcon, Ic
@Override @Override
public String toString() { public String toString() {
return AssistUtils.toString(new StringJoiner(", ", PluginLazyIcon.class.getSimpleName() + "[", "]") return new StringJoiner(", ", PluginLazyIcon.class.getSimpleName() + "[", "]")
.add("source='" + source + "'") .add("source='" + source + "'")
.add("id='" + id + "'") .add("id='" + id + "'")
.add("size=" + "[w=" + scale(dimension.width) + ",h=" + scale(dimension.height) + "]") .add("size=" + "[w=" + scale(dimension.width) + ",h=" + scale(dimension.height) + "]")
.add("type=" + type) .add("type=" + type)
.toString()); .toString();
} }
} }

54
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginMapIconSet.java

@ -0,0 +1,54 @@
package com.fine.theme.icon.plugin;
import com.fine.theme.icon.AbstractIconSet;
import com.fine.theme.icon.img.ImageIconSource;
import com.fine.theme.icon.svg.SvgIconSource;
import java.util.Map;
import java.util.Objects;
/**
* 插件 map 图标集
*
* @author lemon
* @since
* Created on 2024/08/20
*/
public class PluginMapIconSet extends AbstractIconSet {
public PluginMapIconSet(Map<String, String> iconId2Path) {
addIconWithMap(iconId2Path);
}
/**
* 根据 map 注册图标
* @param iconId2Path key: id, value: icon path
*/
public void addIconWithMap(Map<String, String> iconId2Path) {
for (Map.Entry<String, String> entry: iconId2Path.entrySet()) {
if (PluginIconManager.isSvgIcon(entry.getValue())) {
addIcon(new SvgIconSource(entry.getKey(), entry.getValue()));
} else if (PluginIconManager.isImageIcon(entry.getValue())) {
addIcon(new ImageIconSource(entry.getKey(), entry.getValue()));
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PluginMapIconSet that = (PluginMapIconSet) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
}

4
designer-base/src/main/java/com/fine/theme/light/ui/laf/FineDarkLaf.java

@ -27,14 +27,14 @@ public class FineDarkLaf extends FineLaf {
*/ */
public static boolean setup() { public static boolean setup() {
IconManager.addSet(new FineLightIconSet()); IconManager.addSet(new FineLightIconSet());
// dark_icon 目前还没适配,先使用 light_icon
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
Layouts.setScaleFactor(UIScale.getUserScaleFactor()); Layouts.setScaleFactor(UIScale.getUserScaleFactor());
UIScale.addPropertyChangeListener(evt -> { UIScale.addPropertyChangeListener(evt -> {
if (StringUtils.equals(evt.getPropertyName(), USER_SCALE_FACTOR)) { if (StringUtils.equals(evt.getPropertyName(), USER_SCALE_FACTOR)) {
Layouts.setScaleFactor((float) evt.getNewValue()); Layouts.setScaleFactor((float) evt.getNewValue());
} }
}); });
// dark_icon 目前还没适配,先使用 light_icon
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
return setup(new FineDarkLaf()); return setup(new FineDarkLaf());
} }

25
designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLaf.java

@ -1,8 +1,10 @@
package com.fine.theme.light.ui.laf; package com.fine.theme.light.ui.laf;
import com.fine.theme.icon.IconSet;
import com.fine.theme.icon.UrlIconResource; import com.fine.theme.icon.UrlIconResource;
import com.fine.theme.icon.plugin.PluginIconManager; import com.fine.theme.icon.plugin.PluginIconManager;
import com.fine.theme.icon.plugin.PluginIconSet; import com.fine.theme.icon.plugin.PluginJsonIconSet;
import com.fine.theme.icon.plugin.PluginMapIconSet;
import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.SystemInfo; import com.formdev.flatlaf.util.SystemInfo;
import com.fr.design.ExtraDesignClassManager; import com.fr.design.ExtraDesignClassManager;
@ -15,6 +17,8 @@ import com.fr.plugin.observer.PluginEventListener;
import com.fr.plugin.observer.PluginEventType; import com.fr.plugin.observer.PluginEventType;
import javax.swing.PopupFactory; import javax.swing.PopupFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -32,18 +36,23 @@ public abstract class FineLaf extends FlatLaf {
private static final String NAME = "FineLaf"; private static final String NAME = "FineLaf";
private static void handlePluginEvent(PluginEvent event, LazyIconProvider.THEME category, private static void handlePluginEvent(PluginEvent event, LazyIconProvider.THEME category,
BiConsumer<LazyIconProvider, PluginIconSet> operation) { BiConsumer<LazyIconProvider, List<IconSet>> operation) {
Set<LazyIconProvider> set = event.getContext().getRuntime().get(LazyIconProvider.MARK_STRING); Set<LazyIconProvider> set = event.getContext().getRuntime().get(LazyIconProvider.MARK_STRING);
dealWithPluginIcon(set, category, operation); dealWithPluginIcon(set, category, operation);
} }
private static void dealWithPluginIcon(Set<LazyIconProvider> set, LazyIconProvider.THEME category, private static void dealWithPluginIcon(Set<LazyIconProvider> set, LazyIconProvider.THEME category,
BiConsumer<LazyIconProvider, PluginIconSet> operation) { BiConsumer<LazyIconProvider, List<IconSet>> operation) {
for (LazyIconProvider provider : set) { for (LazyIconProvider provider : set) {
if (!ComparatorUtils.equals(provider.themeCategory(), category)) { if (!ComparatorUtils.equals(provider.themeCategory(), category)) {
continue; continue;
} }
operation.accept(provider, new PluginIconSet(new UrlIconResource(provider.jsonPath()), provider.iconId2Path())); List<IconSet> iconSets = new ArrayList<>();
PluginJsonIconSet jsonIconSet = new PluginJsonIconSet(new UrlIconResource(provider.jsonPath()));
iconSets.add(jsonIconSet);
PluginMapIconSet mapIconSet = new PluginMapIconSet(provider.iconId2Path());
iconSets.add(mapIconSet);
operation.accept(provider, iconSets);
} }
} }
@ -88,7 +97,7 @@ public abstract class FineLaf extends FlatLaf {
*/ */
public static void insertIconProvider(LazyIconProvider.THEME category) { public static void insertIconProvider(LazyIconProvider.THEME category) {
Set<LazyIconProvider> set = ExtraDesignClassManager.getInstance().getArray(LazyIconProvider.MARK_STRING); Set<LazyIconProvider> set = ExtraDesignClassManager.getInstance().getArray(LazyIconProvider.MARK_STRING);
dealWithPluginIcon(set, category, (provider, iconSet) -> PluginIconManager.addSet(provider.pluginId(), iconSet)); dealWithPluginIcon(set, category, (provider, iconSets) -> PluginIconManager.addSet(provider.pluginId(), iconSets));
listenerPlugins(category); listenerPlugins(category);
} }
@ -99,21 +108,21 @@ public abstract class FineLaf extends FlatLaf {
PluginEventListener insert = new PluginEventListener() { PluginEventListener insert = new PluginEventListener() {
@Override @Override
public void on(PluginEvent event) { public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.addSet(provider.pluginId(), iconSet)); handlePluginEvent(event, category, (provider, iconSets) -> PluginIconManager.addSet(provider.pluginId(), iconSets));
} }
}; };
PluginEventListener update = new PluginEventListener() { PluginEventListener update = new PluginEventListener() {
@Override @Override
public void on(PluginEvent event) { public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.updateSet(provider.pluginId(), iconSet)); handlePluginEvent(event, category, (provider, iconSets) -> PluginIconManager.updateSet(provider.pluginId(), iconSets));
} }
}; };
PluginEventListener remove = new PluginEventListener() { PluginEventListener remove = new PluginEventListener() {
@Override @Override
public void on(PluginEvent event) { public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.removeSet(provider.pluginId())); handlePluginEvent(event, category, (provider, iconSets) -> PluginIconManager.removeSet(provider.pluginId()));
} }
}; };

2
designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLightLaf.java

@ -27,13 +27,13 @@ public class FineLightLaf extends FineLaf {
*/ */
public static boolean setup() { public static boolean setup() {
IconManager.addSet(new FineLightIconSet()); IconManager.addSet(new FineLightIconSet());
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
Layouts.setScaleFactor(UIScale.getUserScaleFactor()); Layouts.setScaleFactor(UIScale.getUserScaleFactor());
UIScale.addPropertyChangeListener(evt -> { UIScale.addPropertyChangeListener(evt -> {
if (StringUtils.equals(evt.getPropertyName(), USER_SCALE_FACTOR)) { if (StringUtils.equals(evt.getPropertyName(), USER_SCALE_FACTOR)) {
Layouts.setScaleFactor((float) evt.getNewValue()); Layouts.setScaleFactor((float) evt.getNewValue());
} }
}); });
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
return setup(new FineLightLaf()); return setup(new FineLightLaf());
} }

Loading…
Cancel
Save