From c44b9a01733e86f78a2d9d071b05614d0bb1b23f Mon Sep 17 00:00:00 2001 From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com> Date: Mon, 30 Dec 2024 17:53:44 +0800 Subject: [PATCH 1/4] fix: fix debug crash issue. --- demo/Semi.Avalonia.Demo/Themes/ToggleSwitch.axaml | 4 ++-- demo/Semi.Avalonia.Demo/Views/MainView.axaml | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/demo/Semi.Avalonia.Demo/Themes/ToggleSwitch.axaml b/demo/Semi.Avalonia.Demo/Themes/ToggleSwitch.axaml index a9d085c..1892108 100644 --- a/demo/Semi.Avalonia.Demo/Themes/ToggleSwitch.axaml +++ b/demo/Semi.Avalonia.Demo/Themes/ToggleSwitch.axaml @@ -25,7 +25,7 @@ - + + \ No newline at end of file diff --git a/demo/Semi.Avalonia.Demo/Views/MainView.axaml b/demo/Semi.Avalonia.Demo/Views/MainView.axaml index 3d35910..e0ce3e4 100644 --- a/demo/Semi.Avalonia.Demo/Views/MainView.axaml +++ b/demo/Semi.Avalonia.Demo/Views/MainView.axaml @@ -50,10 +50,6 @@ - @@ -68,6 +64,8 @@ From ef8531d25d85ad65d116567d5233854b78047a67 Mon Sep 17 00:00:00 2001 From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com> Date: Tue, 31 Dec 2024 13:20:23 +0800 Subject: [PATCH 2/4] misc: rearrange demo theme resources. --- demo/Semi.Avalonia.Demo/App.axaml | 9 ++++++++- demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml | 9 --------- demo/Semi.Avalonia.Demo/Pages/PaletteDemo.axaml | 11 ----------- .../{Controls => Themes}/ColorDetailControl.axaml | 0 .../{Controls => Themes}/ColorItemControl.axaml | 0 .../FunctionalColorGroupControl.axaml | 0 .../{Controls => Themes}/ShadowGroupControl.axaml | 0 demo/Semi.Avalonia.Demo/Themes/_index.axaml | 10 ++++++++++ demo/Semi.Avalonia.Demo/Views/MainView.axaml | 8 -------- 9 files changed, 18 insertions(+), 29 deletions(-) rename demo/Semi.Avalonia.Demo/{Controls => Themes}/ColorDetailControl.axaml (100%) rename demo/Semi.Avalonia.Demo/{Controls => Themes}/ColorItemControl.axaml (100%) rename demo/Semi.Avalonia.Demo/{Controls => Themes}/FunctionalColorGroupControl.axaml (100%) rename demo/Semi.Avalonia.Demo/{Controls => Themes}/ShadowGroupControl.axaml (100%) create mode 100644 demo/Semi.Avalonia.Demo/Themes/_index.axaml diff --git a/demo/Semi.Avalonia.Demo/App.axaml b/demo/Semi.Avalonia.Demo/App.axaml index 3de4aaa..a4b39ab 100644 --- a/demo/Semi.Avalonia.Demo/App.axaml +++ b/demo/Semi.Avalonia.Demo/App.axaml @@ -7,8 +7,15 @@ - + + + + + + + + \ 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 55e815e..9b236df 100644 --- a/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml +++ b/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml @@ -11,15 +11,6 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + \ No newline at end of file diff --git a/demo/Semi.Avalonia.Demo/Views/MainView.axaml b/demo/Semi.Avalonia.Demo/Views/MainView.axaml index e0ce3e4..df08ebc 100644 --- a/demo/Semi.Avalonia.Demo/Views/MainView.axaml +++ b/demo/Semi.Avalonia.Demo/Views/MainView.axaml @@ -11,14 +11,6 @@ x:CompileBindings="True" x:DataType="views:MainViewModel" mc:Ignorable="d"> - - - - - - - - Date: Sun, 29 Dec 2024 23:04:46 +0800 Subject: [PATCH 3/4] feat: Color.ToString() -> real Hex. --- .../Controls/ColorDetailControl.cs | 48 +++++++++++-------- .../Converters/ColorConverter.cs | 22 +++++++++ .../Pages/HighContrastDemo.axaml | 4 +- .../Themes/ColorDetailControl.axaml | 38 +++++++++++---- .../ViewModels/HighContrastDemoViewModel.cs | 10 ---- .../ViewModels/PaletteDemoViewModel.cs | 5 +- 6 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs diff --git a/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs b/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs index dcbfa71..4a12060 100644 --- a/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs +++ b/demo/Semi.Avalonia.Demo/Controls/ColorDetailControl.cs @@ -1,8 +1,10 @@ using System.Globalization; +using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Media; +using Semi.Avalonia.Demo.Converters; namespace Semi.Avalonia.Demo.Controls; @@ -10,6 +12,7 @@ public class ColorDetailControl : TemplatedControl { public const string KEY_ResourceKey = "ResourceKey"; public const string KEY_Hex = "Hex"; + public const string KEY_Hex2 = "Hex2"; public const string KEY_Opacity = "Opacity"; public const string KEY_ColorResourceKey = "ColorResourceKey"; @@ -51,6 +54,17 @@ public class ColorDetailControl : TemplatedControl private set => SetAndRaise(HexProperty, ref _hex, value); } + private string? _hex2; + + public static readonly DirectProperty Hex2Property = + AvaloniaProperty.RegisterDirect(nameof(Hex2), o => o.Hex2); + + public string? Hex2 + { + get => _hex2; + set => SetAndRaise(Hex2Property, ref _hex2, value); + } + public static readonly DirectProperty OpacityNumberProperty = AvaloniaProperty.RegisterDirect(nameof(OpacityNumber), o => o.OpacityNumber); @@ -70,34 +84,30 @@ public class ColorDetailControl : TemplatedControl private void OnBackgroundChanged(AvaloniaPropertyChangedEventArgs args) { var color = args.GetNewValue(); - if (color is ISolidColorBrush b) + if (color is ISolidColorBrush brush) { - Hex = b.Color.ToString().ToUpperInvariant(); - OpacityNumber = b.Opacity.ToString(CultureInfo.InvariantCulture); + var hex1 = ColorConverter.ToHex.Convert(brush.Color, typeof(string), false, CultureInfo.InvariantCulture); + var hex2 = ColorConverter.ToHex.Convert(brush.Color, typeof(string), true, CultureInfo.InvariantCulture); + Hex = hex1 as string; + Hex2 = hex2 as string; + OpacityNumber = brush.Opacity.ToString(CultureInfo.InvariantCulture); } } - public async void Copy(object o) + public async Task Copy(object o) { string? text = null; if (o is string s) { - switch (s) + text = s switch { - case KEY_ResourceKey: - text = ResourceKey; - break; - case KEY_Hex: - text = Hex; - break; - case KEY_Opacity: - text = OpacityNumber; - break; - case KEY_ColorResourceKey: - text = ColorResourceKey; - break; - default: text = string.Empty; break; - } + KEY_ResourceKey => ResourceKey, + KEY_Hex => Hex, + KEY_Hex2 => Hex2, + KEY_Opacity => OpacityNumber, + KEY_ColorResourceKey => ColorResourceKey, + _ => string.Empty + }; } var toplevel = TopLevel.GetTopLevel(this); diff --git a/demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs b/demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs new file mode 100644 index 0000000..6b917f1 --- /dev/null +++ b/demo/Semi.Avalonia.Demo/Converters/ColorConverter.cs @@ -0,0 +1,22 @@ +using Avalonia.Data.Converters; +using Avalonia.Media; + +namespace Semi.Avalonia.Demo.Converters; + +public static class ColorConverter +{ + public static readonly IValueConverter ToHex = + new FuncValueConverter( + (obj, withAlpha) => + obj switch + { + Color color => withAlpha + ? $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}" + : $"#{color.R:X2}{color.G:X2}{color.B:X2}", + ISolidColorBrush brush => withAlpha + ? $"#{brush.Color.A:X2}{brush.Color.R:X2}{brush.Color.G:X2}{brush.Color.B:X2}" + : $"#{brush.Color.R:X2}{brush.Color.G:X2}{brush.Color.B:X2}", + _ => string.Empty + } + ); +} \ 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 9b236df..099b0b5 100644 --- a/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml +++ b/demo/Semi.Avalonia.Demo/Pages/HighContrastDemo.axaml @@ -4,6 +4,7 @@ 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" + xmlns:cvt="clr-namespace:Semi.Avalonia.Demo.Converters" mc:Ignorable="d" d:DesignWidth="1000" d:DesignHeight="1450" x:DataType="vm:HighContrastDemoViewModel" x:CompileBindings="True" @@ -277,7 +278,8 @@ + Text="{Binding Brush, + Converter={x:Static cvt:ColorConverter.ToHex},ConverterParameter={x:False}}" /> diff --git a/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml b/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml index ccfda64..17626a3 100644 --- a/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml +++ b/demo/Semi.Avalonia.Demo/Themes/ColorDetailControl.axaml @@ -16,7 +16,7 @@ HorizontalAlignment="Stretch" Background="{TemplateBinding Background}" CornerRadius="6" /> - + - + + Text="Hex" /> - - - + + + +