misc: format codes.

(cherry picked from commit d1d235a1200b589b3dec7e1cf2c7adbf465adc85)
This commit is contained in:
Zhang Dian 2024-11-15 01:00:59 +08:00
parent c87f6a8112
commit ebdb0b1b93
2 changed files with 129 additions and 116 deletions

View File

@ -4,33 +4,38 @@ using Avalonia.Controls.Primitives;
namespace Semi.Avalonia.Demo.Controls; namespace Semi.Avalonia.Demo.Controls;
public class FunctionalColorGroupControl: TemplatedControl public class FunctionalColorGroupControl : TemplatedControl
{ {
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<FunctionalColorGroupControl, string?>( public static readonly StyledProperty<string?> TitleProperty =
nameof(Title)); AvaloniaProperty.Register<FunctionalColorGroupControl, string?>(nameof(Title));
public string? Title public string? Title
{ {
get => GetValue(TitleProperty); get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value); set => SetValue(TitleProperty, value);
} }
public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable?> LightColorsProperty = AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable?>( public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable?> LightColorsProperty =
nameof(LightColors), o => o.LightColors, (o, v) => o.LightColors = v); AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable?>(nameof(LightColors),
o => o.LightColors, (o, v) => o.LightColors = v);
private IEnumerable? _lightColors; private IEnumerable? _lightColors;
public IEnumerable? LightColors public IEnumerable? LightColors
{ {
get => _lightColors; get => _lightColors;
set => SetAndRaise(LightColorsProperty, ref _lightColors, value); set => SetAndRaise(LightColorsProperty, ref _lightColors, value);
} }
public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable?> DarkColorsProperty = AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable?>( public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable?> DarkColorsProperty =
nameof(DarkColors), o => o.DarkColors, (o, v) => o.DarkColors = v); AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable?>(nameof(DarkColors),
o => o.DarkColors, (o, v) => o.DarkColors = v);
private IEnumerable? _darkColors; private IEnumerable? _darkColors;
public IEnumerable? DarkColors public IEnumerable? DarkColors
{ {
get => _darkColors; get => _darkColors;
set => SetAndRaise(DarkColorsProperty, ref _darkColors, value); set => SetAndRaise(DarkColorsProperty, ref _darkColors, value);
} }
} }

View File

