feat: enhance IconDemo.

This commit is contained in:
Zhang Dian
2024-12-20 23:38:53 +08:00
parent 9ef0e418b8
commit e61c07f352
3 changed files with 146 additions and 42 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
@@ -12,24 +14,51 @@ public partial class IconDemoViewModel : ObservableObject
private readonly IResourceDictionary? _resources =
AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Shared/Icon.axaml")) as ResourceDictionary;
public ObservableCollection<IconItem> Icons { get; set; } = [];
private readonly Dictionary<string, IconItem> _filledIcons = new();
private readonly Dictionary<string, IconItem> _strokedIcons = new();
[ObservableProperty] private string? _searchText;
public ObservableCollection<IconItem> FilteredFilledIcons { get; set; } = [];
public ObservableCollection<IconItem> FilteredStrokedIcons { get; set; } = [];
public void InitializeResources()
{
LoadIcons();
}
private void LoadIcons()
{
if (_resources is null) return;
foreach (var key in _resources.Keys)
{
if (_resources[key] is Geometry geometry)
if (_resources[key] is not Geometry geometry) continue;
var icon = new IconItem { ResourceKey = key.ToString(), Geometry = geometry };
if (key.ToString().EndsWith("Stroked"))
{
var icon = new IconItem { ResourceKey = key.ToString(), Geometry = geometry };
Icons.Add(icon);
_strokedIcons[key.ToString()] = icon;
}
else
{
_filledIcons[key.ToString()] = 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))
{
FilteredFilledIcons.Add(icon);
}
FilteredStrokedIcons.Clear();
foreach (var icon in
_strokedIcons.Values.Where(i => i.ResourceKey?.ToLowerInvariant().Contains(search) == true))
{
FilteredStrokedIcons.Add(icon);
}
}
}