vito
3 months ago
67 changed files with 657 additions and 367 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); |
||||
} |
||||
} |
@ -0,0 +1,39 @@
|
||||
package com.fr.design.fun; |
||||
|
||||
import com.fr.stable.fun.mark.Mutable; |
||||
|
||||
/** |
||||
* 插件图标适配接口 |
||||
* |
||||
* @author lemon |
||||
* @since |
||||
* Created on |
||||
*/ |
||||
public interface LazyIconProvider extends Mutable { |
||||
String MARK_STRING = "LazyIconProvider"; |
||||
|
||||
int CURRENT_LEVEL = 1; |
||||
|
||||
/** |
||||
* 插件 id,icon 来源标识 |
||||
* |
||||
* @return 来源标识 |
||||
*/ |
||||
String pluginId(); |
||||
|
||||
|
||||
/** |
||||
* light 主题 |
||||
* |
||||
* @return 图标注册 json 路径 |
||||
*/ |
||||
String lightJsonPath(); |
||||
|
||||
/** |
||||
* dark 主题 |
||||
* |
||||
* @return 图标注册 json 路径 |
||||
*/ |
||||
String darkJsonPath(); |
||||
|
||||
} |
@ -0,0 +1,59 @@
|
||||
package com.fr.design.fun.impl; |
||||
|
||||
import com.fr.design.fun.LazyIconProvider; |
||||
import com.fr.stable.fun.impl.AbstractProvider; |
||||
import com.fr.stable.fun.mark.API; |
||||
|
||||
import java.util.List; |
||||
|
||||
|
||||
/** |
||||
* 插件图标 LazyIcon 加载适配抽象类 |
||||
* |
||||
* @author lemon |
||||
* @since |
||||
* Created on |
||||
*/ |
||||
@API(level = LazyIconProvider.CURRENT_LEVEL) |
||||
public abstract class AbstractLazyIconProvider extends AbstractProvider implements LazyIconProvider { |
||||
|
||||
/** |
||||
* 当前接口的API等级,用于判断是否需要升级插件 |
||||
* @return API等级 |
||||
*/ |
||||
@Override |
||||
public int currentAPILevel() { |
||||
return CURRENT_LEVEL; |
||||
} |
||||
|
||||
/** |
||||
* 区分插件 |
||||
* |
||||
* @return 插件 id |
||||
*/ |
||||
@Override |
||||
public String pluginId() { |
||||
throw new RuntimeException("plugin id is blank"); |
||||
} |
||||
|
||||
/** |
||||
* light 主题 |
||||
* |
||||
* @return 图标注册 json 路径 |
||||
*/ |
||||
@Override |
||||
public String lightJsonPath() { |
||||
return ""; |
||||
} |
||||
|
||||
/** |
||||
* dark 主题 |
||||
* |
||||
* @return 图标注册 json 路径 |
||||
*/ |
||||
@Override |
||||
public String darkJsonPath() { |
||||
return ""; |
||||
} |
||||
|
||||
} |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 2.8 KiB |
After Width: | Height: | Size: 2.0 KiB |
Loading…
Reference in new issue