Merge pull request #472 from Sergio0694/dev/better-di

Improve DI architecture in UWP sample, remove reflection
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-01-16 18:30:14 -08:00
コミット 9dd6fc13f4
11ファイル変更
+131 -79

検索条件に一致するファイルはありません

-34
ファイルの表示
@@ -1,34 +0,0 @@
# Create the tokens folder
$TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\ServiceTokens"
if(!(Test-Path -Path $TOKENS_FOLDER_RELATIVE_PATH)) {
New-Item -ItemType directory -Path $TOKENS_FOLDER_RELATIVE_PATH
}
$TOKENS_FOLDER_PATH = Resolve-Path -Path $TOKENS_FOLDER_RELATIVE_PATH
# AppCenter tokens
$APPCENTER_TOKENS_NAME = "App.AppCenter.cs"
$APPCENTER_TOKENS_PATH = Join-Path -Path $TOKENS_FOLDER_PATH -ChildPath $APPCENTER_TOKENS_NAME
if (!(Test-Path -Path $APPCENTER_TOKENS_PATH))
{
# Prepare the generated source
$APP_APPCENTER_TEXT =
@"
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ComputeSharp.SwapChain.Uwp;
/// <inheritdoc/>
partial class App
{
/// <summary>
/// The secret token for the AppService connection.
/// </summary>
private const string AppServiceSecret = "";
}
"@.Trim()
# Output the source to the target file
Add-Content $APP_APPCENTER_TEXT -Path $APPCENTER_TOKENS_PATH -NoNewline
}
+40
ファイルの表示
@@ -0,0 +1,40 @@
# Create the service tokens folder
$SERVICE_TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\Generated"
if(!(Test-Path -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH)) {
New-Item -ItemType directory -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH
}
$SERVICE_TOKENS_FOLDER_PATH = Resolve-Path -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH
# Service tokens
$SERVICE_TOKENS_NAME = "ServiceTokens.g.cs"
$SERVICE_TOKENS_PATH = Join-Path -Path $SERVICE_TOKENS_FOLDER_PATH -ChildPath $SERVICE_TOKENS_NAME
if (!(Test-Path -Path $SERVICE_TOKENS_PATH))
{
# Prepare the generated source
$SERVICE_TOKENS_TEXT =
@"
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ComputeSharp.SwapChain.Uwp;
/// <summary>
/// A <see langword="class"/> with some secret service tokens.
/// </summary>
public static class ServiceTokens
{
/// <summary>
/// The secret token for the AppService connection.
/// </summary>
#if RELEASE
internal const string AppService = "<APP_CENTER_TOKEN>";
#else
internal const string AppService = "";
#endif
}
"@.Trim()
# Output the source to the target file
Add-Content $SERVICE_TOKENS_TEXT -Path $SERVICE_TOKENS_PATH -NoNewline
}
+14 -24
ファイルの表示
@@ -1,13 +1,7 @@
#if !DEBUG
using System;
using System.Diagnostics;
#endif
using CommunityToolkit.Mvvm.DependencyInjection;
using ComputeSharp.SwapChain.Core.Services;
using ComputeSharp.SwapChain.Core.ViewModels;
#if !DEBUG
using ComputeSharp.SwapChain.Uwp.Services;
#endif
using ComputeSharp.SwapChain.Uwp.Views;
using Microsoft.Extensions.DependencyInjection;
using Windows.ApplicationModel.Activation;
@@ -31,15 +25,22 @@ sealed partial class App : Application
/// </summary>
public App()
{
this.InitializeComponent();
InitializeComponent();
}
/// <inheritdoc cref="Application.Current"/>
public static new App Current => (App)Application.Current;
/// <summary>
/// Gets the current <see cref="IServiceProvider"/> instance.
/// </summary>
public IServiceProvider Services { get; } = ConfigureServices();
/// <inheritdoc/>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (Window.Current.Content is not MainView)
{
ConfigureServices();
StyleTitleBar();
ExpandViewIntoTitleBar();
@@ -55,26 +56,15 @@ sealed partial class App : Application
/// <summary>
/// Configures the services for the application.
/// </summary>
private static void ConfigureServices()
/// <returns>The <see cref="IServiceProvider"/> instance to use.</returns>
private static IServiceProvider ConfigureServices()
{
ServiceCollection services = new();
#if !DEBUG
if (Guid.TryParse(AppServiceSecret, out _) && !Debugger.IsAttached)
{
services.AddSingleton<IAnalyticsService>(new AppCenterService(AppServiceSecret));
}
else
{
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
}
#else
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
#endif
_ = services.AddAnalyticsService();
_ = services.AddTransient(static services => new MainViewModel(services.GetRequiredService<IAnalyticsService>()));
services.AddTransient<MainViewModel>();
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
return services.BuildServiceProvider();
}
/// <summary>
+20
ファイルの表示
@@ -0,0 +1,20 @@
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ComputeSharp.SwapChain.Uwp;
/// <summary>
/// A <see langword="class"/> with some secret service tokens.
/// </summary>
public static class ServiceTokens
{
/// <summary>
/// The secret token for the AppService connection.
/// </summary>
#if RELEASE
internal const string AppService = "<APP_CENTER_TOKEN>";
#else
internal const string AppService = "";
#endif
}
+3 -2
ファイルの表示
@@ -78,9 +78,10 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Assets\ServiceTokens\App.AppCenter.cs" />
<Compile Include="Assets\Generated\ServiceTokens.g.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\AppCenterService.cs" />
<Compile Include="Services\ServiceCollectionExtensions.cs" />
<Compile Include="Views\MainView.xaml.cs">
<DependentUpon>MainView.xaml</DependentUpon>
</Compile>
@@ -209,6 +210,6 @@
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1</PreBuildEvent>
<PreBuildEvent>powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankServiceTokens.ps1</PreBuildEvent>
</PropertyGroup>
</Project>
+31
ファイルの表示
@@ -0,0 +1,31 @@
using System;
using System.Diagnostics;
using ComputeSharp.SwapChain.Core.Services;
using Microsoft.Extensions.DependencyInjection;
namespace ComputeSharp.SwapChain.Uwp.Services;
/// <summary>
/// Extensions for the <see cref="ServiceCollection"/> type.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the correct <see cref="IAnalyticsService"/> registration to the given <see cref="IServiceCollection"/> instance.
/// </summary>
/// <param name="services">The input <see cref="IServiceCollection"/> instance to use to register services.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
public static IServiceCollection AddAnalyticsService(this IServiceCollection services)
{
if (Guid.TryParse(ServiceTokens.AppService, out _) && !Debugger.IsAttached)
{
_ = services.AddSingleton<IAnalyticsService>(new AppCenterService(ServiceTokens.AppService));
}
else
{
_ = services.AddSingleton<IAnalyticsService>(new DebugAnalyticsService());
}
return services;
}
}
+10 -8
ファイルの表示
@@ -1,9 +1,9 @@
using System;
using CommunityToolkit.Mvvm.DependencyInjection;
using ComputeSharp.SwapChain.Core.Constants;
using ComputeSharp.SwapChain.Core.Services;
using ComputeSharp.SwapChain.Core.ViewModels;
using ComputeSharp.Uwp;
using Microsoft.Extensions.DependencyInjection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
@@ -17,8 +17,9 @@ public sealed partial class MainView : UserControl
{
public MainView()
{
this.InitializeComponent();
this.DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
InitializeComponent();
DataContext = App.Current.Services.GetRequiredService<MainViewModel>();
}
/// <summary>
@@ -29,16 +30,17 @@ public sealed partial class MainView : UserControl
// Opens the shader selection panel
private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(Event.OpenShaderSelectionPanel);
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(Event.OpenShaderSelectionPanel);
_ = this.Root.Resources.Remove(nameof(this.ShaderSelectionPanel));
_ = this.Root.Resources.Remove("ShaderSelectionPanel");
this.Root.Children.Add(this.ShaderSelectionPanel);
}
// Hides the shader selection panel
private void ShaderSelectionPanel_Tapped(object sender, TappedRoutedEventArgs e)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(Event.CloseShaderSelectionPanel);
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(Event.CloseShaderSelectionPanel);
_ = this.Root.Children.Remove(this.ShaderSelectionPanel);
}
@@ -52,7 +54,7 @@ public sealed partial class MainView : UserControl
// Logs rendering failed in the main panel
private void MainShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel));
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnMainPanel));
this.RenderingErrorInfoBar.IsOpen = true;
}
@@ -60,7 +62,7 @@ public sealed partial class MainView : UserControl
// Logs rendering failed in a secondary panel
private void SelectionShaderPanel_RenderingFailed(AnimatedComputeShaderPanel sender, RenderingFailedEventArgs args)
{
Ioc.Default.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel));
App.Current.Services.GetRequiredService<IAnalyticsService>().Log(args.Exception, (nameof(Error), Error.RenderingFailedOnSelectionPanel));
this.RenderingErrorInfoBar.IsOpen = true;
}
+4 -2
ファイルの表示
@@ -20,8 +20,9 @@ public sealed partial class MainWindow : Window
public MainWindow()
{
this.InitializeComponent();
this.ExtendsContentIntoTitleBar = true;
InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(this.TitleBarRectangle);
@@ -50,6 +51,7 @@ public sealed partial class MainWindow : Window
private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e)
{
_ = this.Root.Resources.Remove("ShaderSelectionPanel");
this.Root.Children.Add(this.ShaderSelectionPanel);
}
+4 -4
ファイルの表示
@@ -47,10 +47,10 @@ public sealed partial class AnimatedComputeShaderPanel : SwapChainPanel, IDispos
{
this.swapChainManager = new SwapChainManager<AnimatedComputeShaderPanel>(this, device);
this.Loaded += AnimatedComputeShaderPanel_Loaded;
this.Unloaded += AnimatedComputeShaderPanel_Unloaded;
this.SizeChanged += AnimatedComputeShaderPanel_SizeChanged;
this.CompositionScaleChanged += AnimatedComputeShaderPanel_CompositionScaleChanged;
Loaded += AnimatedComputeShaderPanel_Loaded;
Unloaded += AnimatedComputeShaderPanel_Unloaded;
SizeChanged += AnimatedComputeShaderPanel_SizeChanged;
CompositionScaleChanged += AnimatedComputeShaderPanel_CompositionScaleChanged;
}
// Initializes the swap chain and starts the render thread
+4 -4
ファイルの表示
@@ -47,10 +47,10 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel, IDisposable
{
this.swapChainManager = new SwapChainManager<ComputeShaderPanel>(this, device);
this.Loaded += ComputeShaderPanel_Loaded;
this.Unloaded += ComputeShaderPanel_Unloaded;
this.SizeChanged += ComputeShaderPanel_SizeChanged;
this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged;
Loaded += ComputeShaderPanel_Loaded;
Unloaded += ComputeShaderPanel_Unloaded;
SizeChanged += ComputeShaderPanel_SizeChanged;
CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged;
}
// Initializes the swap chain and starts the render thread
+1 -1
ファイルの表示
@@ -124,7 +124,7 @@ public abstract unsafe partial class TransferTexture2D<T> : IReferenceTrackedObj
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
using ReferenceTracker.Lease _0 = this.GetReferenceTracker().GetLease();
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
return new(this.mappedData, Width, Height, (int)this.d3D12PlacedSubresourceFootprint.Footprint.RowPitch);
}