Skip to content

Commit

Permalink
Add ProtectedCursor code for WinUI 3 - NOTE CRASHES
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-hawker committed May 10, 2022
1 parent fbfbda6 commit fe30b87
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion common/Labs.MultiTarget.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@

<ItemGroup>
<PackageReference Condition="'$(TargetFramework)' == '$(UwpTargetFramework)'" Include="Microsoft.UI.Xaml" Version="2.7.0" />
<PackageReference Condition="'$(TargetFramework)' == '$(WinAppSdkTargetFramework)'" Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageReference Condition="'$(TargetFramework)' == '$(WinAppSdkTargetFramework)'" Include="Microsoft.WindowsAppSDK" Version="1.1.0-preview3" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion common/Labs.WinAppSdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0-preview3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0-preview3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0-preview3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down
28 changes: 22 additions & 6 deletions labs/SizerBase/src/SizerBase.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#if !WINAPPSDK
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using CursorEnum = Windows.UI.Core.CoreCursorType;
#else
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using CursorEnum = Microsoft.UI.Input.InputSystemCursorShape;
#endif

namespace CommunityToolkit.Labs.WinUI;
Expand All @@ -19,20 +22,22 @@ namespace CommunityToolkit.Labs.WinUI;
/// </summary>
public partial class SizerBase : Control
{
private CursorEnum _cursorToUse = CursorEnum.SizeWestEast;

/// <summary>
/// Gets or sets the cursor to use when hovering over the gripper bar.
/// Gets or sets the cursor to use when hovering over the gripper bar. If left as <c>null</c>, the control will manage the cursor automatically based on the <see cref="Orientation"/> property value.
/// </summary>
public CoreCursorType Cursor
public CursorEnum Cursor
{
get { return (CoreCursorType)GetValue(CursorProperty); }
get { return (CursorEnum)GetValue(CursorProperty); }
set { SetValue(CursorProperty, value); }
}

/// <summary>
/// Identifies the <see cref="Cursor"/> dependency property.
/// </summary>
public static readonly DependencyProperty CursorProperty =
DependencyProperty.Register(nameof(Cursor), typeof(CoreCursorType), typeof(SizerBase), new PropertyMetadata(CoreCursorType.SizeWestEast));
DependencyProperty.Register(nameof(Cursor), typeof(CursorEnum), typeof(SizerBase), new PropertyMetadata(null));

/// <summary>
/// Gets or sets the incremental amount of change for draging with the mouse or touch of a sizer control. Effectively a snapping increment for changes. The default is 1.
Expand Down Expand Up @@ -95,8 +100,19 @@ private static void OnOrientationPropertyChanged(DependencyObject d, DependencyP
{
if (d is SizerBase gripper)
{
// TODO: For WinUI 3, we will just be setting the ProtectedCursor property directly.
gripper.Cursor = gripper.Orientation == Orientation.Vertical ? CoreCursorType.SizeWestEast : CoreCursorType.SizeNorthSouth;
gripper._cursorToUse = gripper.Orientation == Orientation.Vertical ? CursorEnum.SizeWestEast : CursorEnum.SizeNorthSouth;
#if WINAPPSDK
var cursor = gripper.ReadLocalValue(CursorProperty);
if (cursor == DependencyProperty.UnsetValue)
{
cursor = gripper._cursorToUse;
}
var scursor = InputSystemCursor.Create(gripper._cursorToUse);
gripper.ProtectedCursor = scursor;
#else
// On UWP, we use the extension in XAML to control this behavior, so we'll update it here.
gripper.Cursor = gripper._cursorToUse;
#endif
}
}
}
4 changes: 4 additions & 0 deletions labs/SizerBase/src/SizerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ protected override void OnApplyTemplate()

// Trigger initial state transition based on if we're Enabled or not currently.
SizerBase_IsEnabledChanged(this, null!);
#if WINAPPSDK
// On WinAppSDK, we'll trigger this to setup the initial ProtectedCursor value.
OnOrientationPropertyChanged(this, null!);
#endif
}

private void SizerBase_Loaded(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit fe30b87

Please sign in to comment.