dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

Auto-generate static class for icon font glyphs #3811

Open davidortinau opened 2 years ago

davidortinau commented 2 years ago

Description

When using a font icon it's most helpful to have a static class or other method to access the glyphs by name rather than with the unicode.

I often use https://andreinitescu.github.io/IconFont2Code/ to generate the class so I can do things like

xmlns:icon="clr-namespace:VolumeMixer.Resources.Styles"
...
<Button Text="{x:Static icon:IconFont.VolumeUp}" FontFamily="FontAwesome"/>

What I would prefer is to be able to do is:

  1. Drop an icon font file in the Resources/fonts folder
  2. Have maui (build task or whatever) see that it contains glyphs OR annotate the font in properties to indicate such
  3. Have a generated class of the same name as the fontfile (or alias) that I can use:
xmlns:icon="clr-namespace:VolumeMixer.Resources.Styles"
...
<Button Text="{x:Static icon:FontAwesome.VolumeUp}" FontFamily="FontAwesome"/>

If we could avoid the xmlns that'd be ideal.

Better yet, can we add a markup extension that further simplifies this? Can we derive FontFamily from the autogenerated class and provide that to the view?

<Button Text="{IconFont FontAwesome.VolumeUp}"/>

If we do plan something here, we should propose related XAML changes https://github.com/microsoft/microsoft-ui-xaml

For C# UI having the static class alone makes this easier.

Button btn = new Button {
    Text = FontAwesome.VolumeUp,
    FontFamily = "FontAwesome"
};

Public API Changes

None

Intended Use-Case

All apps everywhere will benefit from this amazing time saver.

Redth commented 2 years ago

A few items we need to research:

  1. Can we infer from a font file if it's likely an "icon" font file? Maybe the lack of presence of normal alphabet Unicode characters is a sign? We probably should have an ItemGroup metadata property like GenerateCharacterMap=true to override the default inferred behaviour
  2. Do the font files contain enough useful string info to describe each character/icon that we can generate meaningfully named properties for them?
  3. We probably want properties to explicitly override the namespace, and class these would be generated in (sort of like resx allows)
  4. How about some mechanism to provide an explicit inclusion or exclusion list to trim down the unused items. This could potentially be a custom linker step that strips out unreferenced characters. This is probably an optimization that isn't high value at this point but maybe worth future consideration?
  5. What if any future tooling experience would we want around this? A viewer/dialog in the ide to see the characters in a font?
ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.