Skip to content

Commit c3113f4

Browse files
committed
Cleanup + Fix element layout state when loaded
1 parent b257e12 commit c3113f4

15 files changed

+202
-358
lines changed

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.Events.Loaders.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using Hi3Helper.Win32.Native.Enums.DXGI;
2-
using Hi3Helper.Win32.Native.Structs.DXGI;
3-
using Hi3Helper.Win32.WinRT.SwapChainPanelHelper;
4-
using Microsoft.Graphics.Canvas;
5-
using Microsoft.Graphics.Canvas.UI.Xaml;
6-
using Microsoft.Graphics.DirectX;
1+
using Microsoft.Graphics.Canvas;
72
using Microsoft.UI.Xaml;
83
using Microsoft.UI.Xaml.Controls;
94
using Microsoft.UI.Xaml.Data;
@@ -14,15 +9,10 @@
149
using System.IO;
1510
using System.Linq;
1611
using System.Runtime.CompilerServices;
17-
using System.Runtime.InteropServices;
1812
using System.Threading;
1913
using System.Threading.Tasks;
20-
using Windows.Foundation;
21-
using Windows.Graphics.Capture;
22-
using Windows.Graphics.DirectX;
2314
using Windows.Media.Playback;
2415
using Windows.Storage.Streams;
25-
using WinRT.Interop;
2616

2717
// ReSharper disable StringLiteralTypo
2818
// ReSharper disable CommentTypo
@@ -96,6 +86,8 @@ private enum MediaSourceType
9686
private bool _isResizingVideoCanvas;
9787
private bool _isDrawingVideoFrame;
9888

89+
private MediaSourceType? _lastMediaType;
90+
9991
#endregion
10092

