SKProCH/Material.Icons

Custom icons

montex12 opened this issue · 6 comments

Hello, is it possible to add custom icons?

I have some SVG files that I would like to integrate in Material.Icons so I can use the same API for Material Icons and Custom Icons.

Thanks

Hello, @montex12

Unfortunately, this will not be the easiest task at the moment. Everything rests on the fact that this library is based on the fact that all the icons are enum elements, and it will be difficult to just expand them. The only way I see - you could take code for Material.Icons, add enum element in MaterialIconEnum.cs, and data for it in MaterialIconDataProvider.PathData.cs, then build and plug dll locally. In theory, this could work.

I've had a few thoughts on how to make this more extensible, but so far I don't think any of them will work.

Could the MaterialIconDataProvider.Instance property be made public and writable? If we could set the provider at application startup, we could override ProvideData (which is already virtual) to return custom SVG code for certain icons in the enumeration and defer back to the library implementation for anything not overridden.

We wouldn't be able to add new icons, but we could override existing icons. That would be sufficient for my needs.

I am currently doing this using reflection:

public class CustomIconProvider : MaterialIconDataProvider
{
    public override string ProvideData(MaterialIconKind kind)
    {
        return kind switch
        {
            MaterialIconKind.TrophyVariant => "...",
            _ => base.ProvideData(kind)
        };
    }
}


internal static class Program
{
    public static int Main(string[] args)
    {
        typeof(MaterialIconDataProvider)
            .GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic)!
            .SetValue(null, new CustomIconProvider());

        return BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);
    }
}

Hm, nice suggestion, actually!

I'll try to implement it on the weekend, or you can send a PR

I would be happy to make a PR this weekend.

Opened PR #27

Merged. Thanks. I'll publish as a new version a bit later.