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

Make an IconViewModel and an IconControl #218

Closed
wants to merge 4 commits into from

Conversation

zadjii-msft
Copy link
Owner

oh no, this stupid idea works really well

This is mostly for @michael-hawker to look at and tell me if it's a totally stupid idea. But it does away with all the weird virtualization issues we had (I think)

Ref #213 (might close it?)

Base automatically changed from dev/migrie/f/error-context-pr to main December 12, 2024 23:11
michael-hawker added a commit that referenced this pull request Dec 13, 2024
Replaces PR #218

IconBox is a custom control that's a ContentControl, it's generic (toolkitable) and should be able to be styled and templated.
It knows how to take an IconSource and create the underlying IconElement as its content.
It can also take any general value as a `SourceKey` and via an implementation of the SourceRequested event, translate a bound general object into the `IconSource` required. This is how caching can be provided by an application as well, for instance (like we'll do here).
This uses the deferred events pattern to await the call to the `SourceRequested` event which may need to load data asynchronously

We create a static x:Bind helper `IconCacheProvider` to encapsulate our shared logic for our eventual Icon cache.

Renamed IconCacheService.xaml.cs -> IconCacheService.cs
Removed old broken behavior (believe ultimate issue was due to instability in loaded/unloaded events, i.e. issue microsoft/microsoft-ui-xaml#1900)
XAML Styler also did its thing...
michael-hawker added a commit that referenced this pull request Dec 13, 2024
Replaces PR #218

IconBox is a custom control that's a ContentControl, it's generic (toolkitable) and should be able to be styled and templated.
It knows how to take an IconSource and create the underlying IconElement as its content.
It can also take any general value as a `SourceKey` and via an implementation of the SourceRequested event, translate a bound general object into the `IconSource` required. This is how caching can be provided by an application as well, for instance (like we'll do here).
This uses the deferred events pattern to await the call to the `SourceRequested` event which may need to load data asynchronously

We create a static x:Bind helper `IconCacheProvider` to encapsulate our shared logic for our eventual Icon cache.

Renamed IconCacheService.xaml.cs -> IconCacheService.cs
Removed old broken behavior (believe ultimate issue was due to instability in loaded/unloaded events, i.e. issue microsoft/microsoft-ui-xaml#1900)
XAML Styler also did its thing...
@michael-hawker
Copy link
Collaborator

michael-hawker commented Dec 13, 2024

Replacing with #222 thanks for testing this as the end goal!

zadjii-msft added a commit that referenced this pull request Dec 13, 2024
Fixes #213
Replaces PR #218

FYI @Ryken100 (thanks for the info and assist in debugging the issue and discussing possible avenues of resolution)
Thanks @zadjii-msft for validating the end path in #218

Before:
```xml
                        <Border x:Name="IconBorder"
                                Grid.Column="0"
                                Width="16"
                                Height="16"
                                Margin="0,0,0,0">
                            <!-- LoadIconBehavior will magically fill this border up with an icon -->
                            <Interactivity:Interaction.Behaviors>
                                <cmdpalUI:LoadIconBehavior Source="{x:Bind Icon, Mode=OneWay}"/>
                            </Interactivity:Interaction.Behaviors>
                        </Border>
```

After:
```xml
                        <cpcontrols:IconBox
                            Grid.Column="0"
                            Width="16"
                            Height="16"
                            Margin="0,0,0,0"
                            SourceKey="{x:Bind Icon, Mode=OneWay}"
                            SourceRequested="{x:Bind help:IconCacheProvider.SourceRequested}" />
```

The IconCacheProvider is the translation layer between having a light-weight control and our specific app's logic/desire for an icon cache, using the deferred event pattern:

```cs
public static partial class IconCacheProvider
{
    private static readonly IconCacheService IconService = new(Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread());

    public static async void SourceRequested(IconBox sender, SourceRequestedEventArgs args)
    {
        if (args.Key == null)
        {
            return;
        }

        if (args.Key is IconDataType iconData)
        {
            var deferral = args.GetDeferral();

            args.Value = await IconService.GetIconSource(iconData);

            deferral.Complete();
        }
    }
}
```

## Details

`IconBox` is a custom control that's a ContentControl, its generic (toolkitable) and should be able to be styled and templated (haven't tested, but no reason it shouldn't as a XAML `ContentControl`, should help @niels9001 a ton). 

It knows how to take an `IconSource` and create the underlying `IconElement` as its content.

It can also take any general value as a `SourceKey` and via an implementation of the `SourceRequested` event, translate a bound general object into the `IconSource` required. This is how caching can be provided by an application as well, for instance (like we'll do here). This uses the deferred events pattern to await the call to the `SourceRequested` event which may need to load data asynchronously

We create a static x:Bind helper `IconCacheProvider` to encapsulate our shared logic for our eventual Icon cache.

Also:
- Renamed IconCacheService.xaml.cs -> IconCacheService.cs 
- Removed old broken behavior (believe ultimate issue was due to instability in loaded/unloaded events, i.e. issue microsoft/microsoft-ui-xaml#1900)
- XAML Styler also did its thing... (some conflict here in config from PowerToys to resolve later, imagine this is also a consequence of us not having CI setup in fork...)
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

Successfully merging this pull request may close these issues.

3 participants