10193
#region Loaders
@@ -169,16 +161,6 @@ private async void LoadFromSourceAsyncDetached(
169161
{
170162
try
171163
{
172-
if (IsInPreloadGrid())
173-
{
174-
return;
175-
}
176-
177-
if (grid.Name.StartsWith("Background"))
178-
{
179-
DisposeVideoPlayer();
180-
}
181-
182164
object? source = GetValue(sourceProperty);
183165
if (source is null)
184166
{
@@ -196,6 +178,14 @@ private async void LoadFromSourceAsyncDetached(
196178
goto ClearGrid;
197179
}
198180

181+
if (grid.Name.StartsWith("Background") &&
182+
_lastMediaType == MediaSourceType.Video)
183+
{
184+
DisposeVideoPlayer();
185+
}
186+
187+
_lastMediaType = mediaType;
188+
199189
// ReSharper disable once ConvertIfStatementToSwitchStatement
200190
if (mediaType == MediaSourceType.Image &&
201191
await LoadImageFromSourceAsync(source,

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.Events.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.Graphics.Canvas;
2-
using Microsoft.UI.Composition;
1+
using Microsoft.UI.Composition;
32
using Microsoft.UI.Xaml;
43
using Microsoft.UI.Xaml.Input;
54
using System;
@@ -33,19 +32,8 @@ private static bool IsSourceKindEquals(object? left, object? right)
3332
return left == right;
3433
}
3534

36-
private bool IsInPreloadGrid()
37-
{
38-
return Parent is FrameworkElement asGrid &&
39-
asGrid.Name.StartsWith("Preload", StringComparison.OrdinalIgnoreCase);
40-
}
41-
4235
private void LayeredBackgroundImage_OnLoaded(object sender, RoutedEventArgs e)
4336
{
44-
if (IsInPreloadGrid())
45-
{
46-
return;
47-
}
48-
4937
ParallaxView_ToggleEnable(IsParallaxEnabled);
5038
ParallaxGrid_OnUpdateCenterPoint();
5139

@@ -93,11 +81,6 @@ private void LayeredBackgroundImage_OnLoaded(object sender, RoutedEventArgs e)
9381

9482
private void LayeredBackgroundImage_OnUnloaded(object sender, RoutedEventArgs e)
9583
{
96-
if (IsInPreloadGrid())
97-
{
98-
return;
99-
}
100-
10184
ParallaxGrid_UnregisterEffect();
10285
_lastParallaxHoverSource = null;
10386

@@ -112,8 +95,7 @@ private void LayeredBackgroundImage_OnUnloaded(object sender, RoutedEventArgs e)
11295
private static void IsParallaxEnabled_OnChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
11396
{
11497
LayeredBackgroundImage element = (LayeredBackgroundImage)d;
115-
if (!element.IsLoaded ||
116-
element.IsInPreloadGrid())
98+
if (!element.IsLoaded)
11799
{
118100
return;
119101
}
@@ -124,8 +106,7 @@ private static void IsParallaxEnabled_OnChange(DependencyObject d, DependencyPro
124106
private static void ParallaxHover_OnChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
125107
{
126108
LayeredBackgroundImage element = (LayeredBackgroundImage)d;
127-
if (!element.IsLoaded ||
128-
element.IsInPreloadGrid())
109+
if (!element.IsLoaded)
129110
{
130111
return;
131112
}
@@ -313,12 +294,7 @@ public void Play()
313294
{
314295
try
315296
{
316-
MediaPlayerState state = _videoPlayer?.CurrentState ?? MediaPlayerState.Closed;
317-
bool isInPreloadGrid = IsInPreloadGrid();
318-
319-
if (!isInPreloadGrid &&
320-
_videoPlayer != null! &&
321-
state is MediaPlayerState.Paused or MediaPlayerState.Stopped)
297+
if (_videoPlayer != null!)
322298
{
323299
_videoPlayer.Play();
324300
}
@@ -333,8 +309,7 @@ public void Pause()
333309
{
334310
try
335311
{
336-
if (_videoPlayer != null! &&
337-
_videoPlayer is { CanPause: true, CurrentState: MediaPlayerState.Playing })
312+
if (_videoPlayer != null!)
338313
{
339314
_videoPlayer.Pause();
340315
}

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.Templates.cs

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
using Hi3Helper.Win32.Native.Enums.DXGI;
2-
using Hi3Helper.Win32.Native.Structs.DXGI;
3-
using Hi3Helper.Win32.WinRT.SwapChainPanelHelper;
4-
using Microsoft.Graphics.Canvas;
1+
using Microsoft.Graphics.Canvas;
52
using Microsoft.Graphics.Canvas.UI.Xaml;
63
using Microsoft.UI.Composition;
74
using Microsoft.UI.Xaml;
85
using Microsoft.UI.Xaml.Controls;
96
using Microsoft.UI.Xaml.Hosting;
10-
using System;
117
using System.Threading;
128
using Windows.Foundation;
13-
using Windows.Graphics.Capture;
149
using Windows.Media.Playback;
15-
using WinRT.Interop;
1610

1711
namespace BackgroundTest.CustomControl.LayeredBackgroundImage;
18-
19-
[TemplatePart(Name = TemplateNameParallaxGrid, Type = typeof(Grid))]
20-
[TemplatePart(Name = TemplateNamePlaceholderGrid, Type = typeof(Grid))]
21-
[TemplatePart(Name = TemplateNameBackgroundGrid, Type = typeof(Grid))]
22-
[TemplatePart(Name = TemplateNameForegroundGrid, Type = typeof(Grid))]
2312
public partial class LayeredBackgroundImage
2413
{
2514
#region Constants
@@ -35,10 +24,10 @@ public partial class LayeredBackgroundImage
3524

3625
#region Fields
3726

38-
private Grid _parallaxGrid = null!;
27+
private Grid _parallaxGrid = null!;
3928
private Grid _placeholderGrid = null!;
40-
private Grid _backgroundGrid = null!;
41-
private Grid _foregroundGrid = null!;
29+
private Grid _backgroundGrid = null!;
30+
private Grid _foregroundGrid = null!;
4231

4332
private Visual _parallaxGridVisual = null!;
4433
private Compositor _parallaxGridCompositor = null!;
@@ -50,44 +39,23 @@ public partial class LayeredBackgroundImage
5039
private CanvasDrawingSession? _currentCanvasDrawingSession = null;
5140
private Rect _currentCanvasRenderSize = default;
5241

53-
private SwapChainContext _swapChainContext = null!;
54-
private SwapChainPanel _swapChainPanel = null!;
55-
5642
private float _canvasWidth;
5743
private float _canvasHeight;
58-
private float _lastCanvasWidth;
59-
private float _lastCanvasHeight;
6044

6145
private MediaPlayer _videoPlayer = null!;
6246

6347
private bool _isTemplateLoaded;
6448

6549
#endregion
6650

67-
#region Deconstructor
68-
69-
~LayeredBackgroundImage()
70-
{
71-
if (_videoPlayer == null!)
72-
{
73-
return;
74-
}
75-
76-
_videoPlayer.Dispose();
77-
_videoPlayer = null!;
78-
}
79-
#endregion
80-
8151
#region Apply Template Methods
8252

8353
protected override void OnApplyTemplate()
8454
{
85-
base.OnApplyTemplate();
86-
87-
_parallaxGrid = this.GetTemplateChild<Grid>(TemplateNameParallaxGrid);
55+
_parallaxGrid = this.GetTemplateChild<Grid>(TemplateNameParallaxGrid);
8856
_placeholderGrid = this.GetTemplateChild<Grid>(TemplateNamePlaceholderGrid);
89-
_backgroundGrid = this.GetTemplateChild<Grid>(TemplateNameBackgroundGrid);
90-
_foregroundGrid = this.GetTemplateChild<Grid>(TemplateNameForegroundGrid);
57+
_backgroundGrid = this.GetTemplateChild<Grid>(TemplateNameBackgroundGrid);
58+
_foregroundGrid = this.GetTemplateChild<Grid>(TemplateNameForegroundGrid);
9159

9260
ElementCompositionPreview.SetIsTranslationEnabled(_parallaxGrid, true);
9361

@@ -96,12 +64,11 @@ protected override void OnApplyTemplate()
9664

9765
Interlocked.Exchange(ref _isTemplateLoaded, true);
9866

99-
Loaded += LayeredBackgroundImage_OnLoaded;
100-
Unloaded += LayeredBackgroundImage_OnUnloaded;
101-
10267
#if USENATIVESWAPCHAIN
10368
SizeChanged += LayeredBackgroundImage_SizeChanged;
10469
#endif
70+
71+
base.OnApplyTemplate();
10572
}
10673

10774
private void SetupVideoPlayer()
@@ -118,40 +85,9 @@ private void SetupVideoPlayer()
11885
_videoPlayer.VideoFrameAvailable += VideoPlayer_VideoFrameAvailable;
11986
}
12087

121-
private void DisposeVideoPlayer()
122-
{
123-
MediaPlayer? pastMediaPlayer = _videoPlayer;
124-
if (pastMediaPlayer != null!)
125-
{
126-
pastMediaPlayer.VideoFrameAvailable -= VideoPlayer_VideoFrameAvailable;
127-
pastMediaPlayer.Dispose();
128-
}
129-
130-
if (_currentCanvasDrawingSession is { } previousCanvasDrawingSession)
131-
{
132-
previousCanvasDrawingSession.Dispose();
133-
Interlocked.Exchange(ref _currentCanvasDrawingSession, null);
134-
}
135-
136-
Interlocked.Exchange(ref _isDrawingVideoFrame, false);
137-
}
138-
13988
private void InitializeCanvasBitmapSource(Image image, MediaPlaybackSession playbackSession)
14089
{
141-
if (Interlocked.Exchange(ref _isResizingVideoCanvas, true))
142-
{
143-
return;
144-
}
145-
146-
if (_canvasRenderTarget is { } previousCanvasBitmap)
147-
{
148-
previousCanvasBitmap.Dispose();
149-
}
150-
151-
if (_canvasDevice is { } previousCanvasDevice)
152-
{
153-
previousCanvasDevice.Dispose();
154-
}
90+
DisposeAndInvalidateCanvas();
15591

15692
float currentCanvasWidth = playbackSession.NaturalVideoWidth;
15793
float currentCanvasHeight = playbackSession.NaturalVideoHeight;
@@ -168,7 +104,7 @@ private void InitializeCanvasBitmapSource(Image image, MediaPlaybackSession play
168104
currentCanvasHeight,
169105
currentCanvasDpi,
170106
CanvasAlphaMode.Premultiplied);
171-
_canvasRenderTarget = new CanvasRenderTarget(_canvasImageSource,
107+
_canvasRenderTarget = new CanvasRenderTarget(_canvasDevice,
172108
currentCanvasWidth,
173109
currentCanvasHeight,
174110
currentCanvasDpi);
@@ -182,6 +118,42 @@ private void InitializeCanvasBitmapSource(Image image, MediaPlaybackSession play
182118
Interlocked.Exchange(ref _isResizingVideoCanvas, false);
183119
}
184120

121+
private void DisposeAndInvalidateCanvas()
122+
{
123+
if (Interlocked.Exchange(ref _isResizingVideoCanvas, true))
124+
{
125+
return;
126+
}
127+
128+
if (_canvasRenderTarget is { } previousCanvasBitmap)
129+
{
130+
previousCanvasBitmap.Dispose();
131+
}
132+
133+
if (_canvasDevice is { } previousCanvasDevice)
134+
{
135+
previousCanvasDevice.Dispose();
136+
}
137+
}
138+
139+
private void DisposeVideoPlayer()
140+
{
141+
if (_videoPlayer != null!)
142+
{
143+
_videoPlayer.VideoFrameAvailable -= VideoPlayer_VideoFrameAvailable;
144+
_videoPlayer.Dispose();
145+
Interlocked.Exchange(ref _videoPlayer!, null);
146+
}
147+
148+
if (_currentCanvasDrawingSession is { } previousCanvasDrawingSession)
149+
{
150+
previousCanvasDrawingSession.Dispose();
151+
Interlocked.Exchange(ref _currentCanvasDrawingSession, null);
152+
}
153+
154+
Interlocked.Exchange(ref _isDrawingVideoFrame, false);
155+
}
156+
185157
#if USENATIVESWAPCHAIN
186158
private void LayeredBackgroundImage_SizeChanged(object sender, SizeChangedEventArgs e)
187159
{
@@ -241,6 +213,6 @@ private void InitializeOrResizeCanvas(bool ignoreCheck = false)
241213
}
242214
#endif
243215

244-
#endregion
216+
#endregion
245217
}
246218

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,21 @@
22

33
namespace BackgroundTest.CustomControl.LayeredBackgroundImage;
44

5-
public partial class LayeredBackgroundImage : Control;
5+
public partial class LayeredBackgroundImage : Control
6+
{
7+
public LayeredBackgroundImage()
8+
{
9+
Loaded += LayeredBackgroundImage_OnLoaded;
10+
Unloaded += LayeredBackgroundImage_OnUnloaded;
11+
12+
DefaultStyleKey = typeof(LayeredBackgroundImage);
13+
}
14+
15+
~LayeredBackgroundImage()
16+
{
17+
Loaded -= LayeredBackgroundImage_OnLoaded;
18+
Unloaded -= LayeredBackgroundImage_OnUnloaded;
19+
20+
DisposeVideoPlayer();
21+
}
22+
}

BackgroundTest/CustomControl/LayeredBackgroundImage/LayeredBackgroundImage.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
</VisualState>
5656
</VisualStateGroup>
5757
</VisualStateManager.VisualStateGroups>
58-
<MediaPlayerElement Opacity="0"/>
5958
<Grid x:Name="ParallaxGrid"
6059
HorizontalAlignment="Stretch"
6160
VerticalAlignment="Stretch">

BackgroundTest/CustomControl/NewPipsPager/NewPipsPager.Events.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,6 @@ private void NewPipsPager_Unloaded(object sender, RoutedEventArgs e)
357357
UnapplyKeyPressEvents();
358358
UnapplyItemsRepeaterEvents();
359359

360-
Loaded -= NewPipsPager_Loaded;
361-
Unloaded -= NewPipsPager_Unloaded;
362-
363360
_pipsPagerItemsRepeater.ItemsSource = null;
364361
}
365362

0 commit comments

Comments
 (0)