mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-03-20 08:26:35 +08:00
feat: Add ColorItemControl and color detail preview.
This commit is contained in:
@@ -4,30 +4,61 @@ using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Media;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Semi.Avalonia.Demo.ViewModels;
|
||||
|
||||
public class PaletteDemoViewModel: ObservableObject
|
||||
{
|
||||
private string[] _colors = { "Amber","Blue","Cyan","Green","Grey","Indigo","LightBlue","LightGreen","Lime","Orange","Pink","Purple","Red","Teal","Violet","Yellow" };
|
||||
private ObservableCollection<ColorSeries> _series;
|
||||
private ObservableCollection<ColorSeries> _lightSeries;
|
||||
|
||||
public ObservableCollection<ColorSeries> Series
|
||||
private ColorItemViewModel _selectedColor;
|
||||
|
||||
public ColorItemViewModel SelectedColor
|
||||
{
|
||||
get => _series;
|
||||
set => SetProperty(ref _series, value);
|
||||
get => _selectedColor;
|
||||
set => SetProperty(ref _selectedColor, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<ColorSeries> LightSeries
|
||||
{
|
||||
get => _lightSeries;
|
||||
set => SetProperty(ref _lightSeries, value);
|
||||
}
|
||||
|
||||
private ObservableCollection<ColorSeries> _darkSeries;
|
||||
|
||||
public ObservableCollection<ColorSeries> DarkSeries
|
||||
{
|
||||
get => _darkSeries;
|
||||
set => SetProperty(ref _darkSeries, value);
|
||||
}
|
||||
|
||||
public PaletteDemoViewModel()
|
||||
{
|
||||
Series = new ObservableCollection<ColorSeries>();
|
||||
var resouceDictionary = (ResourceDictionary)(AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml")));
|
||||
LightSeries = new ObservableCollection<ColorSeries>();
|
||||
var lightResourceDictionary = (ResourceDictionary)(AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml")));
|
||||
foreach (var color in _colors)
|
||||
{
|
||||
ColorSeries s = new ColorSeries();
|
||||
s.Initialize(resouceDictionary, color);
|
||||
Series.Add(s);
|
||||
s.Initialize(lightResourceDictionary, color, true);
|
||||
LightSeries.Add(s);
|
||||
}
|
||||
DarkSeries = new ObservableCollection<ColorSeries>();
|
||||
var darkResouceDictionary = (ResourceDictionary)(AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Dark/Palette.axaml")));
|
||||
foreach (var color in _colors)
|
||||
{
|
||||
ColorSeries s = new ColorSeries();
|
||||
s.Initialize(darkResouceDictionary, color, false);
|
||||
DarkSeries.Add(s);
|
||||
}
|
||||
WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem);
|
||||
}
|
||||
|
||||
private void OnClickColorItem(PaletteDemoViewModel vm, ColorItemViewModel item)
|
||||
{
|
||||
SelectedColor = item;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +80,7 @@ public class ColorSeries: ObservableObject
|
||||
set => SetProperty(ref _seriesName, value);
|
||||
}
|
||||
|
||||
internal void Initialize(IResourceDictionary resourceDictionary, string color)
|
||||
internal void Initialize(IResourceDictionary resourceDictionary, string color, bool light)
|
||||
{
|
||||
SeriesName = color;
|
||||
Color = new ObservableCollection<ColorItemViewModel>();
|
||||
@@ -62,7 +93,7 @@ public class ColorSeries: ObservableObject
|
||||
if (value is SolidColorBrush brush)
|
||||
{
|
||||
string name = color + " " + i;
|
||||
var item = new ColorItemViewModel(name, brush, key);
|
||||
var item = new ColorItemViewModel(name, brush, key, light, i);
|
||||
Color.Add(item);
|
||||
}
|
||||
}
|
||||
@@ -79,6 +110,13 @@ public class ColorItemViewModel : ObservableObject
|
||||
get => _color;
|
||||
set => SetProperty(ref _color, value);
|
||||
}
|
||||
|
||||
private IBrush _textColor;
|
||||
public IBrush TextColor
|
||||
{
|
||||
get => _textColor;
|
||||
set => SetProperty(ref _textColor, value);
|
||||
}
|
||||
|
||||
private string _name;
|
||||
public string Name
|
||||
@@ -94,11 +132,28 @@ public class ColorItemViewModel : ObservableObject
|
||||
get => _resourceKey;
|
||||
set => SetProperty(ref _resourceKey, value);
|
||||
}
|
||||
|
||||
private string _hex;
|
||||
|
||||
public string Hex
|
||||
{
|
||||
get => _hex;
|
||||
set => SetProperty(ref _hex, value);
|
||||
}
|
||||
|
||||
public ColorItemViewModel(string name, IBrush color, string resourceKey)
|
||||
public ColorItemViewModel(string name, IBrush color, string resourceKey, bool light, int index)
|
||||
{
|
||||
Name = name;
|
||||
Color = color;
|
||||
ResourceKey = resourceKey;
|
||||
Hex = color.ToString().ToUpperInvariant();
|
||||
if ((light && index < 5) || (!light && index > 5))
|
||||
{
|
||||
TextColor = Brushes.Black;
|
||||
}
|
||||
else
|
||||
{
|
||||
TextColor = Brushes.White;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user