Skip to content
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

Use With FluentAvalonia IconSource #104

Open
Ricent82 opened this issue Dec 17, 2023 · 10 comments
Open

Use With FluentAvalonia IconSource #104

Ricent82 opened this issue Dec 17, 2023 · 10 comments

Comments

@Ricent82
Copy link

Could you maybe provide some sample code for how to use these icons with FluentAvalonia's IconSource? I've so far been unable to get it to work. As a quick example, here's their NavigationView control (from the sample project):

peek

You specify the items like:

<ui:NavigationViewItem.MenuItems>
    <ui:NavigationViewItem Content="Mail" IconSource="Mail" />
</ui:NavigationViewItem.MenuItems>

Rather than their selection of icons, I'd love to be able to use these. I tried something like:

      <ui:NavigationView.MenuItemTemplate>
        <DataTemplate>
          <ui:NavigationViewItem>
            <StackPanel Orientation="Horizontal">
              <i:Icon Value="fa-brands fa-anchor"/>
              <TextBlock Text="Anchor" />
            </StackPanel>
          </ui:NavigationViewItem>
        </DataTemplate>
      </ui:NavigationView.MenuItemTemplate>

But then you end up having to tweak around with margins - and it still looks glitchy when you expand/collapse the item:

peek

I'm sure there must be a simple way to do this, but I haven't found any way that's compatible. Any pointers would be greatly appreciated. Here's a bit about their IconSource, if it's helpful: amwx/FluentAvalonia#257

@just-seba
Copy link
Member

You could try something like this:

<ui:NavigationViewItem.MenuItems>
  <ui:NavigationViewItem Content="Anchor">
    <ui:NavigationViewItem.IconSource>
      <ui:ImageIconSource>
        <i:IconImage Value="fa-brands fa-anchor" Brush="(defaults to black)" />
      </ui:ImageIconSource>
    </ui:NavigationViewItem.IconSource>
  </ui:NavigationViewItem>
</ui:NavigationViewItem.MenuItems>

@Ricent82
Copy link
Author

Yessssss, that's it! Thanks so much :)

@just-seba
Copy link
Member

Nice 👍

@Ricent82
Copy link
Author

Quick follow-up: the only thing I can't seem to get working is the brush. I'm using the same DynamicResource as everywhere else in xaml, but the icons seem to be the only thing that don't follow it. As an example:

      <ui:NavigationView.MenuItemTemplate>
        <DataTemplate DataType="{x:Type local:ViewModelNavPageBase}">
            <ui:NavigationViewItem>
              <ui:NavigationViewItem.Content>
                <!--This border is just to prove to myself that the DynamicResource is getting the right color-->
                <Border BorderThickness="1" CornerRadius="10" Margin="10" Background="{DynamicResource TextFillColorPrimaryBrush}">
                  <--Not actually visible due to the border-->
                  <TextBlock Text="{Binding PageName}" />
                </Border>
              </ui:NavigationViewItem.Content>
              <ui:NavigationViewItem.IconSource>
                <ui:ImageIconSource>
                   <--However, the icons don't follow the color-->
                  <i:IconImage Value="{Binding PageIcon}" Brush="{DynamicResource TextFillColorPrimaryBrush}" />
                </ui:ImageIconSource>
              </ui:NavigationViewItem.IconSource>
            </ui:NavigationViewItem>
        </DataTemplate>
      </ui:NavigationView.MenuItemTemplate>

And here's the behavior - the "debug" button toggles the them, and you can see the border following the text foreground color, but the icons always remain black:

peek

@just-seba
Copy link
Member

The IconImage is derived from the DrawingImage which itself is not stylable (see AvaloniaUI/Avalonia#5633). I think that's why it's not working with DynamicResource as well.

You could try to create your own Image Source like this

public class MyIconSource: ImageIconSource
{
    public static readonly StyledProperty<string?> ValueProperty =
        AvaloniaProperty.Register<MyIconSource, string?>(nameof(Value));
    
    public static readonly StyledProperty<IBrush?> BrushProperty =
        AvaloniaProperty.Register<MyIconSource, IBrush?>(nameof(Brush));

    
    public string? Value 
    {
        get => GetValue(ValueProperty);
        set => SetValue(ValueProperty, value);
    }

    public IBrush? Brush 
    {
        get => GetValue(BrushProperty);
        set => SetValue(BrushProperty, value);
    }

    protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
    {
        base.OnPropertyChanged(change);
        if (change.Property == ValueProperty || change.Property == BrushProperty)
        {
            Source = new IconImage(Value, Brush);
        }
    }
}

and use it in your UI

<ui:NavigationViewItem.IconSource>
  <foo:MyIconSource Value="{Binding PageIcon}" Brush="{DynamicResource TextFillColorPrimaryBrush}" />
</ui:NavigationViewItem.IconSource>

@just-seba just-seba reopened this Dec 20, 2023
@Ricent82
Copy link
Author

Thanks for the quick response :)

Unfortunately, it won't let me bind:

Avalonia error AVLN:0004: Unable to find suitable setter or adder for property Source of type FluentAvalonia:FluentAvalonia.UI.Controls.ImageIconSource for argument IconSourceStyleable, available setter parameter lists are:
Avalonia.Media.IImage

@houstonhaynes
Copy link

houstonhaynes commented Dec 30, 2023

Thanks for raising this @Ricent82 - I just encountered this a week after you and we had to divert to using bitmaps. I'd love to see a proper "bridge" between this and FluentAvalonia - for both "inner loop" to ease working in the code as well as smoothing out the rough edges of mapping icons to color changes in theme variants.

Screenshot 2023-12-30 at 8 29 20 AM

@Ricent82
Copy link
Author

Ricent82 commented Dec 30, 2023

I ended up just switching to https://github.com/davidxuang/FluentIcons. In addition to the theme issue, was also having issues with icons appearing stretched/squished, or getting oddly mixed up (i.e. sometimes one of the icons in the NavigationView would actually appear as one of the other icons, until you tap it in the UI, then it would switch to the correct icon). FluentIcons mitigated all of those issues out of the box.

@houstonhaynes
Copy link

Thanks for that advice - I did the same for now, but would love to have access to FontAwesome (I have a pro license and like the duotone fonts for various purposes). So I want to keep an eye on this and use it with FluentAvalonia when I (or someone else) figures out how to map the icons in.

Screenshot 2023-12-31 at 10 27 30 AM

Screenshot 2023-12-31 at 10 24 32 AM

@just-seba
Copy link
Member

just-seba commented Jan 5, 2024

Thank's for the feedback. It seems it's not that easy to support FluentAvalonia. But I will look into this in the feature.

I'll keep this issue open for tracking.

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

No branches or pull requests

3 participants