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

extract icon resources & resolve AOT issue #504

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions demo/Semi.Avalonia.Demo/ViewModels/IconDemoViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;

namespace Semi.Avalonia.Demo.ViewModels;

public partial class IconDemoViewModel : ObservableObject
{
private readonly IResourceDictionary? _resources =
AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Shared/Icon.axaml")) as ResourceDictionary;
private readonly IResourceDictionary? _resources = new Icons();

private readonly Dictionary<string, IconItem> _filledIcons = new();
private readonly Dictionary<string, IconItem> _strokedIcons = new();
Expand All @@ -26,39 +23,43 @@ public void InitializeResources()
{
if (_resources is null) return;

foreach (var key in _resources.Keys)
foreach (var provider in _resources.MergedDictionaries)
{
if (_resources[key] is not Geometry geometry) continue;
var icon = new IconItem { ResourceKey = key.ToString(), Geometry = geometry };
if (provider is not ResourceDictionary dic) continue;

if (key.ToString().EndsWith("Stroked"))
foreach (var key in dic.Keys)
{
_strokedIcons[key.ToString()] = icon;
}
else
{
_filledIcons[key.ToString()] = icon;
}
if (dic[key] is not Geometry geometry) continue;
var icon = new IconItem
{
ResourceKey = key.ToString(),
Geometry = geometry
};

OnSearchTextChanged(string.Empty);
if (key.ToString().EndsWith("Stroked"))
_strokedIcons[key.ToString().ToLowerInvariant()] = icon;
else
_filledIcons[key.ToString().ToLowerInvariant()] = icon;
}
}

OnSearchTextChanged(string.Empty);
}

partial void OnSearchTextChanged(string? value)
{
var search = value?.ToLowerInvariant() ?? string.Empty;

FilteredFilledIcons.Clear();
foreach (var icon in _filledIcons.Values.Where(i => i.ResourceKey?.ToLowerInvariant().Contains(search) == true))
foreach (var pair in _filledIcons.Where(i => i.Key.Contains(search)))
{
FilteredFilledIcons.Add(icon);
FilteredFilledIcons.Add(pair.Value);
}

FilteredStrokedIcons.Clear();
foreach (var icon in
_strokedIcons.Values.Where(i => i.ResourceKey?.ToLowerInvariant().Contains(search) == true))
foreach (var pair in _strokedIcons.Where(i => i.Key.Contains(search)))
{
FilteredStrokedIcons.Add(icon);
FilteredStrokedIcons.Add(pair.Value);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Semi.Avalonia/Icons/IconPatch.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StreamGeometry x:Key="SemiIconCaretDown">
m17.55 15.66-4.8 5.48a1 1 0 0 1-1.5 0l-4.8-5.48A1 1 0 0 1 7.2 14h9.6a1 1 0 0 1 .75 1.66Z
</StreamGeometry>
<StreamGeometry x:Key="SemiIconCaretUp">
m6.45 8.34 4.8-5.48a1 1 0 0 1 1.5 0l4.8 5.48A1 1 0 0 1 16.8 10H7.2a1 1 0 0 1-.75-1.66Z
</StreamGeometry>
<StreamGeometry x:Key="SemiIconCheckBoxIndeterminate">
M5 12.5c0-.83.67-1.5 1.5-1.5h11a1.5 1.5 0 0 1 0 3h-11A1.5 1.5 0 0 1 5 12.5Z
</StreamGeometry>
<StreamGeometry x:Key="SemiIconCheckBoxTick">
M17.41 7.3c.66.51.78 1.45.28 2.11l-6.5 8.5a1.5 1.5 0 0 1-2.37.01l-3.5-4.5a1.5 1.5 0 1 1 2.36-1.84L10 14.54l5.32-6.95a1.5 1.5 0 0 1 2.1-.28Z
</StreamGeometry>
</ResourceDictionary>
6 changes: 6 additions & 0 deletions src/Semi.Avalonia/Icons/_index.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ResourceDictionary x:Class="Semi.Avalonia.Icons" xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="avares://Semi.Avalonia/Icons/Icon.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Icons/IconPatch.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
5 changes: 5 additions & 0 deletions src/Semi.Avalonia/Icons/_index.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Avalonia.Controls;

namespace Semi.Avalonia;

public class Icons : ResourceDictionary;
3 changes: 2 additions & 1 deletion src/Semi.Avalonia/Themes/Index.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Base.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/_index.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Locale/zh-cn.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Icons/_index.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Styles.Resources>
</Styles>
</Styles>
1 change: 1 addition & 0 deletions src/Semi.Avalonia/Themes/SemiTheme.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Base.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/_index.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Locale/zh-cn.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Icons/_index.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Styles.Resources>
Expand Down
6 changes: 0 additions & 6 deletions src/Semi.Avalonia/Themes/Shared/IconPatch.axaml

This file was deleted.

2 changes: 0 additions & 2 deletions src/Semi.Avalonia/Themes/Shared/_index.axaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/Palette.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/Icon.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/IconPatch.axaml" />
<!-- Controls -->
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/AdornerLayer.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/AutoCompleteBox.axaml" />
Expand Down