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

72 lines
2.2 KiB
C#
Raw Normal View History

using System;
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 15:06:57 +08:00
if (provider is not ResourceDictionary dic) continue;
2024-12-26 02:35:39 +08:00
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;
2024-12-26 15:06:57 +08:00
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;
2024-12-26 02:35:39 +08:00
else
_filledIcons[key.ToString()] = icon;
2024-12-26 02:35:39 +08:00
}
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 = string.IsNullOrWhiteSpace(value) ? string.Empty : value.Trim();
2024-12-20 23:38:53 +08:00
FilteredFilledIcons.Clear();
foreach (var pair in _filledIcons.Where(kv => kv.Key.Contains(search, StringComparison.InvariantCultureIgnoreCase)))
2024-12-20 23:38:53 +08:00
{
2024-12-26 15:41:17 +08:00
FilteredFilledIcons.Add(pair.Value);
2024-12-20 23:38:53 +08:00
}
FilteredStrokedIcons.Clear();
foreach (var pair in _strokedIcons.Where(kv => kv.Key.Contains(search, StringComparison.InvariantCultureIgnoreCase)))
2024-12-20 23:38:53 +08:00
{
2024-12-26 15:41:17 +08:00
FilteredStrokedIcons.Add(pair.Value);
2024-12-20 21:56:04 +08:00
}
}
}
public class IconItem
{
public string? ResourceKey { get; set; }
public Geometry? Geometry { get; set; }
}