Skip to content

Commit

Permalink
Merge pull request #888 from Sergio0694/dev/observable-property
Browse files Browse the repository at this point in the history
Use '[ObservableProperty]' on partial properties
  • Loading branch information
Sergio0694 authored Dec 21, 2024
2 parents bb7d395 + bc29d9e commit 0f45b09
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>12.0</LangVersion>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
40 changes: 21 additions & 19 deletions samples/ComputeSharp.SwapChain.UI/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ComputeSharp.SwapChain.Core.Enums;
Expand All @@ -25,62 +25,57 @@ public sealed partial class MainViewModel : ObservableObject
/// </summary>
public MainViewModel()
{
this.selectedRenderingMode = RenderingMode.DirectX12;
this.isVerticalSyncEnabled = true;
this.isDynamicResolutionEnabled = true;
this.selectedResolutionScale = 100;
this.selectedComputeShader = ComputeShaderOptions[0];
this.selectedComputeShader.IsSelected = true;
SelectedComputeShader = ComputeShaderOptions[0];
}

/// <summary>
/// Gets or sets the selected rendering mode.
/// </summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))]
private RenderingMode selectedRenderingMode;
public partial RenderingMode SelectedRenderingMode { get; set; } = RenderingMode.DirectX12;

/// <summary>
/// Gets or sets whether the vertical sync is enabled.
/// </summary>
[ObservableProperty]
private bool isVerticalSyncEnabled;
public partial bool IsVerticalSyncEnabled { get; set; } = true;

/// <summary>
/// Gets or sets whether the dynamic resolution is enabled.
/// </summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsResolutionScaleOptionEnabled))]
private bool isDynamicResolutionEnabled;
public partial bool IsDynamicResolutionEnabled { get; set; } = true;

/// <summary>
/// Gets the currently selected resolution scale setting (as percentage value).
/// </summary>
[ObservableProperty]
private int selectedResolutionScale;
public partial int SelectedResolutionScale { get; set; } = 100;

/// <summary>
/// Gets or sets the currently selected compute shader.
/// </summary>
[ObservableProperty]
private ShaderRunnerViewModel selectedComputeShader;
public partial ShaderRunnerViewModel SelectedComputeShader { get; set; }

/// <summary>
/// Gets or sets whether the rendering is currently paused.
/// </summary>
[ObservableProperty]
private bool isRenderingPaused;
public partial bool IsRenderingPaused { get; set; }

/// <summary>
/// Gets the available resolution scaling options (as percentage values).
/// </summary>
public IList<int> ResolutionScaleOptions { get; } = new int[] { 25, 50, 75, 100 };
public ReadOnlyCollection<int> ResolutionScaleOptions { get; } = new((int[])[25, 50, 75, 100]);

/// <summary>
/// Gets the collection of available compute shader.
/// </summary>
public IReadOnlyList<ShaderRunnerViewModel> ComputeShaderOptions { get; } = new ShaderRunnerViewModel[]
{
public ReadOnlyCollection<ShaderRunnerViewModel> ComputeShaderOptions { get; } = new((ShaderRunnerViewModel[])
[
new(
nameof(ColorfulInfinity),
new ShaderRunner<ColorfulInfinity>(static time => new((float)time.TotalSeconds)),
Expand Down Expand Up @@ -118,7 +113,7 @@ public MainViewModel()
nameof(TerracedHills),
new ShaderRunner<TerracedHills>(static time => new((float)time.TotalSeconds)),
new PixelShaderEffect.For<SwapChain.Shaders.D2D1.TerracedHills>(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))),
};
]);

/// <summary>
/// Checks whether the resolution scale can currently be expliclty set.
Expand Down Expand Up @@ -153,9 +148,16 @@ private void ToggleRenderingPaused()
}

/// <inheritdoc/>
partial void OnSelectedComputeShaderChanging(ShaderRunnerViewModel? oldValue, ShaderRunnerViewModel newValue)
partial void OnSelectedComputeShaderChanging(ShaderRunnerViewModel oldValue, ShaderRunnerViewModel newValue)
{
oldValue!.IsSelected = false;
// Known issue with partial properties: while the property is marked as not nullable, assigning it in the
// constructor will suppress the warning but cause the setter to be invoked, with 'field' being 'null'.
// As such, as need to check for that scenario here to avoid an exception. Possibly fixed in C# 14.
if (oldValue is not null)
{
oldValue.IsSelected = false;
}

newValue.IsSelected = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public sealed partial class ShaderRunnerViewModel(
/// Gets or sets whether the current shader is selected.
/// </summary>
[ObservableProperty]
private bool isSelected;
public partial bool IsSelected { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="CommunityToolkit.Uwp.Controls.Primitives" Version="8.2.241221-build.1360" />
<PackageReference Include="CommunityToolkit.Uwp.Media" Version="8.2.241221-build.1360" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" Version="8.2.241112-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Media" Version="8.2.241112-preview1" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
Expand Down
2 changes: 1 addition & 1 deletion tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="AutoConstructor" Version="5.6.0" PrivateAssets="all" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.3.2" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
<PackageReference Include="MSTest" Version="3.7.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.1" />
Expand Down

0 comments on commit 0f45b09

Please sign in to comment.