Files
Semi.Avalonia/demo/Semi.Avalonia.Demo/ViewModels/IconDemoViewModel.cs

72 lines
2.2 KiB
C#
Raw Normal View History

2024-12-26 02:35:39 +08:00
using System.Collections.Generic;
2024-12-20 21:56:04 +08:00
using System.Collections.ObjectModel;
2024-12-20 23:38:53 +08:00
using System.Linq;
2024-12-20 21:56:04 +08:00
using Avalonia.Controls;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Semi.Avalonia.Demo.ViewModels;
public partial class IconDemoViewModel : ObservableObject
{
2024-12-26 02:35:39 +08:00
private readonly IResourceDictionary? _resources = new Icons();
2024-12-20 21:56:04 +08:00
2024-12-20 23:38:53 +08:00
private readonly Dictionary<string, IconItem> _filledIcons = new();
private readonly Dictionary<string, IconItem> _strokedIcons = new();
2024-12-20 21:56:04 +08:00
2024-12-20 23:38:53 +08:00
[ObservableProperty] private string? _searchText;
2024-12-20 21:56:04 +08:00
2024-12-20 23:38:53 +08:00
public ObservableCollection<IconItem> FilteredFilledIcons { get; set; } = [];
public ObservableCollection<IconItem> FilteredStrokedIcons { get; set; } = [];
public void InitializeResources()
2024-12-20 21:56:04 +08:00
{
if (_resources is null) return;
2024-12-26 02:35:39 +08:00
foreach (var provider in _resources.MergedDictionaries)
2024-12-20 21:56:04 +08:00
{
2024-12-26 02:35:39 +08:00
var dic = provider as ResourceDictionary;
if (dic?.Keys is null) continue;
foreach (var key in dic.Keys)
2024-12-20 21:56:04 +08:00
{
2024-12-26 02:35:39 +08:00
if (dic[key] is not Geometry geometry) continue;
var icon = new IconItem { ResourceKey = key.ToString(), Geometry = geometry };
2024-12-20 23:38:53 +08:00
2024-12-26 02:35:39 +08:00
if (key.ToString().EndsWith("Stroked"))
{
_strokedIcons[key.ToString()] = icon;
}
else
{
_filledIcons[key.ToString()] = icon;
}
}
2024-12-20 23:38:53 +08:00
}
2024-12-26 02:35:39 +08:00
OnSearchTextChanged(string.Empty);
2024-12-20 23:38:53 +08:00
}
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);
2024-12-20 21:56:04 +08:00
}
}
}
public class IconItem
{
public string? ResourceKey { get; set; }
public Geometry? Geometry { get; set; }
}