@ -2,25 +2,25 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media; using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging;
namespace Semi.Avalonia.Demo.ViewModels; namespace Semi.Avalonia.Demo.ViewModels;
public class PaletteDemoViewModel: ObservableObject public class PaletteDemoViewModel : ObservableObject
{ {
private readonly string[] _predefinedColorNames = private readonly string[] _predefinedColorNames =
{ [
"Red", "Pink", "Purple", "Violet", "Indigo", "Red", "Pink", "Purple", "Violet", "Indigo",
"Blue", "LightBlue", "Cyan", "Teal", "Green", "Blue", "LightBlue", "Cyan", "Teal", "Green",
"LightGreen", "Lime", "Yellow", "Amber", "Orange", "LightGreen", "Lime", "Yellow", "Amber", "Orange",
"Grey" "Grey"
}; ];
private readonly IResourceDictionary? _lightResourceDictionary; private readonly IResourceDictionary? _lightResourceDictionary;
private readonly IResourceDictionary? _darkResourceDictionary; private readonly IResourceDictionary? _darkResourceDictionary;
private ColorItemViewModel _selectedColor = null!; private ColorItemViewModel _selectedColor = null!;
public ColorItemViewModel SelectedColor public ColorItemViewModel SelectedColor
@ -28,26 +28,29 @@ public class PaletteDemoViewModel: ObservableObject
get => _selectedColor; get => _selectedColor;
set => SetProperty(ref _selectedColor, value); set => SetProperty(ref _selectedColor, value);
} }
private ObservableCollection<ColorListViewModel>? _lightLists; private ObservableCollection<ColorListViewModel>? _lightLists;
public ObservableCollection<ColorListViewModel>? LightLists public ObservableCollection<ColorListViewModel>? LightLists
{ {
get => _lightLists; get => _lightLists;
set => SetProperty(ref _lightLists, value); set => SetProperty(ref _lightLists, value);
} }
private ObservableCollection<ColorListViewModel>? _darkLists; private ObservableCollection<ColorListViewModel>? _darkLists;
public ObservableCollection<ColorListViewModel>? DarkLists public ObservableCollection<ColorListViewModel>? DarkLists
{ {
get => _darkLists; get => _darkLists;
set => SetProperty(ref _darkLists, value); set => SetProperty(ref _darkLists, value);
} }
public ObservableCollection<FunctionalColorGroupViewModel> FunctionalColors { get; set; } = new(); public ObservableCollection<FunctionalColorGroupViewModel> FunctionalColors { get; set; } = [];
public PaletteDemoViewModel() public PaletteDemoViewModel()
{ {
_lightResourceDictionary = new Light.Palette(); _lightResourceDictionary = new Light.Palette();
_darkResourceDictionary = new Dark.Palette(); _darkResourceDictionary = new Dark.Palette();
WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem); WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem);
} }
@ -60,14 +63,15 @@ public class PaletteDemoViewModel: ObservableObject
private void InitializePalette() private void InitializePalette()
{ {
LightLists = new ObservableCollection<ColorListViewModel>(); LightLists = [];
foreach (var color in _predefinedColorNames) foreach (var color in _predefinedColorNames)
{ {
ColorListViewModel s = new ColorListViewModel(); ColorListViewModel s = new ColorListViewModel();
s.Initialize(_lightResourceDictionary, color, true); s.Initialize(_lightResourceDictionary, color, true);
LightLists.Add(s); LightLists.Add(s);
} }
DarkLists = new ObservableCollection<ColorListViewModel>();
DarkLists = [];
foreach (var color in _predefinedColorNames) foreach (var color in _predefinedColorNames)
{ {
ColorListViewModel s = new ColorListViewModel(); ColorListViewModel s = new ColorListViewModel();
@ -92,13 +96,14 @@ public class PaletteDemoViewModel: ObservableObject
FunctionalColors.Add(new FunctionalColorGroupViewModel("Border", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.BorderTokens)); FunctionalColors.Add(new FunctionalColorGroupViewModel("Border", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.BorderTokens));
FunctionalColors.Add(new FunctionalColorGroupViewModel("Disabled", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.DisabledTokens)); FunctionalColors.Add(new FunctionalColorGroupViewModel("Disabled", _lightResourceDictionary, _darkResourceDictionary, ColorTokens.DisabledTokens));
} }
private void OnClickColorItem(PaletteDemoViewModel vm, ColorItemViewModel item) private void OnClickColorItem(PaletteDemoViewModel vm, ColorItemViewModel item)
{ {
SelectedColor = item; SelectedColor = item;
} }
} }
public class ColorListViewModel: ObservableObject public class ColorListViewModel : ObservableObject
{ {
private ObservableCollection<ColorItemViewModel>? _colors; private ObservableCollection<ColorItemViewModel>? _colors;
@ -115,17 +120,18 @@ public class ColorListViewModel: ObservableObject
get => _seriesName; get => _seriesName;
set => SetProperty(ref _seriesName, value); set => SetProperty(ref _seriesName, value);
} }
internal void Initialize(IResourceDictionary? resourceDictionary, string color, bool light) internal void Initialize(IResourceDictionary? resourceDictionary, string color, bool light)
{ {
if (resourceDictionary is null) if (resourceDictionary is null)
{ {
return; return;
} }
SeriesName = color; SeriesName = color;
Color = new ObservableCollection<ColorItemViewModel>(); Color = [];
for (int i = 0; i < 10; i++) for (var i = 0; i < 10; i++)
{ {
var key = "Semi" + color + i; var key = "Semi" + color + i;
if (resourceDictionary.TryGetValue(key, out var value)) if (resourceDictionary.TryGetValue(key, out var value))
@ -136,7 +142,7 @@ public class ColorListViewModel: ObservableObject
var item = new ColorItemViewModel(name, brush, key, light, i); var item = new ColorItemViewModel(name, brush, key, light, i);
item.ColorResourceKey = item.ResourceKey + "Color"; item.ColorResourceKey = item.ResourceKey + "Color";
Color.Add(item); Color.Add(item);
} }
} }
} }
} }
@ -144,15 +150,16 @@ public class ColorListViewModel: ObservableObject
public class ColorItemViewModel : ObservableObject public class ColorItemViewModel : ObservableObject
{ {
private IBrush _brush = null!; private IBrush _brush = null!;
public IBrush Brush public IBrush Brush
{ {
get => _brush; get => _brush;
set => SetProperty(ref _brush, value); set => SetProperty(ref _brush, value);
} }
private IBrush _textBrush = null!; private IBrush _textBrush = null!;
public IBrush TextBrush public IBrush TextBrush
{ {
get => _textBrush; get => _textBrush;
@ -160,6 +167,7 @@ public class ColorItemViewModel : ObservableObject
} }
private string _colorDisplayName = null!; private string _colorDisplayName = null!;
public string ColorDisplayName public string ColorDisplayName
{ {
get => _colorDisplayName; get => _colorDisplayName;
@ -189,8 +197,9 @@ public class ColorItemViewModel : ObservableObject
get => _hex; get => _hex;
set => SetProperty(ref _hex, value); set => SetProperty(ref _hex, value);
} }
public ColorItemViewModel(string colorDisplayName, ISolidColorBrush brush, string resourceKey, bool light, int index) public ColorItemViewModel(string colorDisplayName, ISolidColorBrush brush, string resourceKey, bool light,
int index)
{ {
ColorDisplayName = colorDisplayName; ColorDisplayName = colorDisplayName;
Brush = brush; Brush = brush;
@ -210,22 +219,22 @@ public class ColorItemViewModel : ObservableObject
public class FunctionalColorGroupViewModel : ObservableObject public class FunctionalColorGroupViewModel : ObservableObject
{ {
private string _title = null!; private string _title = null!;
public string Title public string Title
{ {
get => _title; get => _title;
set => SetProperty(ref _title, value); set => SetProperty(ref _title, value);
} }
public ObservableCollection<ColorItemViewModel> LightColors { get; set; } = new(); public ObservableCollection<ColorItemViewModel> LightColors { get; set; } = [];
public ObservableCollection<ColorItemViewModel> DarkColors { get; set; } = new(); public ObservableCollection<ColorItemViewModel> DarkColors { get; set; } = [];
public FunctionalColorGroupViewModel(string title, IResourceDictionary? lightDictionary, IResourceDictionary? darkDictionary, IReadOnlyList<Tuple<string, string>> tokens) public FunctionalColorGroupViewModel(string title, IResourceDictionary? lightDictionary,
IResourceDictionary? darkDictionary, IReadOnlyList<Tuple<string, string>> tokens)
{ {
Title = title; Title = title;
foreach (var token in tokens) foreach (var (key, name) in tokens)
{ {
string key = token.Item1;
string name = token.Item2;
if (lightDictionary?.TryGetValue(key, out var lightValue) ?? false) if (lightDictionary?.TryGetValue(key, out var lightValue) ?? false)
{ {
if (lightValue is ISolidColorBrush lightBrush) if (lightValue is ISolidColorBrush lightBrush)
@ -249,121 +258,120 @@ public static class ColorTokens
{ {
public static IReadOnlyList<Tuple<string, string>> PrimaryTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> PrimaryTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorPrimary", "Primary"), new("SemiColorPrimary", "Primary"),
new ("SemiColorPrimaryPointerover", "Primary Pointerover"), new("SemiColorPrimaryPointerover", "Primary Pointerover"),
new ("SemiColorPrimaryActive", "Primary Active"), new("SemiColorPrimaryActive", "Primary Active"),
new ("SemiColorPrimaryDisabled", "Primary Disabled"), new("SemiColorPrimaryDisabled", "Primary Disabled"),
new ("SemiColorPrimaryLight", "Primary Light"), new("SemiColorPrimaryLight", "Primary Light"),
new ("SemiColorPrimaryLightPointerover", "Primary Light Pointerover"), new("SemiColorPrimaryLightPointerover", "Primary Light Pointerover"),
new ("SemiColorPrimaryLightActive", "Primary Light Active"), new("SemiColorPrimaryLightActive", "Primary Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> SecondaryTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> SecondaryTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorSecondary", "Secondary"), new("SemiColorSecondary", "Secondary"),
new ("SemiColorSecondaryPointerover", "Secondary Pointerover"), new("SemiColorSecondaryPointerover", "Secondary Pointerover"),
new ("SemiColorSecondaryActive", "Secondary Active"), new("SemiColorSecondaryActive", "Secondary Active"),
new ("SemiColorSecondaryDisabled", "Secondary Disabled"), new("SemiColorSecondaryDisabled", "Secondary Disabled"),
new ("SemiColorSecondaryLight", "Secondary Light"), new("SemiColorSecondaryLight", "Secondary Light"),
new ("SemiColorSecondaryLightPointerover", "Secondary Light Pointerover"), new("SemiColorSecondaryLightPointerover", "Secondary Light Pointerover"),
new ("SemiColorSecondaryLightActive", "Secondary Light Active"), new("SemiColorSecondaryLightActive", "Secondary Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> TertiaryTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> TertiaryTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorTertiary", "Tertiary"), new("SemiColorTertiary", "Tertiary"),
new ("SemiColorTertiaryPointerover", "Tertiary Pointerover"), new("SemiColorTertiaryPointerover", "Tertiary Pointerover"),
new ("SemiColorTertiaryActive", "Tertiary Active"), new("SemiColorTertiaryActive", "Tertiary Active"),
new ("SemiColorTertiaryLight", "Tertiary Light"), new("SemiColorTertiaryLight", "Tertiary Light"),
new ("SemiColorTertiaryLightPointerover", "Tertiary Light Pointerover"), new("SemiColorTertiaryLightPointerover", "Tertiary Light Pointerover"),
new ("SemiColorTertiaryLightActive", "Tertiary Light Active"), new("SemiColorTertiaryLightActive", "Tertiary Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> InformationTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> InformationTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorInformation", "Information"), new("SemiColorInformation", "Information"),
new ("SemiColorInformationPointerover", "Information Pointerover"), new("SemiColorInformationPointerover", "Information Pointerover"),
new ("SemiColorInformationActive", "Information Active"), new("SemiColorInformationActive", "Information Active"),
new ("SemiColorInformationDisabled", "Information Disabled"), new("SemiColorInformationDisabled", "Information Disabled"),
new ("SemiColorInformationLight", "Information Light"), new("SemiColorInformationLight", "Information Light"),
new ("SemiColorInformationLightPointerover", "Information Light Pointerover"), new("SemiColorInformationLightPointerover", "Information Light Pointerover"),
new ("SemiColorInformationLightActive", "Information Light Active"), new("SemiColorInformationLightActive", "Information Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> SuccessTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> SuccessTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorSuccess", "Success"), new("SemiColorSuccess", "Success"),
new ("SemiColorSuccessPointerover", "Success Pointerover"), new("SemiColorSuccessPointerover", "Success Pointerover"),
new ("SemiColorSuccessActive", "Success Active"), new("SemiColorSuccessActive", "Success Active"),
new ("SemiColorSuccessDisabled", "Success Disabled"), new("SemiColorSuccessDisabled", "Success Disabled"),
new ("SemiColorSuccessLight", "Success Light"), new("SemiColorSuccessLight", "Success Light"),
new ("SemiColorSuccessLightPointerover", "Success Light Pointerover"), new("SemiColorSuccessLightPointerover", "Success Light Pointerover"),
new ("SemiColorSuccessLightActive", "Success Light Active"), new("SemiColorSuccessLightActive", "Success Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> WarningTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> WarningTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorWarning", "Warning"), new("SemiColorWarning", "Warning"),
new ("SemiColorWarningPointerover", "Warning Pointerover"), new("SemiColorWarningPointerover", "Warning Pointerover"),
new ("SemiColorWarningActive", "Warning Active"), new("SemiColorWarningActive", "Warning Active"),
new ("SemiColorWarningLight", "Warning Light"), new("SemiColorWarningLight", "Warning Light"),
new ("SemiColorWarningLightPointerover", "Warning Light Pointerover"), new("SemiColorWarningLightPointerover", "Warning Light Pointerover"),
new ("SemiColorWarningLightActive", "Warning Light Active"), new("SemiColorWarningLightActive", "Warning Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> DangerTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> DangerTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorDanger", "Danger"), new("SemiColorDanger", "Danger"),
new ("SemiColorDangerPointerover", "Danger Pointerover"), new("SemiColorDangerPointerover", "Danger Pointerover"),
new ("SemiColorDangerActive", "Danger Active"), new("SemiColorDangerActive", "Danger Active"),
new ("SemiColorDangerLight", "Danger Light"), new("SemiColorDangerLight", "Danger Light"),
new ("SemiColorDangerLightPointerover", "Danger Light Pointerover"), new("SemiColorDangerLightPointerover", "Danger Light Pointerover"),
new ("SemiColorDangerLightActive", "Danger Light Active"), new("SemiColorDangerLightActive", "Danger Light Active"),
}; };
public static IReadOnlyList<Tuple<string, string>> TextTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> TextTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorText0", "Text 0"), new("SemiColorText0", "Text 0"),
new ("SemiColorText1", "Text 1"), new("SemiColorText1", "Text 1"),
new ("SemiColorText2", "Text 2"), new("SemiColorText2", "Text 2"),
new ("SemiColorText3", "Text 3"), new("SemiColorText3", "Text 3"),
}; };
public static IReadOnlyList<Tuple<string, string>> LinkTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> LinkTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorLink", "Link"), new("SemiColorLink", "Link"),
new ("SemiColorLinkPointerover", "Link Pointerover"), new("SemiColorLinkPointerover", "Link Pointerover"),
new ("SemiColorLinkActive", "Link Active"), new("SemiColorLinkActive", "Link Active"),
new ("SemiColorLinkVisited", "Link Visited"), new("SemiColorLinkVisited", "Link Visited"),
}; };
public static IReadOnlyList<Tuple<string, string>> BackgroundTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> BackgroundTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorBackground0", "Background 0"), new("SemiColorBackground0", "Background 0"),
new ("SemiColorBackground1", "Background 1"), new("SemiColorBackground1", "Background 1"),
new ("SemiColorBackground2", "Background 2"), new("SemiColorBackground2", "Background 2"),
new ("SemiColorBackground3", "Background 3"), new("SemiColorBackground3", "Background 3"),
new ("SemiColorBackground4", "Background 4"), new("SemiColorBackground4", "Background 4"),
}; };
public static IReadOnlyList<Tuple<string, string>> FillTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> FillTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorFill0", "Fill 0"), new("SemiColorFill0", "Fill 0"),
new ("SemiColorFill1", "Fill 1"), new("SemiColorFill1", "Fill 1"),
new ("SemiColorFill2", "Fill 2"), new("SemiColorFill2", "Fill 2"),
}; };
public static IReadOnlyList<Tuple<string, string>> BorderTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> BorderTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorBorder", "Border"), new("SemiColorBorder", "Border"),
}; };
public static IReadOnlyList<Tuple<string, string>> DisabledTokens { get; } = new List<Tuple<string, string>> public static IReadOnlyList<Tuple<string, string>> DisabledTokens { get; } = new List<Tuple<string, string>>
{ {
new ("SemiColorDisabledText", "Disabled Text"), new("SemiColorDisabledText", "Disabled Text"),
new ("SemiColorDisabledBorder", "Disabled Border"), new("SemiColorDisabledBorder", "Disabled Border"),
new ("SemiColorDisabledBackground", "Disabled Background"), new("SemiColorDisabledBackground", "Disabled Background"),
new ("SemiColorDisabledFill", "Disabled Fill"), new("SemiColorDisabledFill", "Disabled Fill"),
}; };
} }