From dd7f075f16fa1fad0e798bebd14c84e9dba6d97c Mon Sep 17 00:00:00 2001 From: vito Date: Thu, 13 Jun 2024 14:21:45 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-114888=20icon=E6=97=A0=E6=B3=95=E6=89=BE?= =?UTF-8?q?=E5=88=B0=5Fdisable=E5=9B=BE=E6=A0=87=E6=97=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=B3=BB=E7=BB=9F=E9=BB=98=E8=AE=A4=E7=81=B0=E5=8C=96?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fine/theme/icon/AbstractIconSource.java | 8 ++-- .../com/fine/theme/icon/UrlIconResource.java | 22 +++++++-- .../java/com/fine/theme/icon/svg/SvgIcon.java | 36 ++++++++++++-- .../fr/design/gui/storybook/DebugStory.java | 21 +++++++++ .../fr/design/gui/storybook/Storybook.java | 47 +++++++++---------- 5 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 designer-base/src/test/java/com/fr/design/gui/storybook/DebugStory.java diff --git a/designer-base/src/main/java/com/fine/theme/icon/AbstractIconSource.java b/designer-base/src/main/java/com/fine/theme/icon/AbstractIconSource.java index 1eb6c18637..98601b0bba 100644 --- a/designer-base/src/main/java/com/fine/theme/icon/AbstractIconSource.java +++ b/designer-base/src/main/java/com/fine/theme/icon/AbstractIconSource.java @@ -24,7 +24,7 @@ import java.awt.Dimension; @Immutable public abstract class AbstractIconSource implements IconSource { - private static final String ICON_DISABLE_SUFFIX = "_disable"; + public static final String ICON_DISABLE_SUFFIX = "_disable"; protected String id; @@ -100,8 +100,10 @@ public abstract class AbstractIconSource implements IconSource 1) { + SVGLoader loader = new SVGLoader(); + SVGDocument document = loader.load(new UrlIconResource(names[0] + names[1]).getInputStream()); + if (document != null) { + document.render((JComponent) c, grayGraphics(g), + new ViewBox(x, y, scaleSize.width, scaleSize.height)); + } + } + } } private Graphics2D grayGraphics(Graphics g) { @@ -92,7 +115,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { return scaleSize.height; } - private void render(Component c, Graphics g, int x, int y) { + private void render(Component c, Graphics g, int x, int y, FallbackRender fallbackRender) { try { if (type == IconType.white) { Objects.requireNonNull(whiteSvgDocument.getValue()) @@ -103,6 +126,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { } } catch (Exception e) { FineLoggerFactory.getLogger().error("SvgIcon from url: " + resource + "can not paint.", e); + fallbackRender.render(c, g, x, y); } } @@ -141,4 +165,10 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { public @NotNull SvgIcon disabled() { return new SvgIcon(resource, size, IconType.disable); } + + + @FunctionalInterface + interface FallbackRender { + void render(Component c, Graphics g, int x, int y); + } } diff --git a/designer-base/src/test/java/com/fr/design/gui/storybook/DebugStory.java b/designer-base/src/test/java/com/fr/design/gui/storybook/DebugStory.java new file mode 100644 index 0000000000..ef000aabe5 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/gui/storybook/DebugStory.java @@ -0,0 +1,21 @@ +package com.fr.design.gui.storybook; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 单面板调试注解,如果components目录下带有这个注解, + * 那么Storybook中只有带有这个注解的界面被展示 + * + * @author vito + * @since 11.0 + * Created on 2024/6/11 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface DebugStory { +} diff --git a/designer-base/src/test/java/com/fr/design/gui/storybook/Storybook.java b/designer-base/src/test/java/com/fr/design/gui/storybook/Storybook.java index 635b342c15..d8b09ca523 100644 --- a/designer-base/src/test/java/com/fr/design/gui/storybook/Storybook.java +++ b/designer-base/src/test/java/com/fr/design/gui/storybook/Storybook.java @@ -9,6 +9,7 @@ import com.formdev.flatlaf.extras.FlatAnimatedLafChange; import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.formdev.flatlaf.util.UIScale; import com.fr.design.gui.UILookAndFeel; +import com.fr.third.org.reflections.Reflections; import com.fr.value.NotNullLazyValue; import javax.swing.DefaultListCellRenderer; @@ -29,11 +30,10 @@ import java.awt.BorderLayout; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; -import java.io.File; -import java.net.URISyntaxException; -import java.net.URL; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import static com.fine.swing.ui.layout.Layouts.cell; @@ -108,33 +108,30 @@ public class Storybook { private StoryBookComponent[] components() { ArrayList components = new ArrayList<>(); - for (String s : getClasses()) { - components.add(new StoryBookComponent(s.replace("StoryBoard", ""), COMPONENTS_PACKAGE + s)); + List> componentList = getDebugClasses(); + if (componentList.isEmpty()) { + componentList = getClasses(); + } + for (Class s : componentList) { + components.add( + new StoryBookComponent(s.getSimpleName().replace("StoryBoard", ""), s.getTypeName())); } return components.toArray(new StoryBookComponent[0]); } - private static List getClasses() { - List classNames = new ArrayList<>(); - URL resource = Storybook.class.getResource(COMPONENTS_DIR); - try { - File folder = new File(resource.toURI()); - File[] files = folder.listFiles(); - if (files != null) { - for (File file : files) { - if (file.isFile() && file.getName().endsWith(".class")) { - String className = file.getName().replace(".class", ""); - if (isStory(className)) { - classNames.add(className); - } - } - } - } - return classNames.stream().sorted().collect(Collectors.toList()); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } + private static List> getClasses() { + Reflections reflections = new Reflections("com.fr.design.gui.storybook.components"); + Set> storyClass = reflections.getTypesAnnotatedWith(Story.class); + Set> classSet = reflections.getSubTypesOf(StoryBoard.class); + storyClass.addAll(classSet); + return storyClass.stream().sorted(Comparator.comparing(Class::getSimpleName)).collect(Collectors.toList()); + } + + private static List> getDebugClasses() { + Reflections reflections = new Reflections("com.fr.design.gui.storybook.components"); + Set> types = reflections.getTypesAnnotatedWith(DebugStory.class); + return new ArrayList<>(types); } /**