Browse Source

Merge branch 'master' into xdatatype

pull/8203/head
Nikita Tsukanov 4 years ago
committed by GitHub
parent
commit
6604fa0336
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Avalonia.Base/Animation/Animators/GradientBrushAnimator.cs
  2. 2
      src/Avalonia.Base/Collections/Pooled/PooledList.cs
  3. 40
      src/Avalonia.Base/Data/Core/Plugins/AvaloniaPropertyAccessorPlugin.cs
  4. 15
      src/Avalonia.Base/Media/Brush.cs
  5. 5
      src/Avalonia.Base/Media/IBrush.cs
  6. 4
      src/Avalonia.Base/Media/Immutable/ImmutableConicGradientBrush.cs
  7. 11
      src/Avalonia.Base/Media/Immutable/ImmutableGradientBrush.cs
  8. 3
      src/Avalonia.Base/Media/Immutable/ImmutableImageBrush.cs
  9. 4
      src/Avalonia.Base/Media/Immutable/ImmutableLinearGradientBrush.cs
  10. 4
      src/Avalonia.Base/Media/Immutable/ImmutableRadialGradientBrush.cs
  11. 5
      src/Avalonia.Base/Media/Immutable/ImmutableSolidColorBrush.cs
  12. 9
      src/Avalonia.Base/Media/Immutable/ImmutableTileBrush.cs
  13. 3
      src/Avalonia.Base/Media/Immutable/ImmutableVisualBrush.cs
  14. 15
      src/Avalonia.Base/Utilities/WeakEvents.cs
  15. 39
      src/Skia/Avalonia.Skia/DrawingContextImpl.cs
  16. 18
      src/Windows/Avalonia.Win32/Input/Imm32InputMethod.cs
  17. 6
      src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs
  18. 95
      tests/Avalonia.LeakTests/ControlTests.cs

6
src/Avalonia.Base/Animation/Animators/GradientBrushAnimator.cs

@ -31,6 +31,7 @@ namespace Avalonia.Animation.Animators
InterpolateStops(progress, oldValue.GradientStops, newValue.GradientStops),
s_doubleAnimator.Interpolate(progress, oldValue.Opacity, newValue.Opacity),
oldValue.Transform is { } ? new ImmutableTransform(oldValue.Transform.Value) : null,
s_relativePointAnimator.Interpolate(progress, oldValue.TransformOrigin, newValue.TransformOrigin),
oldValue.SpreadMethod,
s_relativePointAnimator.Interpolate(progress, oldRadial.Center, newRadial.Center),
s_relativePointAnimator.Interpolate(progress, oldRadial.GradientOrigin, newRadial.GradientOrigin),
@ -41,6 +42,7 @@ namespace Avalonia.Animation.Animators
InterpolateStops(progress, oldValue.GradientStops, newValue.GradientStops),
s_doubleAnimator.Interpolate(progress, oldValue.Opacity, newValue.Opacity),
oldValue.Transform is { } ? new ImmutableTransform(oldValue.Transform.Value) : null,
s_relativePointAnimator.Interpolate(progress, oldValue.TransformOrigin, newValue.TransformOrigin),
oldValue.SpreadMethod,
s_relativePointAnimator.Interpolate(progress, oldConic.Center, newConic.Center),
s_doubleAnimator.Interpolate(progress, oldConic.Angle, newConic.Angle));
@ -50,6 +52,7 @@ namespace Avalonia.Animation.Animators
InterpolateStops(progress, oldValue.GradientStops, newValue.GradientStops),
s_doubleAnimator.Interpolate(progress, oldValue.Opacity, newValue.Opacity),
oldValue.Transform is { } ? new ImmutableTransform(oldValue.Transform.Value) : null,
s_relativePointAnimator.Interpolate(progress, oldValue.TransformOrigin, newValue.TransformOrigin),
oldValue.SpreadMethod,
s_relativePointAnimator.Interpolate(progress, oldLinear.StartPoint, newLinear.StartPoint),
s_relativePointAnimator.Interpolate(progress, oldLinear.EndPoint, newLinear.EndPoint));
@ -102,18 +105,21 @@ namespace Avalonia.Animation.Animators
return new ImmutableRadialGradientBrush(
CreateStopsFromSolidColorBrush(solidColorBrush, oldRadial.GradientStops), solidColorBrush.Opacity,
oldRadial.Transform is { } ? new ImmutableTransform(oldRadial.Transform.Value) : null,
oldRadial.TransformOrigin,
oldRadial.SpreadMethod, oldRadial.Center, oldRadial.GradientOrigin, oldRadial.Radius);
case IConicGradientBrush oldConic:
return new ImmutableConicGradientBrush(
CreateStopsFromSolidColorBrush(solidColorBrush, oldConic.GradientStops), solidColorBrush.Opacity,
oldConic.Transform is { } ? new ImmutableTransform(oldConic.Transform.Value) : null,
oldConic.TransformOrigin,
oldConic.SpreadMethod, oldConic.Center, oldConic.Angle);
case ILinearGradientBrush oldLinear:
return new ImmutableLinearGradientBrush(
CreateStopsFromSolidColorBrush(solidColorBrush, oldLinear.GradientStops), solidColorBrush.Opacity,
oldLinear.Transform is { } ? new ImmutableTransform(oldLinear.Transform.Value) : null,
oldLinear.TransformOrigin,
oldLinear.SpreadMethod, oldLinear.StartPoint, oldLinear.EndPoint);
default:

