Skip to content

Commit

Permalink
Fixes #388 SizerBase Orientation Property
Browse files Browse the repository at this point in the history
Removes Mouse Extension from XAML Template and does in code-behind as to remove the need to set the Cursor property directly via binding
By setting the Cursor DependencyProperty we were losing our detection mechanism, now if it's set it's an explicit override, otherwise we use our logic based on Orientation property
Also, switches to new CommunityToolkit.*.Extensions Dependency vs. old copies of Tree helpers
Fixes some doc typos
Bumps version, tested on UWP, WASDK, and Uno.UI/WASM
  • Loading branch information
michael-hawker committed Apr 15, 2023
1 parent 26be61d commit 9261a43
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 474 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ToolkitComponentName>SizerBase</ToolkitComponentName>
<Description>This package contains SizerBase.</Description>
<Version>0.0.4</Version>
<Version>0.0.5</Version>

<!-- Rns suffix is required for namespaces shared across projects. See https://github.com/CommunityToolkit/Labs-Windows/issues/152 -->
<RootNamespace>CommunityToolkit.Labs.WinUI.SizerBaseRns</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Labs.WinUI.SizerBaseLocal;
using CommunityToolkit.WinUI;

namespace CommunityToolkit.Labs.WinUI;

Expand Down
26 changes: 8 additions & 18 deletions components/SizerBase/src/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,13 @@
For UWP / WinAppSDK / Uno packages, place the package references here.
-->
<Project>
<!-- WinUI 2 / UWP -->
<ItemGroup Condition="'$(IsUwp)' == 'true'">
<!-- <PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.2"/> -->
</ItemGroup>
<!-- WinUI 2 / UWP / Uno -->
<ItemGroup Condition="'$(IsUwp)' == 'true' OR ('$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '2')">
<PackageReference Include="CommunityToolkit.Uwp.Extensions" Version="8.0.0-beta.1"/>
</ItemGroup>

<!-- WinUI 2 / Uno -->
<ItemGroup Condition="'$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '2'">
<!-- <PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls.Primitives" Version="7.1.11"/> -->
</ItemGroup>

<!-- WinUI 3 / WinAppSdk -->
<ItemGroup Condition="'$(IsWinAppSdk)' == 'true'">
<!-- <PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.2"/> -->
</ItemGroup>

