Skip to content

Commit

Permalink
Initial port of GridSplitter PR from Toolkit Repo
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-hawker committed May 10, 2022
1 parent 19e3bfb commit b1c9d1a
Show file tree
Hide file tree
Showing 21 changed files with 2,040 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
</Description>
<Version>0.0.1</Version>
</PropertyGroup>

<ItemGroup>
<Folder Include="Dependencies\" />
</ItemGroup>
</Project>
82 changes: 82 additions & 0 deletions labs/SizerBase/src/ContentSizer/ContentSizer.Events.cs
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 labs/SizerBase/src/ContentSizer/ContentSizer.Properties.cs
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;
}
}
}
}
18 changes: 18 additions & 0 deletions labs/SizerBase/src/ContentSizer/ContentSizer.cs
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
{
}
Loading

0 comments on commit b1c9d1a

Please sign in to comment.