From d531b36b9631e069424f98f22057f00584311b9c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 8 Sep 2018 19:55:36 +0200 Subject: [PATCH 01/56] Make brushes raise a Changed event. When their visual representation changes. --- src/Avalonia.Visuals/Media/Brush.cs | 36 +++++++- src/Avalonia.Visuals/Media/GradientBrush.cs | 65 +++++++++++--- src/Avalonia.Visuals/Media/GradientStop.cs | 40 ++++++--- src/Avalonia.Visuals/Media/GradientStops.cs | 23 +++++ src/Avalonia.Visuals/Media/IGradientBrush.cs | 4 +- src/Avalonia.Visuals/Media/IGradientStop.cs | 18 ++++ src/Avalonia.Visuals/Media/IMutableBrush.cs | 9 +- src/Avalonia.Visuals/Media/ImageBrush.cs | 12 ++- .../Media/Immutable/ImmutableGradientBrush.cs | 9 +- .../Media/Immutable/ImmutableGradientStop.cs | 20 +++++ .../Immutable/ImmutableLinearGradientBrush.cs | 4 +- .../Immutable/ImmutableRadialGradientBrush.cs | 4 +- .../Media/LinearGradientBrush.cs | 13 ++- .../Media/RadialGradientBrush.cs | 10 ++- src/Avalonia.Visuals/Media/SolidColorBrush.cs | 13 ++- src/Avalonia.Visuals/Media/TileBrush.cs | 7 ++ src/Avalonia.Visuals/Media/VisualBrush.cs | 12 ++- .../Controls/CustomRenderTests.cs | 2 +- .../Media/LinearGradientBrushTests.cs | 4 +- .../Media/RadialGradientBrushTests.cs | 2 +- .../Avalonia.RenderTests/OpacityMaskTests.cs | 4 +- .../Media/ImageBrushTests.cs | 27 ++++++ .../Media/LinearGradientBrushTests.cs | 86 +++++++++++++++++++ .../Media/SolidColorBrushTests.cs | 21 +++++ 24 files changed, 386 insertions(+), 59 deletions(-) create mode 100644 src/Avalonia.Visuals/Media/GradientStops.cs create mode 100644 src/Avalonia.Visuals/Media/IGradientStop.cs create mode 100644 src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs create mode 100644 tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs create mode 100644 tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs create mode 100644 tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs diff --git a/src/Avalonia.Visuals/Media/Brush.cs b/src/Avalonia.Visuals/Media/Brush.cs index eef6e1a43c..8ba7c1be04 100644 --- a/src/Avalonia.Visuals/Media/Brush.cs +++ b/src/Avalonia.Visuals/Media/Brush.cs @@ -10,7 +10,7 @@ namespace Avalonia.Media /// Describes how an area is painted. /// [TypeConverter(typeof(BrushConverter))] - public abstract class Brush : AvaloniaObject, IBrush + public abstract class Brush : AvaloniaObject, IMutableBrush { /// /// Defines the property. @@ -18,6 +18,9 @@ namespace Avalonia.Media public static readonly StyledProperty OpacityProperty = AvaloniaProperty.Register(nameof(Opacity), 1.0); + /// + public event EventHandler Changed; + /// /// Gets or sets the opacity of the brush. /// @@ -50,5 +53,36 @@ namespace Avalonia.Media throw new FormatException($"Invalid brush string: '{s}'."); } + + /// + public abstract IBrush ToImmutable(); + + /// + /// Marks a property as affecting the brush's visual representation. + /// + /// The properties. + /// + /// After a call to this method in a brush's static constructor, any change to the + /// property will cause the event to be raised on the brush. + /// + protected static void AffectsRender(params AvaloniaProperty[] properties) + where T : Brush + { + void Invalidate(AvaloniaPropertyChangedEventArgs e) + { + (e.Sender as T)?.RaiseChanged(EventArgs.Empty); + } + + foreach (var property in properties) + { + property.Changed.Subscribe(Invalidate); + } + } + + /// + /// Raises the event. + /// + /// The event args. + protected void RaiseChanged(EventArgs e) => Changed?.Invoke(this, e); } } diff --git a/src/Avalonia.Visuals/Media/GradientBrush.cs b/src/Avalonia.Visuals/Media/GradientBrush.cs index 41c3afc8c3..c123813cee 100644 --- a/src/Avalonia.Visuals/Media/GradientBrush.cs +++ b/src/Avalonia.Visuals/Media/GradientBrush.cs @@ -1,7 +1,11 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System; using System.Collections.Generic; +using System.Collections.Specialized; +using System.ComponentModel; +using Avalonia.Collections; using Avalonia.Metadata; namespace Avalonia.Media @@ -20,35 +24,74 @@ namespace Avalonia.Media /// /// Defines the property. /// - public static readonly StyledProperty> GradientStopsProperty = - AvaloniaProperty.Register>(nameof(GradientStops)); + public static readonly StyledProperty GradientStopsProperty = + AvaloniaProperty.Register(nameof(GradientStops)); + + private IDisposable _gradientStopsSubscription; + + static GradientBrush() + { + GradientStopsProperty.Changed.Subscribe(GradientStopsChanged); + AffectsRender(SpreadMethodProperty); + } /// /// Initializes a new instance of the class. /// public GradientBrush() { - this.GradientStops = new List(); + this.GradientStops = new GradientStops(); } - /// - /// Gets or sets the brush's spread method that defines how to draw a gradient that - /// doesn't fill the bounds of the destination control. - /// + /// public GradientSpreadMethod SpreadMethod { get { return GetValue(SpreadMethodProperty); } set { SetValue(SpreadMethodProperty, value); } } - /// - /// Gets or sets the brush's gradient stops. - /// + /// [Content] - public IList GradientStops + public GradientStops GradientStops { get { return GetValue(GradientStopsProperty); } set { SetValue(GradientStopsProperty, value); } } + + /// + IReadOnlyList IGradientBrush.GradientStops => GradientStops; + + private static void GradientStopsChanged(AvaloniaPropertyChangedEventArgs e) + { + if (e.Sender is GradientBrush brush) + { + var oldValue = (GradientStops)e.OldValue; + var newValue = (GradientStops)e.NewValue; + + if (oldValue != null) + { + oldValue.CollectionChanged -= brush.GradientStopsChanged; + brush._gradientStopsSubscription.Dispose(); + } + + if (newValue != null) + { + newValue.CollectionChanged += brush.GradientStopsChanged; + brush._gradientStopsSubscription = newValue.TrackItemPropertyChanged(brush.GradientStopChanged); + } + + brush.RaiseChanged(EventArgs.Empty); + } + } + + private void GradientStopsChanged(object sender, NotifyCollectionChangedEventArgs e) + { + RaiseChanged(EventArgs.Empty); + } + + private void GradientStopChanged(Tuple e) + { + RaiseChanged(EventArgs.Empty); + } } } diff --git a/src/Avalonia.Visuals/Media/GradientStop.cs b/src/Avalonia.Visuals/Media/GradientStop.cs index 78dd32a18a..00d96a0b3c 100644 --- a/src/Avalonia.Visuals/Media/GradientStop.cs +++ b/src/Avalonia.Visuals/Media/GradientStop.cs @@ -4,10 +4,22 @@ namespace Avalonia.Media { /// - /// GradientStop + /// Describes the location and color of a transition point in a gradient. /// - public sealed class GradientStop + public sealed class GradientStop : AvaloniaObject, IGradientStop { + /// + /// Describes the property. + /// + public static StyledProperty OffsetProperty = + AvaloniaProperty.Register(nameof(Offset)); + + /// + /// Describes the property. + /// + public static StyledProperty ColorProperty = + AvaloniaProperty.Register(nameof(Color)); + /// /// Initializes a new instance of the class. /// @@ -24,16 +36,18 @@ namespace Avalonia.Media Offset = offset; } - // TODO: Make these dependency properties. - - /// - /// The offset - /// - public double Offset { get; set; } + /// + public double Offset + { + get => GetValue(OffsetProperty); + set => SetValue(OffsetProperty, value); + } - /// - /// The color - /// - public Color Color { get; set; } + /// + public Color Color + { + get => GetValue(ColorProperty); + set => SetValue(ColorProperty, value); + } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/GradientStops.cs b/src/Avalonia.Visuals/Media/GradientStops.cs new file mode 100644 index 0000000000..efc11bacd6 --- /dev/null +++ b/src/Avalonia.Visuals/Media/GradientStops.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Linq; +using Avalonia.Collections; +using Avalonia.Media.Immutable; + +namespace Avalonia.Media +{ + /// + /// A collection of s. + /// + public class GradientStops : AvaloniaList + { + public GradientStops() + { + ResetBehavior = ResetBehavior.Remove; + } + + public IReadOnlyList ToImmutable() + { + return this.Select(x => new ImmutableGradientStop(x.Offset, x.Color)).ToList(); + } + } +} diff --git a/src/Avalonia.Visuals/Media/IGradientBrush.cs b/src/Avalonia.Visuals/Media/IGradientBrush.cs index 390ce6ee5b..18db0af660 100644 --- a/src/Avalonia.Visuals/Media/IGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/IGradientBrush.cs @@ -10,7 +10,7 @@ namespace Avalonia.Media /// /// Gets the brush's gradient stops. /// - IList GradientStops { get; } + IReadOnlyList GradientStops { get; } /// /// Gets the brush's spread method that defines how to draw a gradient that doesn't fill @@ -18,4 +18,4 @@ namespace Avalonia.Media /// GradientSpreadMethod SpreadMethod { get; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/IGradientStop.cs b/src/Avalonia.Visuals/Media/IGradientStop.cs new file mode 100644 index 0000000000..22eb9df60d --- /dev/null +++ b/src/Avalonia.Visuals/Media/IGradientStop.cs @@ -0,0 +1,18 @@ +namespace Avalonia.Media +{ + /// + /// Describes the location and color of a transition point in a gradient. + /// + public interface IGradientStop + { + /// + /// Gets the gradient stop color. + /// + Color Color { get; } + + /// + /// Gets the gradient stop offset. + /// + double Offset { get; } + } +} diff --git a/src/Avalonia.Visuals/Media/IMutableBrush.cs b/src/Avalonia.Visuals/Media/IMutableBrush.cs index 39dd8b80c4..762731a6a8 100644 --- a/src/Avalonia.Visuals/Media/IMutableBrush.cs +++ b/src/Avalonia.Visuals/Media/IMutableBrush.cs @@ -1,10 +1,17 @@ -namespace Avalonia.Media +using System; + +namespace Avalonia.Media { /// /// Represents a mutable brush which can return an immutable clone of itself. /// public interface IMutableBrush : IBrush { + /// + /// Raised when the brush changes visually. + /// + event EventHandler Changed; + /// /// Creates an immutable clone of the brush. /// diff --git a/src/Avalonia.Visuals/Media/ImageBrush.cs b/src/Avalonia.Visuals/Media/ImageBrush.cs index fa491ed3e1..8b42a51d9f 100644 --- a/src/Avalonia.Visuals/Media/ImageBrush.cs +++ b/src/Avalonia.Visuals/Media/ImageBrush.cs @@ -2,13 +2,14 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia.Media.Imaging; +using Avalonia.Media.Immutable; namespace Avalonia.Media { /// /// Paints an area with an . /// - public class ImageBrush : TileBrush, IImageBrush, IMutableBrush + public class ImageBrush : TileBrush, IImageBrush { /// /// Defines the property. @@ -16,6 +17,11 @@ namespace Avalonia.Media public static readonly StyledProperty SourceProperty = AvaloniaProperty.Register(nameof(Source)); + static ImageBrush() + { + AffectsRender(SourceProperty); + } + /// /// Initializes a new instance of the class. /// @@ -42,9 +48,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableImageBrush(this); + return new ImmutableImageBrush(this); } } } diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs index 6664a2b30e..1f6e3bbcfd 100644 --- a/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace Avalonia.Media.Immutable { @@ -15,7 +14,7 @@ namespace Avalonia.Media.Immutable /// The opacity of the brush. /// The spread method. protected ImmutableGradientBrush( - IList gradientStops, + IReadOnlyList gradientStops, double opacity, GradientSpreadMethod spreadMethod) { @@ -28,14 +27,14 @@ namespace Avalonia.Media.Immutable /// Initializes a new instance of the class. /// /// The brush from which this brush's properties should be copied. - protected ImmutableGradientBrush(IGradientBrush source) - : this(source.GradientStops.ToList(), source.Opacity, source.SpreadMethod) + protected ImmutableGradientBrush(GradientBrush source) + : this(source.GradientStops.ToImmutable(), source.Opacity, source.SpreadMethod) { } /// - public IList GradientStops { get; } + public IReadOnlyList GradientStops { get; } /// public double Opacity { get; } diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs new file mode 100644 index 0000000000..f3e2e52fd0 --- /dev/null +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs @@ -0,0 +1,20 @@ +namespace Avalonia.Media.Immutable +{ + /// + /// Describes the location and color of a transition point in a gradient. + /// + public class ImmutableGradientStop : IGradientStop + { + public ImmutableGradientStop(double offset, Color color) + { + Offset = offset; + Color = color; + } + + /// + public double Offset { get; } + + /// + public Color Color { get; } + } +} diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs index 142eb34625..912d77d763 100644 --- a/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs @@ -16,7 +16,7 @@ namespace Avalonia.Media.Immutable /// The start point for the gradient. /// The end point for the gradient. public ImmutableLinearGradientBrush( - IList gradientStops, + IReadOnlyList gradientStops, double opacity = 1, GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad, RelativePoint? startPoint = null, @@ -31,7 +31,7 @@ namespace Avalonia.Media.Immutable /// Initializes a new instance of the class. /// /// The brush from which this brush's properties should be copied. - public ImmutableLinearGradientBrush(ILinearGradientBrush source) + public ImmutableLinearGradientBrush(LinearGradientBrush source) : base(source) { StartPoint = source.StartPoint; diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs index f36a1cd2de..e26fbab5f5 100644 --- a/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs @@ -21,7 +21,7 @@ namespace Avalonia.Media.Immutable /// The horizontal and vertical radius of the outermost circle of the radial gradient. /// public ImmutableRadialGradientBrush( - IList gradientStops, + IReadOnlyList gradientStops, double opacity = 1, GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad, RelativePoint? center = null, @@ -38,7 +38,7 @@ namespace Avalonia.Media.Immutable /// Initializes a new instance of the class. /// /// The brush from which this brush's properties should be copied. - public ImmutableRadialGradientBrush(IRadialGradientBrush source) + public ImmutableRadialGradientBrush(RadialGradientBrush source) : base(source) { Center = source.Center; diff --git a/src/Avalonia.Visuals/Media/LinearGradientBrush.cs b/src/Avalonia.Visuals/Media/LinearGradientBrush.cs index d092bebf0f..14adc0e0cd 100644 --- a/src/Avalonia.Visuals/Media/LinearGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/LinearGradientBrush.cs @@ -1,12 +1,14 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; + namespace Avalonia.Media { /// /// A brush that draws with a linear gradient. /// - public sealed class LinearGradientBrush : GradientBrush, ILinearGradientBrush, IMutableBrush + public sealed class LinearGradientBrush : GradientBrush, ILinearGradientBrush { /// /// Defines the property. @@ -24,6 +26,11 @@ namespace Avalonia.Media nameof(EndPoint), RelativePoint.BottomRight); + static LinearGradientBrush() + { + AffectsRender(StartPointProperty, EndPointProperty); + } + /// /// Gets or sets the start point for the gradient. /// @@ -43,9 +50,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableLinearGradientBrush(this); + return new ImmutableLinearGradientBrush(this); } } } diff --git a/src/Avalonia.Visuals/Media/RadialGradientBrush.cs b/src/Avalonia.Visuals/Media/RadialGradientBrush.cs index 003e2e05f9..589cd83ca1 100644 --- a/src/Avalonia.Visuals/Media/RadialGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/RadialGradientBrush.cs @@ -1,12 +1,14 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; + namespace Avalonia.Media { /// /// Paints an area with a radial gradient. /// - public sealed class RadialGradientBrush : GradientBrush, IRadialGradientBrush, IMutableBrush + public sealed class RadialGradientBrush : GradientBrush, IRadialGradientBrush { /// /// Defines the property. @@ -63,9 +65,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableRadialGradientBrush(this); + return new ImmutableRadialGradientBrush(this); } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/SolidColorBrush.cs b/src/Avalonia.Visuals/Media/SolidColorBrush.cs index d84e407cb4..32b87df56b 100644 --- a/src/Avalonia.Visuals/Media/SolidColorBrush.cs +++ b/src/Avalonia.Visuals/Media/SolidColorBrush.cs @@ -1,12 +1,14 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; + namespace Avalonia.Media { /// /// Fills an area with a solid color. /// - public class SolidColorBrush : Brush, ISolidColorBrush, IMutableBrush + public class SolidColorBrush : Brush, ISolidColorBrush { /// /// Defines the property. @@ -14,6 +16,11 @@ namespace Avalonia.Media public static readonly StyledProperty ColorProperty = AvaloniaProperty.Register(nameof(Color)); + static SolidColorBrush() + { + AffectsRender(ColorProperty); + } + /// /// Initializes a new instance of the class. /// @@ -75,9 +82,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableSolidColorBrush(this); + return new ImmutableSolidColorBrush(this); } } } diff --git a/src/Avalonia.Visuals/Media/TileBrush.cs b/src/Avalonia.Visuals/Media/TileBrush.cs index 2033754137..47f20fa285 100644 --- a/src/Avalonia.Visuals/Media/TileBrush.cs +++ b/src/Avalonia.Visuals/Media/TileBrush.cs @@ -79,6 +79,13 @@ namespace Avalonia.Media static TileBrush() { + AffectsRender( + AlignmentXProperty, + AlignmentYProperty, + DestinationRectProperty, + SourceRectProperty, + StretchProperty, + TileModeProperty); RenderOptions.BitmapInterpolationModeProperty.OverrideDefaultValue(BitmapInterpolationMode.Default); } diff --git a/src/Avalonia.Visuals/Media/VisualBrush.cs b/src/Avalonia.Visuals/Media/VisualBrush.cs index 435f4ba1b1..963ba8f4a1 100644 --- a/src/Avalonia.Visuals/Media/VisualBrush.cs +++ b/src/Avalonia.Visuals/Media/VisualBrush.cs @@ -1,6 +1,7 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; using Avalonia.VisualTree; namespace Avalonia.Media @@ -8,7 +9,7 @@ namespace Avalonia.Media /// /// Paints an area with an . /// - public class VisualBrush : TileBrush, IVisualBrush, IMutableBrush + public class VisualBrush : TileBrush, IVisualBrush { /// /// Defines the property. @@ -16,6 +17,11 @@ namespace Avalonia.Media public static readonly StyledProperty VisualProperty = AvaloniaProperty.Register(nameof(Visual)); + static VisualBrush() + { + AffectsRender(VisualProperty); + } + /// /// Initializes a new instance of the class. /// @@ -42,9 +48,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableVisualBrush(this); + return new ImmutableVisualBrush(this); } } } diff --git a/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs b/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs index 8356e78cc3..6a01536b12 100644 --- a/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs +++ b/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs @@ -124,7 +124,7 @@ namespace Avalonia.Direct2D1.RenderTests.Controls { StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative), - GradientStops = new[] + GradientStops = { new GradientStop(Color.FromUInt32(0xffffffff), 0), new GradientStop(Color.FromUInt32(0x00ffffff), 1) diff --git a/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs b/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs index 656e77fc31..a0d6f1e423 100644 --- a/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs +++ b/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs @@ -36,7 +36,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media { StartPoint = new RelativePoint(0, 0.5, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 0.5, RelativeUnit.Relative), - GradientStops = new[] + GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, new GradientStop { Color = Colors.Blue, Offset = 1 } @@ -63,7 +63,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media { StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(0.5, 1, RelativeUnit.Relative), - GradientStops = new[] + GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, new GradientStop { Color = Colors.Blue, Offset = 1 } diff --git a/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs b/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs index 0017feb106..bd1d26ce70 100644 --- a/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs +++ b/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs @@ -34,7 +34,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media { Background = new RadialGradientBrush { - GradientStops = new[] + GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, new GradientStop { Color = Colors.Blue, Offset = 1 } diff --git a/tests/Avalonia.RenderTests/OpacityMaskTests.cs b/tests/Avalonia.RenderTests/OpacityMaskTests.cs index 4edf4daa13..2f01b03db6 100644 --- a/tests/Avalonia.RenderTests/OpacityMaskTests.cs +++ b/tests/Avalonia.RenderTests/OpacityMaskTests.cs @@ -29,7 +29,7 @@ namespace Avalonia.Direct2D1.RenderTests { StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative), - GradientStops = new List + GradientStops = { new GradientStop(Color.FromUInt32(0xffffffff), 0), new GradientStop(Color.FromUInt32(0x00ffffff), 1) @@ -65,7 +65,7 @@ namespace Avalonia.Direct2D1.RenderTests { StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative), - GradientStops = new List + GradientStops = { new GradientStop(Color.FromUInt32(0xffffffff), 0), new GradientStop(Color.FromUInt32(0x00ffffff), 1) diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs new file mode 100644 index 0000000000..f843a6e333 --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Moq; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class ImageBrushTests + { + [Fact] + public void Changing_Source_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new ImageBrush(bitmap1); + var raised = false; + + target.Changed += (s, e) => raised = true; + target.Source = bitmap2; + + Assert.True(raised); + } + } +} diff --git a/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs new file mode 100644 index 0000000000..62f53108e6 --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs @@ -0,0 +1,86 @@ +using System; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Moq; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class LinearGradientBrushTests + { + [Fact] + public void Changing_StartPoint_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.StartPoint = new RelativePoint(); + target.Changed += (s, e) => raised = true; + target.StartPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); + + Assert.True(raised); + } + + [Fact] + public void Changing_EndPoint_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.EndPoint = new RelativePoint(); + target.Changed += (s, e) => raised = true; + target.EndPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); + + Assert.True(raised); + } + + [Fact] + public void Changing_GradientStops_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; + target.Changed += (s, e) => raised = true; + target.GradientStops = new GradientStops { new GradientStop(Colors.Green, 0) }; + + Assert.True(raised); + } + + [Fact] + public void Adding_GradientStop_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; + target.Changed += (s, e) => raised = true; + target.GradientStops.Add(new GradientStop(Colors.Green, 1)); + + Assert.True(raised); + } + + [Fact] + public void Changing_GradientStop_Offset_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; + target.Changed += (s, e) => raised = true; + target.GradientStops[0].Offset = 0.5; + + Assert.True(raised); + } + } +} diff --git a/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs new file mode 100644 index 0000000000..4e87b7081d --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs @@ -0,0 +1,21 @@ +using System; +using Avalonia.Media; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class SolidColorBrushTests + { + [Fact] + public void Changing_Color_Raises_Changed() + { + var target = new SolidColorBrush(Colors.Red); + var raised = false; + + target.Changed += (s, e) => raised = true; + target.Color = Colors.Green; + + Assert.True(raised); + } + } +} From 0b4e6b847170d34184288bb031d294b62a471a1b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 Jun 2018 10:04:49 +0200 Subject: [PATCH 02/56] Make centralized RenderLoop. - Renamed `RenderLoop` to `RenderTimer` - Added new `RenderLoop` which `DeferredRenderer`s register themselves with for updates --- .../InternalPlatformThreadingInterface.cs | 2 +- src/Avalonia.Controls/TopLevel.cs | 2 +- .../Remote/PreviewerWindowingPlatform.cs | 3 +- src/Avalonia.Visuals/Avalonia.Visuals.csproj | 1 + ...ultRenderLoop.cs => DefaultRenderTimer.cs} | 9 +- .../Rendering/DeferredRenderer.cs | 44 +++---- src/Avalonia.Visuals/Rendering/IRenderLoop.cs | 17 +-- .../Rendering/IRenderLoopTask.cs | 12 ++ .../Rendering/IRenderTimer.cs | 20 ++++ src/Avalonia.Visuals/Rendering/RenderLoop.cs | 111 ++++++++++++++++++ src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs | 3 +- .../LinuxFramebufferPlatform.cs | 3 +- src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs | 5 +- .../{RenderLoop.cs => RenderTimer.cs} | 4 +- .../{RenderLoop.cs => RenderTimer.cs} | 4 +- src/Windows/Avalonia.Win32/Win32Platform.cs | 3 +- tests/Avalonia.UnitTests/TestServices.cs | 4 +- .../Rendering/DeferredRendererTests.cs | 6 +- 18 files changed, 191 insertions(+), 62 deletions(-) rename src/Avalonia.Visuals/Rendering/{DefaultRenderLoop.cs => DefaultRenderTimer.cs} (92%) create mode 100644 src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs create mode 100644 src/Avalonia.Visuals/Rendering/IRenderTimer.cs create mode 100644 src/Avalonia.Visuals/Rendering/RenderLoop.cs rename src/OSX/Avalonia.MonoMac/{RenderLoop.cs => RenderTimer.cs} (91%) rename src/Windows/Avalonia.Win32/{RenderLoop.cs => RenderTimer.cs} (88%) diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index 400bf5ccc3..47e600b9c8 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -9,7 +9,7 @@ using Avalonia.Threading; namespace Avalonia.Controls.Platform { - public class InternalPlatformThreadingInterface : IPlatformThreadingInterface, IRenderLoop + public class InternalPlatformThreadingInterface : IPlatformThreadingInterface, IRenderTimer { public InternalPlatformThreadingInterface() { diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 1161ded25f..fb5b932fd8 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -96,7 +96,7 @@ namespace Avalonia.Controls _applicationLifecycle = TryGetService(dependencyResolver); _renderInterface = TryGetService(dependencyResolver); - var renderLoop = TryGetService(dependencyResolver); + var renderLoop = TryGetService(dependencyResolver); Renderer = impl.CreateRenderer(this); impl.SetInputRoot(this); diff --git a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs index 01998052d9..20acc30118 100644 --- a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs +++ b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs @@ -53,7 +53,8 @@ namespace Avalonia.DesignerSupport.Remote .Bind().ToConstant(Keyboard) .Bind().ToConstant(instance) .Bind().ToConstant(threading) - .Bind().ToConstant(threading) + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(threading) .Bind().ToSingleton() .Bind().ToConstant(instance) .Bind().ToSingleton(); diff --git a/src/Avalonia.Visuals/Avalonia.Visuals.csproj b/src/Avalonia.Visuals/Avalonia.Visuals.csproj index c34752a3ef..c88001cc0a 100644 --- a/src/Avalonia.Visuals/Avalonia.Visuals.csproj +++ b/src/Avalonia.Visuals/Avalonia.Visuals.csproj @@ -1,6 +1,7 @@  netstandard2.0 + Avalonia diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderLoop.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs similarity index 92% rename from src/Avalonia.Visuals/Rendering/DefaultRenderLoop.cs rename to src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index 9cf849f59b..7ad8915bc4 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -2,18 +2,19 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Threading.Tasks; using Avalonia.Platform; namespace Avalonia.Rendering { /// - /// Defines a default render loop that uses a standard timer. + /// Defines a default render timer that uses a standard timer. /// /// /// This class may be overridden by platform implementations to use a specialized timer /// implementation. /// - public class DefaultRenderLoop : IRenderLoop + public class DefaultRenderTimer : IRenderTimer { private IRuntimePlatform _runtime; private int _subscriberCount; @@ -21,12 +22,12 @@ namespace Avalonia.Rendering private IDisposable _subscription; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// /// The number of frames per second at which the loop should run. /// - public DefaultRenderLoop(int framesPerSecond) + public DefaultRenderTimer(int framesPerSecond) { FramesPerSecond = framesPerSecond; } diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index dc1d2933d0..f937964994 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -13,6 +13,7 @@ using Avalonia.Rendering.SceneGraph; using Avalonia.Threading; using Avalonia.Utilities; using Avalonia.VisualTree; +using System.Threading.Tasks; namespace Avalonia.Rendering { @@ -20,7 +21,7 @@ namespace Avalonia.Rendering /// A renderer which renders the state of the visual tree to an intermediate scene graph /// representation which is then rendered on a rendering thread. /// - public class DeferredRenderer : RendererBase, IRenderer, IVisualBrushRenderer + public class DeferredRenderer : RendererBase, IRenderer, IRenderLoopTask, IVisualBrushRenderer { private readonly IDispatcher _dispatcher; private readonly IRenderLoop _renderLoop; @@ -149,7 +150,7 @@ namespace Avalonia.Rendering { if (!_running && _renderLoop != null) { - _renderLoop.Tick += OnRenderLoopTick; + _renderLoop.Add(this); _running = true; } } @@ -159,11 +160,23 @@ namespace Avalonia.Rendering { if (_running && _renderLoop != null) { - _renderLoop.Tick -= OnRenderLoopTick; + _renderLoop.Remove(this); _running = false; } } + bool IRenderLoopTask.NeedsUpdate => _dirty == null || _dirty.Count > 0; + + void IRenderLoopTask.Update() => UpdateScene(); + + void IRenderLoopTask.Render() + { + using (var scene = _scene?.Clone()) + { + Render(scene?.Item); + } + } + /// Size IVisualBrushRenderer.GetRenderTargetSize(IVisualBrush brush) { @@ -420,31 +433,6 @@ namespace Avalonia.Rendering } } - private void OnRenderLoopTick(object sender, EventArgs e) - { - if (Monitor.TryEnter(_rendering)) - { - try - { - if (!_updateQueued && (_dirty == null || _dirty.Count > 0)) - { - _updateQueued = true; - _dispatcher.Post(UpdateScene, DispatcherPriority.Render); - } - - using (var scene = _scene?.Clone()) - { - Render(scene?.Item); - } - } - catch { } - finally - { - Monitor.Exit(_rendering); - } - } - } - private IRef GetOverlay( IDrawingContextImpl parentContext, Size size, diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs index 36d915ddbd..bd1086d178 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs @@ -1,19 +1,8 @@ -using System; - -namespace Avalonia.Rendering +namespace Avalonia.Rendering { - /// - /// Defines the interface implemented by an application render loop. - /// public interface IRenderLoop { - /// - /// Raised when the render loop ticks to signal a new frame should be drawn. - /// - /// - /// This event can be raised on any thread; it is the responsibility of the subscriber to - /// switch execution to the right thread. - /// - event EventHandler Tick; + void Add(IRenderLoopTask i); + void Remove(IRenderLoopTask i); } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs new file mode 100644 index 0000000000..2f251a5c17 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; + +namespace Avalonia.Rendering +{ + public interface IRenderLoopTask + { + bool NeedsUpdate { get; } + void Update(); + void Render(); + } +} diff --git a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs new file mode 100644 index 0000000000..2665d6dd0b --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; + +namespace Avalonia.Rendering +{ + /// + /// Defines the interface implemented by an application render timer. + /// + public interface IRenderTimer + { + /// + /// Raised when the render timer ticks to signal a new frame should be drawn. + /// + /// + /// This event can be raised on any thread; it is the responsibility of the subscriber to + /// switch execution to the right thread. + /// + event EventHandler Tick; + } +} \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs new file mode 100644 index 0000000000..25febf8187 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using Avalonia.Logging; +using Avalonia.Threading; + +namespace Avalonia.Rendering +{ + public class RenderLoop : IRenderLoop + { + private readonly IDispatcher _dispatcher; + private List _items = new List(); + private IRenderTimer _timer; + private volatile bool inTick; + + public RenderLoop() + { + _dispatcher = Dispatcher.UIThread; + } + + public RenderLoop(IRenderTimer timer, IDispatcher dispatcher) + { + _timer = timer; + _dispatcher = dispatcher; + } + + protected IRenderTimer Timer + { + get + { + if (_timer == null) + { + _timer = AvaloniaLocator.Current.GetService(); + } + + return _timer; + } + } + + public void Add(IRenderLoopTask i) + { + Contract.Requires(i != null); + Dispatcher.UIThread.VerifyAccess(); + + _items.Add(i); + + if (_items.Count == 1) + { + Timer.Tick += TimerTick; + } + } + + public void Remove(IRenderLoopTask i) + { + Contract.Requires(i != null); + Dispatcher.UIThread.VerifyAccess(); + + _items.Remove(i); + + if (_items.Count == 0) + { + Timer.Tick -= TimerTick; + } + } + + private async void TimerTick(object sender, EventArgs e) + { + if (!inTick) + { + inTick = true; + + try + { + var needsUpdate = false; + + foreach (var i in _items) + { + if (i.NeedsUpdate) + { + needsUpdate = true; + break; + } + } + + if (needsUpdate) + { + await _dispatcher.InvokeAsync(() => + { + foreach (var i in _items) + { + i.Update(); + } + }); + } + + foreach (var i in _items) + { + i.Render(); + } + } + catch (Exception ex) + { + Logger.Error(LogArea.Visual, this, "Exception in render loop: {Error}", ex); + } + finally + { + inTick = false; + } + } + } + } +} diff --git a/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs b/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs index ca8a1ad3a4..bbb6a01c05 100644 --- a/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs +++ b/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs @@ -52,7 +52,8 @@ namespace Avalonia.Gtk3 .Bind().ToConstant(Instance) .Bind().ToConstant(Instance) .Bind().ToSingleton() - .Bind().ToConstant(new DefaultRenderLoop(60)) + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(new DefaultRenderTimer(60)) .Bind().ToConstant(new PlatformIconLoader()); } diff --git a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs index 810be77b2b..9046c26cee 100644 --- a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs +++ b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs @@ -35,7 +35,8 @@ namespace Avalonia.LinuxFramebuffer .Bind().ToConstant(KeyboardDevice) .Bind().ToSingleton() .Bind().ToConstant(Threading) - .Bind().ToConstant(Threading); + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(Threading); } internal static TopLevel Initialize(T builder, string fbdev = null) where T : AppBuilderBase, new() diff --git a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs index ba45ad8403..5dbf18b1de 100644 --- a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs +++ b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs @@ -21,6 +21,7 @@ namespace Avalonia.MonoMac private static bool s_monoMacInitialized; private static bool s_showInDock = true; private static IRenderLoop s_renderLoop; + private static IRenderTimer s_renderTimer; void DoInitialize() { @@ -35,6 +36,7 @@ namespace Avalonia.MonoMac .Bind().ToSingleton() .Bind().ToSingleton() .Bind().ToConstant(s_renderLoop) + .Bind().ToConstant(s_renderTimer) .Bind().ToConstant(PlatformThreadingInterface.Instance) /*.Bind().ToTransient()*/; } @@ -83,7 +85,8 @@ namespace Avalonia.MonoMac ThreadHelper.InitializeCocoaThreadingLocks(); App = NSApplication.SharedApplication; UpdateActivationPolicy(); - s_renderLoop = new RenderLoop(); //TODO: use CVDisplayLink + s_renderLoop = new RenderLoop(); + s_renderTimer = new RenderTimer(); //TODO: use CVDisplayLink s_monoMacInitialized = true; } diff --git a/src/OSX/Avalonia.MonoMac/RenderLoop.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs similarity index 91% rename from src/OSX/Avalonia.MonoMac/RenderLoop.cs rename to src/OSX/Avalonia.MonoMac/RenderTimer.cs index 4d1f9b4201..d2fd50484a 100644 --- a/src/OSX/Avalonia.MonoMac/RenderLoop.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -6,12 +6,12 @@ using MonoMac.Foundation; namespace Avalonia.MonoMac { //TODO: Switch to using CVDisplayLink - public class RenderLoop : IRenderLoop + public class RenderTimer : IRenderTimer { private readonly object _lock = new object(); private readonly IDisposable _timer; - public RenderLoop() + public RenderTimer() { _timer = AvaloniaLocator.Current.GetService().StartSystemTimer(new TimeSpan(0, 0, 0, 0, 1000 / 60), () => diff --git a/src/Windows/Avalonia.Win32/RenderLoop.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs similarity index 88% rename from src/Windows/Avalonia.Win32/RenderLoop.cs rename to src/Windows/Avalonia.Win32/RenderTimer.cs index 7d7befcc33..321a745fae 100644 --- a/src/Windows/Avalonia.Win32/RenderLoop.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -5,11 +5,11 @@ using Avalonia.Win32.Interop; namespace Avalonia.Win32 { - internal class RenderLoop : DefaultRenderLoop + internal class RenderTimer : DefaultRenderTimer { private UnmanagedMethods.TimeCallback timerDelegate; - public RenderLoop(int framesPerSecond) + public RenderTimer(int framesPerSecond) : base(framesPerSecond) { } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 9afb1218af..d5eb97292a 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -82,7 +82,8 @@ namespace Avalonia.Win32 .Bind().ToConstant(WindowsKeyboardDevice.Instance) .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance) - .Bind().ToConstant(new RenderLoop(60)) + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(new RenderTimer(60)) .Bind().ToSingleton() .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance); diff --git a/tests/Avalonia.UnitTests/TestServices.cs b/tests/Avalonia.UnitTests/TestServices.cs index d990defe3d..d68f1d167a 100644 --- a/tests/Avalonia.UnitTests/TestServices.cs +++ b/tests/Avalonia.UnitTests/TestServices.cs @@ -64,7 +64,7 @@ namespace Avalonia.UnitTests Func mouseDevice = null, IRuntimePlatform platform = null, IPlatformRenderInterface renderInterface = null, - IRenderLoop renderLoop = null, + IRenderTimer renderLoop = null, IScheduler scheduler = null, IStandardCursorFactory standardCursorFactory = null, IStyler styler = null, @@ -115,7 +115,7 @@ namespace Avalonia.UnitTests Func mouseDevice = null, IRuntimePlatform platform = null, IPlatformRenderInterface renderInterface = null, - IRenderLoop renderLoop = null, + IRenderTimer renderLoop = null, IScheduler scheduler = null, IStandardCursorFactory standardCursorFactory = null, IStyler styler = null, diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs index 2350a31d5c..1af9a9499d 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs @@ -42,7 +42,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void First_Frame_Calls_SceneBuilder_UpdateAll() { - var loop = new Mock(); + var loop = new Mock(); var root = new TestRoot(); var sceneBuilder = MockSceneBuilder(root); @@ -198,7 +198,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Should_Create_Layer_For_Root() { - var loop = new Mock(); + var loop = new Mock(); var root = new TestRoot(); var rootLayer = new Mock(); @@ -374,7 +374,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering private void RunFrame(Mock loop) { - loop.Raise(x => x.Tick += null, EventArgs.Empty); + //loop.Raise(x => x.Tick += null, EventArgs.Empty); } private IRenderTargetBitmapImpl CreateLayer() From 8ec2c8f661839f7826e2e262431c0b7debe4ad0e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 Jun 2018 10:36:35 +0200 Subject: [PATCH 03/56] Notify tick count in IRenderTimer.Tick. --- .../Platform/InternalPlatformThreadingInterface.cs | 4 ++-- src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs | 12 ++++++------ src/Avalonia.Visuals/Rendering/IRenderTimer.cs | 2 +- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 2 +- src/OSX/Avalonia.MonoMac/RenderTimer.cs | 4 ++-- .../Avalonia.Win32/Interop/UnmanagedMethods.cs | 3 +++ src/Windows/Avalonia.Win32/RenderTimer.cs | 8 ++++++-- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index 47e600b9c8..501e15653a 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -14,7 +14,7 @@ namespace Avalonia.Controls.Platform public InternalPlatformThreadingInterface() { TlsCurrentThreadIsLoopThread = true; - StartTimer(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 0, 66), () => Tick?.Invoke(this, new EventArgs())); + StartTimer(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 0, 66), () => Tick?.Invoke(Environment.TickCount)); } private readonly AutoResetEvent _signaled = new AutoResetEvent(false); @@ -105,7 +105,7 @@ namespace Avalonia.Controls.Platform public bool CurrentThreadIsLoopThread => TlsCurrentThreadIsLoopThread; public event Action Signaled; - public event EventHandler Tick; + public event Action Tick; } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index 7ad8915bc4..6b16ff8038 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -18,7 +18,7 @@ namespace Avalonia.Rendering { private IRuntimePlatform _runtime; private int _subscriberCount; - private EventHandler _tick; + private Action _tick; private IDisposable _subscription; /// @@ -38,7 +38,7 @@ namespace Avalonia.Rendering public int FramesPerSecond { get; } /// - public event EventHandler Tick + public event Action Tick { add { @@ -77,14 +77,14 @@ namespace Avalonia.Rendering /// This can be overridden by platform implementations to use a specialized timer /// implementation. /// - protected virtual IDisposable StartCore(Action tick) + protected virtual IDisposable StartCore(Action tick) { if (_runtime == null) { _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), tick); + return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); } /// @@ -96,9 +96,9 @@ namespace Avalonia.Rendering _subscription = null; } - private void InternalTick() + private void InternalTick(long tickCount) { - _tick(this, EventArgs.Empty); + _tick(tickCount); } } } diff --git a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs index 2665d6dd0b..78f6183994 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs @@ -15,6 +15,6 @@ namespace Avalonia.Rendering /// This event can be raised on any thread; it is the responsibility of the subscriber to /// switch execution to the right thread. /// - event EventHandler Tick; + event Action Tick; } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 25febf8187..bdcfdcba8e 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -62,7 +62,7 @@ namespace Avalonia.Rendering } } - private async void TimerTick(object sender, EventArgs e) + private async void TimerTick(long tickCount) { if (!inTick) { diff --git a/src/OSX/Avalonia.MonoMac/RenderTimer.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs index d2fd50484a..71f657b690 100644 --- a/src/OSX/Avalonia.MonoMac/RenderTimer.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -20,12 +20,12 @@ namespace Avalonia.MonoMac { using (new NSAutoreleasePool()) { - Tick?.Invoke(this, EventArgs.Empty); + Tick?.Invoke(Environment.TickCount); } } }); } - public event EventHandler Tick; + public event Action Tick; } } diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index f89086ccb7..1c70b3f03d 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -972,6 +972,9 @@ namespace Avalonia.Win32.Interop [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetMonitorInfo([In] IntPtr hMonitor, [Out] MONITORINFO lpmi); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool QueryPerformanceCounter(out long lpPerformanceCount); + [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PostMessageW")] public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index 321a745fae..cea1bf94a5 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -14,9 +14,13 @@ namespace Avalonia.Win32 { } - protected override IDisposable StartCore(Action tick) + protected override IDisposable StartCore(Action tick) { - timerDelegate = (id, uMsg, user, dw1, dw2) => tick(); + timerDelegate = (id, uMsg, user, dw1, dw2) => + { + UnmanagedMethods.QueryPerformanceCounter(out long tickCount); + tick(tickCount); + }; var handle = UnmanagedMethods.timeSetEvent( (uint)(1000 / FramesPerSecond), From 166f9f8cf01d27dd8a9afa040497c357f0e5eb6b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 Jun 2018 11:02:50 +0200 Subject: [PATCH 04/56] Pulse animation timer from render loop. --- src/Avalonia.Animation/Timing.cs | 35 +++++++++----------- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 4 ++- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/Avalonia.Animation/Timing.cs b/src/Avalonia.Animation/Timing.cs index 6367425911..d6a353fb34 100644 --- a/src/Avalonia.Animation/Timing.cs +++ b/src/Avalonia.Animation/Timing.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using System.Reactive.Linq; +using Avalonia.Reactive; using Avalonia.Threading; namespace Avalonia.Animation @@ -13,42 +14,38 @@ namespace Avalonia.Animation /// public static class Timing { - /// - /// The number of frames per second. - /// - public const int FramesPerSecond = 60; - - /// - /// The time span of each frame. - /// - internal static readonly TimeSpan FrameTick = TimeSpan.FromSeconds(1.0 / FramesPerSecond); + static TimerObservable _timer = new TimerObservable(); /// /// Initializes static members of the class. /// static Timing() { - var globalTimer = Observable.Interval(FrameTick, AvaloniaScheduler.Instance); - - AnimationsTimer = globalTimer - .Select(_ => GetTickCount()) + AnimationsTimer = _timer .Publish() .RefCount(); } + public static bool HasSubscriptions => _timer.HasSubscriptions; + internal static TimeSpan GetTickCount() => TimeSpan.FromMilliseconds(Environment.TickCount); /// /// Gets the animation timer. /// - /// - /// The animation timer triggers usually at 60 times per second or as - /// defined in . - /// The parameter passed to a subsciber is the current playstate of the animation. - /// internal static IObservable AnimationsTimer { get; } + + public static void Pulse(long tickCount) => _timer.Pulse(tickCount); + + private class TimerObservable : LightweightObservableBase + { + public bool HasSubscriptions { get; private set; } + public void Pulse(long tickCount) => PublishNext(TimeSpan.FromMilliseconds(tickCount)); + protected override void Initialize() => HasSubscriptions = true; + protected override void Deinitialize() => HasSubscriptions = false; + } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index bdcfdcba8e..5cac6993ff 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -70,7 +70,7 @@ namespace Avalonia.Rendering try { - var needsUpdate = false; + var needsUpdate = Animation.Timing.HasSubscriptions; foreach (var i in _items) { @@ -85,6 +85,8 @@ namespace Avalonia.Rendering { await _dispatcher.InvokeAsync(() => { + Animation.Timing.Pulse(tickCount); + foreach (var i in _items) { i.Update(); From c7c9b0a2052488c8642fe155c80a7e4686c0dbe1 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 29 Jun 2018 23:14:00 +0200 Subject: [PATCH 05/56] Use `Stopwatch.GetTimestamp()` for tickCount. --- src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index 6b16ff8038..b05ecd5456 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Diagnostics; using System.Threading.Tasks; using Avalonia.Platform; @@ -84,7 +85,7 @@ namespace Avalonia.Rendering _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); + return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Stopwatch.GetTimestamp())); } /// From 33aec77f16313cfc6beb4f28701de4d3adc1411f Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sat, 30 Jun 2018 18:31:35 +0800 Subject: [PATCH 06/56] Move the playstate handling to the State machine instead of being pipelined by the timer itself. Removed unused AnimationStateTimer. --- src/Avalonia.Animation/Animatable.cs | 2 +- src/Avalonia.Animation/Timing.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index 8a1a17a6fc..e51103aa55 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -74,4 +74,4 @@ namespace Avalonia.Animation } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Timing.cs b/src/Avalonia.Animation/Timing.cs index d6a353fb34..8ee78018a9 100644 --- a/src/Avalonia.Animation/Timing.cs +++ b/src/Avalonia.Animation/Timing.cs @@ -12,7 +12,7 @@ namespace Avalonia.Animation /// /// Provides global timing functions for animations. /// - public static class Timing + public class Timing { static TimerObservable _timer = new TimerObservable(); From f2ba884e0ad3d210660bb60f8e387b652aa5d33b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 27 Jul 2018 22:45:25 -0500 Subject: [PATCH 07/56] Cleanup logic in LightweightObservableBase. --- .../Reactive/LightweightObservableBase.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Avalonia.Base/Reactive/LightweightObservableBase.cs b/src/Avalonia.Base/Reactive/LightweightObservableBase.cs index a2786d63da..41009e4cd3 100644 --- a/src/Avalonia.Base/Reactive/LightweightObservableBase.cs +++ b/src/Avalonia.Base/Reactive/LightweightObservableBase.cs @@ -82,18 +82,10 @@ namespace Avalonia.Reactive if (observers.Count == 0) { observers.TrimExcess(); + Deinitialize(); } - else - { - return; - } - } else - { - return; } } - - Deinitialize(); } } From 87e98cacf994eab709c368f283d8f58293926cfa Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 27 Jul 2018 23:57:44 -0500 Subject: [PATCH 08/56] Rewrite Win32 RenderTimer to use undeprecated APIs. --- .../Interop/UnmanagedMethods.cs | 28 +++++++++---- src/Windows/Avalonia.Win32/RenderTimer.cs | 41 +++++++++++++------ 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index 1c70b3f03d..d6b95bc7b0 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -26,6 +26,9 @@ namespace Avalonia.Win32.Interop public delegate void TimeCallback(uint uTimerID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void WaitOrTimerCallback(IntPtr lpParameter, bool timerOrWaitFired); + public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); public enum Cursor @@ -848,11 +851,25 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow); - [DllImport("Winmm.dll")] - public static extern uint timeKillEvent(uint uTimerID); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr CreateTimerQueue(); + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool DeleteTimerQueueEx(IntPtr TimerQueue, IntPtr CompletionEvent); + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool CreateTimerQueueTimer( + out IntPtr phNewTimer, + IntPtr TimerQueue, + WaitOrTimerCallback Callback, + IntPtr Parameter, + uint DueTime, + uint Period, + uint Flags); - [DllImport("Winmm.dll")] - public static extern uint timeSetEvent(uint uDelay, uint uResolution, TimeCallback lpTimeProc, UIntPtr dwUser, uint fuEvent); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool DeleteTimerQueueTimer(IntPtr TimerQueue, IntPtr Timer, IntPtr CompletionEvent); [DllImport("user32.dll")] public static extern int ToUnicode( @@ -972,9 +989,6 @@ namespace Avalonia.Win32.Interop [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetMonitorInfo([In] IntPtr hMonitor, [Out] MONITORINFO lpmi); - [DllImport("kernel32.dll", SetLastError = true)] - public static extern bool QueryPerformanceCounter(out long lpPerformanceCount); - [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PostMessageW")] public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index cea1bf94a5..0cb107d3a9 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -1,5 +1,6 @@ using System; using System.Reactive.Disposables; +using System.Threading; using Avalonia.Rendering; using Avalonia.Win32.Interop; @@ -7,7 +8,21 @@ namespace Avalonia.Win32 { internal class RenderTimer : DefaultRenderTimer { - private UnmanagedMethods.TimeCallback timerDelegate; + private UnmanagedMethods.WaitOrTimerCallback timerDelegate; + + private static IntPtr _timerQueue; + + private static void EnsureTimerQueueCreated() + { + if (Volatile.Read(ref _timerQueue) == null) + { + var queue = UnmanagedMethods.CreateTimerQueue(); + if (Interlocked.CompareExchange(ref _timerQueue, queue, IntPtr.Zero) != IntPtr.Zero) + { + UnmanagedMethods.DeleteTimerQueueEx(queue, IntPtr.Zero); + } + } + } public RenderTimer(int framesPerSecond) : base(framesPerSecond) @@ -16,23 +31,25 @@ namespace Avalonia.Win32 protected override IDisposable StartCore(Action tick) { - timerDelegate = (id, uMsg, user, dw1, dw2) => - { - UnmanagedMethods.QueryPerformanceCounter(out long tickCount); - tick(tickCount); - }; + EnsureTimerQueueCreated(); + var msPerFrame = 1000 / FramesPerSecond; + + timerDelegate = (_, __) => tick(TimeStampToFrames()); - var handle = UnmanagedMethods.timeSetEvent( - (uint)(1000 / FramesPerSecond), - 0, + UnmanagedMethods.CreateTimerQueueTimer( + out var timer, + _timerQueue, timerDelegate, - UIntPtr.Zero, - 1); + IntPtr.Zero, + (uint)msPerFrame, + (uint)msPerFrame, + 0 + ); return Disposable.Create(() => { timerDelegate = null; - UnmanagedMethods.timeKillEvent(handle); + UnmanagedMethods.DeleteTimerQueueTimer(_timerQueue, timer, IntPtr.Zero); }); } } From e24a125ec0b4add42060e25935521e5557a644f7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Sat, 28 Jul 2018 00:49:13 -0500 Subject: [PATCH 09/56] Make MonoMac platform use DefaultRenderTimer infrastructure in its RenderTimer. --- src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs | 4 ++-- src/OSX/Avalonia.MonoMac/RenderTimer.cs | 21 +++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs index 5dbf18b1de..5757413b7a 100644 --- a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs +++ b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs @@ -86,7 +86,7 @@ namespace Avalonia.MonoMac App = NSApplication.SharedApplication; UpdateActivationPolicy(); s_renderLoop = new RenderLoop(); - s_renderTimer = new RenderTimer(); //TODO: use CVDisplayLink + s_renderTimer = new RenderTimer(60); //TODO: use CVDisplayLink s_monoMacInitialized = true; } @@ -136,4 +136,4 @@ namespace Avalonia return builder.UseWindowingSubsystem(MonoMac.MonoMacPlatform.Initialize, "MonoMac"); } } -} \ No newline at end of file +} diff --git a/src/OSX/Avalonia.MonoMac/RenderTimer.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs index 71f657b690..22ad2e81a2 100644 --- a/src/OSX/Avalonia.MonoMac/RenderTimer.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -6,26 +6,23 @@ using MonoMac.Foundation; namespace Avalonia.MonoMac { //TODO: Switch to using CVDisplayLink - public class RenderTimer : IRenderTimer + public class RenderTimer : DefaultRenderTimer { - private readonly object _lock = new object(); - private readonly IDisposable _timer; + public RenderTimer(int framesPerSecond) : base(framesPerSecond) + { + } - public RenderTimer() + protected override IDisposable StartCore(Action tick) { - _timer = AvaloniaLocator.Current.GetService().StartSystemTimer(new TimeSpan(0, 0, 0, 0, 1000 / 60), + return AvaloniaLocator.Current.GetService().StartSystemTimer( + TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => { - lock (_lock) + using (new NSAutoreleasePool()) { - using (new NSAutoreleasePool()) - { - Tick?.Invoke(Environment.TickCount); - } + tick?.Invoke(Environment.TickCount); } }); } - - public event Action Tick; } } From 179ea4eca9666e53dfc678abb4d1f670128a3243 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 10 Aug 2018 21:38:54 +0200 Subject: [PATCH 10/56] inTick doesn't need to be volatile. --- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 5cac6993ff..076e8bbd33 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -10,7 +10,7 @@ namespace Avalonia.Rendering private readonly IDispatcher _dispatcher; private List _items = new List(); private IRenderTimer _timer; - private volatile bool inTick; + private bool inTick; public RenderLoop() { From 25b72971880093256d8773c19f950a8c470f1a9f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 10 Aug 2018 21:46:11 +0200 Subject: [PATCH 11/56] Added RenderLoop docs. --- src/Avalonia.Visuals/Rendering/IRenderLoop.cs | 22 ++++++++++++++++++- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 20 +++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs index bd1086d178..dd7442e7f8 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs @@ -1,8 +1,28 @@ namespace Avalonia.Rendering { + /// + /// The application render loop. + /// + /// + /// The render loop is responsible for advancing the animation timer and updating the scene + /// graph for visible windows. + /// public interface IRenderLoop { + /// + /// Adds an update task. + /// + /// The update task. + /// + /// Registered update tasks will be polled on each tick of the render loop after the + /// animation timer has been pulsed. + /// void Add(IRenderLoopTask i); + + /// + /// Removes an update task. + /// + /// The update task. void Remove(IRenderLoopTask i); } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 076e8bbd33..98fc12b4b1 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -5,6 +5,13 @@ using Avalonia.Threading; namespace Avalonia.Rendering { + /// + /// The application render loop. + /// + /// + /// The render loop is responsible for advancing the animation timer and updating the scene + /// graph for visible windows. + /// public class RenderLoop : IRenderLoop { private readonly IDispatcher _dispatcher; @@ -12,17 +19,28 @@ namespace Avalonia.Rendering private IRenderTimer _timer; private bool inTick; + /// + /// Initializes a new instance of the class. + /// public RenderLoop() { _dispatcher = Dispatcher.UIThread; } + /// + /// Initializes a new instance of the class. + /// + /// The render timer. + /// The UI thread dispatcher. public RenderLoop(IRenderTimer timer, IDispatcher dispatcher) { _timer = timer; _dispatcher = dispatcher; } + /// + /// Gets the render timer. + /// protected IRenderTimer Timer { get @@ -36,6 +54,7 @@ namespace Avalonia.Rendering } } + /// public void Add(IRenderLoopTask i) { Contract.Requires(i != null); @@ -49,6 +68,7 @@ namespace Avalonia.Rendering } } + /// public void Remove(IRenderLoopTask i) { Contract.Requires(i != null); From 5e1e25e6fa8a17e7ead7c55b1b0b17b73b0fbb05 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 17:31:05 -0500 Subject: [PATCH 12/56] Clean up render timers to use Environment.TickCount. --- src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs | 2 +- src/Windows/Avalonia.Win32/RenderTimer.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index b05ecd5456..a83334ff5e 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -85,7 +85,7 @@ namespace Avalonia.Rendering _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Stopwatch.GetTimestamp())); + return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); } /// diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index 0cb107d3a9..c911bc3adf 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -34,7 +34,7 @@ namespace Avalonia.Win32 EnsureTimerQueueCreated(); var msPerFrame = 1000 / FramesPerSecond; - timerDelegate = (_, __) => tick(TimeStampToFrames()); + timerDelegate = (_, __) => tick(Environment.TickCount); UnmanagedMethods.CreateTimerQueueTimer( out var timer, From c5898b98af57b33b2db31a5de19680eedc5b1349 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 17:31:37 -0500 Subject: [PATCH 13/56] Remove unused field. --- .../Rendering/DeferredRenderer.cs | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index f937964994..fcc90edd43 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -32,7 +32,6 @@ namespace Avalonia.Rendering private volatile IRef _scene; private DirtyVisuals _dirty; private IRef _overlay; - private bool _updateQueued; private object _rendering = new object(); private int _lastSceneId = -1; private DisplayDirtyRects _dirtyRectsDisplay = new DisplayDirtyRects(); @@ -394,42 +393,34 @@ namespace Avalonia.Rendering private void UpdateScene() { Dispatcher.UIThread.VerifyAccess(); - - try + if (_root.IsVisible) { - if (_root.IsVisible) - { - var sceneRef = RefCountable.Create(_scene?.Item.CloneScene() ?? new Scene(_root)); - var scene = sceneRef.Item; + var sceneRef = RefCountable.Create(_scene?.Item.CloneScene() ?? new Scene(_root)); + var scene = sceneRef.Item; - if (_dirty == null) - { - _dirty = new DirtyVisuals(); - _sceneBuilder.UpdateAll(scene); - } - else if (_dirty.Count > 0) + if (_dirty == null) + { + _dirty = new DirtyVisuals(); + _sceneBuilder.UpdateAll(scene); + } + else if (_dirty.Count > 0) + { + foreach (var visual in _dirty) { - foreach (var visual in _dirty) - { - _sceneBuilder.Update(scene, visual); - } + _sceneBuilder.Update(scene, visual); } + } - var oldScene = Interlocked.Exchange(ref _scene, sceneRef); - oldScene?.Dispose(); + var oldScene = Interlocked.Exchange(ref _scene, sceneRef); + oldScene?.Dispose(); - _dirty.Clear(); - (_root as IRenderRoot)?.Invalidate(new Rect(scene.Size)); - } - else - { - var oldScene = Interlocked.Exchange(ref _scene, null); - oldScene?.Dispose(); - } + _dirty.Clear(); + (_root as IRenderRoot)?.Invalidate(new Rect(scene.Size)); } - finally + else { - _updateQueued = false; + var oldScene = Interlocked.Exchange(ref _scene, null); + oldScene?.Dispose(); } } From 6fe8dba8d957648109978237d9aa665d9524f967 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 22:23:13 -0500 Subject: [PATCH 14/56] Remove timing class and create Clock classes that can hook into the render loop. --- src/Avalonia.Animation/AnimationInstance`1.cs | 5 +- src/Avalonia.Animation/Clock.cs | 47 +++++++++++++++++ src/Avalonia.Animation/Timing.cs | 51 ------------------- src/Avalonia.Animation/TransitionInstance.cs | 7 ++- .../Animation/RenderLoopClock.cs | 21 ++++++++ .../Rendering/DeferredRenderer.cs | 2 +- .../Rendering/IRenderLoopTask.cs | 15 +++++- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 17 +++---- src/Windows/Avalonia.Win32/Win32Platform.cs | 6 +++ 9 files changed, 101 insertions(+), 70 deletions(-) create mode 100644 src/Avalonia.Animation/Clock.cs delete mode 100644 src/Avalonia.Animation/Timing.cs create mode 100644 src/Avalonia.Visuals/Animation/RenderLoopClock.cs diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index 5a72904ed2..a8d8dc73ef 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -82,8 +82,7 @@ namespace Avalonia.Animation protected override void Subscribed() { - _timerSubscription = Timing.AnimationsTimer - .Subscribe(p => this.Step(p)); + _timerSubscription = Clock.GlobalClock.Subscribe(Step); } public void Step(TimeSpan frameTick) @@ -225,4 +224,4 @@ namespace Avalonia.Animation } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs new file mode 100644 index 0000000000..4c37d663b1 --- /dev/null +++ b/src/Avalonia.Animation/Clock.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Text; +using Avalonia.Reactive; + +namespace Avalonia.Animation +{ + public class Clock : IObservable + { + public static Clock GlobalClock => AvaloniaLocator.Current.GetService(); + + private ClockObservable _observable; + + private IObservable _connectedObservable; + + public Clock() + { + _observable = new ClockObservable(); + _connectedObservable = _observable.Publish().RefCount(); + } + + public bool HasSubscriptions => _observable.HasSubscriptions; + + public TimeSpan CurrentTime { get; private set; } + + public void Pulse(long tickCount) + { + var time = TimeSpan.FromMilliseconds(tickCount); + _observable.Pulse(time); + CurrentTime = time; + } + + public IDisposable Subscribe(IObserver observer) + { + return _connectedObservable.Subscribe(observer); + } + + private class ClockObservable : LightweightObservableBase + { + public bool HasSubscriptions { get; private set; } + public void Pulse(TimeSpan tickCount) => PublishNext(tickCount); + protected override void Initialize() => HasSubscriptions = true; + protected override void Deinitialize() => HasSubscriptions = false; + } + } +} diff --git a/src/Avalonia.Animation/Timing.cs b/src/Avalonia.Animation/Timing.cs deleted file mode 100644 index 8ee78018a9..0000000000 --- a/src/Avalonia.Animation/Timing.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -using System; -using System.Linq; -using System.Reactive.Linq; -using Avalonia.Reactive; -using Avalonia.Threading; - -namespace Avalonia.Animation -{ - /// - /// Provides global timing functions for animations. - /// - public class Timing - { - static TimerObservable _timer = new TimerObservable(); - - /// - /// Initializes static members of the class. - /// - static Timing() - { - AnimationsTimer = _timer - .Publish() - .RefCount(); - } - - public static bool HasSubscriptions => _timer.HasSubscriptions; - - internal static TimeSpan GetTickCount() => TimeSpan.FromMilliseconds(Environment.TickCount); - - /// - /// Gets the animation timer. - /// - internal static IObservable AnimationsTimer - { - get; - } - - public static void Pulse(long tickCount) => _timer.Pulse(tickCount); - - private class TimerObservable : LightweightObservableBase - { - public bool HasSubscriptions { get; private set; } - public void Pulse(long tickCount) => PublishNext(TimeSpan.FromMilliseconds(tickCount)); - protected override void Initialize() => HasSubscriptions = true; - protected override void Deinitialize() => HasSubscriptions = false; - } - } -} diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index e2719cb472..4c61adea28 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -45,10 +45,9 @@ namespace Avalonia.Animation protected override void Subscribed() { - startTime = Timing.GetTickCount(); - timerSubscription = Timing.AnimationsTimer - .Subscribe(t => TimerTick(t)); + startTime = Clock.GlobalClock.CurrentTime; + timerSubscription = Clock.GlobalClock.Subscribe(TimerTick); PublishNext(0.0d); } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs new file mode 100644 index 0000000000..3d166035ec --- /dev/null +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Rendering; + +namespace Avalonia.Animation +{ + public class RenderLoopClock : Clock, IRenderLoopTask + { + bool IRenderLoopTask.NeedsUpdate => HasSubscriptions; + + void IRenderLoopTask.Render() + { + } + + void IRenderLoopTask.Update(long tickCount) + { + Pulse(tickCount); + } + } +} diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index fcc90edd43..3221dd85c6 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -166,7 +166,7 @@ namespace Avalonia.Rendering bool IRenderLoopTask.NeedsUpdate => _dirty == null || _dirty.Count > 0; - void IRenderLoopTask.Update() => UpdateScene(); + void IRenderLoopTask.Update(long tickCount) => UpdateScene(); void IRenderLoopTask.Render() { diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs index 2f251a5c17..b031bf00df 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs @@ -6,7 +6,20 @@ namespace Avalonia.Rendering public interface IRenderLoopTask { bool NeedsUpdate { get; } - void Update(); + void Update(long tickCount); void Render(); } + + public class MockRenderLoopTask : IRenderLoopTask + { + public bool NeedsUpdate => true; + + public void Render() + { + } + + public void Update(long tickCount) + { + } + } } diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 98fc12b4b1..a850b99c5e 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using Avalonia.Logging; using Avalonia.Threading; @@ -17,7 +18,7 @@ namespace Avalonia.Rendering private readonly IDispatcher _dispatcher; private List _items = new List(); private IRenderTimer _timer; - private bool inTick; + private int inTick; /// /// Initializes a new instance of the class. @@ -84,13 +85,11 @@ namespace Avalonia.Rendering private async void TimerTick(long tickCount) { - if (!inTick) + if (Interlocked.CompareExchange(ref inTick, 1, 0) == 0) { - inTick = true; - try { - var needsUpdate = Animation.Timing.HasSubscriptions; + var needsUpdate = false; foreach (var i in _items) { @@ -105,13 +104,11 @@ namespace Avalonia.Rendering { await _dispatcher.InvokeAsync(() => { - Animation.Timing.Pulse(tickCount); - foreach (var i in _items) { - i.Update(); + i.Update(tickCount); } - }); + }).ConfigureAwait(false); } foreach (var i in _items) @@ -125,7 +122,7 @@ namespace Avalonia.Rendering } finally { - inTick = false; + Interlocked.Exchange(ref inTick, 0); } } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index d5eb97292a..812a557765 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -9,6 +9,7 @@ using System.IO; using System.Reactive.Disposables; using System.Runtime.InteropServices; using System.Threading; +using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Controls.Platform; using Avalonia.Input; @@ -76,6 +77,8 @@ namespace Avalonia.Win32 public static void Initialize(bool deferredRendering = true) { + var clock = new RenderLoopClock(); + AvaloniaLocator.CurrentMutable .Bind().ToSingleton() .Bind().ToConstant(CursorFactory.Instance) @@ -84,6 +87,7 @@ namespace Avalonia.Win32 .Bind().ToConstant(s_instance) .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new RenderTimer(60)) + .Bind().ToConstant(clock) .Bind().ToSingleton() .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance); @@ -93,6 +97,8 @@ namespace Avalonia.Win32 if (OleContext.Current != null) AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); + + AvaloniaLocator.Current.GetService().Add(clock); } public bool HasMessages() From 5c8ced89a30e3339f070d75b3f361f8342ae99fa Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 22:52:41 -0500 Subject: [PATCH 15/56] Move GlobalPlayState tracking to Clock. --- src/Avalonia.Animation/Animation.cs | 7 ++++++- src/Avalonia.Animation/Clock.cs | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 2c359ecac3..7b3aa06ea0 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -17,10 +17,15 @@ namespace Avalonia.Animation /// public class Animation : AvaloniaList, IAnimation { + /// /// Gets or sets the animation play state for all animations /// - public static PlayState GlobalPlayState { get; set; } = PlayState.Run; + public static PlayState GlobalPlayState + { + get => AvaloniaLocator.Current.GetService().PlayState; + set => AvaloniaLocator.Current.GetService().PlayState = value; + } /// /// Gets or sets the active time of this animation. diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index 4c37d663b1..1ba6425551 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -24,6 +24,8 @@ namespace Avalonia.Animation public TimeSpan CurrentTime { get; private set; } + public PlayState PlayState { get; set; } + public void Pulse(long tickCount) { var time = TimeSpan.FromMilliseconds(tickCount); From a987c4648b77407c9b17519a52747d4868ae524a Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 22:57:06 -0500 Subject: [PATCH 16/56] Move Pause PlayState time tracking into Clock. --- src/Avalonia.Animation/AnimationInstance`1.cs | 26 +++--------------- src/Avalonia.Animation/Clock.cs | 27 ++++++++++++++++--- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index a8d8dc73ef..a2d25e4524 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -8,7 +8,7 @@ using Avalonia.Reactive; namespace Avalonia.Animation { /// - /// Handles interpolatoin and time-related functions + /// Handles interpolation and time-related functions /// for keyframe animations. /// internal class AnimationInstance : SingleSubscriberObservableBase @@ -30,8 +30,6 @@ namespace Avalonia.Animation private TimeSpan _delay; private TimeSpan _duration; private TimeSpan _firstFrameCount; - private TimeSpan _internalClock; - private TimeSpan? _previousClock; private Easings.Easing _easeFunc; private Action _onCompleteAction; private Func _interpolator; @@ -120,23 +118,6 @@ namespace Avalonia.Animation if (Animation.GlobalPlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) DoComplete(); - if (!_previousClock.HasValue) - { - _previousClock = systemTime; - _internalClock = TimeSpan.Zero; - } - else - { - if (Animation.GlobalPlayState == PlayState.Pause || _targetControl.PlayState == PlayState.Pause) - { - _previousClock = systemTime; - return; - } - var delta = systemTime - _previousClock; - _internalClock += delta.Value; - _previousClock = systemTime; - } - if (!_gotFirstKFValue) { _firstKFValue = (T)_parent.First().Value; @@ -145,7 +126,7 @@ namespace Avalonia.Animation if (!_gotFirstFrameCount) { - _firstFrameCount = _internalClock; + _firstFrameCount = systemTime; _gotFirstFrameCount = true; } } @@ -154,7 +135,7 @@ namespace Avalonia.Animation { DoPlayStatesAndTime(systemTime); - var time = _internalClock - _firstFrameCount; + var time = systemTime - _firstFrameCount; var delayEndpoint = _delay; var iterationEndpoint = delayEndpoint + _duration; @@ -179,7 +160,6 @@ namespace Avalonia.Animation } else { - _previousClock = systemTime; return; } diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index 1ba6425551..ced63163c9 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -14,6 +14,9 @@ namespace Avalonia.Animation private IObservable _connectedObservable; + private TimeSpan? _previousTime; + private TimeSpan _internalTime; + public Clock() { _observable = new ClockObservable(); @@ -28,9 +31,27 @@ namespace Avalonia.Animation public void Pulse(long tickCount) { - var time = TimeSpan.FromMilliseconds(tickCount); - _observable.Pulse(time); - CurrentTime = time; + var systemTime = TimeSpan.FromMilliseconds(tickCount); + + if (!_previousTime.HasValue) + { + _previousTime = systemTime; + _internalTime = TimeSpan.Zero; + } + else + { + if (PlayState == PlayState.Pause) + { + _previousTime = systemTime; + return; + } + var delta = systemTime - _previousTime; + _internalTime += delta.Value; + _previousTime = systemTime; + } + + _observable.Pulse(_internalTime); + CurrentTime = _internalTime; } public IDisposable Subscribe(IObserver observer) From 9b9676d3fe346610fdc05eededd2c90253430bd7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:03:53 -0500 Subject: [PATCH 17/56] Enable chaining clocks. --- src/Avalonia.Animation/Clock.cs | 16 +++++++++++----- .../Animation/RenderLoopClock.cs | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index ced63163c9..2de781ea57 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -14,25 +14,31 @@ namespace Avalonia.Animation private IObservable _connectedObservable; + private IDisposable _parentSubscription; + private TimeSpan? _previousTime; private TimeSpan _internalTime; - public Clock() + protected Clock() { _observable = new ClockObservable(); _connectedObservable = _observable.Publish().RefCount(); } + public Clock(Clock parent) + :this() + { + _parentSubscription = parent.Subscribe(Pulse); + } + public bool HasSubscriptions => _observable.HasSubscriptions; public TimeSpan CurrentTime { get; private set; } public PlayState PlayState { get; set; } - public void Pulse(long tickCount) + protected void Pulse(TimeSpan systemTime) { - var systemTime = TimeSpan.FromMilliseconds(tickCount); - if (!_previousTime.HasValue) { _previousTime = systemTime; @@ -62,7 +68,7 @@ namespace Avalonia.Animation private class ClockObservable : LightweightObservableBase { public bool HasSubscriptions { get; private set; } - public void Pulse(TimeSpan tickCount) => PublishNext(tickCount); + public void Pulse(TimeSpan time) => PublishNext(time); protected override void Initialize() => HasSubscriptions = true; protected override void Deinitialize() => HasSubscriptions = false; } diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index 3d166035ec..d60d366ad4 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -15,7 +15,7 @@ namespace Avalonia.Animation void IRenderLoopTask.Update(long tickCount) { - Pulse(tickCount); + Pulse(TimeSpan.FromMilliseconds(tickCount)); } } } From bf1b78c4ab0da4ccb60a1d5456da50a2bf5092f9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:39:25 -0500 Subject: [PATCH 18/56] Enable Animations to run on a non-global clock. --- src/Avalonia.Animation/Animation.cs | 15 ++++++++++----- src/Avalonia.Animation/AnimationInstance`1.cs | 8 +++++--- src/Avalonia.Animation/Animator`1.cs | 8 ++++---- src/Avalonia.Animation/Clock.cs | 10 ++++++++++ src/Avalonia.Animation/IAnimation.cs | 4 ++-- src/Avalonia.Animation/IAnimator.cs | 2 +- src/Avalonia.Styling/Styling/Style.cs | 2 +- src/Avalonia.Visuals/Animation/RenderLoopClock.cs | 5 +++++ .../Animation/TransformAnimator.cs | 6 +++--- 9 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 7b3aa06ea0..e787143b59 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -154,12 +154,12 @@ namespace Avalonia.Animation } /// - public IDisposable Apply(Animatable control, IObservable match, Action onComplete) + public IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete) { var (animators, subscriptions) = InterpretKeyframes(control); if (animators.Count == 1) { - subscriptions.Add(animators[0].Apply(this, control, match, onComplete)); + subscriptions.Add(animators[0].Apply(this, control, clock, match, onComplete)); } else { @@ -173,7 +173,7 @@ namespace Avalonia.Animation animatorOnComplete = () => tcs.SetResult(null); completionTasks.Add(tcs.Task); } - subscriptions.Add(animator.Apply(this, control, match, animatorOnComplete)); + subscriptions.Add(animator.Apply(this, control, clock, match, animatorOnComplete)); } if (onComplete != null) @@ -185,15 +185,20 @@ namespace Avalonia.Animation } /// - public Task RunAsync(Animatable control) + public Task RunAsync(Animatable control, Clock clock = null) { + if (clock == null) + { + clock = Clock.GlobalClock; + } + var run = new TaskCompletionSource(); if (this.RepeatCount == RepeatCount.Loop) run.SetException(new InvalidOperationException("Looping animations must not use the Run method.")); IDisposable subscriptions = null; - subscriptions = this.Apply(control, Observable.Return(true), () => + subscriptions = this.Apply(control, clock, Observable.Return(true), () => { run.SetResult(null); subscriptions?.Dispose(); diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index a2d25e4524..fe84dd879a 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -34,8 +34,9 @@ namespace Avalonia.Animation private Action _onCompleteAction; private Func _interpolator; private IDisposable _timerSubscription; + private readonly Clock _clock; - public AnimationInstance(Animation animation, Animatable control, Animator animator, Action OnComplete, Func Interpolator) + public AnimationInstance(Animation animation, Animatable control, Animator animator, Clock clock, Action OnComplete, Func Interpolator) { if (animation.SpeedRatio <= 0) throw new InvalidOperationException("Speed ratio cannot be negative or zero."); @@ -71,6 +72,7 @@ namespace Avalonia.Animation _fillMode = animation.FillMode; _onCompleteAction = OnComplete; _interpolator = Interpolator; + _clock = clock; } protected override void Unsubscribed() @@ -80,7 +82,7 @@ namespace Avalonia.Animation protected override void Subscribed() { - _timerSubscription = Clock.GlobalClock.Subscribe(Step); + _timerSubscription = _clock.Subscribe(Step); } public void Step(TimeSpan frameTick) @@ -115,7 +117,7 @@ namespace Avalonia.Animation private void DoPlayStatesAndTime(TimeSpan systemTime) { - if (Animation.GlobalPlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) + if (_clock.PlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) DoComplete(); if (!_gotFirstKFValue) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index f0ef55aa9e..c699ff635a 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -32,7 +32,7 @@ namespace Avalonia.Animation } /// - public virtual IDisposable Apply(Animation animation, Animatable control, IObservable match, Action onComplete) + public virtual IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable match, Action onComplete) { if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); @@ -41,7 +41,7 @@ namespace Avalonia.Animation .Where(p => p) .Subscribe(_ => { - var timerObs = RunKeyFrames(animation, control, onComplete); + var timerObs = RunKeyFrames(animation, control, clock, onComplete); }); } @@ -101,9 +101,9 @@ namespace Avalonia.Animation /// /// Runs the KeyFrames Animation. /// - private IDisposable RunKeyFrames(Animation animation, Animatable control, Action onComplete) + private IDisposable RunKeyFrames(Animation animation, Animatable control, Clock clock, Action onComplete) { - var instance = new AnimationInstance(animation, control, this, onComplete, DoInterpolation); + var instance = new AnimationInstance(animation, control, this, clock, onComplete, DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); } diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index 2de781ea57..e8616c9694 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -58,6 +58,16 @@ namespace Avalonia.Animation _observable.Pulse(_internalTime); CurrentTime = _internalTime; + + if (PlayState == PlayState.Stop) + { + Stop(); + } + } + + protected virtual void Stop() + { + _parentSubscription?.Dispose(); } public IDisposable Subscribe(IObserver observer) diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index 1d545a322a..f726cf43dc 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -11,11 +11,11 @@ namespace Avalonia.Animation /// /// Apply the animation to the specified control /// - IDisposable Apply(Animatable control, IObservable match, Action onComplete = null); + IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete = null); /// /// Run the animation to the specified control /// - Task RunAsync(Animatable control); + Task RunAsync(Animatable control, Clock clock); } } diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 9a4da35a02..134b30a555 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -16,6 +16,6 @@ namespace Avalonia.Animation /// /// Applies the current KeyFrame group to the specified control. /// - IDisposable Apply(Animation animation, Animatable control, IObservable obsMatch, Action onComplete); + IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete); } } diff --git a/src/Avalonia.Styling/Styling/Style.cs b/src/Avalonia.Styling/Styling/Style.cs index 399be5470d..a033184588 100644 --- a/src/Avalonia.Styling/Styling/Style.cs +++ b/src/Avalonia.Styling/Styling/Style.cs @@ -120,7 +120,7 @@ namespace Avalonia.Styling obsMatch = Observable.Return(true); } - var sub = animation.Apply((Animatable)control, obsMatch); + var sub = animation.Apply((Animatable)control, Clock.GlobalClock, obsMatch); subs.Add(sub); } diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index d60d366ad4..d9ee269739 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -7,6 +7,11 @@ namespace Avalonia.Animation { public class RenderLoopClock : Clock, IRenderLoopTask { + protected override void Stop() + { + AvaloniaLocator.Current.GetService().Remove(this); + } + bool IRenderLoopTask.NeedsUpdate => HasSubscriptions; void IRenderLoopTask.Render() diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 2be1226abe..4476058bfe 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -12,7 +12,7 @@ namespace Avalonia.Animation DoubleAnimator childKeyFrames; /// - public override IDisposable Apply(Animation animation, Animatable control, IObservable obsMatch, Action onComplete) + public override IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete) { var ctrl = (Visual)control; @@ -44,7 +44,7 @@ namespace Avalonia.Animation // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childKeyFrames.Apply(animation, ctrl.RenderTransform, obsMatch, onComplete); + return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (transform.GetType() == Property.OwnerType) { - return childKeyFrames.Apply(animation, transform, obsMatch, onComplete); + return childKeyFrames.Apply(animation, transform, clock, obsMatch, onComplete); } } } From b0368c80b29e7ea7fcc99f32c53252c2218fbc4b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:43:06 -0500 Subject: [PATCH 19/56] Enable transitions to run on custom clocks. --- src/Avalonia.Animation/Animatable.cs | 2 +- src/Avalonia.Animation/ITransition.cs | 2 +- src/Avalonia.Animation/TransitionInstance.cs | 8 +++++--- src/Avalonia.Animation/Transition`1.cs | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index e51103aa55..5208356570 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -69,7 +69,7 @@ namespace Avalonia.Animation if (match != null) { - match.Apply(this, e.OldValue, e.NewValue); + match.Apply(this, Clock.GlobalClock, e.OldValue, e.NewValue); } } } diff --git a/src/Avalonia.Animation/ITransition.cs b/src/Avalonia.Animation/ITransition.cs index e2ffe7fc6e..7afaa2325a 100644 --- a/src/Avalonia.Animation/ITransition.cs +++ b/src/Avalonia.Animation/ITransition.cs @@ -13,7 +13,7 @@ namespace Avalonia.Animation /// /// Applies the transition to the specified . /// - IDisposable Apply(Animatable control, object oldValue, object newValue); + IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue); /// /// Gets the property to be animated. diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index 4c61adea28..b0c927f3cd 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -18,10 +18,12 @@ namespace Avalonia.Animation private IDisposable timerSubscription; private TimeSpan startTime; private TimeSpan duration; + private readonly Clock _clock; - public TransitionInstance(TimeSpan Duration) + public TransitionInstance(Clock clock, TimeSpan Duration) { duration = Duration; + _clock = clock; } private void TimerTick(TimeSpan t) @@ -45,8 +47,8 @@ namespace Avalonia.Animation protected override void Subscribed() { - startTime = Clock.GlobalClock.CurrentTime; - timerSubscription = Clock.GlobalClock.Subscribe(TimerTick); + startTime = _clock.CurrentTime; + timerSubscription = _clock.Subscribe(TimerTick); PublishNext(0.0d); } } diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs index 4b01c54f5c..23df7f9807 100644 --- a/src/Avalonia.Animation/Transition`1.cs +++ b/src/Avalonia.Animation/Transition`1.cs @@ -49,9 +49,9 @@ namespace Avalonia.Animation public abstract IObservable DoTransition(IObservable progress, T oldValue, T newValue); /// - public virtual IDisposable Apply(Animatable control, object oldValue, object newValue) + public virtual IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue) { - var transition = DoTransition(new TransitionInstance(Duration), (T)oldValue, (T)newValue); + var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue); return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation); } From 6a380d6591e0f07e15edcbb6ace96baabec9b287 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:49:52 -0500 Subject: [PATCH 20/56] Reorganize RenderLoopClock registration. --- src/Avalonia.Controls/AppBuilderBase.cs | 2 +- src/Avalonia.Controls/Application.cs | 7 +++++++ src/Windows/Avalonia.Win32/Win32Platform.cs | 5 ----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/AppBuilderBase.cs b/src/Avalonia.Controls/AppBuilderBase.cs index c92d5d7694..9561282274 100644 --- a/src/Avalonia.Controls/AppBuilderBase.cs +++ b/src/Avalonia.Controls/AppBuilderBase.cs @@ -272,10 +272,10 @@ namespace Avalonia.Controls s_setupWasAlreadyCalled = true; - Instance.RegisterServices(); RuntimePlatformServicesInitializer(); WindowingSubsystemInitializer(); RenderingSubsystemInitializer(); + Instance.RegisterServices(); Instance.Initialize(); AfterSetupCallback(Self); } diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 4c549ac7d4..8f6544ccd8 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -4,12 +4,14 @@ using System; using System.Reactive.Concurrency; using System.Threading; +using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.Input.Platform; using Avalonia.Input.Raw; using Avalonia.Platform; +using Avalonia.Rendering; using Avalonia.Styling; using Avalonia.Threading; @@ -335,6 +337,11 @@ namespace Avalonia .Bind().ToConstant(AvaloniaScheduler.Instance) .Bind().ToConstant(DragDropDevice.Instance) .Bind().ToTransient(); + + var clock = new RenderLoopClock(); + AvaloniaLocator.CurrentMutable + .Bind().ToConstant(clock) + .GetService().Add(clock); } } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 812a557765..ef2dfd3c1a 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -77,8 +77,6 @@ namespace Avalonia.Win32 public static void Initialize(bool deferredRendering = true) { - var clock = new RenderLoopClock(); - AvaloniaLocator.CurrentMutable .Bind().ToSingleton() .Bind().ToConstant(CursorFactory.Instance) @@ -87,7 +85,6 @@ namespace Avalonia.Win32 .Bind().ToConstant(s_instance) .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new RenderTimer(60)) - .Bind().ToConstant(clock) .Bind().ToSingleton() .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance); @@ -97,8 +94,6 @@ namespace Avalonia.Win32 if (OleContext.Current != null) AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); - - AvaloniaLocator.Current.GetService().Add(clock); } public bool HasMessages() From 0c611b981d86e979b0989b0c5b3c15ce0a5ef81a Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:57:38 -0500 Subject: [PATCH 21/56] Refactor clock types. --- src/Avalonia.Animation/Clock.cs | 71 ++---------------- src/Avalonia.Animation/ClockBase.cs | 75 +++++++++++++++++++ src/Avalonia.Animation/IClock.cs | 13 ++++ src/Avalonia.Controls/Application.cs | 2 +- .../Animation/RenderLoopClock.cs | 2 +- 5 files changed, 95 insertions(+), 68 deletions(-) create mode 100644 src/Avalonia.Animation/ClockBase.cs create mode 100644 src/Avalonia.Animation/IClock.cs diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index e8616c9694..f61a4c7db1 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -6,81 +6,20 @@ using Avalonia.Reactive; namespace Avalonia.Animation { - public class Clock : IObservable + public class Clock : ClockBase { - public static Clock GlobalClock => AvaloniaLocator.Current.GetService(); - - private ClockObservable _observable; - - private IObservable _connectedObservable; + public static IClock GlobalClock => AvaloniaLocator.Current.GetService(); private IDisposable _parentSubscription; - - private TimeSpan? _previousTime; - private TimeSpan _internalTime; - - protected Clock() - { - _observable = new ClockObservable(); - _connectedObservable = _observable.Publish().RefCount(); - } - - public Clock(Clock parent) - :this() + + public Clock(IClock parent) { _parentSubscription = parent.Subscribe(Pulse); } - public bool HasSubscriptions => _observable.HasSubscriptions; - - public TimeSpan CurrentTime { get; private set; } - - public PlayState PlayState { get; set; } - - protected void Pulse(TimeSpan systemTime) - { - if (!_previousTime.HasValue) - { - _previousTime = systemTime; - _internalTime = TimeSpan.Zero; - } - else - { - if (PlayState == PlayState.Pause) - { - _previousTime = systemTime; - return; - } - var delta = systemTime - _previousTime; - _internalTime += delta.Value; - _previousTime = systemTime; - } - - _observable.Pulse(_internalTime); - CurrentTime = _internalTime; - - if (PlayState == PlayState.Stop) - { - Stop(); - } - } - - protected virtual void Stop() + protected override void Stop() { _parentSubscription?.Dispose(); } - - public IDisposable Subscribe(IObserver observer) - { - return _connectedObservable.Subscribe(observer); - } - - private class ClockObservable : LightweightObservableBase - { - public bool HasSubscriptions { get; private set; } - public void Pulse(TimeSpan time) => PublishNext(time); - protected override void Initialize() => HasSubscriptions = true; - protected override void Deinitialize() => HasSubscriptions = false; - } } } diff --git a/src/Avalonia.Animation/ClockBase.cs b/src/Avalonia.Animation/ClockBase.cs new file mode 100644 index 0000000000..ea784269d9 --- /dev/null +++ b/src/Avalonia.Animation/ClockBase.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Text; +using Avalonia.Reactive; + +namespace Avalonia.Animation +{ + public class ClockBase : IClock + { + private ClockObservable _observable; + + private IObservable _connectedObservable; + + private TimeSpan? _previousTime; + private TimeSpan _internalTime; + + protected ClockBase() + { + _observable = new ClockObservable(); + _connectedObservable = _observable.Publish().RefCount(); + } + + public bool HasSubscriptions => _observable.HasSubscriptions; + + public TimeSpan CurrentTime { get; private set; } + + public PlayState PlayState { get; set; } + + protected void Pulse(TimeSpan systemTime) + { + if (!_previousTime.HasValue) + { + _previousTime = systemTime; + _internalTime = TimeSpan.Zero; + } + else + { + if (PlayState == PlayState.Pause) + { + _previousTime = systemTime; + return; + } + var delta = systemTime - _previousTime; + _internalTime += delta.Value; + _previousTime = systemTime; + } + + _observable.Pulse(_internalTime); + CurrentTime = _internalTime; + + if (PlayState == PlayState.Stop) + { + Stop(); + } + } + + protected virtual void Stop() + { + } + + public IDisposable Subscribe(IObserver observer) + { + return _connectedObservable.Subscribe(observer); + } + + private class ClockObservable : LightweightObservableBase + { + public bool HasSubscriptions { get; private set; } + public void Pulse(TimeSpan time) => PublishNext(time); + protected override void Initialize() => HasSubscriptions = true; + protected override void Deinitialize() => HasSubscriptions = false; + } + } +} diff --git a/src/Avalonia.Animation/IClock.cs b/src/Avalonia.Animation/IClock.cs new file mode 100644 index 0000000000..58c997841d --- /dev/null +++ b/src/Avalonia.Animation/IClock.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Avalonia.Animation +{ + public interface IClock : IObservable + { + bool HasSubscriptions { get; } + TimeSpan CurrentTime { get; } + PlayState PlayState { get; set; } + } +} diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 8f6544ccd8..8c03bac61a 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -340,7 +340,7 @@ namespace Avalonia var clock = new RenderLoopClock(); AvaloniaLocator.CurrentMutable - .Bind().ToConstant(clock) + .Bind().ToConstant(clock) .GetService().Add(clock); } } diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index d9ee269739..e59b3aac0d 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -5,7 +5,7 @@ using Avalonia.Rendering; namespace Avalonia.Animation { - public class RenderLoopClock : Clock, IRenderLoopTask + public class RenderLoopClock : ClockBase, IRenderLoopTask { protected override void Stop() { From 59cad1cf86651e4ab3f3c015d1e0636c50c2ea08 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 00:02:41 -0500 Subject: [PATCH 22/56] Fix unfinished clock type refactor. --- src/Avalonia.Animation/Animatable.cs | 5 ++++- src/Avalonia.Animation/Animation.cs | 4 ++-- src/Avalonia.Animation/Animator`1.cs | 4 ++-- src/Avalonia.Animation/Clock.cs | 5 +++++ src/Avalonia.Animation/IAnimation.cs | 4 ++-- src/Avalonia.Animation/IAnimator.cs | 2 +- src/Avalonia.Animation/ITransition.cs | 2 +- src/Avalonia.Animation/Transition`1.cs | 2 +- 8 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index 5208356570..03a7a32e02 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -14,7 +14,10 @@ namespace Avalonia.Animation /// Base class for all animatable objects. /// public class Animatable : AvaloniaObject - { + { + public static readonly StyledProperty ClockProperty = + AvaloniaProperty.Register(nameof(Clock), inherits: true); + /// /// Defines the property. /// diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index e787143b59..1d2a7b4559 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -154,7 +154,7 @@ namespace Avalonia.Animation } /// - public IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete) + public IDisposable Apply(Animatable control, IClock clock, IObservable match, Action onComplete) { var (animators, subscriptions) = InterpretKeyframes(control); if (animators.Count == 1) @@ -185,7 +185,7 @@ namespace Avalonia.Animation } /// - public Task RunAsync(Animatable control, Clock clock = null) + public Task RunAsync(Animatable control, IClock clock = null) { if (clock == null) { diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index c699ff635a..44a10db545 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -32,7 +32,7 @@ namespace Avalonia.Animation } /// - public virtual IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable match, Action onComplete) + public virtual IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable match, Action onComplete) { if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); @@ -101,7 +101,7 @@ namespace Avalonia.Animation /// /// Runs the KeyFrames Animation. /// - private IDisposable RunKeyFrames(Animation animation, Animatable control, Clock clock, Action onComplete) + private IDisposable RunKeyFrames(Animation animation, Animatable control, IClock clock, Action onComplete) { var instance = new AnimationInstance(animation, control, this, clock, onComplete, DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index f61a4c7db1..e009c2aad5 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -11,6 +11,11 @@ namespace Avalonia.Animation public static IClock GlobalClock => AvaloniaLocator.Current.GetService(); private IDisposable _parentSubscription; + + public Clock() + :this(GlobalClock) + { + } public Clock(IClock parent) { diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index f726cf43dc..34b0a5d769 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -11,11 +11,11 @@ namespace Avalonia.Animation /// /// Apply the animation to the specified control /// - IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete = null); + IDisposable Apply(Animatable control, IClock clock, IObservable match, Action onComplete = null); /// /// Run the animation to the specified control /// - Task RunAsync(Animatable control, Clock clock); + Task RunAsync(Animatable control, IClock clock); } } diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 134b30a555..04bad8e112 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -16,6 +16,6 @@ namespace Avalonia.Animation /// /// Applies the current KeyFrame group to the specified control. /// - IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete); + IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete); } } diff --git a/src/Avalonia.Animation/ITransition.cs b/src/Avalonia.Animation/ITransition.cs index 7afaa2325a..e5d8466f04 100644 --- a/src/Avalonia.Animation/ITransition.cs +++ b/src/Avalonia.Animation/ITransition.cs @@ -13,7 +13,7 @@ namespace Avalonia.Animation /// /// Applies the transition to the specified . /// - IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue); + IDisposable Apply(Animatable control, IClock clock, object oldValue, object newValue); /// /// Gets the property to be animated. diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs index 23df7f9807..b54ec8f51c 100644 --- a/src/Avalonia.Animation/Transition`1.cs +++ b/src/Avalonia.Animation/Transition`1.cs @@ -49,7 +49,7 @@ namespace Avalonia.Animation public abstract IObservable DoTransition(IObservable progress, T oldValue, T newValue); /// - public virtual IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue) + public virtual IDisposable Apply(Animatable control, IClock clock, object oldValue, object newValue) { var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue); return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation); From ec9c61bbbe4afcb18852b4992428817d3fa111a9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 00:11:49 -0500 Subject: [PATCH 23/56] Allow clocks to be bindable and inherited down the logical tree. --- src/Avalonia.Animation/Animatable.cs | 23 ++++--------------- src/Avalonia.Animation/AnimationInstance`1.cs | 4 ++-- src/Avalonia.Animation/TransitionInstance.cs | 2 +- src/Avalonia.Styling/Styling/Style.cs | 19 ++++++++------- .../Animation/TransformAnimator.cs | 2 +- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index 03a7a32e02..516f383b92 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -18,25 +18,10 @@ namespace Avalonia.Animation public static readonly StyledProperty ClockProperty = AvaloniaProperty.Register(nameof(Clock), inherits: true); - /// - /// Defines the property. - /// - public static readonly DirectProperty PlayStateProperty = - AvaloniaProperty.RegisterDirect( - nameof(PlayState), - o => o.PlayState, - (o, v) => o.PlayState = v); - - private PlayState _playState = PlayState.Run; - - /// - /// Gets or sets the state of the animation for this - /// control. - /// - public PlayState PlayState + public IClock Clock { - get { return _playState; } - set { SetAndRaise(PlayStateProperty, ref _playState, value); } + get => GetValue(ClockProperty); + set => SetValue(ClockProperty, value); } /// @@ -72,7 +57,7 @@ namespace Avalonia.Animation if (match != null) { - match.Apply(this, Clock.GlobalClock, e.OldValue, e.NewValue); + match.Apply(this, Clock ?? Avalonia.Animation.Clock.GlobalClock, e.OldValue, e.NewValue); } } } diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index fe84dd879a..99ebbe752a 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -34,7 +34,7 @@ namespace Avalonia.Animation private Action _onCompleteAction; private Func _interpolator; private IDisposable _timerSubscription; - private readonly Clock _clock; + private readonly IClock _clock; public AnimationInstance(Animation animation, Animatable control, Animator animator, Clock clock, Action OnComplete, Func Interpolator) { @@ -117,7 +117,7 @@ namespace Avalonia.Animation private void DoPlayStatesAndTime(TimeSpan systemTime) { - if (_clock.PlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) + if (_clock.PlayState == PlayState.Stop) DoComplete(); if (!_gotFirstKFValue) diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index b0c927f3cd..ad87ad7010 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -18,7 +18,7 @@ namespace Avalonia.Animation private IDisposable timerSubscription; private TimeSpan startTime; private TimeSpan duration; - private readonly Clock _clock; + private readonly IClock _clock; public TransitionInstance(Clock clock, TimeSpan Duration) { diff --git a/src/Avalonia.Styling/Styling/Style.cs b/src/Avalonia.Styling/Styling/Style.cs index a033184588..62b3ca72ae 100644 --- a/src/Avalonia.Styling/Styling/Style.cs +++ b/src/Avalonia.Styling/Styling/Style.cs @@ -111,17 +111,20 @@ namespace Avalonia.Styling { var subs = GetSubscriptions(control); - foreach (var animation in Animations) + if (control is Animatable animatable) { - IObservable obsMatch = match.ObservableResult; - - if (match.ImmediateResult == true) + foreach (var animation in Animations) { - obsMatch = Observable.Return(true); - } + IObservable obsMatch = match.ObservableResult; - var sub = animation.Apply((Animatable)control, Clock.GlobalClock, obsMatch); - subs.Add(sub); + if (match.ImmediateResult == true) + { + obsMatch = Observable.Return(true); + } + + var sub = animation.Apply(animatable, animatable.Clock ?? Clock.GlobalClock, obsMatch); + subs.Add(sub); + } } foreach (var setter in Setters) diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 4476058bfe..6336c49dc5 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -12,7 +12,7 @@ namespace Avalonia.Animation DoubleAnimator childKeyFrames; /// - public override IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete) + public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete) { var ctrl = (Visual)control; From 51faa94534c2ca0a0de03305f96d10a8cdb6ce8e Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 13:49:44 -0500 Subject: [PATCH 24/56] Allow users to supply custom clocks in XAML or code before animations are applied. Change AnimationsPage to show an example with a custom clock and the animations on that page. --- samples/RenderDemo/Pages/AnimationsPage.xaml | 7 +++-- .../RenderDemo/Pages/AnimationsPage.xaml.cs | 16 ++++++++++++ .../ViewModels/AnimationsPageViewModel.cs | 26 +++++-------------- src/Avalonia.Animation/Animation.cs | 10 ------- src/Avalonia.Animation/Animator`1.cs | 8 +++++- src/Avalonia.Styling/Styling/Style.cs | 2 +- .../Animation/TransformAnimator.cs | 4 +-- 7 files changed, 37 insertions(+), 36 deletions(-) diff --git a/samples/RenderDemo/Pages/AnimationsPage.xaml b/samples/RenderDemo/Pages/AnimationsPage.xaml index 5287e4e373..473807ac50 100644 --- a/samples/RenderDemo/Pages/AnimationsPage.xaml +++ b/samples/RenderDemo/Pages/AnimationsPage.xaml @@ -107,9 +107,12 @@ + + + Hover to activate Transform Keyframe Animations. - public class Animation : AvaloniaList, IAnimation { - - /// - /// Gets or sets the animation play state for all animations - /// - public static PlayState GlobalPlayState - { - get => AvaloniaLocator.Current.GetService().PlayState; - set => AvaloniaLocator.Current.GetService().PlayState = value; - } - /// /// Gets or sets the active time of this animation. /// diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 44a10db545..b68f2fc79a 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -103,7 +103,13 @@ namespace Avalonia.Animation /// private IDisposable RunKeyFrames(Animation animation, Animatable control, IClock clock, Action onComplete) { - var instance = new AnimationInstance(animation, control, this, clock, onComplete, DoInterpolation); + var instance = new AnimationInstance( + animation, + control, + this, + clock ?? control.Clock ?? Clock.GlobalClock, + onComplete, + DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); } diff --git a/src/Avalonia.Styling/Styling/Style.cs b/src/Avalonia.Styling/Styling/Style.cs index 62b3ca72ae..067bb59fe9 100644 --- a/src/Avalonia.Styling/Styling/Style.cs +++ b/src/Avalonia.Styling/Styling/Style.cs @@ -122,7 +122,7 @@ namespace Avalonia.Styling obsMatch = Observable.Return(true); } - var sub = animation.Apply(animatable, animatable.Clock ?? Clock.GlobalClock, obsMatch); + var sub = animation.Apply(animatable, null, obsMatch); subs.Add(sub); } } diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 6336c49dc5..721a895900 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -44,7 +44,7 @@ namespace Avalonia.Animation // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock, obsMatch, onComplete); + return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (transform.GetType() == Property.OwnerType) { - return childKeyFrames.Apply(animation, transform, clock, obsMatch, onComplete); + return childKeyFrames.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); } } } From 6b0ef13027e60cd63131077a6695af14d40cb717 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 15:29:11 -0500 Subject: [PATCH 25/56] Clean up naming in TransformAnimator. --- .../Animation/TransformAnimator.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 721a895900..64b3ebd626 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -9,7 +9,7 @@ namespace Avalonia.Animation /// public class TransformAnimator : Animator { - DoubleAnimator childKeyFrames; + DoubleAnimator childAnimator; /// public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete) @@ -36,15 +36,15 @@ namespace Avalonia.Animation var renderTransformType = ctrl.RenderTransform.GetType(); - if (childKeyFrames == null) + if (childAnimator == null) { - InitializeChildKeyFrames(); + InitializeChildAnimator(); } // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); + return childAnimator.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (transform.GetType() == Property.OwnerType) { - return childKeyFrames.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); + return childAnimator.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); } } } @@ -73,16 +73,16 @@ namespace Avalonia.Animation return null; } - void InitializeChildKeyFrames() + void InitializeChildAnimator() { - childKeyFrames = new DoubleAnimator(); + childAnimator = new DoubleAnimator(); foreach (AnimatorKeyFrame keyframe in this) { - childKeyFrames.Add(keyframe); + childAnimator.Add(keyframe); } - childKeyFrames.Property = Property; + childAnimator.Property = Property; } /// From 58a85c53c757962040abe68b7f1e00f02b3b50c3 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 16:15:06 -0500 Subject: [PATCH 26/56] Have each AnimationInstance and TransitionInstance use their own internal clock instead of relying on tracking the start time of the global clock. Use a binary search to find the correct keyframe instead of linear search. --- .../ViewModels/AnimationsPageViewModel.cs | 2 +- src/Avalonia.Animation/Animation.cs | 5 -- src/Avalonia.Animation/AnimationInstance`1.cs | 32 +++---- src/Avalonia.Animation/Animator`1.cs | 84 +++++++++++-------- src/Avalonia.Animation/ClockBase.cs | 5 +- src/Avalonia.Animation/IClock.cs | 2 - src/Avalonia.Animation/TransitionInstance.cs | 26 +++--- src/Avalonia.Animation/Transition`1.cs | 3 - 8 files changed, 76 insertions(+), 83 deletions(-) diff --git a/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs b/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs index e4276fa1b5..7b89b7944c 100644 --- a/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs +++ b/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs @@ -8,7 +8,7 @@ namespace RenderDemo.ViewModels { private bool _isPlaying = true; - private string _playStateText = "Pause all animations"; + private string _playStateText = "Pause animations on this page"; public void TogglePlayState() { diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 65463f3d52..d7efc69e10 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -177,11 +177,6 @@ namespace Avalonia.Animation /// public Task RunAsync(Animatable control, IClock clock = null) { - if (clock == null) - { - clock = Clock.GlobalClock; - } - var run = new TaskCompletionSource(); if (this.RepeatCount == RepeatCount.Loop) diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index 99ebbe752a..468c9c93a4 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -19,7 +19,6 @@ namespace Avalonia.Animation private double _currentIteration; private bool _isLooping; private bool _gotFirstKFValue; - private bool _gotFirstFrameCount; private bool _iterationDelay; private FillMode _fillMode; private PlaybackDirection _animationDirection; @@ -29,14 +28,14 @@ namespace Avalonia.Animation private double _speedRatio; private TimeSpan _delay; private TimeSpan _duration; - private TimeSpan _firstFrameCount; private Easings.Easing _easeFunc; private Action _onCompleteAction; private Func _interpolator; private IDisposable _timerSubscription; - private readonly IClock _clock; + private readonly IClock _baseClock; + private IClock _clock; - public AnimationInstance(Animation animation, Animatable control, Animator animator, Clock clock, Action OnComplete, Func Interpolator) + public AnimationInstance(Animation animation, Animatable control, Animator animator, IClock baseClock, Action OnComplete, Func Interpolator) { if (animation.SpeedRatio <= 0) throw new InvalidOperationException("Speed ratio cannot be negative or zero."); @@ -72,16 +71,18 @@ namespace Avalonia.Animation _fillMode = animation.FillMode; _onCompleteAction = OnComplete; _interpolator = Interpolator; - _clock = clock; + _baseClock = baseClock; } protected override void Unsubscribed() { _timerSubscription?.Dispose(); + _clock.PlayState = PlayState.Stop; } protected override void Subscribed() { + _clock = new Clock(_baseClock); _timerSubscription = _clock.Subscribe(Step); } @@ -115,9 +116,9 @@ namespace Avalonia.Animation PublishNext(_lastInterpValue); } - private void DoPlayStatesAndTime(TimeSpan systemTime) + private void DoPlayStates() { - if (_clock.PlayState == PlayState.Stop) + if (_clock.PlayState == PlayState.Stop || _baseClock.PlayState == PlayState.Stop) DoComplete(); if (!_gotFirstKFValue) @@ -125,19 +126,12 @@ namespace Avalonia.Animation _firstKFValue = (T)_parent.First().Value; _gotFirstKFValue = true; } - - if (!_gotFirstFrameCount) - { - _firstFrameCount = systemTime; - _gotFirstFrameCount = true; - } } - private void InternalStep(TimeSpan systemTime) + private void InternalStep(TimeSpan time) { - DoPlayStatesAndTime(systemTime); - - var time = systemTime - _firstFrameCount; + DoPlayStates(); + var delayEndpoint = _delay; var iterationEndpoint = delayEndpoint + _duration; @@ -158,14 +152,14 @@ namespace Avalonia.Animation } //Calculate the current iteration number - _currentIteration = (int)Math.Floor((double)time.Ticks / iterationEndpoint.Ticks) + 2; + _currentIteration = (int)Math.Floor((double)((double)time.Ticks / iterationEndpoint.Ticks)) + 2; } else { return; } - time = TimeSpan.FromTicks(time.Ticks % iterationEndpoint.Ticks); + time = TimeSpan.FromTicks((long)(time.Ticks % iterationEndpoint.Ticks)); if (!_isLooping) { diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index b68f2fc79a..b79e2d9342 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -52,52 +52,72 @@ namespace Avalonia.Animation /// (i.e., the normalized time between the selected keyframes, relative to the /// time parameter). /// - /// The time parameter, relative to the total animation time - protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double t) + /// The time parameter, relative to the total animation time + protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double animationTime) { - AnimatorKeyFrame firstCue, lastCue ; + AnimatorKeyFrame firstKeyframe, lastKeyframe ; int kvCount = _convertedKeyframes.Count; if (kvCount > 2) { - if (t <= 0.0) + if (animationTime <= 0.0) { - firstCue = _convertedKeyframes[0]; - lastCue = _convertedKeyframes[1]; + firstKeyframe = _convertedKeyframes[0]; + lastKeyframe = _convertedKeyframes[1]; } - else if (t >= 1.0) + else if (animationTime >= 1.0) { - firstCue = _convertedKeyframes[_convertedKeyframes.Count - 2]; - lastCue = _convertedKeyframes[_convertedKeyframes.Count - 1]; + firstKeyframe = _convertedKeyframes[_convertedKeyframes.Count - 2]; + lastKeyframe = _convertedKeyframes[_convertedKeyframes.Count - 1]; } else { - (double time, int index) maxval = (0.0d, 0); - for (int i = 0; i < _convertedKeyframes.Count; i++) - { - var comp = _convertedKeyframes[i].Cue.CueValue; - if (t >= comp) - { - maxval = (comp, i); - } - } - firstCue = _convertedKeyframes[maxval.index]; - lastCue = _convertedKeyframes[maxval.index + 1]; + int index = FindClosestBeforeKeyFrame(animationTime); + firstKeyframe = _convertedKeyframes[index]; + lastKeyframe = _convertedKeyframes[index + 1]; } } else { - firstCue = _convertedKeyframes[0]; - lastCue = _convertedKeyframes[1]; + firstKeyframe = _convertedKeyframes[0]; + lastKeyframe = _convertedKeyframes[1]; } - double t0 = firstCue.Cue.CueValue; - double t1 = lastCue.Cue.CueValue; - var intraframeTime = (t - t0) / (t1 - t0); - var firstFrameData = (firstCue.GetTypedValue(), firstCue.isNeutral); - var lastFrameData = (lastCue.GetTypedValue(), lastCue.isNeutral); + double t0 = firstKeyframe.Cue.CueValue; + double t1 = lastKeyframe.Cue.CueValue; + var intraframeTime = (animationTime - t0) / (t1 - t0); + var firstFrameData = (firstKeyframe.GetTypedValue(), firstKeyframe.isNeutral); + var lastFrameData = (lastKeyframe.GetTypedValue(), lastKeyframe.isNeutral); return (intraframeTime, new KeyFramePair(firstFrameData, lastFrameData)); } + private int FindClosestBeforeKeyFrame(double time) + { + int FindClosestBeforeKeyFrame(int startIndex, int length) + { + if (length == 0 || length == 1) + { + return startIndex; + } + + int middle = startIndex + (length / 2); + + if (_convertedKeyframes[middle].Cue.CueValue < time) + { + return FindClosestBeforeKeyFrame(middle, length - middle); + } + else if (_convertedKeyframes[middle].Cue.CueValue > time) + { + return FindClosestBeforeKeyFrame(startIndex, middle - startIndex); + } + else + { + return middle; + } + } + + return FindClosestBeforeKeyFrame(0, _convertedKeyframes.Count); + } + /// /// Runs the KeyFrames Animation. /// @@ -130,14 +150,6 @@ namespace Avalonia.Animation AddNeutralKeyFramesIfNeeded(); - var copy = _convertedKeyframes.ToList().OrderBy(p => p.Cue.CueValue); - _convertedKeyframes.Clear(); - - foreach (AnimatorKeyFrame keyframe in copy) - { - _convertedKeyframes.Add(keyframe); - } - _isVerifiedAndConverted = true; } @@ -167,7 +179,7 @@ namespace Avalonia.Animation { if (!hasStartKey) { - _convertedKeyframes.Add(new AnimatorKeyFrame(null, new Cue(0.0d)) { Value = default(T), isNeutral = true }); + _convertedKeyframes.Insert(0, new AnimatorKeyFrame(null, new Cue(0.0d)) { Value = default(T), isNeutral = true }); } if (!hasEndKey) diff --git a/src/Avalonia.Animation/ClockBase.cs b/src/Avalonia.Animation/ClockBase.cs index ea784269d9..a2b29e728e 100644 --- a/src/Avalonia.Animation/ClockBase.cs +++ b/src/Avalonia.Animation/ClockBase.cs @@ -21,9 +21,7 @@ namespace Avalonia.Animation _connectedObservable = _observable.Publish().RefCount(); } - public bool HasSubscriptions => _observable.HasSubscriptions; - - public TimeSpan CurrentTime { get; private set; } + protected bool HasSubscriptions => _observable.HasSubscriptions; public PlayState PlayState { get; set; } @@ -47,7 +45,6 @@ namespace Avalonia.Animation } _observable.Pulse(_internalTime); - CurrentTime = _internalTime; if (PlayState == PlayState.Stop) { diff --git a/src/Avalonia.Animation/IClock.cs b/src/Avalonia.Animation/IClock.cs index 58c997841d..ae44102077 100644 --- a/src/Avalonia.Animation/IClock.cs +++ b/src/Avalonia.Animation/IClock.cs @@ -6,8 +6,6 @@ namespace Avalonia.Animation { public interface IClock : IObservable { - bool HasSubscriptions { get; } - TimeSpan CurrentTime { get; } PlayState PlayState { get; set; } } } diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index ad87ad7010..eff2c4e9f3 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -15,23 +15,22 @@ namespace Avalonia.Animation /// internal class TransitionInstance : SingleSubscriberObservableBase { - private IDisposable timerSubscription; - private TimeSpan startTime; - private TimeSpan duration; - private readonly IClock _clock; + private IDisposable _timerSubscription; + private TimeSpan _duration; + private readonly IClock _baseClock; + private IClock _clock; - public TransitionInstance(Clock clock, TimeSpan Duration) + public TransitionInstance(IClock clock, TimeSpan Duration) { - duration = Duration; - _clock = clock; + _duration = Duration; + _baseClock = clock; } private void TimerTick(TimeSpan t) { - var interpVal = (double)(t.Ticks - startTime.Ticks) / duration.Ticks; + var interpVal = (double)t.Ticks / _duration.Ticks; - if (interpVal > 1d - || interpVal < 0d) + if (interpVal > 1d || interpVal < 0d) { PublishCompleted(); return; @@ -42,13 +41,14 @@ namespace Avalonia.Animation protected override void Unsubscribed() { - timerSubscription?.Dispose(); + _timerSubscription?.Dispose(); + _clock.PlayState = PlayState.Stop; } protected override void Subscribed() { - startTime = _clock.CurrentTime; - timerSubscription = _clock.Subscribe(TimerTick); + _clock = new Clock(_baseClock); + _timerSubscription = _clock.Subscribe(TimerTick); PublishNext(0.0d); } } diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs index b54ec8f51c..cd0d5d9ce9 100644 --- a/src/Avalonia.Animation/Transition`1.cs +++ b/src/Avalonia.Animation/Transition`1.cs @@ -14,7 +14,6 @@ namespace Avalonia.Animation public abstract class Transition : AvaloniaObject, ITransition { private AvaloniaProperty _prop; - private Easing _easing; /// /// Gets the duration of the animation. @@ -54,7 +53,5 @@ namespace Avalonia.Animation var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue); return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation); } - - } } From 3f5ec49b4a32762da011346f0e989cc8970c9524 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 16:36:41 -0500 Subject: [PATCH 27/56] Update iOS and Android projects to use RenderTimers. --- src/Android/Avalonia.Android/AndroidPlatform.cs | 3 ++- ...isplayLinkRenderLoop.cs => DisplayLinkRenderTimer.cs} | 9 +++++---- src/iOS/Avalonia.iOS/iOSPlatform.cs | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) rename src/iOS/Avalonia.iOS/{DisplayLinkRenderLoop.cs => DisplayLinkRenderTimer.cs} (72%) diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index 2b46bfa492..5f0edadf63 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -52,7 +52,8 @@ namespace Avalonia.Android .Bind().ToTransient() .Bind().ToConstant(Instance) .Bind().ToSingleton() - .Bind().ToConstant(new DefaultRenderLoop(60)) + .Bind().ToConstant(new DefaultRenderTimer(60)) + .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new AssetLoader(app.GetType().Assembly)); SkiaPlatform.Initialize(); diff --git a/src/iOS/Avalonia.iOS/DisplayLinkRenderLoop.cs b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs similarity index 72% rename from src/iOS/Avalonia.iOS/DisplayLinkRenderLoop.cs rename to src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs index 4f275dd8ea..1357a4f642 100644 --- a/src/iOS/Avalonia.iOS/DisplayLinkRenderLoop.cs +++ b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs @@ -5,11 +5,12 @@ using Foundation; namespace Avalonia.iOS { - class DisplayLinkRenderLoop : IRenderLoop + class DisplayLinkRenderTimer : IRenderTimer { - public event EventHandler Tick; + public event Action Tick; private CADisplayLink _link; - public DisplayLinkRenderLoop() + + public DisplayLinkRenderTimer() { _link = CADisplayLink.Create(OnFrame); @@ -20,7 +21,7 @@ namespace Avalonia.iOS { try { - Tick?.Invoke(this, new EventArgs()); + Tick?.Invoke(Environment.TickCount); } catch (Exception) { diff --git a/src/iOS/Avalonia.iOS/iOSPlatform.cs b/src/iOS/Avalonia.iOS/iOSPlatform.cs index abaebca489..7b50040bf2 100644 --- a/src/iOS/Avalonia.iOS/iOSPlatform.cs +++ b/src/iOS/Avalonia.iOS/iOSPlatform.cs @@ -41,7 +41,8 @@ namespace Avalonia.iOS .Bind().ToConstant(PlatformThreadingInterface.Instance) .Bind().ToSingleton() .Bind().ToSingleton() - .Bind().ToSingleton(); + .Bind().ToSingleton() + .Bind().ToSingleton(); } } } From 8357bc86b02980a13a597d52cf72c7e5798b6755 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 18:10:03 -0500 Subject: [PATCH 28/56] Add tests for the new RenderLoop logic. --- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 16 +-- .../Rendering/DeferredRendererTests.cs | 64 ++++------ .../Rendering/RenderLoopTests.cs | 119 ++++++++++++++++++ 3 files changed, 146 insertions(+), 53 deletions(-) create mode 100644 tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index a850b99c5e..d920be2706 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using Avalonia.Logging; using Avalonia.Threading; @@ -89,18 +90,7 @@ namespace Avalonia.Rendering { try { - var needsUpdate = false; - - foreach (var i in _items) - { - if (i.NeedsUpdate) - { - needsUpdate = true; - break; - } - } - - if (needsUpdate) + if (_items.Any(item => item.NeedsUpdate)) { await _dispatcher.InvokeAsync(() => { @@ -108,7 +98,7 @@ namespace Avalonia.Rendering { i.Update(tickCount); } - }).ConfigureAwait(false); + }, DispatcherPriority.Render).ConfigureAwait(false); } foreach (var i in _items) diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs index 1af9a9499d..e2a5c0c54c 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reactive.Subjects; - +using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Media; @@ -22,27 +22,9 @@ namespace Avalonia.Visuals.UnitTests.Rendering { public class DeferredRendererTests { - [Fact] - public void First_Frame_Calls_UpdateScene_On_Dispatcher() - { - var root = new TestRoot(); - - var dispatcher = new Mock(); - dispatcher.Setup(x => x.Post(It.IsAny(), DispatcherPriority.Render)) - .Callback((a, p) => a()); - - CreateTargetAndRunFrame(root, dispatcher: dispatcher.Object); - - dispatcher.Verify(x => - x.Post( - It.Is(a => a.Method.Name == "UpdateScene"), - DispatcherPriority.Render)); - } - [Fact] public void First_Frame_Calls_SceneBuilder_UpdateAll() { - var loop = new Mock(); var root = new TestRoot(); var sceneBuilder = MockSceneBuilder(root); @@ -54,6 +36,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Frame_Does_Not_Call_SceneBuilder_If_No_Dirty_Controls() { + var dispatcher = new ImmediateDispatcher(); var loop = new Mock(); var root = new TestRoot(); var sceneBuilder = MockSceneBuilder(root); @@ -63,8 +46,8 @@ namespace Avalonia.Visuals.UnitTests.Rendering sceneBuilder: sceneBuilder.Object); target.Start(); - IgnoreFirstFrame(loop, sceneBuilder); - RunFrame(loop); + IgnoreFirstFrame(target, sceneBuilder); + RunFrame(target); sceneBuilder.Verify(x => x.UpdateAll(It.IsAny()), Times.Never); sceneBuilder.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Never); @@ -73,8 +56,8 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Should_Update_Dirty_Controls_In_Order() { - var loop = new Mock(); var dispatcher = new ImmediateDispatcher(); + var loop = new Mock(); Border border; Decorator decorator; @@ -98,7 +81,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering dispatcher: dispatcher); target.Start(); - IgnoreFirstFrame(loop, sceneBuilder); + IgnoreFirstFrame(target, sceneBuilder); target.AddDirty(border); target.AddDirty(canvas); target.AddDirty(root); @@ -108,7 +91,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering sceneBuilder.Setup(x => x.Update(It.IsAny(), It.IsAny())) .Callback((_, v) => result.Add(v)); - RunFrame(loop); + RunFrame(target); Assert.Equal(new List { root, decorator, border, canvas }, result); } @@ -198,7 +181,6 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Should_Create_Layer_For_Root() { - var loop = new Mock(); var root = new TestRoot(); var rootLayer = new Mock(); @@ -239,19 +221,19 @@ namespace Avalonia.Visuals.UnitTests.Rendering root.Measure(Size.Infinity); root.Arrange(new Rect(root.DesiredSize)); - var loop = new Mock(); - var target = CreateTargetAndRunFrame(root, loop: loop); + var timer = new Mock(); + var target = CreateTargetAndRunFrame(root, timer); Assert.Equal(new[] { root }, target.Layers.Select(x => x.LayerRoot)); var animation = new BehaviorSubject(0.5); border.Bind(Border.OpacityProperty, animation, BindingPriority.Animation); - RunFrame(loop); + RunFrame(target); Assert.Equal(new IVisual[] { root, border }, target.Layers.Select(x => x.LayerRoot)); animation.OnCompleted(); - RunFrame(loop); + RunFrame(target); Assert.Equal(new[] { root }, target.Layers.Select(x => x.LayerRoot)); } @@ -280,8 +262,8 @@ namespace Avalonia.Visuals.UnitTests.Rendering root.Measure(Size.Infinity); root.Arrange(new Rect(root.DesiredSize)); - var loop = new Mock(); - var target = CreateTargetAndRunFrame(root, loop: loop); + var timer = new Mock(); + var target = CreateTargetAndRunFrame(root, timer); Assert.Single(target.Layers); } @@ -345,19 +327,20 @@ namespace Avalonia.Visuals.UnitTests.Rendering private DeferredRenderer CreateTargetAndRunFrame( TestRoot root, - Mock loop = null, + Mock timer = null, ISceneBuilder sceneBuilder = null, IDispatcher dispatcher = null) { - loop = loop ?? new Mock(); + timer = timer ?? new Mock(); + dispatcher = dispatcher ?? new ImmediateDispatcher(); var target = new DeferredRenderer( root, - loop.Object, + new RenderLoop(timer.Object, dispatcher), sceneBuilder: sceneBuilder, - dispatcher: dispatcher ?? new ImmediateDispatcher()); + dispatcher: dispatcher); root.Renderer = target; target.Start(); - RunFrame(loop); + RunFrame(target); return target; } @@ -366,15 +349,16 @@ namespace Avalonia.Visuals.UnitTests.Rendering return Mock.Get(renderer.Layers[layerRoot].Bitmap.Item.CreateDrawingContext(null)); } - private void IgnoreFirstFrame(Mock loop, Mock sceneBuilder) + private void IgnoreFirstFrame(IRenderLoopTask task, Mock sceneBuilder) { - RunFrame(loop); + RunFrame(task); sceneBuilder.ResetCalls(); } - private void RunFrame(Mock loop) + private void RunFrame(IRenderLoopTask task) { - //loop.Raise(x => x.Tick += null, EventArgs.Empty); + task.Update(0); + task.Render(); } private IRenderTargetBitmapImpl CreateLayer() diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs new file mode 100644 index 0000000000..30ef35a2bb --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Avalonia.Rendering; +using Avalonia.Threading; +using Moq; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Rendering +{ + public class RenderLoopTests + { + [Fact] + public void RenderLoop_Update_Runs_On_Dispatcher() + { + var dispatcher = new Mock(); + + bool inDispatcher = false; + + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => + { + inDispatcher = true; + a(); + inDispatcher = false; + }) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + + var loop = new RenderLoop(timer.Object, dispatcher.Object); + + var renderTask = new Mock(); + + renderTask.Setup(t => t.NeedsUpdate).Returns(true); + renderTask.Setup(t => t.Update(It.IsAny())) + .Callback((long _) => Assert.True(inDispatcher)); + + loop.Add(renderTask.Object); + + timer.Raise(t => t.Tick += null, 0L); + + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + } + + [Fact] + public void RenderLoop_Does_Not_Update_When_No_Tasks_Need_Update() + { + var dispatcher = new Mock(); + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => a()) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + var loop = new RenderLoop(timer.Object, dispatcher.Object); + var renderTask = new Mock(); + renderTask.Setup(t => t.NeedsUpdate).Returns(false); + + loop.Add(renderTask.Object); + timer.Raise(t => t.Tick += null, 0L); + + renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); + } + + [Fact] + public void RenderLoop_Render_Runs_Off_Dispatcher() + { + var dispatcher = new Mock(); + bool inDispatcher = false; + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => + { + inDispatcher = true; + a(); + inDispatcher = false; + }) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + var loop = new RenderLoop(timer.Object, dispatcher.Object); + + var renderTask = new Mock(); + + renderTask.Setup(t => t.NeedsUpdate).Returns(true); + renderTask.Setup(t => t.Render()) + .Callback(() => Assert.False(inDispatcher)); + + loop.Add(renderTask.Object); + timer.Raise(t => t.Tick += null, 0L); + + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + } + + [Fact] + public void RenderLoop_Passes_Tick_Count_To_Update() + { + var dispatcher = new Mock(); + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => a()) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + var loop = new RenderLoop(timer.Object, dispatcher.Object); + var renderTask = new Mock(); + renderTask.Setup(t => t.NeedsUpdate).Returns(true); + + loop.Add(renderTask.Object); + var tickCount = 12345L; + timer.Raise(t => t.Tick += null, tickCount); + + renderTask.Verify(t => t.Update(tickCount), Times.Once()); + } + } +} From ce95625d66509c15bb9f20607596eff900eef3a7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 18:45:06 -0500 Subject: [PATCH 29/56] Only add the clock to the render loop if there is a render loop. --- src/Avalonia.Controls/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 8c03bac61a..586a73b75c 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -341,7 +341,7 @@ namespace Avalonia var clock = new RenderLoopClock(); AvaloniaLocator.CurrentMutable .Bind().ToConstant(clock) - .GetService().Add(clock); + .GetService()?.Add(clock); } } } From afdfb28da8b507370771438133c9e0a4916ea010 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 9 Sep 2018 02:02:50 +0200 Subject: [PATCH 30/56] Added failing tests for brush invalidation. --- .../BorderTests.cs | 21 ++++++++ .../Avalonia.Controls.UnitTests/PanelTests.cs | 21 ++++++++ .../ContentPresenterTests_Standalone.cs | 20 +++++++- .../Shapes/RectangleTests.cs | 48 +++++++++++++++++++ .../TextBlockTests.cs | 38 +++++++++++++++ tests/Avalonia.UnitTests/TestRoot.cs | 7 +++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs diff --git a/tests/Avalonia.Controls.UnitTests/BorderTests.cs b/tests/Avalonia.Controls.UnitTests/BorderTests.cs index 9a6a041ec7..0ac9392bc6 100644 --- a/tests/Avalonia.Controls.UnitTests/BorderTests.cs +++ b/tests/Avalonia.Controls.UnitTests/BorderTests.cs @@ -1,6 +1,10 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media; +using Avalonia.Rendering; +using Avalonia.UnitTests; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -42,5 +46,22 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new Rect(6, 6, 0, 0), content.Bounds); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new Border() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } } diff --git a/tests/Avalonia.Controls.UnitTests/PanelTests.cs b/tests/Avalonia.Controls.UnitTests/PanelTests.cs index ed239120d6..4a404ea97e 100644 --- a/tests/Avalonia.Controls.UnitTests/PanelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/PanelTests.cs @@ -3,7 +3,11 @@ using System.Linq; using Avalonia.LogicalTree; +using Avalonia.Media; +using Avalonia.Rendering; +using Avalonia.UnitTests; using Avalonia.VisualTree; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -115,5 +119,22 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new[] { child2, child1 }, panel.GetLogicalChildren()); Assert.Equal(new[] { child2, child1 }, panel.GetVisualChildren()); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new Panel() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } } diff --git a/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs b/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs index 6716456c78..9d65f2cba7 100644 --- a/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs +++ b/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs @@ -13,6 +13,7 @@ using System; using System.Linq; using Xunit; using Avalonia.Rendering; +using Avalonia.Media; namespace Avalonia.Controls.UnitTests.Presenters { @@ -203,5 +204,22 @@ namespace Avalonia.Controls.UnitTests.Presenters Assert.NotEqual(foo, logicalChildren.First()); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new ContentPresenter() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } -} \ No newline at end of file +} diff --git a/tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs b/tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs new file mode 100644 index 0000000000..0ec73edec0 --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Controls.Shapes; +using Avalonia.Media; +using Avalonia.UnitTests; +using Moq; +using Xunit; + +namespace Avalonia.Controls.UnitTests.Shapes +{ + public class RectangleTests + { + [Fact] + public void Changing_Fill_Brush_Color_Should_Invalidate_Visual() + { + var target = new Rectangle() + { + Fill = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Fill).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } + + [Fact] + public void Changing_Stroke_Brush_Color_Should_Invalidate_Visual() + { + var target = new Rectangle() + { + Stroke = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Stroke).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } + } +} diff --git a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs index 9a1140fc05..45e683455b 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs @@ -2,6 +2,10 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia.Data; +using Avalonia.Media; +using Avalonia.Rendering; +using Avalonia.UnitTests; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -25,5 +29,39 @@ namespace Avalonia.Controls.UnitTests "", textBlock.Text); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new TextBlock() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } + + [Fact] + public void Changing_Foreground_Brush_Color_Should_Invalidate_Visual() + { + var target = new TextBlock() + { + Foreground = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Foreground).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } } diff --git a/tests/Avalonia.UnitTests/TestRoot.cs b/tests/Avalonia.UnitTests/TestRoot.cs index 884df33fc0..972b1d78c0 100644 --- a/tests/Avalonia.UnitTests/TestRoot.cs +++ b/tests/Avalonia.UnitTests/TestRoot.cs @@ -19,6 +19,13 @@ namespace Avalonia.UnitTests public TestRoot() { + Renderer = Mock.Of(); + } + + public TestRoot(IControl child) + : this() + { + Child = child; } event EventHandler INameScope.Registered From a5a5b36ddc2f8a1334bc152438600f0eb93a7912 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 9 Sep 2018 02:06:30 +0200 Subject: [PATCH 31/56] Invalidate controls when brush changed. --- src/Avalonia.Controls/Border.cs | 3 +- src/Avalonia.Controls/Panel.cs | 1 + .../Presenters/ContentPresenter.cs | 3 +- src/Avalonia.Controls/Shapes/Shape.cs | 4 +- src/Avalonia.Controls/TextBlock.cs | 2 +- src/Avalonia.Visuals/Visual.cs | 40 +++++++++++++++++++ 6 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/Border.cs b/src/Avalonia.Controls/Border.cs index 5f84421c64..c4bc121a27 100644 --- a/src/Avalonia.Controls/Border.cs +++ b/src/Avalonia.Controls/Border.cs @@ -43,8 +43,9 @@ namespace Avalonia.Controls /// static Border() { - AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); + AffectsRender(BorderThicknessProperty, CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty); + BrushAffectsRender(BackgroundProperty, BorderBrushProperty); } /// diff --git a/src/Avalonia.Controls/Panel.cs b/src/Avalonia.Controls/Panel.cs index c0d211effb..9b768749df 100644 --- a/src/Avalonia.Controls/Panel.cs +++ b/src/Avalonia.Controls/Panel.cs @@ -30,6 +30,7 @@ namespace Avalonia.Controls /// static Panel() { + BrushAffectsRender(BackgroundProperty); ClipToBoundsProperty.OverrideDefaultValue(true); } diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index 6badf91367..6c1d6e2cc1 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -90,8 +90,9 @@ namespace Avalonia.Controls.Presenters /// static ContentPresenter() { - AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); + AffectsRender(BorderThicknessProperty, CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty, PaddingProperty); + BrushAffectsRender(BackgroundProperty, BorderBrushProperty); ContentProperty.Changed.AddClassHandler(x => x.ContentChanged); ContentTemplateProperty.Changed.AddClassHandler(x => x.ContentChanged); TemplatedParentProperty.Changed.AddClassHandler(x => x.TemplatedParentChanged); diff --git a/src/Avalonia.Controls/Shapes/Shape.cs b/src/Avalonia.Controls/Shapes/Shape.cs index 604051ef28..d2a4a37531 100644 --- a/src/Avalonia.Controls/Shapes/Shape.cs +++ b/src/Avalonia.Controls/Shapes/Shape.cs @@ -30,11 +30,11 @@ namespace Avalonia.Controls.Shapes private Geometry _renderedGeometry; bool _calculateTransformOnArrange = false; - static Shape() { AffectsMeasure(StretchProperty, StrokeThicknessProperty); - AffectsRender(FillProperty, StrokeProperty, StrokeDashArrayProperty); + AffectsRender(StrokeDashArrayProperty); + BrushAffectsRender(FillProperty, StrokeProperty); } public Geometry DefiningGeometry diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index e91d2e8fa7..8689d11e13 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -99,10 +99,10 @@ namespace Avalonia.Controls static TextBlock() { ClipToBoundsProperty.OverrideDefaultValue(true); - AffectsRender(ForegroundProperty); AffectsRender(FontWeightProperty); AffectsRender(FontSizeProperty); AffectsRender(FontStyleProperty); + BrushAffectsRender(BackgroundProperty, ForegroundProperty); } /// diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 81e1a93a6f..8631d2001d 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -328,6 +328,44 @@ namespace Avalonia } } + /// + /// Indicates that a brush property change should cause to be + /// called. + /// + /// The properties. + /// + /// This method should be called in a control's static constructor with each property + /// on the control which when changed should cause a redraw. It not only triggers an + /// invalidation when the property itself changes, but also when the brush raises + /// the event. + /// + protected static void BrushAffectsRender(params AvaloniaProperty[] properties) + where T : Visual + { + void Invalidate(AvaloniaPropertyChangedEventArgs e) + { + if (e.Sender is T sender) + { + if (e.OldValue is IMutableBrush oldValue) + { + oldValue.Changed -= sender.BrushChanged; + } + + if (e.NewValue is IMutableBrush newValue) + { + newValue.Changed += sender.BrushChanged; + } + + sender.InvalidateVisual(); + } + } + + foreach (var property in properties) + { + property.Changed.Subscribe(Invalidate); + } + } + /// /// Calls the method /// for this control and all of its visual descendants. @@ -530,6 +568,8 @@ namespace Avalonia OnVisualParentChanged(old, value); } + private void BrushChanged(object sender, EventArgs e) => InvalidateVisual(); + /// /// Called when the collection changes. /// From 460d63736da2bd98f5b216b4a1860aa0354ff53f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 9 Sep 2018 03:17:28 +0200 Subject: [PATCH 32/56] Use immutable brush as default Foreground value. Prevents memory leak. --- src/Avalonia.Controls/TextBlock.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 8689d11e13..ee3e3e361a 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -6,6 +6,7 @@ using System.Reactive; using System.Reactive.Linq; using Avalonia.LogicalTree; using Avalonia.Media; +using Avalonia.Media.Immutable; using Avalonia.Metadata; namespace Avalonia.Controls @@ -65,7 +66,7 @@ namespace Avalonia.Controls public static readonly AttachedProperty ForegroundProperty = AvaloniaProperty.RegisterAttached( nameof(Foreground), - new SolidColorBrush(0xff000000), + Brushes.Black, inherits: true); /// From ec695433dec3a6b66967f692b66538e95366a842 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Mon, 10 Sep 2018 01:02:35 +0800 Subject: [PATCH 33/56] Cancel an animation instance when the selector turns false. --- src/Avalonia.Animation/Animator`1.cs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index f0ef55aa9e..74f09d5488 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -37,12 +37,29 @@ namespace Avalonia.Animation if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - return match + var matchStream = match + .DistinctUntilChanged() + .Publish() + .RefCount(); + + var activeInstance = matchStream .Where(p => p) - .Subscribe(_ => - { - var timerObs = RunKeyFrames(animation, control, onComplete); - }); + .Select(p => RunKeyFrames(animation, control, onComplete)); + + var negationStream = matchStream + .Where(p => !p); + + return Observable + .WithLatestFrom( + negationStream, + activeInstance, + (isMatch, instance) => + { + if (!isMatch && animation.RepeatCount.IsLoop) + instance?.Dispose(); + return true; + }) + .Subscribe(); } /// From bc850b49c16090d81d5d049dd9ff2cfc7efa0de1 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Sun, 9 Sep 2018 23:52:20 -0500 Subject: [PATCH 34/56] Add in custom hit testing for ImmediateRenderer. Fixes #1879. --- src/Avalonia.Controls/Primitives/AdornerLayer.cs | 8 +++++++- .../Rendering/ICustomSimpleHitTest.cs | 12 ++++++++++++ src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs | 11 ++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs diff --git a/src/Avalonia.Controls/Primitives/AdornerLayer.cs b/src/Avalonia.Controls/Primitives/AdornerLayer.cs index 676cdc456a..4b58197ef3 100644 --- a/src/Avalonia.Controls/Primitives/AdornerLayer.cs +++ b/src/Avalonia.Controls/Primitives/AdornerLayer.cs @@ -6,11 +6,12 @@ using System.Collections.Specialized; using System.Linq; using Avalonia.VisualTree; using Avalonia.Media; +using Avalonia.Rendering; namespace Avalonia.Controls.Primitives { // TODO: Need to track position of adorned elements and move the adorner if they move. - public class AdornerLayer : Panel + public class AdornerLayer : Panel, ICustomSimpleHitTest { public static AttachedProperty AdornedElementProperty = AvaloniaProperty.RegisterAttached("AdornedElement"); @@ -137,6 +138,11 @@ namespace Avalonia.Controls.Primitives } } + public bool HitTest(Point point) + { + return Children.Any(ctrl => ctrl.TransformedBounds?.Contains(point) == true); + } + private class AdornedElementInfo { public IDisposable Subscription { get; set; } diff --git a/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs b/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs new file mode 100644 index 0000000000..7199053b08 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/ICustomSimpleHitTest.cs @@ -0,0 +1,12 @@ +namespace Avalonia.Rendering +{ + /// + /// An interface to allow non-templated controls to customize their hit-testing + /// when using a renderer with a simple hit-testing algorithm without a scene graph, + /// such as + /// + public interface ICustomSimpleHitTest + { + bool HitTest(Point point); + } +} diff --git a/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs b/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs index fd62c8a59f..d373e7ef2a 100644 --- a/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs @@ -200,7 +200,16 @@ namespace Avalonia.Rendering if (filter?.Invoke(visual) != false) { - bool containsPoint = visual.TransformedBounds?.Contains(p) == true; + bool containsPoint = false; + + if (visual is ICustomSimpleHitTest custom) + { + containsPoint = custom.HitTest(p); + } + else + { + containsPoint = visual.TransformedBounds?.Contains(p) == true; + } if ((containsPoint || !visual.ClipToBounds) && visual.VisualChildren.Count > 0) { From 64a4a6d82af01a3ba2c69182b2fcd16c33ddc153 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Tue, 11 Sep 2018 15:04:12 +0800 Subject: [PATCH 35/56] Simplify Fix; Invalidate when IsIndeterminate property changes. --- src/Avalonia.Animation/Animator`1.cs | 39 ++++++++++------------------ src/Avalonia.Controls/ProgressBar.cs | 8 +++++- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 74f09d5488..ab82bfb35d 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -17,7 +17,7 @@ namespace Avalonia.Animation /// List of type-converted keyframes. /// private readonly List _convertedKeyframes = new List(); - + private bool _isVerifiedAndConverted; /// @@ -28,38 +28,25 @@ namespace Avalonia.Animation public Animator() { // Invalidate keyframes when changed. - this.CollectionChanged += delegate { _isVerifiedAndConverted = false; }; + this.CollectionChanged += delegate { _isVerifiedAndConverted = false; }; } /// public virtual IDisposable Apply(Animation animation, Animatable control, IObservable match, Action onComplete) { - if (!_isVerifiedAndConverted) + if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - var matchStream = match - .DistinctUntilChanged() - .Publish() - .RefCount(); - - var activeInstance = matchStream - .Where(p => p) - .Select(p => RunKeyFrames(animation, control, onComplete)); - - var negationStream = matchStream - .Where(p => !p); - - return Observable - .WithLatestFrom( - negationStream, - activeInstance, - (isMatch, instance) => + return match + .DistinctUntilChanged() + .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) + .Buffer(2, 1) + .Where(x => x.Count > 1) + .Subscribe(x => { - if (!isMatch && animation.RepeatCount.IsLoop) - instance?.Dispose(); - return true; - }) - .Subscribe(); + if (animation.RepeatCount.IsLoop) + x[0]?.Dispose(); + }); } /// @@ -72,7 +59,7 @@ namespace Avalonia.Animation /// The time parameter, relative to the total animation time protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double t) { - AnimatorKeyFrame firstCue, lastCue ; + AnimatorKeyFrame firstCue, lastCue; int kvCount = _convertedKeyframes.Count; if (kvCount > 2) { diff --git a/src/Avalonia.Controls/ProgressBar.cs b/src/Avalonia.Controls/ProgressBar.cs index 7f4b549849..fe7b8e64c7 100644 --- a/src/Avalonia.Controls/ProgressBar.cs +++ b/src/Avalonia.Controls/ProgressBar.cs @@ -38,6 +38,7 @@ namespace Avalonia.Controls PseudoClass(IsIndeterminateProperty, ":indeterminate"); ValueProperty.Changed.AddClassHandler(x => x.ValueChanged); + IsIndeterminateProperty.Changed.AddClassHandler(x => x.IsIndeterminateChanged); } public bool IsIndeterminate @@ -118,5 +119,10 @@ namespace Avalonia.Controls { UpdateIndicator(Bounds.Size); } + + private void IsIndeterminateChanged(AvaloniaPropertyChangedEventArgs e) + { + UpdateIndicator(Bounds.Size); + } } -} +} \ No newline at end of file From 388df18e52efa177e694156607e369390e9de22e Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 7 Sep 2018 13:58:02 +0200 Subject: [PATCH 36/56] Initial commit of Event Viewer. --- .../Avalonia.Diagnostics.csproj | 8 ++ src/Avalonia.Diagnostics/DevTools.xaml | 1 + src/Avalonia.Diagnostics/DevTools.xaml.cs | 23 +++++ src/Avalonia.Diagnostics/Models/ChainLink.cs | 34 +++++++ .../ViewModels/ControlTreeNode.cs | 65 +++++++++++++ .../ViewModels/DevToolsViewModel.cs | 5 + .../ViewModels/EventEntryTreeNode.cs | 97 +++++++++++++++++++ .../ViewModels/EventTreeNode.cs | 80 +++++++++++++++ .../ViewModels/EventsViewModel.cs | 75 ++++++++++++++ .../ViewModels/FiredEvent.cs | 93 ++++++++++++++++++ .../Views/EventsView.xaml | 53 ++++++++++ .../Views/EventsView.xaml.cs | 28 ++++++ 12 files changed, 562 insertions(+) create mode 100644 src/Avalonia.Diagnostics/Models/ChainLink.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs create mode 100644 src/Avalonia.Diagnostics/Views/EventsView.xaml create mode 100644 src/Avalonia.Diagnostics/Views/EventsView.xaml.cs diff --git a/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj b/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj index a2ff0ecf02..b807571a38 100644 --- a/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj +++ b/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj @@ -2,6 +2,9 @@ netstandard2.0 + + + @@ -17,4 +20,9 @@ + + + MSBuild:Compile + + \ No newline at end of file diff --git a/src/Avalonia.Diagnostics/DevTools.xaml b/src/Avalonia.Diagnostics/DevTools.xaml index 844670e794..a3b12f0ec5 100644 --- a/src/Avalonia.Diagnostics/DevTools.xaml +++ b/src/Avalonia.Diagnostics/DevTools.xaml @@ -3,6 +3,7 @@ + diff --git a/src/Avalonia.Diagnostics/DevTools.xaml.cs b/src/Avalonia.Diagnostics/DevTools.xaml.cs index be0863954a..d084ec3014 100644 --- a/src/Avalonia.Diagnostics/DevTools.xaml.cs +++ b/src/Avalonia.Diagnostics/DevTools.xaml.cs @@ -10,6 +10,7 @@ using Avalonia.Input; using Avalonia.Input.Raw; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.Rendering; using Avalonia.VisualTree; namespace Avalonia @@ -28,6 +29,7 @@ namespace Avalonia.Diagnostics public class DevTools : UserControl { private static Dictionary s_open = new Dictionary(); + private static HashSet s_visualTreeRoots = new HashSet(); private IDisposable _keySubscription; public DevTools(IControl root) @@ -79,6 +81,7 @@ namespace Avalonia.Diagnostics devToolsWindow.Closed += devTools.DevToolsClosed; s_open.Add(control, devToolsWindow); + MarkAsDevTool(devToolsWindow); devToolsWindow.Show(); } } @@ -89,6 +92,7 @@ namespace Avalonia.Diagnostics var devToolsWindow = (Window)sender; var devTools = (DevTools)devToolsWindow.Content; s_open.Remove((TopLevel)devTools.Root); + RemoveDevTool(devToolsWindow); _keySubscription.Dispose(); devToolsWindow.Closed -= DevToolsClosed; } @@ -116,5 +120,24 @@ namespace Avalonia.Diagnostics } } } + + /// + /// Marks a visual as part of the DevTools, so it can be excluded from event tracking. + /// + /// The visual whose root is to be marked. + public static void MarkAsDevTool(IVisual visual) + { + s_visualTreeRoots.Add(visual.GetVisualRoot()); + } + + public static void RemoveDevTool(IVisual visual) + { + s_visualTreeRoots.Remove(visual.GetVisualRoot()); + } + + public static bool BelongsToDevTool(IVisual visual) + { + return s_visualTreeRoots.Contains(visual.GetVisualRoot()); + } } } diff --git a/src/Avalonia.Diagnostics/Models/ChainLink.cs b/src/Avalonia.Diagnostics/Models/ChainLink.cs new file mode 100644 index 0000000000..9ea99ff1ae --- /dev/null +++ b/src/Avalonia.Diagnostics/Models/ChainLink.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Interactivity; + +namespace Avalonia.Diagnostics.Models +{ + internal class ChainLink + { + public object Handler { get; private set; } + public string HandlerName + { + get + { + if (Handler is INamed named && !string.IsNullOrEmpty(named.Name)) + { + return named.Name + " (" + Handler.GetType().Name + ")"; + } + return Handler.GetType().Name; + } + } + public bool Handled { get; private set; } + public RoutingStrategies Route { get; private set; } + + public ChainLink(object handler, bool handled, RoutingStrategies route) + { + Contract.Requires(handler != null); + + this.Handler = handler; + this.Handled = handled; + this.Route = route; + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs new file mode 100644 index 0000000000..d8a6a3bdc3 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs @@ -0,0 +1,65 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Avalonia.Collections; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class ControlTreeNode : EventTreeNode + { + public ControlTreeNode(Type type, IEnumerable events, EventsViewModel vm) + : base(null, type.Name) + { + this.Children = new AvaloniaList(events.OrderBy(e => e.Name).Select(e => new EventEntryTreeNode(this, e, vm) { IsEnabled = IsDefault(e) })); + this.IsExpanded = true; + } + + RoutedEvent[] defaultEvents = new RoutedEvent[] + { + Button.ClickEvent, + InputElement.KeyDownEvent, + InputElement.KeyUpEvent, + InputElement.TextInputEvent, + InputElement.PointerReleasedEvent, + InputElement.PointerPressedEvent, + }; + + private bool IsDefault(RoutedEvent e) + { + return defaultEvents.Contains(e); + } + + public override bool? IsEnabled + { + get => base.IsEnabled; + set + { + if (base.IsEnabled != value) + { + base.IsEnabled = value; + if (_updateChildren && value != null) + { + foreach (var child in Children) + { + try + { + child._updateParent = false; + child.IsEnabled = value; + } + finally + { + child._updateParent = true; + } + } + } + } + } + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs b/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs index ce8ad36c17..c6d3f02e8b 100644 --- a/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs +++ b/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs @@ -14,6 +14,7 @@ namespace Avalonia.Diagnostics.ViewModels private int _selectedTab; private TreePageViewModel _logicalTree; private TreePageViewModel _visualTree; + private EventsViewModel _eventsView; private string _focusedControl; private string _pointerOverElement; @@ -21,6 +22,7 @@ namespace Avalonia.Diagnostics.ViewModels { _logicalTree = new TreePageViewModel(LogicalTreeNode.Create(root)); _visualTree = new TreePageViewModel(VisualTreeNode.Create(root)); + _eventsView = new EventsViewModel(root); UpdateFocusedControl(); KeyboardDevice.Instance.PropertyChanged += (s, e) => @@ -57,6 +59,9 @@ namespace Avalonia.Diagnostics.ViewModels case 1: Content = _visualTree; break; + case 2: + Content = _eventsView; + break; } RaisePropertyChanged(); diff --git a/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs new file mode 100644 index 0000000000..8eead869d5 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs @@ -0,0 +1,97 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using Avalonia.Diagnostics.Models; +using Avalonia.Interactivity; +using Avalonia.Threading; +using Avalonia.VisualTree; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class EventEntryTreeNode : EventTreeNode + { + RoutedEvent _event; + EventsViewModel _parentViewModel; + bool _isRegistered; + FiredEvent _currentEvent; + + public EventEntryTreeNode(ControlTreeNode parent, RoutedEvent @event, EventsViewModel vm) + : base(parent, @event.Name) + { + Contract.Requires(@event != null); + Contract.Requires(vm != null); + + this._event = @event; + this._parentViewModel = vm; + } + + public override bool? IsEnabled + { + get => base.IsEnabled; + set + { + if (base.IsEnabled != value) + { + base.IsEnabled = value; + UpdateTracker(); + if (Parent != null && _updateParent) + { + try + { + Parent._updateChildren = false; + Parent.UpdateChecked(); + } + finally + { + Parent._updateChildren = true; + } + } + } + } + } + + private void UpdateTracker() + { + if (IsEnabled.GetValueOrDefault() && !_isRegistered) + { + _event.AddClassHandler(typeof(object), HandleEvent, (RoutingStrategies)7, handledEventsToo: true); + _isRegistered = true; + } + } + + private void HandleEvent(object sender, RoutedEventArgs e) + { + if (!_isRegistered || IsEnabled == false) + return; + if (sender is IVisual v && DevTools.BelongsToDevTool(v)) + return; + + var s = sender; + var handled = e.Handled; + var route = e.Route; + + Action handler = delegate + { + if (_currentEvent == null || !_currentEvent.IsPartOfSameEventChain(e)) + { + _currentEvent = new FiredEvent(e, new ChainLink(s, handled, route)); + + _parentViewModel.RecordedEvents.Add(_currentEvent); + + while (_parentViewModel.RecordedEvents.Count > 100) + _parentViewModel.RecordedEvents.RemoveAt(0); + } + else + { + _currentEvent.AddToChain(new ChainLink(s, handled, route)); + } + }; + + if (!Dispatcher.UIThread.CheckAccess()) + Dispatcher.UIThread.Post(handler); + else + handler(); + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs new file mode 100644 index 0000000000..50ea0c9c31 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs @@ -0,0 +1,80 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System.Diagnostics; +using Avalonia.Collections; +using Avalonia.VisualTree; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal abstract class EventTreeNode : ViewModelBase + { + internal bool _updateChildren = true; + internal bool _updateParent = true; + private bool _isExpanded; + private bool? _isEnabled = false; + + public EventTreeNode(EventTreeNode parent, string text) + { + this.Parent = parent; + this.Text = text; + } + + public IAvaloniaReadOnlyList Children + { + get; + protected set; + } + + public bool IsExpanded + { + get { return _isExpanded; } + set { RaiseAndSetIfChanged(ref _isExpanded, value); } + } + + public virtual bool? IsEnabled + { + get { return _isEnabled; } + set { RaiseAndSetIfChanged(ref _isEnabled, value); } + } + + public EventTreeNode Parent + { + get; + } + + public string Text + { + get; + private set; + } + + internal void UpdateChecked() + { + IsEnabled = GetValue(); + + bool? GetValue() + { + if (Children == null) + return false; + bool? value = false; + for (int i = 0; i < Children.Count; i++) + { + if (i == 0) + { + value = Children[i].IsEnabled; + continue; + } + + if (value != Children[i].IsEnabled) + { + value = null; + break; + } + } + + return value; + } + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs b/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs new file mode 100644 index 0000000000..a9a6334689 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs @@ -0,0 +1,75 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Windows.Input; +using Avalonia.Collections; +using Avalonia.Controls; +using Avalonia.Data.Converters; +using Avalonia.Interactivity; +using Avalonia.Media; +using Avalonia.Threading; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class EventsViewModel : ViewModelBase + { + private IControl _root; + private FiredEvent _selectedEvent; + private ICommand ClearCommand { get; } + + public EventsViewModel(IControl root) + { + this._root = root; + this.Nodes = RoutedEventRegistry.Instance.GetAllRegistered() + .GroupBy(e => e.OwnerType) + .OrderBy(e => e.Key.Name) + .Select(g => new ControlTreeNode(g.Key, g, this)) + .ToArray(); + } + + private void ClearExecute() + { + Action action = delegate + { + RecordedEvents.Clear(); + }; + if (!Dispatcher.UIThread.CheckAccess()) + { + Dispatcher.UIThread.Post(action); + } + else + { + action(); + } + } + + public EventTreeNode[] Nodes { get; } + + public ObservableCollection RecordedEvents { get; } = new ObservableCollection(); + + public FiredEvent SelectedEvent + { + get => _selectedEvent; + set => RaiseAndSetIfChanged(ref _selectedEvent, value); + } + } + + internal class BoolToBrushConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return (bool)value ? Brushes.LightGreen : Brushes.Transparent; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs b/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs new file mode 100644 index 0000000000..523525b634 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs @@ -0,0 +1,93 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.ObjectModel; +using Avalonia.Diagnostics.Models; +using Avalonia.Interactivity; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class FiredEvent : ViewModelBase + { + private RoutedEventArgs _eventArgs; + + private ChainLink _handledBy; + private ChainLink _originator; + + public FiredEvent(RoutedEventArgs eventArgs, ChainLink originator) + { + Contract.Requires(eventArgs != null); + Contract.Requires(originator != null); + + this._eventArgs = eventArgs; + this._originator = originator; + AddToChain(originator); + } + + public bool IsPartOfSameEventChain(RoutedEventArgs e) + { + return e == _eventArgs; + } + + public RoutedEvent Event => _eventArgs.RoutedEvent; + + public bool IsHandled => HandledBy?.Handled == true; + + public ObservableCollection EventChain { get; } = new ObservableCollection(); + + public string DisplayText + { + get + { + if (IsHandled) + { + return $"{Event.Name} on {Originator.HandlerName};" + Environment.NewLine + + $"strategies: {Event.RoutingStrategies}; handled by: {HandledBy.HandlerName}"; + } + return $"{Event.Name} on {Originator.HandlerName}; strategies: {Event.RoutingStrategies}"; + } + } + + public ChainLink Originator + { + get { return _originator; } + set + { + if (_originator != value) + { + _originator = value; + RaisePropertyChanged(); + RaisePropertyChanged(nameof(DisplayText)); + } + } + } + + public ChainLink HandledBy + { + get { return _handledBy; } + set + { + if (_handledBy != value) + { + _handledBy = value; + RaisePropertyChanged(); + RaisePropertyChanged(nameof(IsHandled)); + RaisePropertyChanged(nameof(DisplayText)); + } + } + } + + public void AddToChain(object handler, bool handled, RoutingStrategies route) + { + AddToChain(new ChainLink(handler, handled, route)); + } + + public void AddToChain(ChainLink link) + { + EventChain.Add(link); + if (HandledBy == null && link.Handled) + HandledBy = link; + } + } +} diff --git a/src/Avalonia.Diagnostics/Views/EventsView.xaml b/src/Avalonia.Diagnostics/Views/EventsView.xaml new file mode 100644 index 0000000000..79f78d2bba --- /dev/null +++ b/src/Avalonia.Diagnostics/Views/EventsView.xaml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + static Border() { - AffectsRender(BorderThicknessProperty, CornerRadiusProperty); + AffectsRender( + BackgroundProperty, + BorderBrushProperty, + BorderThicknessProperty, + CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty); - BrushAffectsRender(BackgroundProperty, BorderBrushProperty); } /// diff --git a/src/Avalonia.Controls/Panel.cs b/src/Avalonia.Controls/Panel.cs index 9b768749df..5415f3974d 100644 --- a/src/Avalonia.Controls/Panel.cs +++ b/src/Avalonia.Controls/Panel.cs @@ -30,7 +30,7 @@ namespace Avalonia.Controls /// static Panel() { - BrushAffectsRender(BackgroundProperty); + AffectsRender(BackgroundProperty); ClipToBoundsProperty.OverrideDefaultValue(true); } diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index a8019fc306..8d703cfc1c 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -90,9 +90,8 @@ namespace Avalonia.Controls.Presenters /// static ContentPresenter() { - AffectsRender(BorderThicknessProperty, CornerRadiusProperty); + AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty, PaddingProperty); - BrushAffectsRender(BackgroundProperty, BorderBrushProperty); ContentProperty.Changed.AddClassHandler(x => x.ContentChanged); ContentTemplateProperty.Changed.AddClassHandler(x => x.ContentChanged); TemplatedParentProperty.Changed.AddClassHandler(x => x.TemplatedParentChanged); diff --git a/src/Avalonia.Controls/Shapes/Shape.cs b/src/Avalonia.Controls/Shapes/Shape.cs index 7ca0867031..f77c43acd0 100644 --- a/src/Avalonia.Controls/Shapes/Shape.cs +++ b/src/Avalonia.Controls/Shapes/Shape.cs @@ -33,8 +33,7 @@ namespace Avalonia.Controls.Shapes static Shape() { AffectsMeasure(StretchProperty, StrokeThicknessProperty); - AffectsRender(StrokeDashArrayProperty); - BrushAffectsRender(FillProperty, StrokeProperty); + AffectsRender(FillProperty, StrokeProperty, StrokeDashArrayProperty); } public Geometry DefiningGeometry diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 5a98ef3ecc..af7b0f835e 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -101,10 +101,11 @@ namespace Avalonia.Controls { ClipToBoundsProperty.OverrideDefaultValue(true); AffectsRender( + BackgroundProperty, + ForegroundProperty, FontWeightProperty, FontSizeProperty, FontStyleProperty); - BrushAffectsRender(BackgroundProperty, ForegroundProperty); } /// diff --git a/src/Avalonia.Visuals/Media/Brush.cs b/src/Avalonia.Visuals/Media/Brush.cs index 8ba7c1be04..c2c041f073 100644 --- a/src/Avalonia.Visuals/Media/Brush.cs +++ b/src/Avalonia.Visuals/Media/Brush.cs @@ -19,7 +19,7 @@ namespace Avalonia.Media AvaloniaProperty.Register(nameof(Opacity), 1.0); /// - public event EventHandler Changed; + public event EventHandler Invalidated; /// /// Gets or sets the opacity of the brush. @@ -63,14 +63,14 @@ namespace Avalonia.Media /// The properties. /// /// After a call to this method in a brush's static constructor, any change to the - /// property will cause the event to be raised on the brush. + /// property will cause the event to be raised on the brush. /// protected static void AffectsRender(params AvaloniaProperty[] properties) where T : Brush { void Invalidate(AvaloniaPropertyChangedEventArgs e) { - (e.Sender as T)?.RaiseChanged(EventArgs.Empty); + (e.Sender as T)?.RaiseInvalidated(EventArgs.Empty); } foreach (var property in properties) @@ -80,9 +80,9 @@ namespace Avalonia.Media } /// - /// Raises the event. + /// Raises the event. /// /// The event args. - protected void RaiseChanged(EventArgs e) => Changed?.Invoke(this, e); + protected void RaiseInvalidated(EventArgs e) => Invalidated?.Invoke(this, e); } } diff --git a/src/Avalonia.Visuals/Media/GradientBrush.cs b/src/Avalonia.Visuals/Media/GradientBrush.cs index c123813cee..8fd2dcf27f 100644 --- a/src/Avalonia.Visuals/Media/GradientBrush.cs +++ b/src/Avalonia.Visuals/Media/GradientBrush.cs @@ -80,18 +80,18 @@ namespace Avalonia.Media brush._gradientStopsSubscription = newValue.TrackItemPropertyChanged(brush.GradientStopChanged); } - brush.RaiseChanged(EventArgs.Empty); + brush.RaiseInvalidated(EventArgs.Empty); } } private void GradientStopsChanged(object sender, NotifyCollectionChangedEventArgs e) { - RaiseChanged(EventArgs.Empty); + RaiseInvalidated(EventArgs.Empty); } private void GradientStopChanged(Tuple e) { - RaiseChanged(EventArgs.Empty); + RaiseInvalidated(EventArgs.Empty); } } } diff --git a/src/Avalonia.Visuals/Media/IAffectsRender.cs b/src/Avalonia.Visuals/Media/IAffectsRender.cs new file mode 100644 index 0000000000..0024195ce5 --- /dev/null +++ b/src/Avalonia.Visuals/Media/IAffectsRender.cs @@ -0,0 +1,16 @@ +using System; + +namespace Avalonia.Media +{ + /// + /// Signals to a self-rendering control that changes to the resource should invoke + /// . + /// + public interface IAffectsRender + { + /// + /// Raised when the resource changes visually. + /// + event EventHandler Invalidated; + } +} diff --git a/src/Avalonia.Visuals/Media/IMutableBrush.cs b/src/Avalonia.Visuals/Media/IMutableBrush.cs index 762731a6a8..415db61d68 100644 --- a/src/Avalonia.Visuals/Media/IMutableBrush.cs +++ b/src/Avalonia.Visuals/Media/IMutableBrush.cs @@ -5,13 +5,8 @@ namespace Avalonia.Media /// /// Represents a mutable brush which can return an immutable clone of itself. /// - public interface IMutableBrush : IBrush + public interface IMutableBrush : IBrush, IAffectsRender { - /// - /// Raised when the brush changes visually. - /// - event EventHandler Changed; - /// /// Creates an immutable clone of the brush. /// diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index fdff16494f..e5fcf1ba1d 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -338,45 +338,20 @@ namespace Avalonia /// FrameworkPropertyMetadata.AffectsRender flag. /// protected static void AffectsRender(params AvaloniaProperty[] properties) - where T : class, IVisual - { - void Invalidate(AvaloniaPropertyChangedEventArgs e) - { - (e.Sender as T)?.InvalidateVisual(); - } - - foreach (var property in properties) - { - property.Changed.Subscribe(Invalidate); - } - } - - /// - /// Indicates that a brush property change should cause to be - /// called. - /// - /// The properties. - /// - /// This method should be called in a control's static constructor with each property - /// on the control which when changed should cause a redraw. It not only triggers an - /// invalidation when the property itself changes, but also when the brush raises - /// the event. - /// - protected static void BrushAffectsRender(params AvaloniaProperty[] properties) where T : Visual { void Invalidate(AvaloniaPropertyChangedEventArgs e) { if (e.Sender is T sender) { - if (e.OldValue is IMutableBrush oldValue) + if (e.OldValue is IAffectsRender oldValue) { - oldValue.Changed -= sender.BrushChanged; + oldValue.Invalidated -= sender.AffectsRenderInvalidated; } - if (e.NewValue is IMutableBrush newValue) + if (e.NewValue is IAffectsRender newValue) { - newValue.Changed += sender.BrushChanged; + newValue.Invalidated += sender.AffectsRenderInvalidated; } sender.InvalidateVisual(); @@ -582,7 +557,7 @@ namespace Avalonia OnVisualParentChanged(old, value); } - private void BrushChanged(object sender, EventArgs e) => InvalidateVisual(); + private void AffectsRenderInvalidated(object sender, EventArgs e) => InvalidateVisual(); /// /// Called when the collection changes. diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs index f843a6e333..ff7b94105a 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs @@ -11,14 +11,14 @@ namespace Avalonia.Visuals.UnitTests.Media public class ImageBrushTests { [Fact] - public void Changing_Source_Raises_Changed() + public void Changing_Source_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); var target = new ImageBrush(bitmap1); var raised = false; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.Source = bitmap2; Assert.True(raised); diff --git a/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs index 62f53108e6..b3f78dfe8f 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs @@ -9,7 +9,7 @@ namespace Avalonia.Visuals.UnitTests.Media public class LinearGradientBrushTests { [Fact] - public void Changing_StartPoint_Raises_Changed() + public void Changing_StartPoint_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -17,14 +17,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.StartPoint = new RelativePoint(); - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.StartPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); Assert.True(raised); } [Fact] - public void Changing_EndPoint_Raises_Changed() + public void Changing_EndPoint_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -32,14 +32,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.EndPoint = new RelativePoint(); - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.EndPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); Assert.True(raised); } [Fact] - public void Changing_GradientStops_Raises_Changed() + public void Changing_GradientStops_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -47,14 +47,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.GradientStops = new GradientStops { new GradientStop(Colors.Green, 0) }; Assert.True(raised); } [Fact] - public void Adding_GradientStop_Raises_Changed() + public void Adding_GradientStop_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -62,14 +62,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.GradientStops.Add(new GradientStop(Colors.Green, 1)); Assert.True(raised); } [Fact] - public void Changing_GradientStop_Offset_Raises_Changed() + public void Changing_GradientStop_Offset_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -77,7 +77,7 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.GradientStops[0].Offset = 0.5; Assert.True(raised); diff --git a/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs index 4e87b7081d..bcf63f9660 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs @@ -7,12 +7,12 @@ namespace Avalonia.Visuals.UnitTests.Media public class SolidColorBrushTests { [Fact] - public void Changing_Color_Raises_Changed() + public void Changing_Color_Raises_Invalidated() { var target = new SolidColorBrush(Colors.Red); var raised = false; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.Color = Colors.Green; Assert.True(raised); From 3cbcd0ac0fab750a413b0e9aaeba88d092faa238 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Wed, 12 Sep 2018 10:42:44 +0800 Subject: [PATCH 38/56] Match CSS's behavior on selectors & animations. --- src/Avalonia.Animation/Animator`1.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index ab82bfb35d..e4af0f356d 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -37,16 +37,11 @@ namespace Avalonia.Animation if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - return match - .DistinctUntilChanged() - .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) - .Buffer(2, 1) - .Where(x => x.Count > 1) - .Subscribe(x => - { - if (animation.RepeatCount.IsLoop) - x[0]?.Dispose(); - }); + return match.DistinctUntilChanged() + .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) + .Buffer(2, 1) + .Where(x => x.Count > 1) + .Subscribe(x => x[0]?.Dispose()); } /// From 5fce271ff8bfff57987199d0fde5839c4788d650 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 11 Sep 2018 21:43:05 -0500 Subject: [PATCH 39/56] PR Feedback --- src/Avalonia.Animation/IAnimation.cs | 4 ++-- src/Avalonia.Animation/IAnimator.cs | 2 +- src/Avalonia.Animation/IGlobalClock.cs | 10 ++++++++++ src/Avalonia.Controls/Application.cs | 2 +- .../InternalPlatformThreadingInterface.cs | 9 ++++++--- src/Avalonia.Controls/TopLevel.cs | 1 - src/Avalonia.Visuals/Animation/RenderLoopClock.cs | 6 +++--- .../Rendering/DefaultRenderTimer.cs | 12 +++++++----- .../Rendering/DeferredRenderer.cs | 2 +- src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs | 15 +-------------- src/Avalonia.Visuals/Rendering/IRenderTimer.cs | 4 ++-- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 4 ++-- .../Rendering/RenderLoopTests.cs | 14 +++++++------- 13 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 src/Avalonia.Animation/IGlobalClock.cs diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index 34b0a5d769..ff85535d8a 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -9,12 +9,12 @@ namespace Avalonia.Animation public interface IAnimation { /// - /// Apply the animation to the specified control + /// Apply the animation to the specified control and run it when produces true. /// IDisposable Apply(Animatable control, IClock clock, IObservable match, Action onComplete = null); /// - /// Run the animation to the specified control + /// Run the animation on the specified control. /// Task RunAsync(Animatable control, IClock clock); } diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 04bad8e112..d0fb173c54 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -16,6 +16,6 @@ namespace Avalonia.Animation /// /// Applies the current KeyFrame group to the specified control. /// - IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete); + IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable match, Action onComplete); } } diff --git a/src/Avalonia.Animation/IGlobalClock.cs b/src/Avalonia.Animation/IGlobalClock.cs new file mode 100644 index 0000000000..b0455e2c80 --- /dev/null +++ b/src/Avalonia.Animation/IGlobalClock.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Avalonia.Animation +{ + public interface IGlobalClock : IClock + { + } +} diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 586a73b75c..37796ff9ba 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -340,7 +340,7 @@ namespace Avalonia var clock = new RenderLoopClock(); AvaloniaLocator.CurrentMutable - .Bind().ToConstant(clock) + .Bind().ToConstant(clock) .GetService()?.Add(clock); } } diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index 501e15653a..bb357453ff 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -14,7 +14,10 @@ namespace Avalonia.Controls.Platform public InternalPlatformThreadingInterface() { TlsCurrentThreadIsLoopThread = true; - StartTimer(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 0, 66), () => Tick?.Invoke(Environment.TickCount)); + StartTimer( + DispatcherPriority.Render, + new TimeSpan(0, 0, 0, 0, 66), + () => Tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount))); } private readonly AutoResetEvent _signaled = new AutoResetEvent(false); @@ -105,7 +108,7 @@ namespace Avalonia.Controls.Platform public bool CurrentThreadIsLoopThread => TlsCurrentThreadIsLoopThread; public event Action Signaled; - public event Action Tick; + public event Action Tick; } -} \ No newline at end of file +} diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index fb5b932fd8..630753396f 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -96,7 +96,6 @@ namespace Avalonia.Controls _applicationLifecycle = TryGetService(dependencyResolver); _renderInterface = TryGetService(dependencyResolver); - var renderLoop = TryGetService(dependencyResolver); Renderer = impl.CreateRenderer(this); impl.SetInputRoot(this); diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index e59b3aac0d..504caef461 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -5,7 +5,7 @@ using Avalonia.Rendering; namespace Avalonia.Animation { - public class RenderLoopClock : ClockBase, IRenderLoopTask + public class RenderLoopClock : ClockBase, IRenderLoopTask, IGlobalClock { protected override void Stop() { @@ -18,9 +18,9 @@ namespace Avalonia.Animation { } - void IRenderLoopTask.Update(long tickCount) + void IRenderLoopTask.Update(TimeSpan time) { - Pulse(TimeSpan.FromMilliseconds(tickCount)); + Pulse(time); } } } diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index a83334ff5e..d0eb181c65 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -19,7 +19,7 @@ namespace Avalonia.Rendering { private IRuntimePlatform _runtime; private int _subscriberCount; - private Action _tick; + private Action _tick; private IDisposable _subscription; /// @@ -39,7 +39,7 @@ namespace Avalonia.Rendering public int FramesPerSecond { get; } /// - public event Action Tick + public event Action Tick { add { @@ -78,14 +78,16 @@ namespace Avalonia.Rendering /// This can be overridden by platform implementations to use a specialized timer /// implementation. /// - protected virtual IDisposable StartCore(Action tick) + protected virtual IDisposable StartCore(Action tick) { if (_runtime == null) { _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); + return _runtime.StartSystemTimer( + TimeSpan.FromSeconds(1.0 / FramesPerSecond), + () => tick(TimeSpan.FromMilliseconds(Environment.TickCount))); } /// @@ -97,7 +99,7 @@ namespace Avalonia.Rendering _subscription = null; } - private void InternalTick(long tickCount) + private void InternalTick(TimeSpan tickCount) { _tick(tickCount); } diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index 3221dd85c6..fc67b5c461 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -166,7 +166,7 @@ namespace Avalonia.Rendering bool IRenderLoopTask.NeedsUpdate => _dirty == null || _dirty.Count > 0; - void IRenderLoopTask.Update(long tickCount) => UpdateScene(); + void IRenderLoopTask.Update(TimeSpan time) => UpdateScene(); void IRenderLoopTask.Render() { diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs index b031bf00df..15f0afc797 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs @@ -6,20 +6,7 @@ namespace Avalonia.Rendering public interface IRenderLoopTask { bool NeedsUpdate { get; } - void Update(long tickCount); + void Update(TimeSpan time); void Render(); } - - public class MockRenderLoopTask : IRenderLoopTask - { - public bool NeedsUpdate => true; - - public void Render() - { - } - - public void Update(long tickCount) - { - } - } } diff --git a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs index 78f6183994..d333e928a0 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs @@ -15,6 +15,6 @@ namespace Avalonia.Rendering /// This event can be raised on any thread; it is the responsibility of the subscriber to /// switch execution to the right thread. /// - event Action Tick; + event Action Tick; } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index d920be2706..d0d5b2250d 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -84,7 +84,7 @@ namespace Avalonia.Rendering } } - private async void TimerTick(long tickCount) + private async void TimerTick(TimeSpan time) { if (Interlocked.CompareExchange(ref inTick, 1, 0) == 0) { @@ -96,7 +96,7 @@ namespace Avalonia.Rendering { foreach (var i in _items) { - i.Update(tickCount); + i.Update(time); } }, DispatcherPriority.Render).ConfigureAwait(false); } diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs index 30ef35a2bb..bf992f4027 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs @@ -35,14 +35,14 @@ namespace Avalonia.Visuals.UnitTests.Rendering var renderTask = new Mock(); renderTask.Setup(t => t.NeedsUpdate).Returns(true); - renderTask.Setup(t => t.Update(It.IsAny())) + renderTask.Setup(t => t.Update(It.IsAny())) .Callback((long _) => Assert.True(inDispatcher)); loop.Add(renderTask.Object); timer.Raise(t => t.Tick += null, 0L); - renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); } [Fact] @@ -62,7 +62,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering loop.Add(renderTask.Object); timer.Raise(t => t.Tick += null, 0L); - renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); + renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); } [Fact] @@ -92,7 +92,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering loop.Add(renderTask.Object); timer.Raise(t => t.Tick += null, 0L); - renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); } [Fact] @@ -110,10 +110,10 @@ namespace Avalonia.Visuals.UnitTests.Rendering renderTask.Setup(t => t.NeedsUpdate).Returns(true); loop.Add(renderTask.Object); - var tickCount = 12345L; - timer.Raise(t => t.Tick += null, tickCount); + var time = new TimeSpan(123456789L); + timer.Raise(t => t.Tick += null, time); - renderTask.Verify(t => t.Update(tickCount), Times.Once()); + renderTask.Verify(t => t.Update(time), Times.Once()); } } } From 0aa5866ea862ae9a4836dc1899361fd90f7b0739 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 11 Sep 2018 22:40:36 -0500 Subject: [PATCH 40/56] Fix missed changes in IRenderTimer. --- src/OSX/Avalonia.MonoMac/RenderTimer.cs | 4 ++-- src/Windows/Avalonia.Win32/RenderTimer.cs | 4 ++-- src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs | 4 ++-- .../Rendering/DeferredRendererTests.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/OSX/Avalonia.MonoMac/RenderTimer.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs index 22ad2e81a2..f3c49828d6 100644 --- a/src/OSX/Avalonia.MonoMac/RenderTimer.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -12,7 +12,7 @@ namespace Avalonia.MonoMac { } - protected override IDisposable StartCore(Action tick) + protected override IDisposable StartCore(Action tick) { return AvaloniaLocator.Current.GetService().StartSystemTimer( TimeSpan.FromSeconds(1.0 / FramesPerSecond), @@ -20,7 +20,7 @@ namespace Avalonia.MonoMac { using (new NSAutoreleasePool()) { - tick?.Invoke(Environment.TickCount); + tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount)); } }); } diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index c911bc3adf..7dbb745a23 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -29,12 +29,12 @@ namespace Avalonia.Win32 { } - protected override IDisposable StartCore(Action tick) + protected override IDisposable StartCore(Action tick) { EnsureTimerQueueCreated(); var msPerFrame = 1000 / FramesPerSecond; - timerDelegate = (_, __) => tick(Environment.TickCount); + timerDelegate = (_, __) => tick(TimeSpan.FromMilliseconds(Environment.TickCount)); UnmanagedMethods.CreateTimerQueueTimer( out var timer, diff --git a/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs index 1357a4f642..0cefba7f19 100644 --- a/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs +++ b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs @@ -7,7 +7,7 @@ namespace Avalonia.iOS { class DisplayLinkRenderTimer : IRenderTimer { - public event Action Tick; + public event Action Tick; private CADisplayLink _link; public DisplayLinkRenderTimer() @@ -21,7 +21,7 @@ namespace Avalonia.iOS { try { - Tick?.Invoke(Environment.TickCount); + Tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount)); } catch (Exception) { diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs index e2a5c0c54c..8c103360d4 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs @@ -357,7 +357,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering private void RunFrame(IRenderLoopTask task) { - task.Update(0); + task.Update(TimeSpan.Zero); task.Render(); } From dcb0d5d090b50fa882e0f9d24300f4af2c5b57d3 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 12 Sep 2018 14:18:15 -0500 Subject: [PATCH 41/56] Fix Raise calls in RenderLoopTests --- .../Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs index bf992f4027..c8fc57285d 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs @@ -40,7 +40,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering loop.Add(renderTask.Object); - timer.Raise(t => t.Tick += null, 0L); + timer.Raise(t => t.Tick += null, TimeSpan.Zero); renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); } @@ -60,7 +60,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering renderTask.Setup(t => t.NeedsUpdate).Returns(false); loop.Add(renderTask.Object); - timer.Raise(t => t.Tick += null, 0L); + timer.Raise(t => t.Tick += null, TimeSpan.Zero); renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); } @@ -90,7 +90,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering .Callback(() => Assert.False(inDispatcher)); loop.Add(renderTask.Object); - timer.Raise(t => t.Tick += null, 0L); + timer.Raise(t => t.Tick += null, TimeSpan.Zero); renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); } From f20ebb3ca7dfb594ec8ccfca5cd531c3a0cb5fa9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 12 Sep 2018 14:55:41 -0500 Subject: [PATCH 42/56] Fix another API mismatch from the API change of IRenderLoopTask that I missed beforehand. --- tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs index c8fc57285d..16c2d3ee18 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs @@ -36,7 +36,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering renderTask.Setup(t => t.NeedsUpdate).Returns(true); renderTask.Setup(t => t.Update(It.IsAny())) - .Callback((long _) => Assert.True(inDispatcher)); + .Callback((TimeSpan _) => Assert.True(inDispatcher)); loop.Add(renderTask.Object); From a8d4c8d799ee1abf5338028361b0855745ebae47 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 13 Sep 2018 14:24:13 +0800 Subject: [PATCH 43/56] Add a new Disposable Extention. --- src/Avalonia.Animation/Animator`1.cs | 11 +++-- .../Reactive/DisposeOnNextObservable.cs | 40 +++++++++++++++++++ src/Avalonia.Base/Reactive/ObservableEx.cs | 17 +++++++- 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index e4af0f356d..888450e7f0 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reactive.Disposables; using System.Reactive.Linq; using Avalonia.Animation.Utils; using Avalonia.Collections; using Avalonia.Data; +using Avalonia.Reactive; + namespace Avalonia.Animation { @@ -38,10 +41,10 @@ namespace Avalonia.Animation VerifyConvertKeyFrames(); return match.DistinctUntilChanged() - .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) - .Buffer(2, 1) - .Where(x => x.Count > 1) - .Subscribe(x => x[0]?.Dispose()); + .ObserveOn(Avalonia.Threading.AvaloniaScheduler.Instance) + .Select(x => x ? RunKeyFrames(animation, control, onComplete) : Disposable.Empty) + .DisposeCurrentOnNext() + .Subscribe(); } /// diff --git a/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs b/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs new file mode 100644 index 0000000000..8650fe5400 --- /dev/null +++ b/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs @@ -0,0 +1,40 @@ +using System; +using Avalonia.Threading; + +namespace Avalonia.Reactive +{ + public class DisposeOnNextObservable : LightweightObservableBase, IObserver where T : IDisposable + { + private IDisposable lastValue; + + private void ValueNext(T value) + { + this.PublishNext(value); + lastValue?.Dispose(); + lastValue = value; + } + + public void OnCompleted() + { + this.PublishCompleted(); + } + + public void OnError(Exception error) + { + this.PublishError(error); + } + + void IObserver.OnNext(T value) + { + ValueNext(value); + } + + protected override void Initialize() + { + } + + protected override void Deinitialize() + { + } + } +} \ No newline at end of file diff --git a/src/Avalonia.Base/Reactive/ObservableEx.cs b/src/Avalonia.Base/Reactive/ObservableEx.cs index 5b2a39d5ff..dc3be36015 100644 --- a/src/Avalonia.Base/Reactive/ObservableEx.cs +++ b/src/Avalonia.Base/Reactive/ObservableEx.cs @@ -22,6 +22,20 @@ namespace Avalonia.Reactive return new SingleValueImpl(value); } + /// + /// Disposes the current and saves the next. + /// + /// The type of the value. + /// The source . + /// The observable. + public static IObservable DisposeCurrentOnNext(this IObservable observable) + where T : IDisposable + { + var subject = new DisposeOnNextObservable(); + observable.Subscribe(subject); + return subject; + } + private class SingleValueImpl : IObservable { private T _value; @@ -30,7 +44,6 @@ namespace Avalonia.Reactive { _value = value; } - public IDisposable Subscribe(IObserver observer) { observer.OnNext(_value); @@ -38,4 +51,4 @@ namespace Avalonia.Reactive } } } -} +} \ No newline at end of file From 74c8cedde9070956d283137e0407d1888572c72b Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 13 Sep 2018 15:17:32 +0800 Subject: [PATCH 44/56] Try fixing the sporadic Bindings Exceptions. --- src/Avalonia.Animation/Animator`1.cs | 7 ++----- src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 888450e7f0..decca8e858 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -1,14 +1,12 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reactive.Disposables; using System.Reactive.Linq; using Avalonia.Animation.Utils; using Avalonia.Collections; using Avalonia.Data; using Avalonia.Reactive; - namespace Avalonia.Animation { /// @@ -41,8 +39,7 @@ namespace Avalonia.Animation VerifyConvertKeyFrames(); return match.DistinctUntilChanged() - .ObserveOn(Avalonia.Threading.AvaloniaScheduler.Instance) - .Select(x => x ? RunKeyFrames(animation, control, onComplete) : Disposable.Empty) + .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) .DisposeCurrentOnNext() .Subscribe(); } @@ -172,4 +169,4 @@ namespace Avalonia.Animation } } } -} +} \ No newline at end of file diff --git a/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs b/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs index 8650fe5400..18af9e8752 100644 --- a/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs +++ b/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs @@ -9,9 +9,9 @@ namespace Avalonia.Reactive private void ValueNext(T value) { - this.PublishNext(value); lastValue?.Dispose(); lastValue = value; + this.PublishNext(value); } public void OnCompleted() From f2f96e2f46a520be9f84febfeb79276b5da91586 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 13 Sep 2018 20:42:18 +0200 Subject: [PATCH 45/56] Don't dispose completed binding. --- src/Avalonia.Base/PriorityBindingEntry.cs | 7 +++++++ src/Avalonia.Base/PriorityLevel.cs | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Base/PriorityBindingEntry.cs b/src/Avalonia.Base/PriorityBindingEntry.cs index 570bfe03dc..d4a47306a7 100644 --- a/src/Avalonia.Base/PriorityBindingEntry.cs +++ b/src/Avalonia.Base/PriorityBindingEntry.cs @@ -50,6 +50,11 @@ namespace Avalonia get; } + /// + /// Gets a value indicating whether the binding has completed. + /// + public bool HasCompleted { get; private set; } + /// /// The current value of the binding. /// @@ -129,6 +134,8 @@ namespace Avalonia private void Completed() { + HasCompleted = true; + if (Dispatcher.UIThread.CheckAccess()) { _owner.Completed(this); diff --git a/src/Avalonia.Base/PriorityLevel.cs b/src/Avalonia.Base/PriorityLevel.cs index 96661bd7ea..909558b0ce 100644 --- a/src/Avalonia.Base/PriorityLevel.cs +++ b/src/Avalonia.Base/PriorityLevel.cs @@ -112,12 +112,16 @@ namespace Avalonia return Disposable.Create(() => { - Bindings.Remove(node); - entry.Dispose(); - - if (entry.Index >= ActiveBindingIndex) + if (!entry.HasCompleted) { - ActivateFirstBinding(); + Bindings.Remove(node); + + entry.Dispose(); + + if (entry.Index >= ActiveBindingIndex) + { + ActivateFirstBinding(); + } } }); } From bca37513b4666b522104be1b1d1b0aa2cbecd19c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 13 Sep 2018 22:05:02 +0200 Subject: [PATCH 46/56] Added failing test for disposing completed binding. --- .../AvaloniaObjectTests_Binding.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Binding.cs b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Binding.cs index 4638aa84a5..23984a7c8d 100644 --- a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Binding.cs +++ b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Binding.cs @@ -479,6 +479,18 @@ namespace Avalonia.Base.UnitTests Assert.False(source.SetterCalled); } + [Fact] + public void Disposing_Completed_Binding_Does_Not_Throw() + { + var target = new Class1(); + var source = new Subject(); + var subscription = target.Bind(Class1.FooProperty, source); + + source.OnCompleted(); + + subscription.Dispose(); + } + /// /// Returns an observable that returns a single value but does not complete. /// @@ -595,4 +607,4 @@ namespace Avalonia.Base.UnitTests public bool SetterCalled { get; private set; } } } -} \ No newline at end of file +} From e58b4d5152ac16e4bb9cafdb0e11ea710b2baef5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 13 Sep 2018 20:42:18 +0200 Subject: [PATCH 47/56] Don't dispose completed binding. --- src/Avalonia.Base/PriorityBindingEntry.cs | 7 +++++++ src/Avalonia.Base/PriorityLevel.cs | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Base/PriorityBindingEntry.cs b/src/Avalonia.Base/PriorityBindingEntry.cs index 570bfe03dc..d4a47306a7 100644 --- a/src/Avalonia.Base/PriorityBindingEntry.cs +++ b/src/Avalonia.Base/PriorityBindingEntry.cs @@ -50,6 +50,11 @@ namespace Avalonia get; } + /// + /// Gets a value indicating whether the binding has completed. + /// + public bool HasCompleted { get; private set; } + /// /// The current value of the binding. /// @@ -129,6 +134,8 @@ namespace Avalonia private void Completed() { + HasCompleted = true; + if (Dispatcher.UIThread.CheckAccess()) { _owner.Completed(this); diff --git a/src/Avalonia.Base/PriorityLevel.cs b/src/Avalonia.Base/PriorityLevel.cs index 96661bd7ea..909558b0ce 100644 --- a/src/Avalonia.Base/PriorityLevel.cs +++ b/src/Avalonia.Base/PriorityLevel.cs @@ -112,12 +112,16 @@ namespace Avalonia return Disposable.Create(() => { - Bindings.Remove(node); - entry.Dispose(); - - if (entry.Index >= ActiveBindingIndex) + if (!entry.HasCompleted) { - ActivateFirstBinding(); + Bindings.Remove(node); + + entry.Dispose(); + + if (entry.Index >= ActiveBindingIndex) + { + ActivateFirstBinding(); + } } }); } From 74a5b1f65ad0fd947c4118854e64fe29327d515c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 13 Sep 2018 22:45:22 +0200 Subject: [PATCH 48/56] Added a failing MenuItem separator test. --- .../MenuItemTests.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/Avalonia.Controls.UnitTests/MenuItemTests.cs diff --git a/tests/Avalonia.Controls.UnitTests/MenuItemTests.cs b/tests/Avalonia.Controls.UnitTests/MenuItemTests.cs new file mode 100644 index 0000000000..e7352af23e --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/MenuItemTests.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace Avalonia.Controls.UnitTests +{ + public class MenuItemTests + { + [Fact] + public void Header_Of_Minus_Should_Apply_Separator_Pseudoclass() + { + var target = new MenuItem { Header = "-" }; + + Assert.True(target.Classes.Contains(":separator")); + } + + [Fact] + public void Separator_Item_Should_Set_Focusable_False() + { + var target = new MenuItem { Header = "-" }; + + Assert.False(target.Focusable); + } + } +} From f55b556b1c42e810ab6e66ad9f986a59d8868c56 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 13 Sep 2018 22:45:35 +0200 Subject: [PATCH 49/56] Make separator MenuItem non-focusable. --- src/Avalonia.Controls/MenuItem.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/MenuItem.cs b/src/Avalonia.Controls/MenuItem.cs index 03a43b5164..055d49fb0b 100644 --- a/src/Avalonia.Controls/MenuItem.cs +++ b/src/Avalonia.Controls/MenuItem.cs @@ -99,13 +99,13 @@ namespace Avalonia.Controls SelectableMixin.Attach(IsSelectedProperty); CommandProperty.Changed.Subscribe(CommandChanged); FocusableProperty.OverrideDefaultValue(true); + HeaderProperty.Changed.AddClassHandler(x => x.HeaderChanged); IconProperty.Changed.AddClassHandler(x => x.IconChanged); IsSelectedProperty.Changed.AddClassHandler(x => x.IsSelectedChanged); ItemsPanelProperty.OverrideDefaultValue(DefaultPanel); ClickEvent.AddClassHandler(x => x.OnClick); SubmenuOpenedEvent.AddClassHandler(x => x.OnSubmenuOpened); IsSubMenuOpenProperty.Changed.AddClassHandler(x => x.SubMenuOpenChanged); - PseudoClass(HeaderProperty, x => x as string == "-", ":separator"); } public MenuItem() @@ -420,6 +420,24 @@ namespace Avalonia.Controls IsEnabled = Command == null || Command.CanExecute(CommandParameter); } + /// + /// Called when the property changes. + /// + /// The property change event. + private void HeaderChanged(AvaloniaPropertyChangedEventArgs e) + { + if (e.NewValue is string newValue && newValue == "-") + { + PseudoClasses.Add(":separator"); + Focusable = false; + } + else if (e.OldValue is string oldValue && oldValue == "-") + { + PseudoClasses.Remove(":separator"); + Focusable = true; + } + } + /// /// Called when the property changes. /// From ee1a8ee30fb0f10e94335ce666af4fe19e59734d Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 14 Sep 2018 10:49:49 +0800 Subject: [PATCH 50/56] Make a specialized observable for instance lifetime handling. Delete DisposeOnNextObservable. --- src/Avalonia.Animation/AnimationInstance`1.cs | 7 +-- src/Avalonia.Animation/Animator`1.cs | 18 +++--- .../DisposeAnimationInstanceObservable.cs | 62 +++++++++++++++++++ .../Reactive/DisposeOnNextObservable.cs | 40 ------------ src/Avalonia.Base/Reactive/ObservableEx.cs | 16 +---- 5 files changed, 73 insertions(+), 70 deletions(-) create mode 100644 src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs delete mode 100644 src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index 5a72904ed2..c264663b56 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -154,7 +154,7 @@ namespace Avalonia.Animation private void InternalStep(TimeSpan systemTime) { DoPlayStatesAndTime(systemTime); - + var time = _internalClock - _firstFrameCount; var delayEndpoint = _delay; var iterationEndpoint = delayEndpoint + _duration; @@ -188,10 +188,7 @@ namespace Avalonia.Animation if (!_isLooping) { - if (_currentIteration > _repeatCount) - DoComplete(); - - if (time > iterationEndpoint) + if ((_currentIteration > _repeatCount) | (time > iterationEndpoint)) DoComplete(); } diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index decca8e858..0de3991a88 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; @@ -38,10 +41,8 @@ namespace Avalonia.Animation if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - return match.DistinctUntilChanged() - .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) - .DisposeCurrentOnNext() - .Subscribe(); + var subject = new DisposeAnimationInstanceObservable(this, animation, control, onComplete); + return match.Subscribe(subject); } /// @@ -96,11 +97,8 @@ namespace Avalonia.Animation var lastFrameData = (lastCue.GetTypedValue(), lastCue.isNeutral); return (intraframeTime, new KeyFramePair(firstFrameData, lastFrameData)); } - - /// - /// Runs the KeyFrames Animation. - /// - private IDisposable RunKeyFrames(Animation animation, Animatable control, Action onComplete) + + internal IDisposable Run(Animation animation, Animatable control, Action onComplete) { var instance = new AnimationInstance(animation, control, this, onComplete, DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); diff --git a/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs b/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs new file mode 100644 index 0000000000..902a09030b --- /dev/null +++ b/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs @@ -0,0 +1,62 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Linq; +using Avalonia.Animation.Utils; +using Avalonia.Collections; +using Avalonia.Data; +using Avalonia.Reactive; + +namespace Avalonia.Animation +{ + /// + /// Manages the lifetime of animation instances as determined by its selector state. + /// + internal class DisposeAnimationInstanceObservable : IObserver, IDisposable + { + private IDisposable _lastInstance; + private bool _lastMatch; + private Animator _animator; + private Animation _animation; + private Animatable _control; + private Action _onComplete; + + public DisposeAnimationInstanceObservable(Animator animator, Animation animation, Animatable control, Action onComplete) + { + this._animator = animator; + this._animation = animation; + this._control = control; + this._onComplete = onComplete; + } + + public void Dispose() + { + _lastInstance?.Dispose(); + } + + public void OnCompleted() + { + } + + public void OnError(Exception error) + { + _lastInstance?.Dispose(); + } + + void IObserver.OnNext(bool matchVal) + { + if (matchVal != _lastMatch) + { + _lastInstance?.Dispose(); + if (matchVal) + { + _lastInstance = _animator.RunAnimation(_animation, _control, _onComplete); + } + _lastMatch = matchVal; + } + } + } +} \ No newline at end of file diff --git a/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs b/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs deleted file mode 100644 index 18af9e8752..0000000000 --- a/src/Avalonia.Base/Reactive/DisposeOnNextObservable.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using Avalonia.Threading; - -namespace Avalonia.Reactive -{ - public class DisposeOnNextObservable : LightweightObservableBase, IObserver where T : IDisposable - { - private IDisposable lastValue; - - private void ValueNext(T value) - { - lastValue?.Dispose(); - lastValue = value; - this.PublishNext(value); - } - - public void OnCompleted() - { - this.PublishCompleted(); - } - - public void OnError(Exception error) - { - this.PublishError(error); - } - - void IObserver.OnNext(T value) - { - ValueNext(value); - } - - protected override void Initialize() - { - } - - protected override void Deinitialize() - { - } - } -} \ No newline at end of file diff --git a/src/Avalonia.Base/Reactive/ObservableEx.cs b/src/Avalonia.Base/Reactive/ObservableEx.cs index dc3be36015..a1ec8f9a8a 100644 --- a/src/Avalonia.Base/Reactive/ObservableEx.cs +++ b/src/Avalonia.Base/Reactive/ObservableEx.cs @@ -21,21 +21,7 @@ namespace Avalonia.Reactive { return new SingleValueImpl(value); } - - /// - /// Disposes the current and saves the next. - /// - /// The type of the value. - /// The source . - /// The observable. - public static IObservable DisposeCurrentOnNext(this IObservable observable) - where T : IDisposable - { - var subject = new DisposeOnNextObservable(); - observable.Subscribe(subject); - return subject; - } - + private class SingleValueImpl : IObservable { private T _value; From a462d0563c75e8c4c461e57847d7b4ad55e94157 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 14 Sep 2018 10:50:18 +0800 Subject: [PATCH 51/56] Add missing license headers on Avalonia.Animations. --- src/Avalonia.Animation/DoubleAnimator.cs | 5 ++++- src/Avalonia.Animation/FillMode.cs | 5 ++++- src/Avalonia.Animation/IAnimation.cs | 3 +++ src/Avalonia.Animation/IAnimationSetter.cs | 3 +++ src/Avalonia.Animation/IAnimator.cs | 5 ++++- src/Avalonia.Animation/KeyFrame.cs | 5 ++++- src/Avalonia.Animation/KeyFramePair`1.cs | 3 +++ src/Avalonia.Animation/PlayState.cs | 5 ++++- src/Avalonia.Animation/PlaybackDirection.cs | 5 ++++- 9 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Animation/DoubleAnimator.cs b/src/Avalonia.Animation/DoubleAnimator.cs index aeeb29a7dd..2e0ce64185 100644 --- a/src/Avalonia.Animation/DoubleAnimator.cs +++ b/src/Avalonia.Animation/DoubleAnimator.cs @@ -1,4 +1,7 @@ -namespace Avalonia.Animation +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +namespace Avalonia.Animation { /// /// Animator that handles properties. diff --git a/src/Avalonia.Animation/FillMode.cs b/src/Avalonia.Animation/FillMode.cs index 001e1cdeb4..39beecf455 100644 --- a/src/Avalonia.Animation/FillMode.cs +++ b/src/Avalonia.Animation/FillMode.cs @@ -1,4 +1,7 @@ -namespace Avalonia.Animation +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +namespace Avalonia.Animation { public enum FillMode { diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index 1d545a322a..831391ce46 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -1,3 +1,6 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + using System; using System.Threading.Tasks; diff --git a/src/Avalonia.Animation/IAnimationSetter.cs b/src/Avalonia.Animation/IAnimationSetter.cs index 2d22377286..9c8365ea37 100644 --- a/src/Avalonia.Animation/IAnimationSetter.cs +++ b/src/Avalonia.Animation/IAnimationSetter.cs @@ -1,3 +1,6 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + namespace Avalonia.Animation { public interface IAnimationSetter diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 9a4da35a02..0f26b7dc2f 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; using System.Collections.Generic; namespace Avalonia.Animation diff --git a/src/Avalonia.Animation/KeyFrame.cs b/src/Avalonia.Animation/KeyFrame.cs index 5eb0d2e901..44e39e042e 100644 --- a/src/Avalonia.Animation/KeyFrame.cs +++ b/src/Avalonia.Animation/KeyFrame.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; using System.Collections.Generic; using Avalonia.Collections; diff --git a/src/Avalonia.Animation/KeyFramePair`1.cs b/src/Avalonia.Animation/KeyFramePair`1.cs index b0622a1580..60a16d094f 100644 --- a/src/Avalonia.Animation/KeyFramePair`1.cs +++ b/src/Avalonia.Animation/KeyFramePair`1.cs @@ -1,3 +1,6 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + namespace Avalonia.Animation { /// diff --git a/src/Avalonia.Animation/PlayState.cs b/src/Avalonia.Animation/PlayState.cs index 313d33d586..8d28f06eb1 100644 --- a/src/Avalonia.Animation/PlayState.cs +++ b/src/Avalonia.Animation/PlayState.cs @@ -1,4 +1,7 @@ -namespace Avalonia.Animation +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +namespace Avalonia.Animation { /// /// Determines the playback state of an animation. diff --git a/src/Avalonia.Animation/PlaybackDirection.cs b/src/Avalonia.Animation/PlaybackDirection.cs index bbce6106e1..a44dd388ae 100644 --- a/src/Avalonia.Animation/PlaybackDirection.cs +++ b/src/Avalonia.Animation/PlaybackDirection.cs @@ -1,4 +1,7 @@ -namespace Avalonia.Animation +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +namespace Avalonia.Animation { /// /// Determines the playback direction of an animation. From d9f1da005251757fcee83de2600f65ecd15f4ba9 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 14 Sep 2018 10:51:59 +0800 Subject: [PATCH 52/56] Fix missed method rename. --- src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs b/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs index 902a09030b..f58e816e54 100644 --- a/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs +++ b/src/Avalonia.Animation/DisposeAnimationInstanceObservable.cs @@ -53,7 +53,7 @@ namespace Avalonia.Animation _lastInstance?.Dispose(); if (matchVal) { - _lastInstance = _animator.RunAnimation(_animation, _control, _onComplete); + _lastInstance = _animator.Run(_animation, _control, _onComplete); } _lastMatch = matchVal; } From 032fc349f93551e37e5855f940470b03cb6de4d1 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 14 Sep 2018 07:35:25 +0200 Subject: [PATCH 53/56] Apply requested changes and some more clean up. --- .../{ChainLink.cs => EventChainLink.cs} | 32 ++--- .../ViewModels/EventEntryTreeNode.cs | 97 -------------- ...ntrolTreeNode.cs => EventOwnerTreeNode.cs} | 18 ++- .../ViewModels/EventTreeNode.cs | 118 ++++++++++-------- .../ViewModels/EventTreeNodeBase.cs | 78 ++++++++++++ .../ViewModels/EventsViewModel.cs | 33 ++--- .../ViewModels/FiredEvent.cs | 31 ++--- .../Views/EventsView.xaml | 4 +- .../Views/EventsView.xaml.cs | 8 +- 9 files changed, 197 insertions(+), 222 deletions(-) rename src/Avalonia.Diagnostics/Models/{ChainLink.cs => EventChainLink.cs} (59%) delete mode 100644 src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs rename src/Avalonia.Diagnostics/ViewModels/{ControlTreeNode.cs => EventOwnerTreeNode.cs} (76%) create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventTreeNodeBase.cs diff --git a/src/Avalonia.Diagnostics/Models/ChainLink.cs b/src/Avalonia.Diagnostics/Models/EventChainLink.cs similarity index 59% rename from src/Avalonia.Diagnostics/Models/ChainLink.cs rename to src/Avalonia.Diagnostics/Models/EventChainLink.cs index 9ea99ff1ae..aab50a13dd 100644 --- a/src/Avalonia.Diagnostics/Models/ChainLink.cs +++ b/src/Avalonia.Diagnostics/Models/EventChainLink.cs @@ -1,13 +1,24 @@ -using System; -using System.Collections.Generic; -using System.Text; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; using Avalonia.Interactivity; namespace Avalonia.Diagnostics.Models { - internal class ChainLink + internal class EventChainLink { - public object Handler { get; private set; } + public EventChainLink(object handler, bool handled, RoutingStrategies route) + { + Contract.Requires(handler != null); + + this.Handler = handler; + this.Handled = handled; + this.Route = route; + } + + public object Handler { get; } + public string HandlerName { get @@ -19,16 +30,9 @@ namespace Avalonia.Diagnostics.Models return Handler.GetType().Name; } } - public bool Handled { get; private set; } - public RoutingStrategies Route { get; private set; } - public ChainLink(object handler, bool handled, RoutingStrategies route) - { - Contract.Requires(handler != null); + public bool Handled { get; } - this.Handler = handler; - this.Handled = handled; - this.Route = route; - } + public RoutingStrategies Route { get; } } } diff --git a/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs deleted file mode 100644 index 8eead869d5..0000000000 --- a/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -using System; -using Avalonia.Diagnostics.Models; -using Avalonia.Interactivity; -using Avalonia.Threading; -using Avalonia.VisualTree; - -namespace Avalonia.Diagnostics.ViewModels -{ - internal class EventEntryTreeNode : EventTreeNode - { - RoutedEvent _event; - EventsViewModel _parentViewModel; - bool _isRegistered; - FiredEvent _currentEvent; - - public EventEntryTreeNode(ControlTreeNode parent, RoutedEvent @event, EventsViewModel vm) - : base(parent, @event.Name) - { - Contract.Requires(@event != null); - Contract.Requires(vm != null); - - this._event = @event; - this._parentViewModel = vm; - } - - public override bool? IsEnabled - { - get => base.IsEnabled; - set - { - if (base.IsEnabled != value) - { - base.IsEnabled = value; - UpdateTracker(); - if (Parent != null && _updateParent) - { - try - { - Parent._updateChildren = false; - Parent.UpdateChecked(); - } - finally - { - Parent._updateChildren = true; - } - } - } - } - } - - private void UpdateTracker() - { - if (IsEnabled.GetValueOrDefault() && !_isRegistered) - { - _event.AddClassHandler(typeof(object), HandleEvent, (RoutingStrategies)7, handledEventsToo: true); - _isRegistered = true; - } - } - - private void HandleEvent(object sender, RoutedEventArgs e) - { - if (!_isRegistered || IsEnabled == false) - return; - if (sender is IVisual v && DevTools.BelongsToDevTool(v)) - return; - - var s = sender; - var handled = e.Handled; - var route = e.Route; - - Action handler = delegate - { - if (_currentEvent == null || !_currentEvent.IsPartOfSameEventChain(e)) - { - _currentEvent = new FiredEvent(e, new ChainLink(s, handled, route)); - - _parentViewModel.RecordedEvents.Add(_currentEvent); - - while (_parentViewModel.RecordedEvents.Count > 100) - _parentViewModel.RecordedEvents.RemoveAt(0); - } - else - { - _currentEvent.AddToChain(new ChainLink(s, handled, route)); - } - }; - - if (!Dispatcher.UIThread.CheckAccess()) - Dispatcher.UIThread.Post(handler); - else - handler(); - } - } -} diff --git a/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventOwnerTreeNode.cs similarity index 76% rename from src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs rename to src/Avalonia.Diagnostics/ViewModels/EventOwnerTreeNode.cs index d8a6a3bdc3..0674918400 100644 --- a/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs +++ b/src/Avalonia.Diagnostics/ViewModels/EventOwnerTreeNode.cs @@ -11,16 +11,9 @@ using Avalonia.Interactivity; namespace Avalonia.Diagnostics.ViewModels { - internal class ControlTreeNode : EventTreeNode + internal class EventOwnerTreeNode : EventTreeNodeBase { - public ControlTreeNode(Type type, IEnumerable events, EventsViewModel vm) - : base(null, type.Name) - { - this.Children = new AvaloniaList(events.OrderBy(e => e.Name).Select(e => new EventEntryTreeNode(this, e, vm) { IsEnabled = IsDefault(e) })); - this.IsExpanded = true; - } - - RoutedEvent[] defaultEvents = new RoutedEvent[] + private static readonly RoutedEvent[] s_defaultEvents = new RoutedEvent[] { Button.ClickEvent, InputElement.KeyDownEvent, @@ -30,9 +23,12 @@ namespace Avalonia.Diagnostics.ViewModels InputElement.PointerPressedEvent, }; - private bool IsDefault(RoutedEvent e) + public EventOwnerTreeNode(Type type, IEnumerable events, EventsViewModel vm) + : base(null, type.Name) { - return defaultEvents.Contains(e); + this.Children = new AvaloniaList(events.OrderBy(e => e.Name) + .Select(e => new EventTreeNode(this, e, vm) { IsEnabled = s_defaultEvents.Contains(e) })); + this.IsExpanded = true; } public override bool? IsEnabled diff --git a/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs index 50ea0c9c31..59c0f82414 100644 --- a/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs +++ b/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs @@ -1,80 +1,98 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System.Diagnostics; -using Avalonia.Collections; +using System; + +using Avalonia.Diagnostics.Models; +using Avalonia.Interactivity; +using Avalonia.Threading; using Avalonia.VisualTree; namespace Avalonia.Diagnostics.ViewModels { - internal abstract class EventTreeNode : ViewModelBase + internal class EventTreeNode : EventTreeNodeBase { - internal bool _updateChildren = true; - internal bool _updateParent = true; - private bool _isExpanded; - private bool? _isEnabled = false; - - public EventTreeNode(EventTreeNode parent, string text) - { - this.Parent = parent; - this.Text = text; - } + private RoutedEvent _event; + private EventsViewModel _parentViewModel; + private bool _isRegistered; + private FiredEvent _currentEvent; - public IAvaloniaReadOnlyList Children + public EventTreeNode(EventOwnerTreeNode parent, RoutedEvent @event, EventsViewModel vm) + : base(parent, @event.Name) { - get; - protected set; - } + Contract.Requires(@event != null); + Contract.Requires(vm != null); - public bool IsExpanded - { - get { return _isExpanded; } - set { RaiseAndSetIfChanged(ref _isExpanded, value); } + this._event = @event; + this._parentViewModel = vm; } - public virtual bool? IsEnabled + public override bool? IsEnabled { - get { return _isEnabled; } - set { RaiseAndSetIfChanged(ref _isEnabled, value); } + get => base.IsEnabled; + set + { + if (base.IsEnabled != value) + { + base.IsEnabled = value; + UpdateTracker(); + if (Parent != null && _updateParent) + { + try + { + Parent._updateChildren = false; + Parent.UpdateChecked(); + } + finally + { + Parent._updateChildren = true; + } + } + } + } } - public EventTreeNode Parent + private void UpdateTracker() { - get; + if (IsEnabled.GetValueOrDefault() && !_isRegistered) + { + _event.AddClassHandler(typeof(object), HandleEvent, (RoutingStrategies)7, handledEventsToo: true); + _isRegistered = true; + } } - public string Text + private void HandleEvent(object sender, RoutedEventArgs e) { - get; - private set; - } + if (!_isRegistered || IsEnabled == false) + return; + if (sender is IVisual v && DevTools.BelongsToDevTool(v)) + return; - internal void UpdateChecked() - { - IsEnabled = GetValue(); + var s = sender; + var handled = e.Handled; + var route = e.Route; - bool? GetValue() + Action handler = delegate { - if (Children == null) - return false; - bool? value = false; - for (int i = 0; i < Children.Count; i++) + if (_currentEvent == null || !_currentEvent.IsPartOfSameEventChain(e)) { - if (i == 0) - { - value = Children[i].IsEnabled; - continue; - } + _currentEvent = new FiredEvent(e, new EventChainLink(s, handled, route)); - if (value != Children[i].IsEnabled) - { - value = null; - break; - } + _parentViewModel.RecordedEvents.Add(_currentEvent); + + while (_parentViewModel.RecordedEvents.Count > 100) + _parentViewModel.RecordedEvents.RemoveAt(0); } + else + { + _currentEvent.AddToChain(new EventChainLink(s, handled, route)); + } + }; - return value; - } + if (!Dispatcher.UIThread.CheckAccess()) + Dispatcher.UIThread.Post(handler); + else + handler(); } } } diff --git a/src/Avalonia.Diagnostics/ViewModels/EventTreeNodeBase.cs b/src/Avalonia.Diagnostics/ViewModels/EventTreeNodeBase.cs new file mode 100644 index 0000000000..146a8cea8e --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventTreeNodeBase.cs @@ -0,0 +1,78 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Collections; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal abstract class EventTreeNodeBase : ViewModelBase + { + internal bool _updateChildren = true; + internal bool _updateParent = true; + private bool _isExpanded; + private bool? _isEnabled = false; + + public EventTreeNodeBase(EventTreeNodeBase parent, string text) + { + this.Parent = parent; + this.Text = text; + } + + public IAvaloniaReadOnlyList Children + { + get; + protected set; + } + + public bool IsExpanded + { + get { return _isExpanded; } + set { RaiseAndSetIfChanged(ref _isExpanded, value); } + } + + public virtual bool? IsEnabled + { + get { return _isEnabled; } + set { RaiseAndSetIfChanged(ref _isEnabled, value); } + } + + public EventTreeNodeBase Parent + { + get; + } + + public string Text + { + get; + private set; + } + + internal void UpdateChecked() + { + IsEnabled = GetValue(); + + bool? GetValue() + { + if (Children == null) + return false; + bool? value = false; + for (int i = 0; i < Children.Count; i++) + { + if (i == 0) + { + value = Children[i].IsEnabled; + continue; + } + + if (value != Children[i].IsEnabled) + { + value = null; + break; + } + } + + return value; + } + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs b/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs index a9a6334689..a23677afc8 100644 --- a/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs +++ b/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs @@ -2,26 +2,22 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; -using System.Text; using System.Windows.Input; -using Avalonia.Collections; + using Avalonia.Controls; using Avalonia.Data.Converters; using Avalonia.Interactivity; using Avalonia.Media; -using Avalonia.Threading; namespace Avalonia.Diagnostics.ViewModels { internal class EventsViewModel : ViewModelBase { - private IControl _root; + private readonly IControl _root; private FiredEvent _selectedEvent; - private ICommand ClearCommand { get; } public EventsViewModel(IControl root) { @@ -29,27 +25,11 @@ namespace Avalonia.Diagnostics.ViewModels this.Nodes = RoutedEventRegistry.Instance.GetAllRegistered() .GroupBy(e => e.OwnerType) .OrderBy(e => e.Key.Name) - .Select(g => new ControlTreeNode(g.Key, g, this)) + .Select(g => new EventOwnerTreeNode(g.Key, g, this)) .ToArray(); } - private void ClearExecute() - { - Action action = delegate - { - RecordedEvents.Clear(); - }; - if (!Dispatcher.UIThread.CheckAccess()) - { - Dispatcher.UIThread.Post(action); - } - else - { - action(); - } - } - - public EventTreeNode[] Nodes { get; } + public EventTreeNodeBase[] Nodes { get; } public ObservableCollection RecordedEvents { get; } = new ObservableCollection(); @@ -58,6 +38,11 @@ namespace Avalonia.Diagnostics.ViewModels get => _selectedEvent; set => RaiseAndSetIfChanged(ref _selectedEvent, value); } + + private void Clear() + { + RecordedEvents.Clear(); + } } internal class BoolToBrushConverter : IValueConverter diff --git a/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs b/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs index 523525b634..049280c390 100644 --- a/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs +++ b/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs @@ -3,6 +3,7 @@ using System; using System.Collections.ObjectModel; + using Avalonia.Diagnostics.Models; using Avalonia.Interactivity; @@ -11,17 +12,15 @@ namespace Avalonia.Diagnostics.ViewModels internal class FiredEvent : ViewModelBase { private RoutedEventArgs _eventArgs; + private EventChainLink _handledBy; - private ChainLink _handledBy; - private ChainLink _originator; - - public FiredEvent(RoutedEventArgs eventArgs, ChainLink originator) + public FiredEvent(RoutedEventArgs eventArgs, EventChainLink originator) { Contract.Requires(eventArgs != null); Contract.Requires(originator != null); this._eventArgs = eventArgs; - this._originator = originator; + this.Originator = originator; AddToChain(originator); } @@ -34,7 +33,7 @@ namespace Avalonia.Diagnostics.ViewModels public bool IsHandled => HandledBy?.Handled == true; - public ObservableCollection EventChain { get; } = new ObservableCollection(); + public ObservableCollection EventChain { get; } = new ObservableCollection(); public string DisplayText { @@ -49,21 +48,9 @@ namespace Avalonia.Diagnostics.ViewModels } } - public ChainLink Originator - { - get { return _originator; } - set - { - if (_originator != value) - { - _originator = value; - RaisePropertyChanged(); - RaisePropertyChanged(nameof(DisplayText)); - } - } - } + public EventChainLink Originator { get; } - public ChainLink HandledBy + public EventChainLink HandledBy { get { return _handledBy; } set @@ -80,10 +67,10 @@ namespace Avalonia.Diagnostics.ViewModels public void AddToChain(object handler, bool handled, RoutingStrategies route) { - AddToChain(new ChainLink(handler, handled, route)); + AddToChain(new EventChainLink(handler, handled, route)); } - public void AddToChain(ChainLink link) + public void AddToChain(EventChainLink link) { EventChain.Add(link); if (HandledBy == null && link.Handled) diff --git a/src/Avalonia.Diagnostics/Views/EventsView.xaml b/src/Avalonia.Diagnostics/Views/EventsView.xaml index 79f78d2bba..a5be86b613 100644 --- a/src/Avalonia.Diagnostics/Views/EventsView.xaml +++ b/src/Avalonia.Diagnostics/Views/EventsView.xaml @@ -7,7 +7,7 @@ - @@ -46,7 +46,7 @@ -