2
src/Avalonia.Base/Collections/Pooled/PooledList.cs

@ -587,7 +587,7 @@ namespace Avalonia.Collections.Pooled
if (size > 0 && _clearOnFree)
{
// Clear the elements so that the gc can reclaim the references.
Array.Clear(_items, 0, _size);
Array.Clear(_items, 0, size);
}
}

40
src/Avalonia.Base/Data/Core/Plugins/AvaloniaPropertyAccessorPlugin.cs

@ -1,5 +1,6 @@
using System;
using System.Runtime.ExceptionServices;
using Avalonia.Utilities;
namespace Avalonia.Data.Core.Plugins
{
@ -60,11 +61,10 @@ namespace Avalonia.Data.Core.Plugins
return AvaloniaPropertyRegistry.Instance.FindRegistered(o, propertyName);
}
private class Accessor : PropertyAccessorBase, IObserver<object?>
private class Accessor : PropertyAccessorBase, IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>
{
private readonly WeakReference<AvaloniaObject> _reference;
private readonly AvaloniaProperty _property;
private IDisposable? _subscription;
public Accessor(WeakReference<AvaloniaObject> reference, AvaloniaProperty property)
{
@ -95,29 +95,45 @@ namespace Avalonia.Data.Core.Plugins
return false;
}
protected override void SubscribeCore()
void IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>.
OnEvent(object? notifyPropertyChanged, WeakEvent ev, AvaloniaPropertyChangedEventArgs e)
{
_subscription = Instance?.GetObservable(_property).Subscribe(this);
if (e.Property == _property)
{
SendCurrentValue();
}
}
protected override void UnsubscribeCore()
protected override void SubscribeCore()
{
_subscription?.Dispose();
_subscription = null;
SubscribeToChanges();
SendCurrentValue();
}
void IObserver<object?>.OnCompleted()
protected override void UnsubscribeCore()
{
var instance = Instance;
if (instance != null)
WeakEvents.AvaloniaPropertyChanged.Unsubscribe(instance, this);
}
void IObserver<object?>.OnError(Exception error)
private void SendCurrentValue()
{
ExceptionDispatchInfo.Capture(error).Throw();
try
{
var value = Value;
PublishValue(value);
}
catch { }
}
void IObserver<object?>.OnNext(object? value)
private void SubscribeToChanges()
{
PublishValue(value);
var instance = Instance;
if (instance != null)
WeakEvents.AvaloniaPropertyChanged.Subscribe(instance, this);
}
}
}

