From 5c16673d059ddcb8fa55cb649405a18ccc10aef9 Mon Sep 17 00:00:00 2001 From: Erik De Bonte Date: Thu, 14 Oct 2021 09:27:38 -0700 Subject: [PATCH] Remove generic StandardControlImplementation; update generator and controls to use non-generic --- samples/Microcharts/Charts/BarChart.cs | 4 ++-- samples/Microcharts/Charts/Chart.cs | 12 ++++++++---- samples/Microcharts/Charts/PointChart.cs | 4 ++-- samples/Microcharts/Charts/RadarChart.cs | 4 ++-- .../InvalidatedWeakEventHandler.cs | 14 +++++++------- .../SimpleControls/RadialGaugeImplementation.cs | 12 ++++++++---- samples/WpfSamplesHost/BarChart.cs | 2 +- samples/WpfSamplesHost/PointChart.cs | 2 +- src/StandardUI.SourceGenerator/SourceGenerator.cs | 4 ++-- .../Controls/StandardControlImplementation.cs | 10 ---------- 10 files changed, 33 insertions(+), 35 deletions(-) diff --git a/samples/Microcharts/Charts/BarChart.cs b/samples/Microcharts/Charts/BarChart.cs index 8db3ded..e0e6fa6 100644 --- a/samples/Microcharts/Charts/BarChart.cs +++ b/samples/Microcharts/Charts/BarChart.cs @@ -20,12 +20,12 @@ namespace Microcharts /// /// A bar chart. /// - public class BarChartImplementation : PointChartImplementation where T : IBarChart + public class BarChartImplementation : PointChartImplementation { /// /// Initializes a new instance of the class. /// - public BarChartImplementation(T control) : base(control) + public BarChartImplementation(IBarChart control) : base(control) { PointSize = 0; } diff --git a/samples/Microcharts/Charts/Chart.cs b/samples/Microcharts/Charts/Chart.cs index 0272855..ea7cd49 100644 --- a/samples/Microcharts/Charts/Chart.cs +++ b/samples/Microcharts/Charts/Chart.cs @@ -31,7 +31,7 @@ namespace Microcharts /// /// A chart. /// - public abstract class ChartImplementation : StandardControlImplementation, INotifyPropertyChanged where T : IChart + public abstract class ChartImplementation : StandardControlImplementation, INotifyPropertyChanged { private float animationProgress; private double margin = 20, labelTextSize = 16; @@ -42,11 +42,15 @@ namespace Microcharts private Task invalidationPlanification; private CancellationTokenSource animationCancellation; + protected IChart Control { get; } + /// /// Initializes a new instance of the class. /// - public ChartImplementation(T control) : base(control) + public ChartImplementation(IChart control) : base((IStandardControlEnvironmentPeer)control) { + Control = control; + PropertyChanged += OnPropertyChanged; // Animation doesn't currently work, so disable it by default @@ -419,10 +423,10 @@ namespace Microcharts /// The target instance. /// Callback when chart is invalidated. /// The target subsriber type. - public InvalidatedWeakEventHandler ObserveInvalidate(TTarget target, Action onInvalidate) + public InvalidatedWeakEventHandler ObserveInvalidate(TTarget target, Action onInvalidate) where TTarget : class { - var weakHandler = new InvalidatedWeakEventHandler(this, target, onInvalidate); + var weakHandler = new InvalidatedWeakEventHandler(this, target, onInvalidate); weakHandler.Subsribe(); return weakHandler; } diff --git a/samples/Microcharts/Charts/PointChart.cs b/samples/Microcharts/Charts/PointChart.cs index ea15831..fd22c1b 100644 --- a/samples/Microcharts/Charts/PointChart.cs +++ b/samples/Microcharts/Charts/PointChart.cs @@ -24,12 +24,12 @@ namespace Microcharts /// /// Point chart. /// - public class PointChartImplementation : ChartImplementation where T : IPointChart + public class PointChartImplementation : ChartImplementation { /// /// Initializes a new instance of the class. /// - public PointChartImplementation(T control) : base(control) + public PointChartImplementation(IPointChart control) : base(control) { LabelOrientation = Orientation.Horizontal; // Orientation.Default; ValueLabelOrientation = Orientation.Default; diff --git a/samples/Microcharts/Charts/RadarChart.cs b/samples/Microcharts/Charts/RadarChart.cs index 5380599..1e90742 100644 --- a/samples/Microcharts/Charts/RadarChart.cs +++ b/samples/Microcharts/Charts/RadarChart.cs @@ -21,11 +21,11 @@ namespace Microcharts /// /// A radar chart. /// - public class RadarChartImplementation : ChartImplementation where T : IRadarChart + public class RadarChartImplementation : ChartImplementation { private const float Epsilon = 0.01f; - public RadarChartImplementation(T control) : base(control) + public RadarChartImplementation(IRadarChart control) : base(control) { } diff --git a/samples/Microcharts/WeakEventHandlers/InvalidatedWeakEventHandler.cs b/samples/Microcharts/WeakEventHandlers/InvalidatedWeakEventHandler.cs index ebdc9af..fa7322a 100644 --- a/samples/Microcharts/WeakEventHandlers/InvalidatedWeakEventHandler.cs +++ b/samples/Microcharts/WeakEventHandlers/InvalidatedWeakEventHandler.cs @@ -11,11 +11,11 @@ namespace Microcharts /// in memory instead of the actual subscriber. /// This could be considered as an acceptable solution in most cases. /// - public class InvalidatedWeakEventHandler : IDisposable where TControlSource : IChart where TTarget : class + public class InvalidatedWeakEventHandler : IDisposable where TTarget : class { #region Fields - private readonly WeakReference> sourceReference; + private readonly WeakReference sourceReference; private readonly WeakReference targetReference; @@ -33,9 +33,9 @@ namespace Microcharts /// The source. /// The target. /// The target method. - public InvalidatedWeakEventHandler(ChartImplementation source, TTarget target, Action targetMethod) + public InvalidatedWeakEventHandler(ChartImplementation source, TTarget target, Action targetMethod) { - this.sourceReference = new WeakReference>(source); + this.sourceReference = new WeakReference(source); this.targetReference = new WeakReference(target); this.targetMethod = targetMethod; } @@ -48,7 +48,7 @@ namespace Microcharts /// Gets a value indicating whether this is alive. /// /// true if is alive; otherwise, false. - public bool IsAlive => this.sourceReference.TryGetTarget(out ChartImplementation s) && this.targetReference.TryGetTarget(out TTarget t); + public bool IsAlive => this.sourceReference.TryGetTarget(out ChartImplementation s) && this.targetReference.TryGetTarget(out TTarget t); #endregion @@ -59,7 +59,7 @@ namespace Microcharts /// public void Subsribe() { - if (!this.isSubscribed && this.sourceReference.TryGetTarget(out ChartImplementation source)) + if (!this.isSubscribed && this.sourceReference.TryGetTarget(out ChartImplementation source)) { source.Invalidated += this.OnEvent; this.isSubscribed = true; @@ -73,7 +73,7 @@ namespace Microcharts { if (this.isSubscribed) { - if (this.sourceReference.TryGetTarget(out ChartImplementation source)) + if (this.sourceReference.TryGetTarget(out ChartImplementation source)) { source.Invalidated -= this.OnEvent; } diff --git a/samples/SimpleControls/RadialGaugeImplementation.cs b/samples/SimpleControls/RadialGaugeImplementation.cs index 4eb1d01..646d274 100644 --- a/samples/SimpleControls/RadialGaugeImplementation.cs +++ b/samples/SimpleControls/RadialGaugeImplementation.cs @@ -12,16 +12,20 @@ namespace SimpleControls IBrush? Fill { get; set; } } - public class RadialGaugeImplementation : StandardControlImplementation where T : IRadialGauge + public class RadialGaugeImplementation : StandardControlImplementation { - public RadialGaugeImplementation(T control) : base(control) - { } + private readonly IRadialGauge control; + + public RadialGaugeImplementation(IRadialGauge control) : base((IStandardControlEnvironmentPeer)control) + { + this.control = control; + } public override IUIElement? Build() { var blueBrush = SolidColorBrush().Color(Colors.Blue); - return Rectangle() .Width(50) .Height(50) .Stroke(blueBrush) .Fill(Control.Fill); + return Rectangle() .Width(50) .Height(50) .Stroke(blueBrush) .Fill(this.control.Fill); } } } diff --git a/samples/WpfSamplesHost/BarChart.cs b/samples/WpfSamplesHost/BarChart.cs index efce316..f796bb3 100644 --- a/samples/WpfSamplesHost/BarChart.cs +++ b/samples/WpfSamplesHost/BarChart.cs @@ -15,7 +15,7 @@ namespace SimpleControls.Wpf public BarChart() { - InitImplementation(new BarChartImplementation(this)); + InitImplementation(new BarChartImplementation(this)); } public IEnumerable Entries diff --git a/samples/WpfSamplesHost/PointChart.cs b/samples/WpfSamplesHost/PointChart.cs index d47e695..f2ac691 100644 --- a/samples/WpfSamplesHost/PointChart.cs +++ b/samples/WpfSamplesHost/PointChart.cs @@ -15,7 +15,7 @@ namespace SimpleControls.Wpf public PointChart() { - InitImplementation(new PointChartImplementation(this)); + InitImplementation(new PointChartImplementation(this)); } public IEnumerable Entries diff --git a/src/StandardUI.SourceGenerator/SourceGenerator.cs b/src/StandardUI.SourceGenerator/SourceGenerator.cs index fee8b16..4ec640f 100644 --- a/src/StandardUI.SourceGenerator/SourceGenerator.cs +++ b/src/StandardUI.SourceGenerator/SourceGenerator.cs @@ -68,11 +68,11 @@ using Microsoft.StandardUI.Wpf; namespace SimpleControls.Wpf {{ - public class {controlTypeName} : StandardControl<{interfaceFullTypeName}>, {interfaceFullTypeName} + public class {controlTypeName} : StandardControl, {interfaceFullTypeName} {{ public {controlTypeName}() {{ - InitImplementation(new {interfaceNamespace}.{controlTypeName}Implementation<{interfaceFullTypeName}>(this)); + InitImplementation(new {interfaceNamespace}.{controlTypeName}Implementation(this)); }}"); SourceGenerator.GenerateProperties(interfaceSymbol, controlTypeName, sourceCode); diff --git a/src/StandardUI/Controls/StandardControlImplementation.cs b/src/StandardUI/Controls/StandardControlImplementation.cs index 3f9ca91..884ec76 100644 --- a/src/StandardUI/Controls/StandardControlImplementation.cs +++ b/src/StandardUI/Controls/StandardControlImplementation.cs @@ -95,14 +95,4 @@ namespace Microsoft.StandardUI.Controls return finalSize; } } - - public abstract class StandardControlImplementation : StandardControlImplementation where T : IControl - { - public T Control { get; } - - public StandardControlImplementation(T control) : base((IStandardControlEnvironmentPeer)control) - { - Control = control; - } - } }