From 7808da21603f63e8d3974cc701bae3a758cd966b Mon Sep 17 00:00:00 2001 From: rabbitism Date: Fri, 2 Jun 2023 12:25:56 +0800 Subject: [PATCH 1/4] feat: attach transitions after ToggleSwitch loaded. --- src/Avalonia.Controls/ToggleSwitch.cs | 34 +++++++++++++++++-- .../Controls/ToggleSwitch.xaml | 21 ++++++------ .../Controls/ToggleSwitch.xaml | 18 +++++++--- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/Avalonia.Controls/ToggleSwitch.cs b/src/Avalonia.Controls/ToggleSwitch.cs index a68a022e67..989c3cb261 100644 --- a/src/Avalonia.Controls/ToggleSwitch.cs +++ b/src/Avalonia.Controls/ToggleSwitch.cs @@ -1,4 +1,5 @@ -using Avalonia.Controls.Metadata; +using Avalonia.Animation; +using Avalonia.Controls.Metadata; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; @@ -42,6 +43,10 @@ namespace Avalonia.Controls x.UpdateKnobPos(x.IsChecked.Value); } }); + KnobTransitionsProperty.Changed.AddClassHandler((x, e) => + { + x.AssignTransitions(); + }); } /// @@ -68,6 +73,8 @@ namespace Avalonia.Controls public static readonly StyledProperty OnContentTemplateProperty = AvaloniaProperty.Register(nameof(OnContentTemplate)); + public static readonly StyledProperty KnobTransitionsProperty = AvaloniaProperty.Register(nameof(KnobTransitions)); + /// /// Gets or Sets the Content that is displayed when in the On State. /// @@ -116,6 +123,14 @@ namespace Avalonia.Controls set { SetValue(OnContentTemplateProperty, value); } } + public Transitions KnobTransitions + { + get { return GetValue(KnobTransitionsProperty); } + set { SetValue(KnobTransitionsProperty, value); } + } + + + private void OffContentChanged(AvaloniaPropertyChangedEventArgs e) { if (e.OldValue is ILogical oldChild) @@ -176,8 +191,23 @@ namespace Avalonia.Controls { UpdateKnobPos(IsChecked.Value); } + } - + + protected override void OnLoaded() + { + base.OnLoaded(); + AssignTransitions(); + } + + private void AssignTransitions() + { + if (_knobsPanel != null) + { + _knobsPanel.Transitions = KnobTransitions; + } + } + private void KnobsPanel_PointerPressed(object? sender, Input.PointerPressedEventArgs e) { _switchStartPoint = e.GetPosition(_switchKnob); diff --git a/src/Avalonia.Themes.Fluent/Controls/ToggleSwitch.xaml b/src/Avalonia.Themes.Fluent/Controls/ToggleSwitch.xaml index 47d7953096..63c11f6ff8 100644 --- a/src/Avalonia.Themes.Fluent/Controls/ToggleSwitch.xaml +++ b/src/Avalonia.Themes.Fluent/Controls/ToggleSwitch.xaml @@ -28,6 +28,14 @@ + + + + + @@ -134,16 +142,9 @@ - - diff --git a/src/Avalonia.Themes.Simple/Controls/ToggleSwitch.xaml b/src/Avalonia.Themes.Simple/Controls/ToggleSwitch.xaml index a51aeb9ee6..1178c96419 100644 --- a/src/Avalonia.Themes.Simple/Controls/ToggleSwitch.xaml +++ b/src/Avalonia.Themes.Simple/Controls/ToggleSwitch.xaml @@ -46,6 +46,14 @@ + + + + + - - - - - + + + - - - - - -