15
src/Avalonia.Base/Media/Brush.cs

@ -24,6 +24,12 @@ namespace Avalonia.Media
public static readonly StyledProperty<ITransform?> TransformProperty =
AvaloniaProperty.Register<Brush, ITransform?>(nameof(Transform));
/// <summary>
/// Defines the <see cref="TransformOrigin"/> property
/// </summary>
public static readonly StyledProperty<RelativePoint> TransformOriginProperty =
AvaloniaProperty.Register<Brush, RelativePoint>(nameof(TransformOrigin));
/// <inheritdoc/>
public event EventHandler? Invalidated;
@ -51,6 +57,15 @@ namespace Avalonia.Media
set { SetValue(TransformProperty, value); }
}
/// <summary>
/// Gets or sets the origin of the brush <see cref="Transform"/>
/// </summary>
public RelativePoint TransformOrigin
{
get => GetValue(TransformOriginProperty);
set => SetValue(TransformOriginProperty, value);
}
/// <summary>
/// Parses a brush string.
/// </summary>

5
src/Avalonia.Base/Media/IBrush.cs

@ -19,5 +19,10 @@ namespace Avalonia.Media
/// Gets the transform of the brush.
/// </summary>
ITransform? Transform { get; }
/// <summary>
/// Gets the origin of the brushes <see cref="Transform"/>
/// </summary>
RelativePoint TransformOrigin { get; }
}
}

4
src/Avalonia.Base/Media/Immutable/ImmutableConicGradientBrush.cs

@ -13,6 +13,7 @@ namespace Avalonia.Media.Immutable
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="spreadMethod">The spread method.</param>
/// <param name="center">The center point for the gradient.</param>
/// <param name="angle">The starting angle for the gradient.</param>
@ -20,10 +21,11 @@ namespace Avalonia.Media.Immutable
IReadOnlyList<ImmutableGradientStop> gradientStops,
double opacity = 1,
ImmutableTransform? transform = null,
RelativePoint? transformOrigin = null,
GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad,
RelativePoint? center = null,
double angle = 0)
: base(gradientStops, opacity, transform, spreadMethod)
: base(gradientStops, opacity, transform, transformOrigin, spreadMethod)
{
Center = center ?? RelativePoint.Center;
Angle = angle;

11
src/Avalonia.Base/Media/Immutable/ImmutableGradientBrush.cs

@ -13,16 +13,19 @@ namespace Avalonia.Media.Immutable
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="spreadMethod">The spread method.</param>
protected ImmutableGradientBrush(
IReadOnlyList<ImmutableGradientStop> gradientStops,
double opacity,
ImmutableTransform? transform,
RelativePoint? transformOrigin,
GradientSpreadMethod spreadMethod)
{
GradientStops = gradientStops;
Opacity = opacity;
Transform = transform;
TransformOrigin = transformOrigin.HasValue ? transformOrigin.Value : RelativePoint.TopLeft;
SpreadMethod = spreadMethod;
}
@ -31,7 +34,8 @@ namespace Avalonia.Media.Immutable
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
protected ImmutableGradientBrush(GradientBrush source)
: this(source.GradientStops.ToImmutable(), source.Opacity, source.Transform?.ToImmutable(), source.SpreadMethod)
: this(source.GradientStops.ToImmutable(), source.Opacity, source.Transform?.ToImmutable(),
source.TransformOrigin, source.SpreadMethod)
{
}
@ -47,6 +51,11 @@ namespace Avalonia.Media.Immutable
/// </summary>
public ITransform? Transform { get; }
/// <summary>
/// Gets the transform origin of the brush
/// </summary>
public RelativePoint TransformOrigin { get; }
/// <inheritdoc/>
public GradientSpreadMethod SpreadMethod { get; }
}

