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

165 lines
4.8 KiB
C#
Raw Normal View History

2023-02-11 23:04:07 +08:00
using System;
using System.Collections.ObjectModel;
2023-02-11 15:50:39 +08:00
using Avalonia.Controls;
2023-02-11 23:04:07 +08:00
using Avalonia.Markup.Xaml;
using Avalonia.Media;
2023-02-11 15:50:39 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
2023-02-11 15:50:39 +08:00
namespace Semi.Avalonia.Demo.ViewModels;
public class PaletteDemoViewModel: ObservableObject
{
2023-02-13 00:14:56 +08:00
private readonly string[] _predefinedColorNames = { "Amber","Blue","Cyan","Green","Grey","Indigo","LightBlue","LightGreen","Lime","Orange","Pink","Purple","Red","Teal","Violet","Yellow" };
private IResourceDictionary _lightResourceDictionary;
private IResourceDictionary _darkResourceDictionary;
private ColorItemViewModel _selectedColor;
public ColorItemViewModel SelectedColor
2023-02-11 23:04:07 +08:00
{
get => _selectedColor;
set => SetProperty(ref _selectedColor, value);
}
2023-02-13 00:14:56 +08:00
private ObservableCollection<ColorListViewModel> _lightLists;
public ObservableCollection<ColorListViewModel> LightLists
{
2023-02-13 00:14:56 +08:00
get => _lightLists;
set => SetProperty(ref _lightLists, value);
}
2023-02-13 00:14:56 +08:00
private ObservableCollection<ColorListViewModel> _darkLists;
public ObservableCollection<ColorListViewModel> DarkLists
{
2023-02-13 00:14:56 +08:00
get => _darkLists;
set => SetProperty(ref _darkLists, value);
2023-02-11 23:04:07 +08:00
}
public PaletteDemoViewModel()
{
2023-02-13 00:14:56 +08:00
_lightResourceDictionary = (ResourceDictionary)AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml"));
_darkResourceDictionary = (ResourceDictionary)AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Dark/Palette.axaml"));
InitializePalette();
WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem);
}
private void InitializePalette()
{
LightLists = new ObservableCollection<ColorListViewModel>();
foreach (var color in _predefinedColorNames)
{
2023-02-13 00:14:56 +08:00
ColorListViewModel s = new ColorListViewModel();
s.Initialize(_lightResourceDictionary, color, true);
LightLists.Add(s);
}
2023-02-13 00:14:56 +08:00
DarkLists = new ObservableCollection<ColorListViewModel>();
foreach (var color in _predefinedColorNames)
2023-02-11 23:04:07 +08:00
{
2023-02-13 00:14:56 +08:00
ColorListViewModel s = new ColorListViewModel();
s.Initialize(_darkResourceDictionary, color, false);
DarkLists.Add(s);
2023-02-11 23:04:07 +08:00
}
}
private void OnClickColorItem(PaletteDemoViewModel vm, ColorItemViewModel item)
{
SelectedColor = item;
2023-02-11 23:04:07 +08:00
}
2023-02-11 15:50:39 +08:00
}
2023-02-13 00:14:56 +08:00
public class ColorListViewModel: ObservableObject
2023-02-11 15:50:39 +08:00
{
2023-02-11 23:04:07 +08:00
private ObservableCollection<ColorItemViewModel>? _colors;
public ObservableCollection<ColorItemViewModel>? Color
{
get => _colors;
set => SetProperty(ref _colors, value);
}
private string? _seriesName;
public string? SeriesName
{
get => _seriesName;
set => SetProperty(ref _seriesName, value);
}
internal void Initialize(IResourceDictionary resourceDictionary, string color, bool light)
2023-02-11 15:50:39 +08:00
{
2023-02-11 23:04:07 +08:00
SeriesName = color;
Color = new ObservableCollection<ColorItemViewModel>();
2023-02-11 15:50:39 +08:00
2023-02-11 23:04:07 +08:00
for (int i = 0; i < 10; i++)
{
var key = "Semi" + color + i;
if (resourceDictionary.TryGetValue(key, out var value))
{
if (value is SolidColorBrush brush)
{
string name = color + " " + i;
var item = new ColorItemViewModel(name, brush, key, light, i);
2023-02-11 23:04:07 +08:00
Color.Add(item);
}
}
}
}
}
public class ColorItemViewModel : ObservableObject
{
2023-02-13 00:14:56 +08:00
private IBrush _brush;
public IBrush Brush
2023-02-11 23:04:07 +08:00
{
2023-02-13 00:14:56 +08:00
get => _brush;
set => SetProperty(ref _brush, value);
2023-02-11 23:04:07 +08:00
}
2023-02-13 00:14:56 +08:00
private IBrush _textBrush;
public IBrush TextBrush
{
2023-02-13 00:14:56 +08:00
get => _textBrush;
set => SetProperty(ref _textBrush, value);
}
2023-02-11 23:04:07 +08:00
2023-02-13 00:14:56 +08:00
private string _colorDisplayName;
public string ColorDisplayName
2023-02-11 23:04:07 +08:00
{
2023-02-13 00:14:56 +08:00
get => _colorDisplayName;
set => SetProperty(ref _colorDisplayName, value);
2023-02-11 23:04:07 +08:00
}
private string _resourceKey;
public string ResourceKey
{
get => _resourceKey;
set => SetProperty(ref _resourceKey, value);
}
private string _hex;
public string Hex
{
get => _hex;
set => SetProperty(ref _hex, value);
}
2023-02-11 23:04:07 +08:00
2023-02-13 00:14:56 +08:00
public ColorItemViewModel(string colorDisplayName, IBrush brush, string resourceKey, bool light, int index)
2023-02-11 23:04:07 +08:00
{
2023-02-13 00:14:56 +08:00
ColorDisplayName = colorDisplayName;
Brush = brush;
2023-02-11 23:04:07 +08:00
ResourceKey = resourceKey;
2023-02-13 00:14:56 +08:00
Hex = brush.ToString().ToUpperInvariant();
if ((light && index < 5) || (!light && index > 5))
{
2023-02-13 00:14:56 +08:00
TextBrush = Brushes.Black;
}
else
{
2023-02-13 00:14:56 +08:00
TextBrush = Brushes.White;
}
2023-02-11 15:50:39 +08:00
}
}