Skip to content

IconFactory.create() should return AbstractIcon #7195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gnagy opened this issue Mar 13, 2025 · 0 comments
Open

IconFactory.create() should return AbstractIcon #7195

gnagy opened this issue Mar 13, 2025 · 0 comments
Labels

Comments

@gnagy
Copy link

gnagy commented Mar 13, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants