更新整理
This commit is contained in:
149
WPFluent.Gallery/ViewModels/Pages/IconsViewModel.cs
Normal file
149
WPFluent.Gallery/ViewModels/Pages/IconsViewModel.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
using WPFluent.Extensions;
|
||||
using WPFluent.Gallery.Models;
|
||||
|
||||
namespace WPFluent.Gallery.ViewModels.Pages;
|
||||
|
||||
public partial class IconsViewModel : ViewModel
|
||||
{
|
||||
private int _selectedIconId = 0;
|
||||
|
||||
private string _autoSuggestBoxText = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private SymbolRegular _selectedSymbol = SymbolRegular.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _selectedSymbolName = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _selectedSymbolUnicodePoint = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _selectedSymbolTextGlyph = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _selectedSymbolXaml = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isIconFilled = false;
|
||||
|
||||
[ObservableProperty]
|
||||
private List<DisplayableIcon> _iconsCollection = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private List<DisplayableIcon> _filteredIconsCollection = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private List<string> _iconNames = [];
|
||||
|
||||
public string AutoSuggestBoxText
|
||||
{
|
||||
get => _autoSuggestBoxText;
|
||||
set
|
||||
{
|
||||
_ = SetProperty(ref _autoSuggestBoxText, value);
|
||||
UpdateSearchResults(value);
|
||||
}
|
||||
}
|
||||
|
||||
public IconsViewModel()
|
||||
{
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
var id = 0;
|
||||
var names = Enum.GetNames(typeof(SymbolRegular));
|
||||
var icons = new List<DisplayableIcon>();
|
||||
|
||||
names = names.OrderBy(n => n).ToArray();
|
||||
|
||||
foreach (string iconName in names)
|
||||
{
|
||||
SymbolRegular icon = SymbolGlyph.Parse(iconName);
|
||||
|
||||
icons.Add(
|
||||
new DisplayableIcon
|
||||
{
|
||||
Id = id++,
|
||||
Name = iconName,
|
||||
Icon = icon,
|
||||
Symbol = ((char)icon).ToString(),
|
||||
Code = ((int)icon).ToString("X4"),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
IconsCollection = icons;
|
||||
FilteredIconsCollection = icons;
|
||||
IconNames = icons.Select(icon => icon.Name).ToList();
|
||||
|
||||
if (icons.Count > 4)
|
||||
{
|
||||
_selectedIconId = 4;
|
||||
|
||||
UpdateSymbolData();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void OnIconSelected(int parameter)
|
||||
{
|
||||
_selectedIconId = parameter;
|
||||
|
||||
UpdateSymbolData();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void OnCheckboxChecked(object sender)
|
||||
{
|
||||
if (sender is not CheckBox checkbox)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsIconFilled = checkbox?.IsChecked ?? false;
|
||||
|
||||
UpdateSymbolData();
|
||||
}
|
||||
|
||||
private void UpdateSymbolData()
|
||||
{
|
||||
if (IconsCollection.Count - 1 < _selectedIconId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DisplayableIcon selectedSymbol = Enumerable.FirstOrDefault<DisplayableIcon>(IconsCollection, sym => sym.Id == _selectedIconId);
|
||||
|
||||
SelectedSymbol = selectedSymbol.Icon;
|
||||
SelectedSymbolName = selectedSymbol.Name;
|
||||
SelectedSymbolUnicodePoint = selectedSymbol.Code;
|
||||
SelectedSymbolTextGlyph = $"&#x{selectedSymbol.Code};";
|
||||
SelectedSymbolXaml =
|
||||
$"<ui:SymbolIcon Symbol=\"{selectedSymbol.Name}\"{(IsIconFilled ? " Filled=\"True\"" : string.Empty)}/>";
|
||||
}
|
||||
|
||||
private void UpdateSearchResults(string searchedText)
|
||||
{
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(searchedText))
|
||||
{
|
||||
FilteredIconsCollection = IconsCollection;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
var formattedText = searchedText.ToLower().Trim();
|
||||
|
||||
FilteredIconsCollection = Enumerable
|
||||
.Where(IconsCollection, icon =>
|
||||
icon.Name.Contains(formattedText, StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user