Skip to content

Commit a1d4b10

Browse files
committed
Avalonia: fix events leaks
1 parent 0d1c047 commit a1d4b10

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/LibVLCSharp.Avalonia/VideoView.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class VideoView : NativeControlHost
2121
private IPlatformHandle? _platformHandle = null;
2222
private MediaPlayer? _mediaPlayer = null;
2323
private Window? _floatingContent = null;
24+
IDisposable? contentChangedHandler = null;
25+
IDisposable? isVisibleChangedHandler = null;
26+
IDisposable? floatingContentChangedHandler = null;
2427

2528
/// <summary>
2629
/// MediaPlayer Data Bound property
@@ -74,15 +77,17 @@ public object? Content
7477
public VideoView()
7578
{
7679
Initialized += (_, _) => { Attach(); };
77-
ContentProperty.Changed.AddClassHandler<VideoView>((s, _) => s.InitializeNativeOverlay());
78-
IsVisibleProperty.Changed.AddClassHandler<VideoView>((s, _) => s.ShowNativeOverlay(s.IsVisible));
80+
81+
contentChangedHandler = ContentProperty.Changed.AddClassHandler<VideoView>((s, _) => s.UpdateOverlayPosition());
82+
isVisibleChangedHandler = IsVisibleProperty.Changed.AddClassHandler<VideoView>((s, _) => s.ShowNativeOverlay(s.IsVisible));
7983
}
8084

8185
private void UpdateOverlayPosition()
8286
{
8387
if (_floatingContent == null || !IsVisible)
88+
{
8489
return;
85-
90+
}
8691
bool forceSetWidth = false, forceSetHeight = false;
8792
var topLeft = new Point();
8893
var child = _floatingContent.Presenter?.Child;
@@ -250,21 +255,21 @@ private void InitializeNativeOverlay()
250255
Opacity = 1.0,
251256
DataContext = DataContext
252257
};
253-
_floatingContent.Bind(ContentControl.ContentProperty, this.GetObservable(ContentProperty));
258+
floatingContentChangedHandler = _floatingContent.Bind(ContentControl.ContentProperty, this.GetObservable(ContentProperty));
254259
_floatingContent.PointerEntered += FloatingContentOnPointerEvent;
255260
_floatingContent.PointerExited += FloatingContentOnPointerEvent;
256261
_floatingContent.PointerPressed += FloatingContentOnPointerEvent;
257262
_floatingContent.PointerReleased += FloatingContentOnPointerEvent;
258263

259-
ContentProperty.Changed.AddClassHandler<VideoView>((o, _) => o.UpdateOverlayPosition());
260-
261-
visualRoot.LayoutUpdated += (_, _) => UpdateOverlayPosition();
262-
visualRoot.PositionChanged += (_, _) => UpdateOverlayPosition();
264+
visualRoot.LayoutUpdated += VisualRoot_UpdateOverlayPosition;
265+
visualRoot.PositionChanged += VisualRoot_UpdateOverlayPosition;
263266
}
264267

265268
ShowNativeOverlay(IsEffectivelyVisible);
266269
}
267270

271+
private void VisualRoot_UpdateOverlayPosition(object sender, EventArgs e) => UpdateOverlayPosition();
272+
268273
private void FloatingContentOnPointerEvent(object? sender, PointerEventArgs e)
269274
{
270275
RaiseEvent(e);
@@ -284,13 +289,30 @@ private void ShowNativeOverlay(bool show)
284289
/// <inheritdoc />
285290
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
286291
{
292+
var parent = this.GetVisualParent();
293+
if(parent != null)
294+
parent.DetachedFromVisualTree += Parent_DetachedFromVisualTree;
287295
base.OnAttachedToVisualTree(e);
288296
InitializeNativeOverlay();
289297
}
290298

299+
private void Parent_DetachedFromVisualTree(object sender, VisualTreeAttachmentEventArgs e)
300+
{
301+
if (VisualRoot is not Window visualRoot)
302+
{
303+
return;
304+
}
305+
306+
visualRoot.LayoutUpdated -= VisualRoot_UpdateOverlayPosition;
307+
visualRoot.PositionChanged -= VisualRoot_UpdateOverlayPosition;
308+
}
309+
291310
/// <inheritdoc />
292311
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
293312
{
313+
var parent = this.GetVisualParent();
314+
if (parent != null)
315+
parent.DetachedFromVisualTree -= Parent_DetachedFromVisualTree;
294316
base.OnDetachedFromVisualTree(e);
295317
ShowNativeOverlay(false);
296318
}
@@ -305,6 +327,10 @@ protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle paren
305327
/// <inheritdoc />
306328
protected override void DestroyNativeControlCore(IPlatformHandle control)
307329
{
330+
contentChangedHandler?.Dispose();
331+
isVisibleChangedHandler?.Dispose();
332+
floatingContentChangedHandler?.Dispose();
333+
308334
Detach();
309335
if (_floatingContent != null)
310336
{

0 commit comments

Comments
 (0)