From 329e041ced1f78529515bf791a765a49ec5fc2e9 Mon Sep 17 00:00:00 2001
From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com>
Date: Tue, 24 Dec 2024 16:39:57 +0800
Subject: [PATCH] feat: add ColorDetail panel.
---
.../Controls/ColorDetailControl.axaml | 7 +-
.../Controls/ColorDetailControl.cs | 54 +-
.../Controls/ColorItemControl.axaml | 20 +-
.../Controls/ColorItemControl.cs | 17 +-
.../Pages/HighContrastDemo.axaml | 535 ++++++++++--------
.../ViewModels/HighContrastDemoViewModel.cs | 15 +
6 files changed, 366 insertions(+), 282 deletions(-)
diff --git a/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.axaml b/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.axaml
index 4dc7d42..038906c 100644
--- a/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.axaml
+++ b/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.axaml
@@ -1,13 +1,10 @@
+ xmlns:controls="clr-namespace:Semi.Avalonia.Demo.Controls">
M5 7C3.89543 7 3 7.89543 3 9V19C3 20.1046 3.89543 21 5 21H15C16.1046 21 17 20.1046 17 19V9C17 7.89543 16.1046 7 15 7H5Z,M7 4C7 2.89543 7.89543 2 9 2H20C21.1046 2 22 2.89543 22 4V15C22 16.1046 21.1046 17 20 17H19V8C19 6 18 5 16 5H7V4Z
-
+
ResourceKeyProperty = AvaloniaProperty.Register(
- nameof(ResourceKey));
+
+ public static readonly StyledProperty ResourceKeyProperty =
+ AvaloniaProperty.Register(nameof(ResourceKey));
+
public string? ResourceKey
{
get => GetValue(ResourceKeyProperty);
set => SetValue(ResourceKeyProperty, value);
}
- public static readonly StyledProperty ResourceNameProperty = AvaloniaProperty.Register(
- nameof(ResourceName));
+ public static readonly StyledProperty ResourceNameProperty =
+ AvaloniaProperty.Register(nameof(ResourceName));
public string? ResourceName
{
@@ -32,8 +31,8 @@ public class ColorDetailControl: TemplatedControl
set => SetValue(ResourceNameProperty, value);
}
- public static readonly StyledProperty ColorResourceKeyProperty = AvaloniaProperty.Register(
- nameof(ColorResourceKey));
+ public static readonly StyledProperty ColorResourceKeyProperty =
+ AvaloniaProperty.Register(nameof(ColorResourceKey));
public string? ColorResourceKey
{
@@ -41,27 +40,28 @@ public class ColorDetailControl: TemplatedControl
set => SetValue(ColorResourceKeyProperty, value);
}
- public static readonly DirectProperty HexProperty = AvaloniaProperty.RegisterDirect(
- nameof(Hex), o => o.Hex);
+ public static readonly DirectProperty HexProperty =
+ AvaloniaProperty.RegisterDirect(nameof(Hex), o => o.Hex);
+
private string? _hex;
+
public string? Hex
{
get => _hex;
private set => SetAndRaise(HexProperty, ref _hex, value);
}
-
- public static readonly DirectProperty OpacityNumberProperty = AvaloniaProperty.RegisterDirect(
- nameof(OpacityNumber), o => o.OpacityNumber);
+
+ public static readonly DirectProperty OpacityNumberProperty =
+ AvaloniaProperty.RegisterDirect(nameof(OpacityNumber), o => o.OpacityNumber);
+
private string? _opacityNumber;
+
public string? OpacityNumber
{
get => _opacityNumber;
private set => SetAndRaise(OpacityNumberProperty, ref _opacityNumber, value);
}
-
-
-
-
+
static ColorDetailControl()
{
BackgroundProperty.Changed.AddClassHandler((o, e) => o.OnBackgroundChanged(e));
@@ -84,13 +84,17 @@ public class ColorDetailControl: TemplatedControl
{
switch (s)
{
- case KEY_ResourceKey: text = ResourceKey;
+ case KEY_ResourceKey:
+ text = ResourceKey;
break;
- case KEY_Hex: text = Hex;
+ case KEY_Hex:
+ text = Hex;
break;
- case KEY_Opacity: text = OpacityNumber;
+ case KEY_Opacity:
+ text = OpacityNumber;
break;
- case KEY_ColorResourceKey: text = ColorResourceKey;
+ case KEY_ColorResourceKey:
+ text = ColorResourceKey;
break;
default: text = string.Empty; break;
}
@@ -99,9 +103,7 @@ public class ColorDetailControl: TemplatedControl
var toplevel = TopLevel.GetTopLevel(this);
if (toplevel?.Clipboard is { } c)
{
- await c.SetTextAsync(text??string.Empty);
+ await c.SetTextAsync(text ?? string.Empty);
}
-
}
-
}
\ No newline at end of file
diff --git a/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.axaml b/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.axaml
index 6e2cd4e..7169196 100644
--- a/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.axaml
+++ b/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.axaml
@@ -1,25 +1,23 @@
+ xmlns:controls="using:Semi.Avalonia.Demo.Controls">
-
-
-
-
+
+
+
+
-
-
+
\ No newline at end of file
diff --git a/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.cs b/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.cs
index 04c8000..b73cf5e 100644
--- a/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.cs
+++ b/demo/Semi.Avalonia.Demo/Controls/ColorItemControl.cs
@@ -1,5 +1,4 @@
using Avalonia;
-using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using CommunityToolkit.Mvvm.Messaging;
@@ -9,8 +8,8 @@ namespace Semi.Avalonia.Demo.Controls;
public class ColorItemControl : TemplatedControl
{
- public static readonly StyledProperty ColorNameProperty = AvaloniaProperty.Register(
- nameof(ColorName));
+ public static readonly StyledProperty ColorNameProperty =
+ AvaloniaProperty.Register(nameof(ColorName));
public string? ColorName
{
@@ -26,14 +25,18 @@ public class ColorItemControl : TemplatedControl
get => GetValue(HexProperty);
set => SetValue(HexProperty, value);
}
-
+
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
- if (this.DataContext is ColorItemViewModel v)
+ switch (this.DataContext)
{
- WeakReferenceMessenger.Default.Send(v);
+ case ColorItemViewModel colorItemViewModel:
+ WeakReferenceMessenger.Default.Send(colorItemViewModel);
+ break;
+ case ColorResource colorResource:
+ WeakReferenceMessenger.Default.Send(colorResource);
+ break;
}
-
}
}
\ No newline at end of file
diff --git a/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml b/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml
index 630b0b8..4639ee9 100644
--- a/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml
+++ b/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml
@@ -3,249 +3,318 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Semi.Avalonia.Demo.ViewModels"
+ xmlns:controls="clr-namespace:Semi.Avalonia.Demo.Controls"
mc:Ignorable="d" d:DesignWidth="1000" d:DesignHeight="1450"
x:DataType="vm:HighContrastDemoViewModel"
x:Class="Semi.Avalonia.Demo.Pages.HighContrastDemo">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/demo/Semi.Avalonia.Demo/ViewModels/HighContrastDemoViewModel.cs b/demo/Semi.Avalonia.Demo/ViewModels/HighContrastDemoViewModel.cs
index df80437..968aefa 100644
--- a/demo/Semi.Avalonia.Demo/ViewModels/HighContrastDemoViewModel.cs
+++ b/demo/Semi.Avalonia.Demo/ViewModels/HighContrastDemoViewModel.cs
@@ -6,12 +6,15 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Media;
using Avalonia.Styling;
using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Messaging;
namespace Semi.Avalonia.Demo.ViewModels;
public partial class HighContrastDemoViewModel : ObservableObject
{
[ObservableProperty] private ThemeVariant? _selectedThemeVariant;
+ [ObservableProperty] private ColorResource _selectedColorResource = null!;
+
public IEnumerable ThemeVariants { get; }
public ObservableCollection ColorResources { get; set; }
@@ -30,6 +33,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "WindowColor",
Brush = new SolidColorBrush(Color.Parse("#202020")),
+ Hex = "#FF202020",
Description = "Background of pages, panes, popups, and windows.",
PairWith = "WindowTextColor"
},
@@ -37,6 +41,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "WindowTextColor",
Brush = new SolidColorBrush(Color.Parse("#FFFFFF")),
+ Hex = "WHITE",
Description = "Headings, body copy, lists, placeholder text, app and window borders.",
PairWith = "WindowColor"
},
@@ -44,6 +49,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "HotlightColor",
Brush = new SolidColorBrush(Color.Parse("#75E9FC")),
+ Hex = "#FF75E9FC",
Description = "Hyperlinks.",
PairWith = "WindowColor"
},
@@ -51,6 +57,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "GrayTextColor",
Brush = new SolidColorBrush(Color.Parse("#A6A6A6")),
+ Hex = "#FFA6A6A6",
Description = "Inactive (disabled) UI.",
PairWith = "WindowColor"
},
@@ -58,6 +65,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "HighlightTextColor",
Brush = new SolidColorBrush(Color.Parse("#263B50")),
+ Hex = "#FF263B50",
Description =
"Foreground color for text or UI that is in selected, interacted with (hover, pressed), or in progress.",
PairWith = "HighlightColor"
@@ -66,6 +74,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "HighlightColor",
Brush = new SolidColorBrush(Color.Parse("#8EE3F0")),
+ Hex = "#FF8EE3F0",
Description =
"Background or accent color for UI that is in selected, interacted with (hover, pressed), or in progress.",
PairWith = "HighlightTextColor"
@@ -74,6 +83,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "ButtonTextColor",
Brush = new SolidColorBrush(Color.Parse("#FFFFFF")),
+ Hex = "WHITE",
Description = "Foreground color for buttons and any UI that can be interacted with.",
PairWith = "ButtonFaceColor"
},
@@ -81,10 +91,13 @@ public partial class HighContrastDemoViewModel : ObservableObject
{
ResourceKey = "ButtonFaceColor",
Brush = new SolidColorBrush(Color.Parse("#202020")),
+ Hex = "#FF202020",
Description = "Background color for buttons and any UI that can be interacted with.",
PairWith = "ButtonTextColor"
},
];
+ WeakReferenceMessenger.Default.Register
+ (this, (_, item) => SelectedColorResource = item);
SelectedThemeVariant = SemiTheme.Aquatic;
}
@@ -98,6 +111,7 @@ public partial class HighContrastDemoViewModel : ObservableObject
if (topLevel?.TryFindResource(colorResource.ResourceKey, value, out var o) == true && o is Color color)
{
colorResource.Brush = new SolidColorBrush(color);
+ colorResource.Hex = color.ToString().ToUpperInvariant();
}
}
}
@@ -117,6 +131,7 @@ public partial class ColorResource : ObservableObject
{
[ObservableProperty] private string? _resourceKey;
[ObservableProperty] private SolidColorBrush? _brush;
+ [ObservableProperty] private string? _hex;
[ObservableProperty] private string? _description;
[ObservableProperty] private string? _pairWith;
}
\ No newline at end of file