<!-- WinUI 3 / Uno -->
<ItemGroup Condition="'$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '3'">
<!-- <PackageReference Include="Uno.CommunityToolkit.WinUI.UI.Controls.Primitives" Version="7.1.100-dev.15.g12261e2626"/> -->
</ItemGroup>
<!-- WinUI 3 / WinAppSdk / Uno -->
<ItemGroup Condition="'$(IsWinAppSdk)' == 'true' OR ('$(IsUno)' == 'true' AND '$(WinUIMajorVersion)' == '3')">
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.0.0-beta.1"/>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion components/SizerBase/src/SizerBase.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected override void OnManipulationStarting(ManipulationStartingRoutedEventAr
/// <inheritdoc />
protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
{
// We use Trancate here to provide 'snapping' points with the DragIncrement property
// We use Truncate here to provide 'snapping' points with the DragIncrement property
// It works for both our negative and positive values, as otherwise we'd need to use
// Ceiling when negative and Floor when positive to maintain the correct behavior.
var horizontalChange =
Expand Down
37 changes: 21 additions & 16 deletions components/SizerBase/src/SizerBase.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.WinUI;

#if !WINAPPSDK
using CursorEnum = Windows.UI.Core.CoreCursorType;
#else
Expand All @@ -17,7 +19,7 @@ namespace CommunityToolkit.Labs.WinUI;
public partial class SizerBase : Control
{
/// <summary>
/// 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.
/// 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 (default).
/// </summary>
public CursorEnum Cursor
{
Expand All @@ -32,13 +34,13 @@ public CursorEnum Cursor
DependencyProperty.Register(nameof(Cursor), typeof(CursorEnum), typeof(SizerBase), new PropertyMetadata(null, OnOrientationPropertyChanged));

/// <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.
/// Gets or sets the incremental amount of change for dragging with the mouse or touch of a sizer control. Effectively a snapping increment for changes. The default is 1.
/// </summary>
/// <example>
/// For instance, if the DragIncrement is set to 16. Then when a component is resized with the sizer, it will only increase or decrease in size in that increment. I.e. -16, 0, 16, 32, 48, etc...
/// </example>
/// <remarks>
/// This value is indepedent of the <see cref="KeyboardIncrement"/> property. If you need to provide consistent snapping when moving regardless of input device, set these properties to the same value.
/// This value is independent of the <see cref="KeyboardIncrement"/> property. If you need to provide consistent snapping when moving regardless of input device, set these properties to the same value.
/// </remarks>
public double DragIncrement
{
Expand Down Expand Up @@ -92,35 +94,38 @@ private static void OnOrientationPropertyChanged(DependencyObject d, DependencyP
{
if (d is SizerBase gripper)
{
CursorEnum cursorToUse = gripper.Orientation == Orientation.Vertical ? CursorEnum.SizeWestEast : CursorEnum.SizeNorthSouth;
CursorEnum cursorByOrientation = gripper.Orientation == Orientation.Vertical ? CursorEnum.SizeWestEast : CursorEnum.SizeNorthSouth;

// See if there's been a cursor override, otherwise we'll pick
var cursor = gripper.ReadLocalValue(CursorProperty);
if (cursor == DependencyProperty.UnsetValue || cursor == null)
{
cursor = cursorToUse;

// On UWP, we use the extension in XAML to control this behavior,
// so we'll update it here (and maintain binding).
// We'll keep it in-sync to maintain behavior for WinUI 3 as well.
gripper.SetValue(CursorProperty, cursor);
cursor = cursorByOrientation;
}

// We return here, as the Cursor will trigger this function again anyway to set for WinUI 3
return;
#if !WINAPPSDK
// On UWP, we use our XAML extension to control this behavior,
// so we'll update it here (and maintain any cursor override).
if (cursor is CursorEnum cursorValue)
{
FrameworkElementExtensions.SetCursor(gripper, cursorValue);
}

return;
#endif

// TODO: [UNO] Only supported on certain platforms
// See ProtectedCursor here: https://github.com/unoplatform/uno/blob/3fe3862b270b99dbec4d830b547942af61b1a1d9/src/Uno.UI/UI/Xaml/UIElement.cs#L1015-L1023
#if WINAPPSDK && !HAS_UNO
// Need to wait until we're at least applying template step of loading before setting Cursor
// See https://github.com/microsoft/microsoft-ui-xaml/issues/7062
if (gripper._applyingTemplate &&
cursor is CursorEnum cursorToSet &&
if (gripper._appliedTemplate &&
cursor is CursorEnum cursorValue &&
(gripper.ProtectedCursor == null ||
(gripper.ProtectedCursor is InputSystemCursor current &&
current.CursorShape != cursorToSet)))
current.CursorShape != cursorValue)))
{
gripper.ProtectedCursor = InputSystemCursor.Create(cursorToSet);
gripper.ProtectedCursor = InputSystemCursor.Create(cursorValue);
}
#endif
}
Expand Down
12 changes: 6 additions & 6 deletions components/SizerBase/src/SizerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected virtual void OnLoaded(RoutedEventArgs e)

/// <summary>
/// Called when the <see cref="SizerBase"/> control starts to be dragged by the user.
/// Implementor should record current state of manipulated target at this point in time.
/// Implementer should record current state of manipulated target at this point in time.
/// They will receive the cumulative change in <see cref="OnDragHorizontal(double)"/> or
/// <see cref="OnDragVertical(double)"/> based on the <see cref="Orientation"/> property.
/// </summary>
Expand All @@ -42,7 +42,7 @@ protected virtual void OnLoaded(RoutedEventArgs e)
/// manipulation. This method will be used regardless of input device. It will already
/// be adjusted for RightToLeft <see cref="FlowDirection"/> of the containing
/// layout/settings. It will also already account for any settings such as
/// <see cref="DragIncrement"/> or <see cref="KeyboardIncrement"/>. The implementor
/// <see cref="DragIncrement"/> or <see cref="KeyboardIncrement"/>. The implementer
/// just needs to use the provided value to manipulate their baseline stored
/// in <see cref="OnDragStarting"/> to provide the desired change.
/// </remarks>
Expand All @@ -57,7 +57,7 @@ protected virtual void OnLoaded(RoutedEventArgs e)
/// The value provided here is the cumulative change from the beginning of the
/// manipulation. This method will be used regardless of input device. It will also
/// already account for any settings such as <see cref="DragIncrement"/> or
/// <see cref="KeyboardIncrement"/>. The implementor just needs
/// <see cref="KeyboardIncrement"/>. The implementer just needs
/// to use the provided value to manipulate their baseline stored
/// in <see cref="OnDragStarting"/> to provide the desired change.
/// </remarks>
Expand All @@ -83,7 +83,7 @@ protected override AutomationPeer OnCreateAutomationPeer()
// On Uno the ProtectedCursor isn't supported yet, so we don't need this value.
#if WINAPPSDK && !HAS_UNO
// Used to track when we're in the OnApplyTemplateStep to change ProtectedCursor value.
private bool _applyingTemplate = false;
private bool _appliedTemplate = false;
#endif

/// <inheritdoc/>
Expand Down Expand Up @@ -115,9 +115,9 @@ protected override void OnApplyTemplate()
SizerBase_IsEnabledChanged(this, null!);
#if WINAPPSDK && !HAS_UNO
// On WinAppSDK, we'll trigger this to setup the initial ProtectedCursor value.
_applyingTemplate = true;
_appliedTemplate = true;
#endif
// On UWP, we'll check the current Orientation and set the Cursor property to use here still.
// Ensure we have the proper cursor value setup, as we can only set now for WinUI 3
OnOrientationPropertyChanged(this, null!);
}

Expand Down
3 changes: 1 addition & 2 deletions components/SizerBase/src/SizerBase.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.Labs.WinUI"
Expand Down Expand Up @@ -49,7 +49,6 @@
<Setter.Value>
<ControlTemplate TargetType="controls:SizerBase">
<Grid x:Name="RootGrid"
local:FrameworkElementExtensions.Cursor="{TemplateBinding Cursor}"
Background="{TemplateBinding Background}">

<!-- Note: These align with Thumb -->
Expand Down
Loading

0 comments on commit 9261a43

Please sign in to comment.