3
src/Avalonia.Base/Media/Immutable/ImmutableImageBrush.cs

@ -16,6 +16,7 @@ namespace Avalonia.Media.Immutable
/// <param name="destinationRect">The rectangle on the destination in which to paint a tile.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="sourceRect">The rectangle of the source image that will be displayed.</param>
/// <param name="stretch">
/// How the source rectangle will be stretched to fill the destination rect.
@ -29,6 +30,7 @@ namespace Avalonia.Media.Immutable
RelativeRect? destinationRect = null,
double opacity = 1,
ImmutableTransform? transform = null,
RelativePoint transformOrigin = new RelativePoint(),
RelativeRect? sourceRect = null,
Stretch stretch = Stretch.Uniform,
TileMode tileMode = TileMode.None,
@ -39,6 +41,7 @@ namespace Avalonia.Media.Immutable
destinationRect ?? RelativeRect.Fill,
opacity,
transform,
transformOrigin,
sourceRect ?? RelativeRect.Fill,
stretch,
tileMode,

4
src/Avalonia.Base/Media/Immutable/ImmutableLinearGradientBrush.cs

@ -13,6 +13,7 @@ namespace Avalonia.Media.Immutable
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="spreadMethod">The spread method.</param>
/// <param name="startPoint">The start point for the gradient.</param>
/// <param name="endPoint">The end point for the gradient.</param>
@ -20,10 +21,11 @@ namespace Avalonia.Media.Immutable
IReadOnlyList<ImmutableGradientStop> gradientStops,
double opacity = 1,
ImmutableTransform? transform = null,
RelativePoint? transformOrigin = null,
GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad,
RelativePoint? startPoint = null,
RelativePoint? endPoint = null)
: base(gradientStops, opacity, transform, spreadMethod)
: base(gradientStops, opacity, transform, transformOrigin, spreadMethod)
{
StartPoint = startPoint ?? RelativePoint.TopLeft;
EndPoint = endPoint ?? RelativePoint.BottomRight;

4
src/Avalonia.Base/Media/Immutable/ImmutableRadialGradientBrush.cs

@ -13,6 +13,7 @@ namespace Avalonia.Media.Immutable
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="spreadMethod">The spread method.</param>
/// <param name="center">The start point for the gradient.</param>
/// <param name="gradientOrigin">
@ -25,11 +26,12 @@ namespace Avalonia.Media.Immutable
IReadOnlyList<ImmutableGradientStop> gradientStops,
double opacity = 1,
ImmutableTransform? transform = null,
RelativePoint? transformOrigin = null,
GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad,
RelativePoint? center = null,
RelativePoint? gradientOrigin = null,
double radius = 0.5)
: base(gradientStops, opacity, transform, spreadMethod)
: base(gradientStops, opacity, transform, transformOrigin, spreadMethod)
{
Center = center ?? RelativePoint.Center;
GradientOrigin = gradientOrigin ?? RelativePoint.Center;

5
src/Avalonia.Base/Media/Immutable/ImmutableSolidColorBrush.cs

@ -53,6 +53,11 @@ namespace Avalonia.Media.Immutable
/// </summary>
public ITransform? Transform { get; }
/// <summary>
/// Gets the transform origin of the brush
/// </summary>
public RelativePoint TransformOrigin { get; }
public bool Equals(ImmutableSolidColorBrush? other)
{
if (ReferenceEquals(null, other)) return false;

9
src/Avalonia.Base/Media/Immutable/ImmutableTileBrush.cs

@ -15,6 +15,7 @@ namespace Avalonia.Media.Immutable
/// <param name="destinationRect">The rectangle on the destination in which to paint a tile.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="sourceRect">The rectangle of the source image that will be displayed.</param>
/// <param name="stretch">
/// How the source rectangle will be stretched to fill the destination rect.
@ -27,6 +28,7 @@ namespace Avalonia.Media.Immutable
RelativeRect destinationRect,
double opacity,
ImmutableTransform? transform,
RelativePoint transformOrigin,
RelativeRect sourceRect,
Stretch stretch,
TileMode tileMode,
@ -37,6 +39,7 @@ namespace Avalonia.Media.Immutable
DestinationRect = destinationRect;
Opacity = opacity;
Transform = transform;
TransformOrigin = transformOrigin;
SourceRect = sourceRect;
Stretch = stretch;
TileMode = tileMode;
@ -54,6 +57,7 @@ namespace Avalonia.Media.Immutable
source.DestinationRect,
source.Opacity,
source.Transform?.ToImmutable(),
source.TransformOrigin,
source.SourceRect,
source.Stretch,
source.TileMode,
@ -78,6 +82,11 @@ namespace Avalonia.Media.Immutable
/// </summary>
public ITransform? Transform { get; }
/// <summary>
/// Gets the transform origin of the brush
/// </summary>
public RelativePoint TransformOrigin { get; }
/// <inheritdoc/>
public RelativeRect SourceRect { get; }

3
src/Avalonia.Base/Media/Immutable/ImmutableVisualBrush.cs

@ -17,6 +17,7 @@ namespace Avalonia.Media.Immutable
/// <param name="destinationRect">The rectangle on the destination in which to paint a tile.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="transform">The transform of the brush.</param>
/// <param name="transformOrigin">The transform origin of the brush</param>
/// <param name="sourceRect">The rectangle of the source image that will be displayed.</param>
/// <param name="stretch">
/// How the source rectangle will be stretched to fill the destination rect.
@ -30,6 +31,7 @@ namespace Avalonia.Media.Immutable
RelativeRect? destinationRect = null,
double opacity = 1,
ImmutableTransform? transform = null,
RelativePoint transformOrigin = new RelativePoint(),
RelativeRect? sourceRect = null,
Stretch stretch = Stretch.Uniform,
TileMode tileMode = TileMode.None,
@ -40,6 +42,7 @@ namespace Avalonia.Media.Immutable
destinationRect ?? RelativeRect.Fill,
opacity,
transform,
transformOrigin,
sourceRect ?? RelativeRect.Fill,
stretch,
tileMode,

15
src/Avalonia.Base/Utilities/WeakEvents.cs

@ -31,10 +31,23 @@ public class WeakEvents
return () => s.PropertyChanged -= handler;
});
/// <summary>
/// Represents PropertyChanged event from <see cref="AvaloniaObject"/>
/// </summary>
public static readonly WeakEvent<AvaloniaObject, AvaloniaPropertyChangedEventArgs>
AvaloniaPropertyChanged = WeakEvent.Register<AvaloniaObject, AvaloniaPropertyChangedEventArgs>(
(s, h) =>
{
EventHandler<AvaloniaPropertyChangedEventArgs> handler = (_, e) => h(s, e);
s.PropertyChanged += handler;
return () => s.PropertyChanged -= handler;
});
/// <summary>
/// Represents CanExecuteChanged event from <see cref="ICommand"/>
/// </summary>
public static readonly WeakEvent<ICommand, EventArgs> CommandCanExecuteChanged =
WeakEvent.Register<ICommand>((s, h) => s.CanExecuteChanged += h,
(s, h) => s.CanExecuteChanged -= h);
}
}

