-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial port of GridSplitter PR from Toolkit Repo
- Loading branch information
1 parent
19e3bfb
commit b1c9d1a
Showing
21 changed files
with
2,040 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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. | ||
|
||
using CommunityToolkit.WinUI.UI; | ||
|
||
#if !WINAPPSDK | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
#else | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
#endif | ||
|
||
namespace CommunityToolkit.Labs.WinUI; | ||
|
||
// Events for ContentSizer. | ||
public partial class ContentSizer | ||
{ | ||
/// <inheritdoc/> | ||
protected override void OnLoaded(RoutedEventArgs e) | ||
{ | ||
if (TargetControl == null) | ||
{ | ||
TargetControl = this.FindAscendant<FrameworkElement>(); | ||
} | ||
} | ||
|
||
private double _currentSize; | ||
|
||
/// <inheritdoc/> | ||
protected override void OnDragStarting() | ||
{ | ||
if (TargetControl != null) | ||
{ | ||
_currentSize = | ||
Orientation == Orientation.Vertical ? | ||
TargetControl.ActualWidth : | ||
TargetControl.ActualHeight; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override bool OnDragHorizontal(double horizontalChange) | ||
{ | ||
if (TargetControl == null) | ||
{ | ||
return true; | ||
} | ||
|
||
horizontalChange = IsDragInverted ? -horizontalChange : horizontalChange; | ||
|
||
if (!IsValidWidth(TargetControl, _currentSize + horizontalChange, ActualWidth)) | ||
{ | ||
return false; | ||
} | ||
|
||
TargetControl.Width = _currentSize + horizontalChange; | ||
|
||
return true; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override bool OnDragVertical(double verticalChange) | ||
{ | ||
if (TargetControl == null) | ||
{ | ||
return false; | ||
} | ||
|
||
verticalChange = IsDragInverted ? -verticalChange : verticalChange; | ||
|
||
if (!IsValidHeight(TargetControl, _currentSize + verticalChange, ActualHeight)) | ||
{ | ||
return false; | ||
} | ||
|
||
TargetControl.Height = _currentSize + verticalChange; | ||
|
||
return true; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
labs/SizerBase/src/ContentSizer/ContentSizer.Properties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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. | ||
|
||
#if !WINAPPSDK | ||
using System; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
#else | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
#endif | ||
|
||
namespace CommunityToolkit.Labs.WinUI; | ||
|
||
// Properties for ContentSizer. | ||
public partial class ContentSizer | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether the <see cref="ContentSizer"/> control is resizing in the opposite direction. | ||
/// </summary> | ||
public bool IsDragInverted | ||
{ | ||
get { return (bool)GetValue(IsDragInvertedProperty); } | ||
set { SetValue(IsDragInvertedProperty, value); } | ||
} | ||
|
||
/// <summary> | ||
/// Identifies the <see cref="IsDragInverted"/> dependency property. | ||
/// </summary> | ||
public static readonly DependencyProperty IsDragInvertedProperty = | ||
DependencyProperty.Register(nameof(IsDragInverted), typeof(bool), typeof(ContentSizer), new PropertyMetadata(false)); | ||
|
||
/// <summary> | ||
/// 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"/>. | ||
/// </summary> | ||
public FrameworkElement? TargetControl | ||
{ | ||
get { return (FrameworkElement?)GetValue(TargetControlProperty); } | ||
set { SetValue(TargetControlProperty, value); } | ||
} | ||
|
||
/// <summary> | ||
/// Identifies the <see cref="TargetControl"/> dependency property. | ||
/// </summary> | ||
public static readonly DependencyProperty TargetControlProperty = | ||
DependencyProperty.Register(nameof(TargetControl), typeof(FrameworkElement), typeof(ContentSizer), new PropertyMetadata(null, OnTargetControlChanged)); | ||
|
||
private static void OnTargetControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
// TODO: Should we do this after the TargetControl is Loaded? (And use ActualWidth?) | ||
// Or should we just do it in the manipulation event if Width is null? | ||
|
||
// Check if our width can be manipulated | ||
if (d is SizerBase splitterBase && e.NewValue is FrameworkElement element) | ||
{ | ||
// TODO: For Auto ResizeDirection we might want to do detection logic (TBD) here first? | ||
if (splitterBase.Orientation != Orientation.Horizontal && double.IsNaN(element.Width)) | ||
{ | ||
// We need to set the Width or Height somewhere, | ||
// as if it's NaN we won't be able to manipulate it. | ||
element.Width = element.DesiredSize.Width; | ||
} | ||
|
||
if (splitterBase.Orientation != Orientation.Vertical && double.IsNaN(element.Height)) | ||
{ | ||
element.Height = element.DesiredSize.Height; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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. | ||
|
||
#if !WINAPPSDK | ||
using Windows.UI.Xaml.Controls; | ||
#else | ||
using Microsoft.UI.Xaml.Controls; | ||
#endif | ||
|
||
namespace CommunityToolkit.Labs.WinUI; | ||
|
||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
public partial class ContentSizer : SizerBase | ||
{ | ||
} |
Oops, something went wrong.