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

93 lines
3.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
{
2025-09-26 00:46:24 +08:00
private readonly Icons _resources = new();
2024-12-20 21:56:04 +08:00
private readonly Dictionary<string, IconItem> _fillIcons = new();
2024-12-20 23:38:53 +08:00
private readonly Dictionary<string, IconItem> _strokedIcons = new();
private readonly Dictionary<string, IconItem> _aiIcons = 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
public ObservableCollection<IconTab> IconTabs { get; } = [];
public ObservableCollection<IconItem> FilteredFillIcons { get; set; } = [];
2024-12-20 23:38:53 +08:00
public ObservableCollection<IconItem> FilteredStrokedIcons { get; set; } = [];
public ObservableCollection<IconItem> FilteredAIIcons { get; set; } = [];
2024-12-20 23:38:53 +08:00
public void InitializeResources()
2024-12-20 21:56:04 +08:00
{
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;
2025-09-26 00:46:24 +08:00
var resourceKey = key.ToString() ?? string.Empty;
2024-12-26 15:06:57 +08:00
var icon = new IconItem
{
2025-09-26 00:46:24 +08:00
ResourceKey = resourceKey,
2024-12-26 15:06:57 +08:00
Geometry = geometry
};
2024-12-20 23:38:53 +08:00
if (resourceKey.StartsWith("SemiIconAI"))
_aiIcons[resourceKey] = icon;
else if (resourceKey.EndsWith("Stroked", StringComparison.InvariantCultureIgnoreCase))
2025-09-26 00:46:24 +08:00
_strokedIcons[resourceKey] = icon;
2024-12-26 02:35:39 +08:00
else
_fillIcons[resourceKey] = 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);
IconTabs.Clear();
IconTabs.Add(new IconTab("Fill Icons", FilteredFillIcons));
IconTabs.Add(new IconTab("Stroked Icons", FilteredStrokedIcons));
IconTabs.Add(new IconTab("AI Icons", FilteredAIIcons));
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
FilteredFillIcons.Clear();
foreach (var pair in _fillIcons.Where(kv => kv.Key.Contains(search, StringComparison.InvariantCultureIgnoreCase)))
2024-12-20 23:38:53 +08:00
{
FilteredFillIcons.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
}
FilteredAIIcons.Clear();
foreach (var pair in _aiIcons.Where(kv => kv.Key.Contains(search, StringComparison.InvariantCultureIgnoreCase)))
{
FilteredAIIcons.Add(pair.Value);
}
2024-12-20 21:56:04 +08:00
}
}
public class IconTab(string header, ObservableCollection<IconItem> iconItems)
{
public string Header { get; set; } = header;
public ObservableCollection<IconItem> IconItems { get; set; } = iconItems;
}
2024-12-20 21:56:04 +08:00
public class IconItem
{
public string? ResourceKey { get; set; }
public Geometry? Geometry { get; set; }
}