flow-components icon indicating copy to clipboard operation
flow-components copied to clipboard

IconFactory.create() should return AbstractIcon

Open gnagy opened this issue 9 months ago • 0 comments

Description

I was trying to create a custom SVG icon set with enums. I also wanted to use it interchangeably with VaadinIcons, which implements IconFactory. However, IconFactory.create() returns Icon, not AbstractIcon -- and SvgIcon is an AbstractIcon, not Icon.

I was basing my code on this example: https://vaadin.com/docs/latest/components/icons#custom-icon-collection-apis

Expected outcome

I want to use my SvgIcons interchangeably with VaadinIcons (and potentially also FontIcons).

Minimal reproducible example

Here is some simplified code of what I wanted to achieve:

public enum MyAppIcon implements IconFactory {

    ICON1("icon1.svg")

    private final String fileName;

    MyAppIcon(String fileName) {
        this.fileName = fileName;
    }

    public AbstractIcon<?> create() {
        StreamResource iconResource = new StreamResource(
            fileName,
            (InputStreamFactory) () -> getClass().getResourceAsStream("/icons/" + fileName)
        );
        return new SvgIcon(iconResource);
    }
}

public AbstractIcon<?> createIconFor(BusinessObject thingamajig) {
    return switch (thingamajig.getType()) {
        case "Something" -> VaadinIcon.ABACUS.create();
        case "SomethingElse" -> MyAppIcon.ICON1.create();
        default -> VaadinIcon.QUESTION_CIRCLE.create();
    };
}

Steps to reproduce

See example above

Environment

Vaadin version(s): 24.6.0 OS: MacOs

Browsers

No response

gnagy avatar Mar 13 '25 09:03 gnagy