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

359 lines
15 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;
2024-12-26 01:42:18 +08:00
using Semi.Avalonia.Tokens.Palette;
2024-12-29 23:04:46 +08:00
using Semi.Avalonia.Demo.Converters;
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));
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 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 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-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
}