From 00d64d55a030bef7c52d2db5583c912880742d18 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sun, 1 Dec 2024 20:37:37 +0800 Subject: [PATCH] chore: add avalonia ToggleButton demo. --- .../Styles/ToggleButtonDemoCtl.axaml | 602 ++++++++++++++ .../Styles/ToggleButtonDemoCtl.axaml.cs | 9 + .../Controls/Attach/IconSwitchElement.cs | 35 + .../Controls/Attach/StatusSwitchElement.cs | 23 + .../Controls/Attach/ToggleButtonAttach.cs | 13 + .../Themes/Basic/Converters.axaml | 2 +- .../Themes/Styles/Border.axaml | 15 + .../Themes/Styles/RadioButton.axaml | 171 +--- .../Themes/Styles/Style.axaml | 1 + .../Themes/Styles/ToggleButton.axaml | 770 ++++++++++++++++++ .../Converter/Boolean2BooleanReConverter.cs | 28 - .../Converter/BorderCircularClipConverter.cs | 30 + .../HandyControl_Net_40/Themes/Theme.xaml | 15 +- .../Styles/ToggleButtonDemoCtl.xaml | 62 +- .../Styles/Base/ToggleButtonBaseStyle.xaml | 13 +- .../Themes/Styles/ToggleButton.xaml | 2 +- .../HandyControl_Shared/Themes/Theme.xaml | 15 +- 17 files changed, 1564 insertions(+), 242 deletions(-) create mode 100644 src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml create mode 100644 src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml.cs create mode 100644 src/Avalonia/HandyControl_Avalonia/Controls/Attach/IconSwitchElement.cs create mode 100644 src/Avalonia/HandyControl_Avalonia/Controls/Attach/StatusSwitchElement.cs create mode 100644 src/Avalonia/HandyControl_Avalonia/Controls/Attach/ToggleButtonAttach.cs create mode 100644 src/Avalonia/HandyControl_Avalonia/Themes/Styles/ToggleButton.axaml delete mode 100644 src/Avalonia/HandyControl_Avalonia/Tools/Converter/Boolean2BooleanReConverter.cs create mode 100644 src/Avalonia/HandyControl_Avalonia/Tools/Converter/BorderCircularClipConverter.cs diff --git a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml new file mode 100644 index 00000000..01f09cc8 --- /dev/null +++ b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml @@ -0,0 +1,602 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml.cs b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml.cs new file mode 100644 index 00000000..5010c713 --- /dev/null +++ b/src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/ToggleButtonDemoCtl.axaml.cs @@ -0,0 +1,9 @@ +namespace HandyControlDemo.UserControl; + +public partial class ToggleButtonDemoCtl : Avalonia.Controls.UserControl +{ + public ToggleButtonDemoCtl() + { + InitializeComponent(); + } +} diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Attach/IconSwitchElement.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/IconSwitchElement.cs new file mode 100644 index 00000000..582f55ba --- /dev/null +++ b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/IconSwitchElement.cs @@ -0,0 +1,35 @@ +using Avalonia; +using Avalonia.Media; + +namespace HandyControl.Controls; + +public class IconSwitchElement +{ + public static readonly AttachedProperty GeometryProperty = + AvaloniaProperty.RegisterAttached("Geometry"); + + public static void SetGeometry(AvaloniaObject element, Geometry value) => element.SetValue(GeometryProperty, value); + + public static Geometry GetGeometry(AvaloniaObject element) => element.GetValue(GeometryProperty); + + public static readonly AttachedProperty WidthProperty = + AvaloniaProperty.RegisterAttached("Width", defaultValue: double.NaN); + + public static void SetWidth(AvaloniaObject element, double value) => element.SetValue(WidthProperty, value); + + public static double GetWidth(AvaloniaObject element) => element.GetValue(WidthProperty); + + public static readonly AttachedProperty HeightProperty = + AvaloniaProperty.RegisterAttached("Height", defaultValue: double.NaN); + + public static void SetHeight(AvaloniaObject element, double value) => element.SetValue(HeightProperty, value); + public static double GetHeight(AvaloniaObject element) => element.GetValue(HeightProperty); + + public static readonly AttachedProperty GeometrySelectedProperty = + AvaloniaProperty.RegisterAttached("GeometrySelected"); + + public static void SetGeometrySelected(AvaloniaObject element, Geometry value) => + element.SetValue(GeometrySelectedProperty, value); + + public static Geometry GetGeometrySelected(AvaloniaObject element) => element.GetValue(GeometrySelectedProperty); +} diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Attach/StatusSwitchElement.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/StatusSwitchElement.cs new file mode 100644 index 00000000..6bede12a --- /dev/null +++ b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/StatusSwitchElement.cs @@ -0,0 +1,23 @@ +using Avalonia; + +namespace HandyControl.Controls; + +public class StatusSwitchElement +{ + public static readonly AttachedProperty CheckedElementProperty = + AvaloniaProperty.RegisterAttached("CheckedElement"); + + public static void SetCheckedElement(AvaloniaObject element, object value) => + element.SetValue(CheckedElementProperty, value); + + public static object GetCheckedElement(AvaloniaObject element) => element.GetValue(CheckedElementProperty); + + public static readonly AttachedProperty HideUncheckedElementProperty = + AvaloniaProperty.RegisterAttached("HideUncheckedElement"); + + public static void SetHideUncheckedElement(AvaloniaObject element, object value) => + element.SetValue(HideUncheckedElementProperty, value); + + public static object GetHideUncheckedElement(AvaloniaObject element) => + element.GetValue(HideUncheckedElementProperty); +} diff --git a/src/Avalonia/HandyControl_Avalonia/Controls/Attach/ToggleButtonAttach.cs b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/ToggleButtonAttach.cs new file mode 100644 index 00000000..646bb7f4 --- /dev/null +++ b/src/Avalonia/HandyControl_Avalonia/Controls/Attach/ToggleButtonAttach.cs @@ -0,0 +1,13 @@ +using Avalonia; + +namespace HandyControl.Controls; + +public class ToggleButtonAttach +{ + public static readonly AttachedProperty ShowLabelProperty = + AvaloniaProperty.RegisterAttached("ShowLabel", inherits: true); + + public static void SetShowLabel(AvaloniaObject element, bool value) => element.SetValue(ShowLabelProperty, value); + + public static bool GetShowLabel(AvaloniaObject element) => element.GetValue(ShowLabelProperty); +} diff --git a/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Converters.axaml b/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Converters.axaml index 75038c6f..f13a5203 100644 --- a/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Converters.axaml +++ b/src/Avalonia/HandyControl_Avalonia/Themes/Basic/Converters.axaml @@ -2,9 +2,9 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converter="clr-namespace:HandyControl.Tools.Converter"> - + diff --git a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml index d6d9649d..6a7b3264 100644 --- a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml +++ b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/Border.axaml @@ -111,4 +111,19 @@ + + + + + + + + + + + diff --git a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/RadioButton.axaml b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/RadioButton.axaml index 276f44f7..6efdbedb 100644 --- a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/RadioButton.axaml +++ b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/RadioButton.axaml @@ -51,7 +51,7 @@ CornerRadius="8" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" - IsVisible="{TemplateBinding IsChecked, Converter={StaticResource Boolean2BooleanReConverter}}" /> + IsVisible="{TemplateBinding IsChecked, Converter={x:Static BoolConverters.Not}}" /> - - - - - - - - - - - - - - - - - - - - - - - - + BasedOn="{StaticResource ToggleButtonDefault}" + TargetType="RadioButton" /> - - - - + BasedOn="{StaticResource ToggleButtonDefault.Small}" + TargetType="RadioButton" /> - - - + BasedOn="{StaticResource ToggleButtonPrimary}" + TargetType="RadioButton" /> - - - - + BasedOn="{StaticResource ToggleButtonPrimary.Small}" + TargetType="RadioButton" /> - - - + BasedOn="{StaticResource ToggleButtonSuccess}" + TargetType="RadioButton" /> - - - - + BasedOn="{StaticResource ToggleButtonSuccess.Small}" + TargetType="RadioButton" /> - - - + BasedOn="{StaticResource ToggleButtonInfo}" + TargetType="RadioButton" /> - - - - + BasedOn="{StaticResource ToggleButtonInfo.Small}" + TargetType="RadioButton" /> - - - + BasedOn="{StaticResource ToggleButtonWarning}" + TargetType="RadioButton" /> - - - - + BasedOn="{StaticResource ToggleButtonWarning.Small}" + TargetType="RadioButton" /> - - - + BasedOn="{StaticResource ToggleButtonDanger}" + TargetType="RadioButton" /> - - - - + BasedOn="{StaticResource ToggleButtonDanger.Small}" + TargetType="RadioButton" /> + diff --git a/src/Avalonia/HandyControl_Avalonia/Themes/Styles/ToggleButton.axaml b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/ToggleButton.axaml new file mode 100644 index 00000000..12ee815b --- /dev/null +++ b/src/Avalonia/HandyControl_Avalonia/Themes/Styles/ToggleButton.axaml @@ -0,0 +1,770 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Avalonia/HandyControl_Avalonia/Tools/Converter/Boolean2BooleanReConverter.cs b/src/Avalonia/HandyControl_Avalonia/Tools/Converter/Boolean2BooleanReConverter.cs deleted file mode 100644 index dd3a55fd..00000000 --- a/src/Avalonia/HandyControl_Avalonia/Tools/Converter/Boolean2BooleanReConverter.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Globalization; -using Avalonia.Data.Converters; - -namespace HandyControl.Tools.Converter; - -public class Boolean2BooleanReConverter : IValueConverter -{ - public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) - { - if (value is bool boolValue) - { - return !boolValue; - } - - return value; - } - - public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) - { - if (value is bool boolValue) - { - return !boolValue; - } - - return value; - } -} diff --git a/src/Avalonia/HandyControl_Avalonia/Tools/Converter/BorderCircularClipConverter.cs b/src/Avalonia/HandyControl_Avalonia/Tools/Converter/BorderCircularClipConverter.cs new file mode 100644 index 00000000..f78b11c6 --- /dev/null +++ b/src/Avalonia/HandyControl_Avalonia/Tools/Converter/BorderCircularClipConverter.cs @@ -0,0 +1,30 @@ +//referenced from https://stackoverflow.com/a/5650367/9639378 + +using System; +using System.Collections.Generic; +using System.Globalization; +using Avalonia; +using Avalonia.Data.Converters; +using Avalonia.Media; + +namespace HandyControl.Tools.Converter; + +public class BorderCircularClipConverter : IMultiValueConverter +{ + private static readonly Geometry Empty = new StreamGeometry(); + + public object Convert(IList values, Type targetType, object? parameter, CultureInfo culture) + { + if (values is not [Rect rect, CornerRadius radius]) + { + return AvaloniaProperty.UnsetValue; + } + + if (rect.Width < double.Epsilon || rect.Height < double.Epsilon) + { + return Empty; + } + + return new RectangleGeometry(new Rect(0, 0, rect.Width, rect.Height), radius.TopLeft, radius.TopLeft); + } +} diff --git a/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml b/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml index 23394238..7987627c 100644 --- a/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml +++ b/src/Net_40/HandyControl_Net_40/Themes/Theme.xaml @@ -1798,7 +1798,6 @@ - @@ -1885,18 +1884,10 @@ - - - - - + - - - - - + @@ -3479,7 +3470,7 @@ - - -