Skip to content

Commit 31297b0

Browse files
Get ResizeElementAdorner sizing to work, though Adorner isn't updating alongside
1 parent cb59341 commit 31297b0

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

components/Adorners/samples/ResizeElement/ResizeElementAdornerSample.xaml renamed to components/Adorners/samples/ResizeElement/ResizeElementAdornerCanvasSample.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- 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. -->
2-
<Page x:Class="AdornersExperiment.Samples.ResizeElement.ResizeElementAdornerSample"
2+
<Page x:Class="AdornersExperiment.Samples.ResizeElement.ResizeElementAdornerCanvasSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:adorners="using:CommunityToolkit.WinUI.Adorners"

components/Adorners/samples/ResizeElement/ResizeElementAdornerSample.xaml.cs renamed to components/Adorners/samples/ResizeElement/ResizeElementAdornerCanvasSample.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace AdornersExperiment.Samples.ResizeElement;
66

77
[ToolkitSampleBoolOption("IsAdornerVisible", false, Title = "Is Adorner Visible")]
8-
[ToolkitSample(id: nameof(ResizeElementAdornerSample), "Resize Element Adorner", description: "A sample for showing how to use an Adorner for resizing an element.")]
9-
public sealed partial class ResizeElementAdornerSample : Page
8+
[ToolkitSample(id: nameof(ResizeElementAdornerCanvasSample), "Resize Element Adorner on Canvas", description: "A sample for showing how to use an Adorner for resizing an element within a Canvas.")]
9+
public sealed partial class ResizeElementAdornerCanvasSample : Page
1010
{
11-
public ResizeElementAdornerSample()
11+
public ResizeElementAdornerCanvasSample()
1212
{
1313
this.InitializeComponent();
1414
}

components/Adorners/samples/ResizeElementAdorner.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ The `ResizeElementAdorner` provides resizing functionality to any `FrameworkElem
2020

2121
The `ResizeElementAdorner` can be attached to any element and displayed to allow a user to resize the adorned element by dragging the resize handles.
2222

23-
> [!SAMPLE ResizeElementAdornerSample]
23+
> [!SAMPLE ResizeElementAdornerCanvasSample]
24+
25+
This can be done above within a `Canvas` layout control, or with general layout using Margins as well:
26+
27+
// TODO: Add Margin example here

components/Adorners/src/ResizeElement/ResizeElementAdorner.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ protected override void OnApplyTemplate()
5656
BottomRightThumbPart = GetTemplateChild(nameof(BottomRightThumbPart)) as ResizeThumb;
5757

5858
// OnApplyTemplate can be called after OnAttached, especially if the Adorner isn't initially visible, so we need to re-apply the TargetControl here.
59-
OnAttached();
59+
if (AdornedElement is not null)
60+
{
61+
// Guard this incase we're getting removed from the visual tree...
62+
// Not sure if this is a bug in the Adorner lifecycle or not or specific to how we've set this up here.
63+
OnAttached();
64+
}
6065
}
6166

6267
/// <inheritdoc/>

components/Adorners/src/ResizeElement/Thumb/ResizePositionMode.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ public enum ResizePositionMode
1818
/// Resize using <see cref="FrameworkElement.Margin"/>'s Top and Left values.
1919
/// </summary>
2020
MarginTopLeft,
21+
22+
// TODO: MarginBottomRight could be added in the future. Not sure of alternate anchor points are useful? e.g. TopRight, BottomLeft
23+
// Alternate anchor points would require more complex calculations during resize to determine when to change the Width/Height of the element.
2124
}

components/Adorners/src/ResizeElement/Thumb/ResizeThumb.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,27 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
7676
}
7777
#endif
7878

79+
// If we're adjusting the opposite boundary then we need to invert the change values.
80+
if (Direction == ResizeDirection.Right || Direction == ResizeDirection.TopRight || Direction == ResizeDirection.BottomRight)
81+
{
82+
horizontalChange *= -1;
83+
}
84+
85+
if (Direction == ResizeDirection.Bottom || Direction == ResizeDirection.BottomLeft || Direction == ResizeDirection.BottomRight)
86+
{
87+
verticalChange *= -1;
88+
}
89+
7990
// Apply the changes to the target control
8091
if (TargetControl != null)
8192
{
8293
// Keep track if we became constrained in a direction and don't adjust position if we didn't update the size.
8394
bool adjustWidth = false;
8495
bool adjustHeight = false;
8596

86-
// Calculate the new size
87-
var newWidth = _originalSize?.Width ?? 0 + horizontalChange;
88-
var newHeight = _originalSize?.Height ?? 0 + verticalChange;
97+
// Calculate the new size (Note: This is the opposite direction to expand the opposing boundary of the thumb)
98+
var newWidth = (_originalSize?.Width ?? 0) - horizontalChange;
99+
var newHeight = (_originalSize?.Height ?? 0) - verticalChange;
89100

90101
if (Direction != ResizeDirection.Top && Direction != ResizeDirection.Bottom)
91102
{

0 commit comments

Comments
 (0)