39
src/Skia/Avalonia.Skia/DrawingContextImpl.cs

@ -181,7 +181,8 @@ namespace Avalonia.Skia
var size = geometry.Bounds.Size;
using (var fill = brush != null ? CreatePaint(_fillPaint, brush, size) : default(PaintWrapper))
using (var stroke = pen?.Brush != null ? CreatePaint(_strokePaint, pen, size) : default(PaintWrapper))
using (var stroke = pen?.Brush != null ? CreatePaint(_strokePaint, pen,
size.Inflate(new Thickness(pen?.Thickness / 2 ?? 0))) : default(PaintWrapper))
{
if (fill.Paint != null)
{
@ -398,7 +399,7 @@ namespace Avalonia.Skia
if (pen?.Brush != null)
{
using (var paint = CreatePaint(_strokePaint, pen, rect.Rect.Size))
using (var paint = CreatePaint(_strokePaint, pen, rect.Rect.Size.Inflate(new Thickness(pen?.Thickness / 2 ?? 0))))
{
if (paint.Paint is object)
{
@ -433,7 +434,7 @@ namespace Avalonia.Skia
if (pen?.Brush != null)
{
using (var paint = CreatePaint(_strokePaint, pen, rect.Size))
using (var paint = CreatePaint(_strokePaint, pen, rect.Size.Inflate(new Thickness(pen?.Thickness / 2 ?? 0))))
{
if (paint.Paint is object)
{
@ -625,8 +626,12 @@ namespace Avalonia.Skia
}
else
{
var transformOrigin = linearGradient.TransformOrigin.ToPixels(targetSize);
var offset = Matrix.CreateTranslation(transformOrigin);
var transform = (-offset) * linearGradient.Transform.Value * (offset);
using (var shader =
SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode, linearGradient.Transform.Value.ToSKMatrix()))
SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode, transform.ToSKMatrix()))
{
paintWrapper.Paint.Shader = shader;
}
@ -654,8 +659,12 @@ namespace Avalonia.Skia
}
else
{
var transformOrigin = radialGradient.TransformOrigin.ToPixels(targetSize);
var offset = Matrix.CreateTranslation(transformOrigin);
var transform = (-offset) * radialGradient.Transform.Value * (offset);
using (var shader =
SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode, radialGradient.Transform.Value.ToSKMatrix()))
SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode, transform.ToSKMatrix()))
{
paintWrapper.Paint.Shader = shader;
}
@ -694,9 +703,14 @@ namespace Avalonia.Skia
}
else
{
var transformOrigin = radialGradient.TransformOrigin.ToPixels(targetSize);
var offset = Matrix.CreateTranslation(transformOrigin);
var transform = (-offset) * radialGradient.Transform.Value * (offset);
using (var shader = SKShader.CreateCompose(
SKShader.CreateColor(reversedColors[0]),
SKShader.CreateTwoPointConicalGradient(center, radius, origin, 0, reversedColors, reversedStops, tileMode, radialGradient.Transform.Value.ToSKMatrix())
SKShader.CreateTwoPointConicalGradient(center, radius, origin, 0, reversedColors, reversedStops, tileMode, transform.ToSKMatrix())
))
{
paintWrapper.Paint.Shader = shader;
@ -717,7 +731,12 @@ namespace Avalonia.Skia
if (conicGradient.Transform is { })
{
rotation = rotation.PreConcat(conicGradient.Transform.Value.ToSKMatrix());
var transformOrigin = conicGradient.TransformOrigin.ToPixels(targetSize);
var offset = Matrix.CreateTranslation(transformOrigin);
var transform = (-offset) * conicGradient.Transform.Value * (offset);
rotation = rotation.PreConcat(transform.ToSKMatrix());
}
using (var shader =
@ -794,7 +813,11 @@ namespace Avalonia.Skia
if (tileBrush.Transform is { })
{
paintTransform = paintTransform.PreConcat(tileBrush.Transform.Value.ToSKMatrix());
var origin = tileBrush.TransformOrigin.ToPixels(targetSize);
var offset = Matrix.CreateTranslation(origin);
var transform = (-offset) * tileBrush.Transform.Value * (offset);
paintTransform = paintTransform.PreConcat(transform.ToSKMatrix());
}
using (var shader = image.ToShader(tileX, tileY, paintTransform))

18
src/Windows/Avalonia.Win32/Input/Imm32InputMethod.cs

@ -37,6 +37,24 @@ namespace Avalonia.Win32.Input
IsComposing = false;
}
public void ClearLanguageAndWindow()
{
if (HWND != IntPtr.Zero && _defaultImc != IntPtr.Zero)
{
ImmReleaseContext(HWND, _defaultImc);
}
_defaultImc = IntPtr.Zero;
HWND = IntPtr.Zero;
_parent = null;
_active = false;
_langId = 0;
_showCompositionWindow = false;
_showCandidateList = false;
IsComposing = false;
}
//Dependant on CurrentThread. When Avalonia will support Multiple Dispatchers -
//every Dispatcher should have their own InputMethod.
public static Imm32InputMethod Current { get; } = new Imm32InputMethod();

6
src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs

@ -82,6 +82,12 @@ namespace Avalonia.Win32
case WindowsMessage.WM_DESTROY:
{
UiaCoreProviderApi.UiaReturnRawElementProvider(_hwnd, IntPtr.Zero, IntPtr.Zero, null);
// We need to release IMM context and state to avoid leaks.
if (Imm32InputMethod.Current.HWND == _hwnd)
{
Imm32InputMethod.Current.ClearLanguageAndWindow();
}
//Window doesn't exist anymore
_hwnd = IntPtr.Zero;

95
tests/Avalonia.LeakTests/ControlTests.cs

@ -2,17 +2,18 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Reactive.Disposables;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Diagnostics;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using JetBrains.dotMemoryUnit;
@ -661,14 +662,96 @@ namespace Avalonia.LeakTests
}
}
[Fact]
public void ElementName_Binding_In_DataTemplate_Is_Freed()
{
using (Start())
{
var items = new ObservableCollection<int>(Enumerable.Range(0, 10));
NameScope ns;
TextBox tb;
ListBox lb;
var window = new Window
{
[NameScope.NameScopeProperty] = ns = new NameScope(),
Width = 100,
Height = 100,
Content = new StackPanel
{
Children =
{
(tb = new TextBox
{
Name = "tb",
Text = "foo",
}),
(lb = new ListBox
{
Items = items,
ItemTemplate = new FuncDataTemplate<int>((_, _) =>
new Canvas
{
Width = 10,
Height = 10,
[!Control.TagProperty] = new Binding
{
ElementName = "tb",
Path = "Text",
NameScope = new WeakReference<INameScope>(ns),
}
})
}),
}
}
};
tb.RegisterInNameScope(ns);
window.Show();
window.LayoutManager.ExecuteInitialLayoutPass();
void AssertInitialItemState()
{
var item0 = (ListBoxItem)lb.ItemContainerGenerator.Containers.First().ContainerControl;
var canvas0 = (Canvas)item0.Presenter.Child;
Assert.Equal("foo", canvas0.Tag);
}
Assert.Equal(10, lb.ItemContainerGenerator.Containers.Count());
AssertInitialItemState();
items.Clear();
window.LayoutManager.ExecuteLayoutPass();
Assert.Empty(lb.ItemContainerGenerator.Containers);
dotMemory.Check(memory =>
Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
}
}
private IDisposable Start()
{
return UnitTestApplication.Start(TestServices.StyledWindow.With(
focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice(),
inputManager: new InputManager()));
void Cleanup()
{
// KeyboardDevice holds a reference to the focused item.
KeyboardDevice.Instance.SetFocusedElement(null, NavigationMethod.Unspecified, KeyModifiers.None);
// Empty the dispatcher queue.
Dispatcher.UIThread.RunJobs();
}
return new CompositeDisposable
{
Disposable.Create(Cleanup),
UnitTestApplication.Start(TestServices.StyledWindow.With(
focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice(),
inputManager: new InputManager()))
};
}
private class Node
{
public string Name { get; set; }

Loading…
Cancel
Save