From 78993eb33cb062c0bba8cc352c17fae1ecd31484 Mon Sep 17 00:00:00 2001 From: JamRemco <58340108+JamRemco@users.noreply.github.com> Date: Fri, 12 Jun 2020 19:48:52 +0200 Subject: [PATCH] Add contentoff and contentOn to toggleswitch Add more resource suport --- src/Avalonia.Controls/ToggleSwitch.cs | 115 ++++- .../Accents/FluentControlResourcesDark.xaml | 7 +- .../Accents/FluentControlResourcesLight.xaml | 10 +- src/Avalonia.Themes.Fluent/ToggleSwitch.xaml | 465 ++++++++++-------- 4 files changed, 375 insertions(+), 222 deletions(-) diff --git a/src/Avalonia.Controls/ToggleSwitch.cs b/src/Avalonia.Controls/ToggleSwitch.cs index 19f282d2d1..16caf8dbfb 100644 --- a/src/Avalonia.Controls/ToggleSwitch.cs +++ b/src/Avalonia.Controls/ToggleSwitch.cs @@ -1,16 +1,119 @@ using Avalonia.Controls.Primitives; +using Avalonia.Controls.Mixins; +using Avalonia.Controls.Presenters; +using Avalonia.Controls.Templates; +using Avalonia.LogicalTree; + namespace Avalonia.Controls { /// /// A WinUi like ToggleSwitch control. /// + /// + public class ToggleSwitch : ToggleButton { - } + public static readonly StyledProperty OffContentProperty = + AvaloniaProperty.Register(nameof(OffContent)); + + public static readonly StyledProperty OffContentTemplateProperty = + AvaloniaProperty.Register(nameof(OffContentTemplate)); + + + public static readonly StyledProperty OnContentProperty = + AvaloniaProperty.Register(nameof(OnContent)); + + public static readonly StyledProperty OnContentTemplateProperty = + AvaloniaProperty.Register(nameof(OnContentTemplate)); + + public object OnContent + { + get { return GetValue(OnContentProperty); } + set { SetValue(OnContentProperty, value); } + } + + public object OffContent + { + get { return GetValue(OffContentProperty); } + set { SetValue(OffContentProperty, value); } + } + + public IContentPresenter OffContentPresenter + { + get; + private set; + } + + public IContentPresenter OnContentPresenter + { + get; + private set; + } + + + public IDataTemplate OffContentTemplate + { + get { return GetValue(OffContentTemplateProperty); } + set { SetValue(OffContentTemplateProperty, value); } + } + + public IDataTemplate OnContentTemplate + { + get { return GetValue(OnContentTemplateProperty); } + set { SetValue(OnContentTemplateProperty, value); } + } + + private void OffContentChanged(AvaloniaPropertyChangedEventArgs e) + { + if (e.OldValue is ILogical oldChild) + { + LogicalChildren.Remove(oldChild); + } + + if (e.NewValue is ILogical newChild) + { + LogicalChildren.Add(newChild); + } + } + + private void OnContentChanged(AvaloniaPropertyChangedEventArgs e) + { + if (e.OldValue is ILogical oldChild) + { + LogicalChildren.Remove(oldChild); + } + + if (e.NewValue is ILogical newChild) + { + LogicalChildren.Add(newChild); + } + } + + static ToggleSwitch() + { + OffContentProperty.Changed.AddClassHandler((x, e) => x.OffContentChanged(e)); + OnContentProperty.Changed.AddClassHandler((x, e) => x.OnContentChanged(e)); + } + + + protected override bool RegisterContentPresenter(IContentPresenter presenter) + { + var result = base.RegisterContentPresenter(presenter); + + if (presenter.Name == "Part_OnContentPresenter") + { + OnContentPresenter = presenter; + result = true; + } + if (presenter.Name == "PART_OffContentPresenter") + { + OffContentPresenter = presenter; + result = true; + } + + return result; + } + } } -/********** Todo *********** - * - * Implement ContenOff property - * Implement ContentOn property. -*/ + diff --git a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml index da3c20e844..9fa0458db3 100644 --- a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml +++ b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesDark.xaml @@ -346,8 +346,10 @@ - 0 - 1 + + 0 + + 1 @@ -380,7 +382,6 @@ - diff --git a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml index 2426c1034c..5c23bc96ee 100644 --- a/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml +++ b/src/Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml @@ -345,8 +345,12 @@ - 0 - 1 + + + + 0 + + 1 @@ -379,7 +383,6 @@ - @@ -404,6 +407,5 @@ - diff --git a/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml b/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml index 2f311043fd..df9be1284a 100644 --- a/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml +++ b/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml @@ -4,7 +4,7 @@ xmlns:sys="clr-namespace:System;assembly=netstandard"> - + - @@ -26,244 +28,289 @@ Text="The previewer Shows a preview off your code, this could slow down your system" TextWrapping="Wrap"/> + /> - - + + + + + + + + - - + + + - - + + + + + + + + + + + + + - - + + + + + + + - - + - - - - - + - + + + + + + - - - - - - - + - - - - - - - + + + - - - - - - - - - - - - - - + - - - - - - - - + + + + + - + + + + + + + + 0,0,0,6 + 6 + 6 + 154 + 5 + 28 + + +