Skip to content

Commit db557c6

Browse files
Initial port of GridSplitter PR from Toolkit Repo
CommunityToolkit/WindowsCommunityToolkit#4083
1 parent b368c9e commit db557c6

21 files changed

+2040
-5
lines changed

labs/SizerBase/src/CommunityToolkit.Labs.WinUI.SizerBase.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
</Description>
1313
<Version>0.0.1</Version>
1414
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<Folder Include="Dependencies\" />
18+
</ItemGroup>
1519
</Project>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.WinUI.UI;
6+
7+
#if !WINAPPSDK
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Controls;
10+
#else
11+
using Microsoft.UI.Xaml;
12+
using Microsoft.UI.Xaml.Controls;
13+
#endif
14+
15+
namespace CommunityToolkit.Labs.WinUI;
16+
17+
// Events for ContentSizer.
18+
public partial class ContentSizer
19+
{
20+
/// <inheritdoc/>
21+
protected override void OnLoaded(RoutedEventArgs e)
22+
{
23+
if (TargetControl == null)
24+
{
25+
TargetControl = this.FindAscendant<FrameworkElement>();
26+
}
27+
}
28+
29+
private double _currentSize;
30+
31+
/// <inheritdoc/>
32+
protected override void OnDragStarting()
33+
{
34+
if (TargetControl != null)
35+
{
36+
_currentSize =
37+
Orientation == Orientation.Vertical ?
38+
TargetControl.ActualWidth :
39+
TargetControl.ActualHeight;
40+
}
41+
}
42+
43+
/// <inheritdoc/>
44+
protected override bool OnDragHorizontal(double horizontalChange)
45+
{
46+
if (TargetControl == null)
47+
{
48+
return true;
49+
}
50+
51+
horizontalChange = IsDragInverted ? -horizontalChange : horizontalChange;
52+
53+
if (!IsValidWidth(TargetControl, _currentSize + horizontalChange, ActualWidth))
54+
{
55+
return false;
56+
}
57+
58+
TargetControl.Width = _currentSize + horizontalChange;
59+
60+
return true;
61+
}
62+
63+
/// <inheritdoc/>
64+
protected override bool OnDragVertical(double verticalChange)
65+
{
66+
if (TargetControl == null)
67+
{
68+
return false;
69+
}
70+
71+
verticalChange = IsDragInverted ? -verticalChange : verticalChange;
72+
73+
if (!IsValidHeight(TargetControl, _currentSize + verticalChange, ActualHeight))
74+
{
75+
return false;
76+
}
77+
78+
TargetControl.Height = _currentSize + verticalChange;
79+
80+
return true;
81+
}
82+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if !WINAPPSDK
6+
using System;
7+
using Windows.UI.Xaml;
8+
using Windows.UI.Xaml.Controls;
9+
#else
10+
using Microsoft.UI.Xaml;
11+
using Microsoft.UI.Xaml.Controls;
12+
#endif
13+
14+
namespace CommunityToolkit.Labs.WinUI;
15+
16+
// Properties for ContentSizer.
17+
public partial class ContentSizer
18+
{
19+
/// <summary>
20+
/// Gets or sets a value indicating whether the <see cref="ContentSizer"/> control is resizing in the opposite direction.
21+
/// </summary>
22+
public bool IsDragInverted
23+
{
24+
get { return (bool)GetValue(IsDragInvertedProperty); }
25+
set { SetValue(IsDragInvertedProperty, value); }
26+
}
27+
28+
/// <summary>
29+
/// Identifies the <see cref="IsDragInverted"/> dependency property.
30+
/// </summary>
31+
public static readonly DependencyProperty IsDragInvertedProperty =
32+
DependencyProperty.Register(nameof(IsDragInverted), typeof(bool), typeof(ContentSizer), new PropertyMetadata(false));
33+
34+
/// <summary>
35+
/// Gets or sets the control that the <see cref="ContentSizer"/> is resizing. Be default, this will be the visual ancestor of the <see cref="ContentSizer"/>.
36+
/// </summary>
37+
public FrameworkElement? TargetControl
38+
{
39+
get { return (FrameworkElement?)GetValue(TargetControlProperty); }
40+
set { SetValue(TargetControlProperty, value); }
41+
}
42+
43+
/// <summary>
44+
/// Identifies the <see cref="TargetControl"/> dependency property.
45+
/// </summary>
46+
public static readonly DependencyProperty TargetControlProperty =
47+
DependencyProperty.Register(nameof(TargetControl), typeof(FrameworkElement), typeof(ContentSizer), new PropertyMetadata(null, OnTargetControlChanged));
48+
49+
private static void OnTargetControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
50+
{
51+
// TODO: Should we do this after the TargetControl is Loaded? (And use ActualWidth?)
52+
// Or should we just do it in the manipulation event if Width is null?
53+
54+
// Check if our width can be manipulated
55+
if (d is SizerBase splitterBase && e.NewValue is FrameworkElement element)
56+
{
57+
// TODO: For Auto ResizeDirection we might want to do detection logic (TBD) here first?
58+
if (splitterBase.Orientation != Orientation.Horizontal && double.IsNaN(element.Width))
59+
{
60+
// We need to set the Width or Height somewhere,
61+
// as if it's NaN we won't be able to manipulate it.
62+
element.Width = element.DesiredSize.Width;
63+
}
64+
65+
if (splitterBase.Orientation != Orientation.Vertical && double.IsNaN(element.Height))
66+
{
67+
element.Height = element.DesiredSize.Height;
68+
}
69+
}
70+
}
71+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if !WINAPPSDK
6+
using Windows.UI.Xaml.Controls;
7+
#else
8+
using Microsoft.UI.Xaml.Controls;
9+
#endif
10+
11+
namespace CommunityToolkit.Labs.WinUI;
12+
13+
/// <summary>
14+
/// The <see cref="ContentSizer"/> is a control which can be used to resize any element, usually its parent. If you are using a <see cref="Grid"/>, use <see cref="GridSplitter"/> instead.
15+
/// </summary>
16+
public partial class ContentSizer : SizerBase
17+
{
18+
}

0 commit comments

Comments
 (0)