463 lines
16 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;
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;
2023-02-11 15:50:39 +08:00
namespace Semi.Avalonia.Demo.ViewModels;
2024-11-15 01:00:59 +08:00
public 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
2023-03-26 19:38:12 +08:00
private ColorItemViewModel _selectedColor = null!;
public ColorItemViewModel SelectedColor
2023-02-11 23:04:07 +08:00
{
get => _selectedColor;
set => SetProperty(ref _selectedColor, value);
}
2024-11-15 01:00:59 +08:00
2023-03-26 19:38:12 +08:00
private ObservableCollection<ColorListViewModel>? _lightLists;
2024-11-15 01:00:59 +08:00
2023-03-26 19:38:12 +08:00
public ObservableCollection<ColorListViewModel>? LightLists
{
2023-02-13 00:14:56 +08:00
get => _lightLists;
set => SetProperty(ref _lightLists, value);
}
2024-11-15 01:00:59 +08:00
2023-03-26 19:38:12 +08:00
private ObservableCollection<ColorListViewModel>? _darkLists;
2024-11-15 01:00:59 +08:00
2023-03-26 19:38:12 +08:00
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
}
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-11-15 01:00:59 +08:00
_lightResourceDictionary = new Light.Palette();
2024-07-18 16:58:05 +08:00
_darkResourceDictionary = new Dark.Palette();
2023-02-13 02:55:29 +08:00
WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem);
}
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()
{
2024-11-15 01:00:59 +08:00
LightLists = [];
2023-02-13 00:14:56 +08:00
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
DarkLists = [];
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()
{
FunctionalColors.Add(new FunctionalColorGroupViewModel("Primary", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.PrimaryTokens));
2023-02-13 02:55:29 +08:00
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));
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()
{
Shadows.Add(new ShadowGroupViewModel("Shadow", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.ShadowTokens));
}
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
}
2024-11-15 01:00:59 +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);
}
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
{
2023-03-26 19:38:12 +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
Color = [];
for (var i = 0; i < 10; i++)
2023-02-11 23:04:07 +08:00
{
var key = "Semi" + color + i;
if (resourceDictionary.TryGetValue(key, out var value))
{
2023-02-13 02:17:38 +08:00
if (value is ISolidColorBrush brush)
2023-02-11 23:04:07 +08:00
{
string name = color + " " + i;
var item = new ColorItemViewModel(name, brush, key, light, i);
2023-03-27 11:25:47 +08:00
item.ColorResourceKey = item.ResourceKey + "Color";
2023-02-11 23:04:07 +08:00
Color.Add(item);
2024-11-15 01:00:59 +08:00
}
2023-02-11 23:04:07 +08:00
}
}
}
}
public class ColorItemViewModel : ObservableObject
{
2023-03-26 19:38:12 +08:00
private IBrush _brush = null!;
2024-11-15 01:00:59 +08:00
2023-02-13 00:14:56 +08:00
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
}
2024-11-15 01:00:59 +08:00
2023-03-26 19:38:12 +08:00
private IBrush _textBrush = null!;
2024-11-15 01:00:59 +08:00
2023-02-13 00:14:56 +08:00
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-03-26 19:38:12 +08:00
private string _colorDisplayName = null!;
2024-11-15 01:00:59 +08:00
2023-02-13 00:14:56 +08:00
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
}
2023-03-26 19:38:12 +08:00
private string _resourceKey = null!;
2023-02-11 23:04:07 +08:00
public string ResourceKey
{
get => _resourceKey;
set => SetProperty(ref _resourceKey, value);
}
2023-03-27 11:25:47 +08:00
private string _colorResourceKey = null!;
public string ColorResourceKey
{
get => _colorResourceKey;
set => SetProperty(ref _colorResourceKey, value);
}
2023-03-26 19:38:12 +08:00
private string _hex = null!;
public string Hex
{
get => _hex;
set => SetProperty(ref _hex, value);
}
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;
2023-02-13 00:14:56 +08:00
Hex = brush.ToString().ToUpperInvariant();
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
}
public class FunctionalColorGroupViewModel : ObservableObject
{
2023-03-26 19:38:12 +08:00
private string _title = null!;
2024-11-15 01:00:59 +08:00
2023-02-13 02:17:38 +08:00
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
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));
}
}
}
}
}
2024-11-15 02:20:32 +08:00
public class ShadowItemViewModel : ObservableObject
{
private string _shadowDisplayName = null!;
public string ShadowDisplayName
{
get => _shadowDisplayName;
set => SetProperty(ref _shadowDisplayName, value);
}
private string _resourceKey = null!;
public string ResourceKey
{
get => _resourceKey;
set => SetProperty(ref _resourceKey, value);
}
private string _boxShadowValue = null!;
public string BoxShadowValue
{
get => _boxShadowValue;
set => SetProperty(ref _boxShadowValue, value);
}
public ShadowItemViewModel(string shadowDisplayName, BoxShadows boxShadows, string resourceKey)
{
ShadowDisplayName = shadowDisplayName;
ResourceKey = resourceKey;
BoxShadowValue = boxShadows.ToString();
}
}
public class ShadowGroupViewModel : ObservableObject
{
private string _title = null!;
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
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-13 02:17:38 +08:00
public static class ColorTokens
{
public static IReadOnlyList<Tuple<string, string>> PrimaryTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorPrimary", "Primary"),
new("SemiColorPrimaryPointerover", "Primary Pointerover"),
new("SemiColorPrimaryActive", "Primary Active"),
new("SemiColorPrimaryDisabled", "Primary Disabled"),
new("SemiColorPrimaryLight", "Primary Light"),
new("SemiColorPrimaryLightPointerover", "Primary Light Pointerover"),
new("SemiColorPrimaryLightActive", "Primary Light Active"),
2023-02-13 02:17:38 +08:00
};
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> SecondaryTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorSecondary", "Secondary"),
new("SemiColorSecondaryPointerover", "Secondary Pointerover"),
new("SemiColorSecondaryActive", "Secondary Active"),
new("SemiColorSecondaryDisabled", "Secondary Disabled"),
new("SemiColorSecondaryLight", "Secondary Light"),
new("SemiColorSecondaryLightPointerover", "Secondary Light Pointerover"),
new("SemiColorSecondaryLightActive", "Secondary Light Active"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> TertiaryTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorTertiary", "Tertiary"),
new("SemiColorTertiaryPointerover", "Tertiary Pointerover"),
new("SemiColorTertiaryActive", "Tertiary Active"),
new("SemiColorTertiaryLight", "Tertiary Light"),
new("SemiColorTertiaryLightPointerover", "Tertiary Light Pointerover"),
new("SemiColorTertiaryLightActive", "Tertiary Light Active"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> InformationTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorInformation", "Information"),
new("SemiColorInformationPointerover", "Information Pointerover"),
new("SemiColorInformationActive", "Information Active"),
new("SemiColorInformationDisabled", "Information Disabled"),
new("SemiColorInformationLight", "Information Light"),
new("SemiColorInformationLightPointerover", "Information Light Pointerover"),
new("SemiColorInformationLightActive", "Information Light Active"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> SuccessTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorSuccess", "Success"),
new("SemiColorSuccessPointerover", "Success Pointerover"),
new("SemiColorSuccessActive", "Success Active"),
new("SemiColorSuccessDisabled", "Success Disabled"),
new("SemiColorSuccessLight", "Success Light"),
new("SemiColorSuccessLightPointerover", "Success Light Pointerover"),
new("SemiColorSuccessLightActive", "Success Light Active"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> WarningTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorWarning", "Warning"),
new("SemiColorWarningPointerover", "Warning Pointerover"),
new("SemiColorWarningActive", "Warning Active"),
new("SemiColorWarningLight", "Warning Light"),
new("SemiColorWarningLightPointerover", "Warning Light Pointerover"),
new("SemiColorWarningLightActive", "Warning Light Active"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> DangerTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorDanger", "Danger"),
new("SemiColorDangerPointerover", "Danger Pointerover"),
new("SemiColorDangerActive", "Danger Active"),
new("SemiColorDangerLight", "Danger Light"),
new("SemiColorDangerLightPointerover", "Danger Light Pointerover"),
new("SemiColorDangerLightActive", "Danger Light Active"),
2023-02-13 02:55:29 +08:00
};
public static IReadOnlyList<Tuple<string, string>> TextTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorText0", "Text 0"),
new("SemiColorText1", "Text 1"),
new("SemiColorText2", "Text 2"),
new("SemiColorText3", "Text 3"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> LinkTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorLink", "Link"),
new("SemiColorLinkPointerover", "Link Pointerover"),
new("SemiColorLinkActive", "Link Active"),
new("SemiColorLinkVisited", "Link Visited"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> BackgroundTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorBackground0", "Background 0"),
new("SemiColorBackground1", "Background 1"),
new("SemiColorBackground2", "Background 2"),
new("SemiColorBackground3", "Background 3"),
new("SemiColorBackground4", "Background 4"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> FillTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorFill0", "Fill 0"),
new("SemiColorFill1", "Fill 1"),
new("SemiColorFill2", "Fill 2"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> BorderTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorBorder", "Border"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 01:00:59 +08:00
2023-02-13 02:55:29 +08:00
public static IReadOnlyList<Tuple<string, string>> DisabledTokens { get; } = new List<Tuple<string, string>>
{
2024-11-15 01:00:59 +08:00
new("SemiColorDisabledText", "Disabled Text"),
new("SemiColorDisabledBorder", "Disabled Border"),
new("SemiColorDisabledBackground", "Disabled Background"),
new("SemiColorDisabledFill", "Disabled Fill"),
2023-02-13 02:55:29 +08:00
};
2024-11-15 02:20:32 +08:00
public static IReadOnlyList<Tuple<string, string>> ShadowTokens { get; } = new List<Tuple<string, string>>
{
new("SemiColorShadow", "Shadow"),
new("SemiShadowElevated", "Shadow Elevated"),
};
2023-02-11 15:50:39 +08:00
}