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

244 lines
9.4 KiB
C#
Raw Normal View History

2023-02-11 23:04:07 +08:00
using System;
2023-02-13 02:17:38 +08:00
using System.Collections.Generic;
2023-02-11 23:04:07 +08:00
using System.Collections.ObjectModel;
2024-12-29 23:04:46 +08:00
using System.Globalization;
2023-02-11 15:50:39 +08:00
using Avalonia.Controls;
2023-02-11 23:04:07 +08:00
using Avalonia.Media;
2023-02-11 15:50:39 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
2025-01-21 18:51:45 +08:00
using Semi.Avalonia.Demo.Constant;
2024-12-29 23:04:46 +08:00
using Semi.Avalonia.Demo.Converters;
2025-01-21 20:24:21 +08:00
using Semi.Avalonia.Tokens.Palette;
2023-02-11 15:50:39 +08:00
namespace Semi.Avalonia.Demo.ViewModels;
2025-01-21 16:43:46 +08:00
public partial class PaletteDemoViewModel : ObservableObject
2023-02-11 15:50:39 +08:00
{
2023-03-26 21:37:32 +08:00
private readonly string[] _predefinedColorNames =
2024-11-15 01:00:59 +08:00
[
2023-03-26 21:37:32 +08:00
"Red", "Pink", "Purple", "Violet", "Indigo",
"Blue", "LightBlue", "Cyan", "Teal", "Green",
"LightGreen", "Lime", "Yellow", "Amber", "Orange",
"Grey"
2024-11-15 01:00:59 +08:00
];
2023-03-26 19:38:12 +08:00
private readonly IResourceDictionary? _lightResourceDictionary;
private readonly IResourceDictionary? _darkResourceDictionary;
2024-11-15 01:00:59 +08:00
2025-01-21 16:43:46 +08:00
[ObservableProperty] private ColorItemViewModel? _selectedColor;
2023-02-11 23:04:07 +08:00
2025-01-21 16:43:46 +08:00
public ObservableCollection<ColorListViewModel> LightLists { get; set; } = [];
public ObservableCollection<ColorListViewModel> DarkLists { get; set; } = [];
2024-11-15 01:00:59 +08:00
public ObservableCollection<FunctionalColorGroupViewModel> FunctionalColors { get; set; } = [];
2024-11-15 02:20:32 +08:00
public ObservableCollection<ShadowGroupViewModel> Shadows { get; set; } = [];
2023-02-13 02:17:38 +08:00
2023-02-11 23:04:07 +08:00
public PaletteDemoViewModel()
{
2024-12-26 01:42:18 +08:00
_lightResourceDictionary = new Light();
_darkResourceDictionary = new Dark();
2025-01-21 16:43:46 +08:00
WeakReferenceMessenger.Default.Register<ColorItemViewModel>(this, (_, item) => SelectedColor = item);
2023-02-13 02:55:29 +08:00
}
public void InitializeResources()
{
2023-02-13 00:14:56 +08:00
InitializePalette();
2023-02-13 02:17:38 +08:00
InitializeFunctionalColors();
2024-11-15 02:20:32 +08:00
InitializeShadows();
2023-02-13 00:14:56 +08:00
}
private void InitializePalette()
{
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);
}
2024-11-15 01:00:59 +08:00
2023-02-13 00:14:56 +08:00
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
}
}
2023-02-13 02:17:38 +08:00
private void InitializeFunctionalColors()
{
2025-01-21 16:43:46 +08:00
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Primary", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.PrimaryTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Secondary", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.SecondaryTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Tertiary", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.TertiaryTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Information", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.InformationTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Success", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.SuccessTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Warning", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.WarningTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Danger", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.DangerTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Text", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.TextTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Link", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.LinkTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Background", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.BackgroundTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Fill", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.FillTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Border", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.BorderTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Disabled", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.DisabledTokens));
2025-01-23 13:46:50 +08:00
FunctionalColors.Add(new FunctionalColorGroupViewModel(
"Others", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.OtherTokens));
2023-02-13 02:17:38 +08:00
}
2024-11-15 01:00:59 +08:00
2024-11-15 02:20:32 +08:00
private void InitializeShadows()
{
2025-01-21 16:43:46 +08:00
Shadows.Add(new ShadowGroupViewModel(
"Shadow", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.ShadowTokens));
2023-02-11 23:04:07 +08:00
}
2023-02-11 15:50:39 +08:00
}
2025-01-21 16:43:46 +08:00
public partial class ColorListViewModel : ObservableObject
2023-02-11 15:50:39 +08:00
{
2025-01-21 16:43:46 +08:00
public ObservableCollection<ColorItemViewModel> Color { get; set; } = [];
2023-02-11 23:04:07 +08:00
2025-01-21 16:43:46 +08:00
[ObservableProperty] private string? _seriesName;
2024-11-15 01:00:59 +08:00
2023-03-26 19:38:12 +08:00
internal void Initialize(IResourceDictionary? resourceDictionary, string color, bool light)
2023-02-11 15:50:39 +08:00
{
2025-01-21 16:43:46 +08:00
if (resourceDictionary is null) return;
2024-11-15 01:00:59 +08:00
2023-02-11 23:04:07 +08:00
SeriesName = color;
2024-11-15 01:00:59 +08:00
for (var i = 0; i < 10; i++)
2023-02-11 23:04:07 +08:00
{
2025-01-21 16:43:46 +08:00
var key = $"Semi{color}{i}";
if (resourceDictionary.TryGetValue(key, out var value) && value is ISolidColorBrush brush)
2023-02-11 23:04:07 +08:00
{
2025-01-21 16:43:46 +08:00
var name = $"{color} {i}";
var item = new ColorItemViewModel(name, brush, key, light, i);
item.ColorResourceKey = $"{item.ResourceKey}Color";
Color.Add(item);
2023-02-11 23:04:07 +08:00
}
}
}
}
2025-01-21 16:43:46 +08:00
public partial class ColorItemViewModel : ObservableObject
2023-02-11 23:04:07 +08:00
{
2025-01-21 16:43:46 +08:00
[ObservableProperty] private IBrush? _brush;
[ObservableProperty] private IBrush? _textBrush;
[ObservableProperty] private string? _colorDisplayName;
[ObservableProperty] private string? _resourceKey;
[ObservableProperty] private string? _colorResourceKey;
[ObservableProperty] private string? _hex;
2024-11-15 01:00:59 +08:00
public string CopyText =>
2025-01-21 20:24:21 +08:00
$"""
<StaticResource x:Key="" ResourceKey="{ResourceKey}" />
""";
2024-11-15 01:00:59 +08:00
public ColorItemViewModel(string colorDisplayName, ISolidColorBrush 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;
2024-12-29 23:04:46 +08:00
var hex = ColorConverter.ToHex.Convert(brush.Color, typeof(string), false, CultureInfo.InvariantCulture);
Hex = hex as string ?? string.Empty;
2023-03-30 16:22:56 +08:00
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
}
2023-02-13 02:17:38 +08:00
}
2025-01-21 16:43:46 +08:00
public partial class FunctionalColorGroupViewModel : ObservableObject
2023-02-13 02:17:38 +08:00
{
2025-01-21 16:43:46 +08:00
[ObservableProperty] private string? _title;
2024-11-15 01:00:59 +08:00
public ObservableCollection<ColorItemViewModel> LightColors { get; set; } = [];
public ObservableCollection<ColorItemViewModel> DarkColors { get; set; } = [];
2023-02-13 02:17:38 +08:00
2024-11-15 01:00:59 +08:00
public FunctionalColorGroupViewModel(string title, IResourceDictionary? lightDictionary,
IResourceDictionary? darkDictionary, IReadOnlyList<Tuple<string, string>> tokens)
2023-02-13 02:17:38 +08:00
{
Title = title;
2024-11-15 01:00:59 +08:00
foreach (var (key, name) in tokens)
2023-02-13 02:17:38 +08:00
{
2023-03-26 19:38:12 +08:00
if (lightDictionary?.TryGetValue(key, out var lightValue) ?? false)
2023-02-13 02:17:38 +08:00
{
if (lightValue is ISolidColorBrush lightBrush)
{
LightColors.Add(new ColorItemViewModel(name, lightBrush, key, true, 0));
}
}
2023-03-26 19:38:12 +08:00
if (darkDictionary?.TryGetValue(key, out var darkValue) ?? false)
2023-02-13 02:17:38 +08:00
{
if (darkValue is ISolidColorBrush darkBrush)
{
DarkColors.Add(new ColorItemViewModel(name, darkBrush, key, true, 0));
}
}
}
}
}
2025-01-21 16:43:46 +08:00
public partial class ShadowItemViewModel : ObservableObject
2024-11-15 02:20:32 +08:00
{
2025-01-21 16:43:46 +08:00
[ObservableProperty] private string? _shadowDisplayName;
[ObservableProperty] private string? _resourceKey;
[ObservableProperty] private string? _boxShadowValue;
2024-11-15 02:20:32 +08:00
public string CopyText =>
2025-01-21 20:24:21 +08:00
$"""
<StaticResource x:Key="" ResourceKey="{ResourceKey}" />
""";
2024-11-15 02:20:32 +08:00
public ShadowItemViewModel(string shadowDisplayName, BoxShadows boxShadows, string resourceKey)
{
ShadowDisplayName = shadowDisplayName;
ResourceKey = resourceKey;
BoxShadowValue = boxShadows.ToString();
}
}
2025-01-21 16:43:46 +08:00
public partial class ShadowGroupViewModel : ObservableObject
2024-11-15 02:20:32 +08:00
{
2025-01-21 16:43:46 +08:00
[ObservableProperty] private string? _title;
2024-11-15 02:20:32 +08:00
public ObservableCollection<ShadowItemViewModel> LightShadows { get; set; } = [];
public ObservableCollection<ShadowItemViewModel> DarkShadows { get; set; } = [];
public ShadowGroupViewModel(string title, IResourceDictionary? lightDictionary,
IResourceDictionary? darkDictionary, IReadOnlyList<Tuple<string, string>> tokens)
{
Title = title;
foreach (var (key, name) in tokens)
{
if (lightDictionary?.TryGetValue(key, out var lightValue) ?? false)
{
if (lightValue is BoxShadows lightShadow)
{
LightShadows.Add(new ShadowItemViewModel(name, lightShadow, key));
}
}
if (darkDictionary?.TryGetValue(key, out var darkValue) ?? false)
{
if (darkValue is BoxShadows darkShadow)
{
DarkShadows.Add(new ShadowItemViewModel(name, darkShadow, key));
}
}
}
}
2023-02-11 15:50:39 +08:00
}