lemon
3 months ago
10 changed files with 195 additions and 204 deletions
@ -0,0 +1,127 @@
|
||||
package com.fine.theme.icon.plugin; |
||||
|
||||
import com.fine.theme.icon.AbstractIconSet; |
||||
import com.fine.theme.icon.IconSet; |
||||
import com.fine.theme.icon.IconType; |
||||
import com.fine.theme.icon.JsonIconSet; |
||||
import com.fine.theme.icon.UrlIconResource; |
||||
import com.formdev.flatlaf.FlatLaf; |
||||
import com.fr.design.fun.LazyIconProvider; |
||||
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 com.fr.stable.AssistUtils; |
||||
import org.jetbrains.annotations.NotNull; |
||||
import org.jetbrains.annotations.Nullable; |
||||
|
||||
import javax.swing.Icon; |
||||
import javax.swing.LookAndFeel; |
||||
import javax.swing.UIManager; |
||||
import java.awt.Dimension; |
||||
import java.util.ArrayList; |
||||
import java.util.Set; |
||||
import java.util.function.Consumer; |
||||
|
||||
|
||||
/** |
||||
* 管理插件 iconSet |
||||
* @author lemon |
||||
* @since |
||||
* Created on |
||||
*/ |
||||
public class PluginIconSet extends AbstractIconSet { |
||||
|
||||
private static final String NAME = "Plugin Icon Set"; |
||||
private static final ArrayList<IconSet> PLUGIN_ICON_SETS = new ArrayList<>(); |
||||
|
||||
public PluginIconSet() { |
||||
name = NAME; |
||||
listenPluginIcons(); |
||||
} |
||||
|
||||
/** |
||||
* 适配插件图标 Icon |
||||
*/ |
||||
public static void listenPluginIcons() { |
||||
//注册插件监听
|
||||
PluginFilter filter = context -> context.contain(LazyIconProvider.MARK_STRING); |
||||
|
||||
PluginEventListener insert = new PluginEventListener() { |
||||
@Override |
||||
public void on(PluginEvent event) { |
||||
handlePluginEvent(event, (provider) -> PLUGIN_ICON_SETS.add(generateJsonIconSet(provider))); |
||||
} |
||||
}; |
||||
|
||||
PluginEventListener remove = new PluginEventListener() { |
||||
@Override |
||||
public void on(PluginEvent event) { |
||||
handlePluginEvent(event, (provider) -> PLUGIN_ICON_SETS.removeIf(iconSet -> iconSet.getId().equals(provider.pluginId()))); |
||||
} |
||||
}; |
||||
|
||||
GeneralContext.listenPlugin(PluginEventType.AfterRun, insert, filter); |
||||
GeneralContext.listenPlugin(PluginEventType.AfterInstall, insert, filter); |
||||
GeneralContext.listenPlugin(PluginEventType.AfterForbid, remove, filter); |
||||
GeneralContext.listenPlugin(PluginEventType.AfterUninstall, remove, filter); |
||||
} |
||||
|
||||
private static void handlePluginEvent(PluginEvent event, Consumer<LazyIconProvider> consumer) { |
||||
Set<LazyIconProvider> set = event.getContext().getRuntime().get(LazyIconProvider.MARK_STRING); |
||||
for (LazyIconProvider provider : set) { |
||||
consumer.accept(provider); |
||||
} |
||||
} |
||||
|
||||
private static JsonIconSet generateJsonIconSet(LazyIconProvider provider) { |
||||
LookAndFeel laf = UIManager.getLookAndFeel(); |
||||
boolean dark = ((FlatLaf) laf).isDark(); |
||||
String jsonPath = dark ? provider.darkJsonPath() : provider.lightJsonPath(); |
||||
return new JsonIconSet(new UrlIconResource(jsonPath)) { |
||||
@Override |
||||
public @NotNull String getId() { |
||||
return provider.pluginId(); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
@Override |
||||
public @Nullable Icon findIcon(@NotNull String id, @NotNull Dimension dimension, IconType type) { |
||||
Icon icon; |
||||
for (IconSet iconSet : PLUGIN_ICON_SETS) { |
||||
icon = iconSet.findIcon(id, dimension, type); |
||||
if (icon != null) { |
||||
return icon; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 根据 id 匹配 icon set |
||||
* @param id 对于 plugin icon set, id 是 plugin_id |
||||
* @return icon set |
||||
*/ |
||||
public static IconSet getIconSet(@NotNull final String id ) { |
||||
for (IconSet iconSet : PLUGIN_ICON_SETS) { |
||||
if (iconSet.getId().equals(id)) { |
||||
return iconSet; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
return obj instanceof PluginIconSet |
||||
&& AssistUtils.equals(this.name, ((PluginIconSet) obj).name); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return AssistUtils.hashCode(name); |
||||
} |
||||
} |
@ -1,22 +0,0 @@
|
||||
package com.fine.theme.icon.plugin; |
||||
|
||||
import com.fine.theme.icon.JsonIconSet; |
||||
import com.fine.theme.icon.UrlIconResource; |
||||
|
||||
/** |
||||
* 插件 json 图标集 |
||||
* json 格式参考 fine_light.icon.json |
||||
* 为了保证 插件之间,插件与设计器 icon id 的唯一性,icons key 值规则参考 {@link PluginListIconSet} |
||||
* |
||||
* @author lemon |
||||
* @since |
||||
* Created on 2024/08/20 |
||||
*/ |
||||
public class PluginJsonIconSet extends JsonIconSet { |
||||
|
||||
public PluginJsonIconSet(String id, UrlIconResource resource) { |
||||
super(resource); |
||||
this.name = id; |
||||
} |
||||
|
||||
} |
@ -1,57 +0,0 @@
|
||||
package com.fine.theme.icon.plugin; |
||||
|
||||
import com.fine.theme.icon.AbstractIconSet; |
||||
import com.fine.theme.icon.IconManager; |
||||
import com.fine.theme.icon.img.ImageIconSource; |
||||
import com.fine.theme.icon.svg.SvgIconSource; |
||||
|
||||
import java.util.List; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 插件 map 图标集 |
||||
* 为了保证 插件之间,插件与设计器 icon id 的唯一性,以图标 path 为 id 进行注册 |
||||
* |
||||
* @author lemon |
||||
* @since |
||||
* Created on 2024/08/20 |
||||
*/ |
||||
public class PluginListIconSet extends AbstractIconSet { |
||||
|
||||
public PluginListIconSet(String id, List<String> icons) { |
||||
this.name = id; |
||||
addIconWithList(icons); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 根据 list 注册图标 |
||||
* @param icons icon path list |
||||
*/ |
||||
public void addIconWithList(List<String> icons) { |
||||
for (String path : icons) { |
||||
if (IconManager.isSvgIcon(path)) { |
||||
addIcon(new SvgIconSource(path, path)); |
||||
} else if (IconManager.isImageIcon(path)) { |
||||
addIcon(new ImageIconSource(path, path)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object o) { |
||||
if (this == o) { |
||||
return true; |
||||
} |
||||
if (o == null || getClass() != o.getClass()) { |
||||
return false; |
||||
} |
||||
PluginListIconSet that = (PluginListIconSet) o; |
||||
return Objects.equals(name, that.name); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return Objects.hashCode(name); |
||||
} |
||||
} |
Loading…
Reference in new issue