Browse Source

REPORT-127437 feat:适配插件图标

fbp/feature
lemon 3 months ago
parent
commit
e2f37f8d72
  1. 28
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconSet.java
  2. 14
      designer-base/src/main/java/com/fine/theme/icon/plugin/PluginLazyIcon.java
  3. 3
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineDarkLaf.java
  4. 72
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLaf.java
  5. 2
      designer-base/src/main/java/com/fine/theme/light/ui/laf/FineLightLaf.java
  6. 4
      designer-base/src/main/java/com/fr/design/fun/LazyIconProvider.java
  7. 4
      designer-base/src/main/java/com/fr/design/fun/impl/AbstractLazyIconProvider.java

28
designer-base/src/main/java/com/fine/theme/icon/plugin/PluginIconSet.java

@ -1,8 +1,10 @@
package com.fine.theme.icon.plugin;
import com.fine.theme.icon.AbstractIconSet;
import com.fine.theme.icon.IconManager;
import com.fine.theme.icon.IconType;
import com.fine.theme.icon.UrlIconResource;
import com.fine.theme.icon.img.ImageIconSource;
import com.fine.theme.icon.svg.SvgIconSource;
import com.formdev.flatlaf.json.Json;
import com.formdev.flatlaf.json.ParseException;
import com.fr.stable.StringUtils;
@ -27,7 +29,7 @@ public class PluginIconSet extends AbstractIconSet {
private String base;
public PluginIconSet(PluginUrlIconResource resource, Map<String, String> iconId2Path) {
public PluginIconSet(UrlIconResource resource, Map<String, String> iconId2Path) {
addIconWithMap(iconId2Path);
if (resource.getPath() == null) {
@ -66,15 +68,15 @@ public class PluginIconSet extends AbstractIconSet {
}
private void dealWithIconString(String key, String value) {
if (IconManager.isSvgIcon(value)) {
if (PluginIconManager.isSvgIcon(value)) {
// 默认字符串提供正常图和灰化图
addIcon(new PluginSvgIconSource(key,
addIcon(new SvgIconSource(key,
base + value,
IconManager.findDisablePath(base + value),
PluginIconManager.findDisablePath(base + value),
null
));
} else if (IconManager.isImageIcon(value)) {
addIcon(new PluginImageIconSource(key, base + value));
} else if (PluginIconManager.isImageIcon(value)) {
addIcon(new ImageIconSource(key, base + value));
}
// 其他无法识别格式不处理
}
@ -88,14 +90,14 @@ public class PluginIconSet extends AbstractIconSet {
String disablePath = (String) value.get(IconType.disable.name());
String whitePath = (String) value.get(IconType.white.name());
// 暂不支持混合格式,每个id的格式需要保持一致
if (IconManager.isSvgIcon(normalPath)) {
addIcon(new PluginSvgIconSource(key,
if (PluginIconManager.isSvgIcon(normalPath)) {
addIcon(new SvgIconSource(key,
base + normalPath,
StringUtils.isNotBlank(disablePath) ? base + disablePath : null,
StringUtils.isNotBlank(whitePath) ? base + whitePath : null
));
} else if (IconManager.isImageIcon(normalPath)) {
addIcon(new PluginImageIconSource(key,
} else if (PluginIconManager.isImageIcon(normalPath)) {
addIcon(new ImageIconSource(key,
base + normalPath,
StringUtils.isNotBlank(disablePath) ? base + disablePath : null,
StringUtils.isNotBlank(whitePath) ? base + whitePath : null
@ -110,9 +112,9 @@ public class PluginIconSet extends AbstractIconSet {
public void addIconWithMap(Map<String, String> iconId2Path) {
for (Map.Entry<String, String> entry: iconId2Path.entrySet()) {
if (PluginIconManager.isSvgIcon(entry.getValue())) {
addIcon(new PluginSvgIconSource(entry.getKey(), entry.getValue()));
addIcon(new SvgIconSource(entry.getKey(), entry.getValue()));
} else if (PluginIconManager.isImageIcon(entry.getValue())) {
addIcon(new PluginImageIconSource(entry.getKey(), entry.getValue()));
addIcon(new ImageIconSource(entry.getKey(), entry.getValue()));
}
}
}

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

@ -6,6 +6,7 @@ import com.fine.theme.icon.IconType;
import com.fine.theme.icon.Identifiable;
import com.fine.theme.icon.WhiteIcon;
import com.fr.design.fun.impl.AbstractLazyIconProvider;
import com.fr.stable.AssistUtils;
import org.jetbrains.annotations.NotNull;
import javax.swing.Icon;
@ -16,6 +17,13 @@ import java.util.StringJoiner;
import static com.fine.theme.utils.FineUIScale.scale;
/**
* 插件图标加载类
*
* @author lemon
* @since
* Created on 2024/08/23
*/
public class PluginLazyIcon implements Identifiable, DisabledIcon, WhiteIcon, Icon {
@NotNull
private final String id;
@ -28,7 +36,7 @@ public class PluginLazyIcon implements Identifiable, DisabledIcon, WhiteIcon, Ic
/**
*
* @param source {@link AbstractLazyIconProvider#source()}
* @param source {@link AbstractLazyIconProvider#pluginId()}
* @param id
*/
public PluginLazyIcon(@NotNull final String source, @NotNull final String id) {
@ -119,11 +127,11 @@ public class PluginLazyIcon implements Identifiable, DisabledIcon, WhiteIcon, Ic
@Override
public String toString() {
return new StringJoiner(", ", PluginLazyIcon.class.getSimpleName() + "[", "]")
return AssistUtils.toString(new StringJoiner(", ", PluginLazyIcon.class.getSimpleName() + "[", "]")
.add("source='" + source + "'")
.add("id='" + id + "'")
.add("size=" + "[w=" + scale(dimension.width) + ",h=" + scale(dimension.height) + "]")
.add("type=" + type)
.toString();
.toString());
}
}

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

@ -4,6 +4,7 @@ import com.fine.swing.ui.layout.Layouts;
import com.fine.theme.icon.IconManager;
import com.fine.theme.light.ui.FineLightIconSet;
import com.formdev.flatlaf.util.UIScale;
import com.fr.design.fun.LazyIconProvider;
import com.fr.stable.StringUtils;
/**
@ -32,6 +33,8 @@ public class FineDarkLaf extends FineLaf {
Layouts.setScaleFactor((float) evt.getNewValue());
}
});
// dark_icon 目前还没适配,先使用 light_icon
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
return setup(new FineDarkLaf());
}

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

@ -1,9 +1,22 @@
package com.fine.theme.light.ui.laf;
import com.fine.theme.icon.UrlIconResource;
import com.fine.theme.icon.plugin.PluginIconManager;
import com.fine.theme.icon.plugin.PluginIconSet;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.SystemInfo;
import com.fr.design.ExtraDesignClassManager;
import com.fr.design.fun.LazyIconProvider;
import com.fr.general.ComparatorUtils;
import com.fr.general.GeneralContext;
import com.fr.plugin.manage.PluginFilter;
import com.fr.plugin.observer.PluginEvent;
import com.fr.plugin.observer.PluginEventListener;
import com.fr.plugin.observer.PluginEventType;
import javax.swing.PopupFactory;
import java.util.Set;
import java.util.function.BiConsumer;
/**
* Fine designer new look and feel
@ -18,6 +31,21 @@ public abstract class FineLaf extends FlatLaf {
private static final String NAME = "FineLaf";
private static void handlePluginEvent(PluginEvent event, LazyIconProvider.THEME category,
BiConsumer<LazyIconProvider, PluginIconSet> operation) {
Set<LazyIconProvider> set = event.getContext().getRuntime().get(LazyIconProvider.MARK_STRING);
dealWithPluginIcon(set, category, operation);
}
private static void dealWithPluginIcon(Set<LazyIconProvider> set, LazyIconProvider.THEME category,
BiConsumer<LazyIconProvider, PluginIconSet> operation) {
for (LazyIconProvider provider : set) {
if (!ComparatorUtils.equals(provider.themeCategory(), category)) {
continue;
}
operation.accept(provider, new PluginIconSet(new UrlIconResource(provider.jsonPath()), provider.iconId2Path()));
}
}
@Override
public String getName() {
@ -53,4 +81,48 @@ public abstract class FineLaf extends FlatLaf {
System.setProperty("flatlaf.menuBarEmbedded", "false");
}
}
/**
* 适配插件图标 Icon
* @param category 图标 Icon 分类 {@link LazyIconProvider.THEME}
*/
public static void insertIconProvider(LazyIconProvider.THEME category) {
Set<LazyIconProvider> set = ExtraDesignClassManager.getInstance().getArray(LazyIconProvider.MARK_STRING);
dealWithPluginIcon(set, category, (provider, iconSet) -> PluginIconManager.addSet(provider.pluginId(), iconSet));
listenerPlugins(category);
}
private static void listenerPlugins(LazyIconProvider.THEME category) {
//注册插件监听
PluginFilter filter = context -> context.contain(LazyIconProvider.MARK_STRING);
PluginEventListener insert = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.addSet(provider.pluginId(), iconSet));
}
};
PluginEventListener update = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.updateSet(provider.pluginId(), iconSet));
}
};
PluginEventListener remove = new PluginEventListener() {
@Override
public void on(PluginEvent event) {
handlePluginEvent(event, category, (provider, iconSet) -> PluginIconManager.removeSet(provider.pluginId()));
}
};
GeneralContext.listenPlugin(PluginEventType.AfterRun, insert, filter);
GeneralContext.listenPlugin(PluginEventType.AfterInstall, insert, filter);
GeneralContext.listenPlugin(PluginEventType.AfterUpdate, update, filter);
GeneralContext.listenPlugin(PluginEventType.AfterActive, update, filter);
GeneralContext.listenPlugin(PluginEventType.AfterForbid, remove, filter);
GeneralContext.listenPlugin(PluginEventType.AfterUninstall, remove, filter);
}
}

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

@ -4,6 +4,7 @@ import com.fine.swing.ui.layout.Layouts;
import com.fine.theme.icon.IconManager;
import com.fine.theme.light.ui.FineLightIconSet;
import com.formdev.flatlaf.util.UIScale;
import com.fr.design.fun.LazyIconProvider;
import com.fr.stable.StringUtils;
/**
@ -32,6 +33,7 @@ public class FineLightLaf extends FineLaf {
Layouts.setScaleFactor((float) evt.getNewValue());
}
});
insertIconProvider(LazyIconProvider.THEME.LIGHT_ICON);
return setup(new FineLightLaf());
}

4
designer-base/src/main/java/com/fr/design/fun/LazyIconProvider.java

@ -17,11 +17,11 @@ public interface LazyIconProvider extends Mutable {
int CURRENT_LEVEL = 1;
/**
* 自定义icon 来源标识
* 插件 idicon 来源标识
*
* @return 来源标识
*/
String source();
String pluginId();
/**
* json 文件路径

4
designer-base/src/main/java/com/fr/design/fun/impl/AbstractLazyIconProvider.java

@ -34,8 +34,8 @@ public class AbstractLazyIconProvider extends AbstractProvider implements LazyIc
* @return 插件 id
*/
@Override
public String source() {
throw new RuntimeException("source is blank");
public String pluginId() {
throw new RuntimeException("plugin id is blank");
}
/**

Loading…
Cancel
Save