From 0c3c16608e1308b44e9280b863020957086f30af Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 21 Jan 2023 20:43:09 +0100 Subject: [PATCH] Add D2D1AnimatedPixelShaderPanel.IsPaused property --- .../Controls/D2D1AnimatedPixelShaderPanel.cs | 36 ++++++++++++++++++- .../Views/MainView.xaml | 6 ++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs index 6134dac8..58bf452f 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs @@ -79,7 +79,41 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control // Pause or start the render thread if a runner is available if (@this.canvasAnimatedControl is { } canvasAnimatedControl) { - canvasAnimatedControl.Paused = e.NewValue is not ID2D1ShaderRunner; + bool shouldRender = @this.IsLoaded && !@this.IsPaused && shaderRunner is not null; + + canvasAnimatedControl.Paused = !shouldRender; + } + } + + /// + /// Gets or sets whether or not the rendering is paused. + /// + public bool IsPaused + { + get => (bool)GetValue(IsPausedProperty); + set => SetValue(IsPausedProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( + nameof(IsPaused), + typeof(bool), + typeof(D2D1AnimatedPixelShaderPanel), + new PropertyMetadata(false, OnIsPausedPropertyChanged)); + + /// + private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d; + bool isPaused = (bool)e.NewValue; + + if (@this.canvasAnimatedControl is { } canvasAnimatedControl) + { + bool shouldRender = @this.IsLoaded && !isPaused && @this.ShaderRunner is ID2D1ShaderRunner; + + canvasAnimatedControl.Paused = !shouldRender; } } } \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml index 116aabab..b2500725 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml @@ -148,7 +148,6 @@ - +