upgraded waterfallpanel
This commit is contained in:
1 parent
1ad4b9a0b9
commit
80cb46a5f8
9 files changed
+196
-114
No files matched your search
@@ -2,6 +2,8 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Expression.Drawing;
|
||||
using HandyControl.Tools;
|
||||
|
||||
namespace HandyControl.Controls
|
||||
{
|
||||
@@ -19,9 +21,30 @@ namespace HandyControl.Controls
|
||||
|
||||
private static bool IsGroupsValid(object value) => (int) value >= 1;
|
||||
|
||||
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
|
||||
"Orientation", typeof(Orientation), typeof(WaterfallPanel), new FrameworkPropertyMetadata(
|
||||
Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsMeasure));
|
||||
public static readonly DependencyProperty AutoGroupProperty = DependencyProperty.Register(
|
||||
"AutoGroup", typeof(bool), typeof(WaterfallPanel), new FrameworkPropertyMetadata(
|
||||
ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsMeasure));
|
||||
|
||||
public bool AutoGroup
|
||||
{
|
||||
get => (bool) GetValue(AutoGroupProperty);
|
||||
set => SetValue(AutoGroupProperty, ValueBoxes.BooleanBox(value));
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty DesiredLengthProperty = DependencyProperty.Register(
|
||||
"DesiredLength", typeof(double), typeof(WaterfallPanel), new FrameworkPropertyMetadata(ValueBoxes.Double0Box,
|
||||
FrameworkPropertyMetadataOptions.AffectsMeasure), ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
|
||||
|
||||
public double DesiredLength
|
||||
{
|
||||
get => (double) GetValue(DesiredLengthProperty);
|
||||
set => SetValue(DesiredLengthProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty OrientationProperty =
|
||||
StackPanel.OrientationProperty.AddOwner(typeof(WaterfallPanel),
|
||||
new FrameworkPropertyMetadata(Orientation.Horizontal,
|
||||
FrameworkPropertyMetadataOptions.AffectsMeasure));
|
||||
|
||||
public Orientation Orientation
|
||||
{
|
||||
@@ -29,56 +52,98 @@ namespace HandyControl.Controls
|
||||
set => SetValue(OrientationProperty, value);
|
||||
}
|
||||
|
||||
private int CaculateGroupCount(Orientation orientation, PanelUvSize size)
|
||||
{
|
||||
if (!AutoGroup)
|
||||
{
|
||||
return Groups;
|
||||
}
|
||||
|
||||
var itemLength = DesiredLength;
|
||||
|
||||
if (MathHelper.IsVerySmall(itemLength))
|
||||
{
|
||||
return Groups;
|
||||
}
|
||||
|
||||
return (int) (size.U / itemLength);
|
||||
}
|
||||
|
||||
protected override Size ArrangeOverride(Size finalSize)
|
||||
{
|
||||
var orientation = Orientation;
|
||||
var uvConstraint = new PanelUvSize(orientation, finalSize);
|
||||
|
||||
var groups = CaculateGroupCount(orientation, uvConstraint);
|
||||
if (groups < 1)
|
||||
{
|
||||
return finalSize;
|
||||
}
|
||||
|
||||
var vArr = new double[groups].ToList();
|
||||
var itemU = uvConstraint.U / groups;
|
||||
|
||||
var children = InternalChildren;
|
||||
for (int i = 0, count = children.Count; i < count; i++)
|
||||
{
|
||||
var child = children[i];
|
||||
if (child == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var minIndex = vArr.IndexOf(vArr.Min());
|
||||
var minV = vArr[minIndex];
|
||||
var childUvSize = new PanelUvSize(orientation, child.DesiredSize);
|
||||
var childSize = new PanelUvSize(orientation, itemU, childUvSize.V);
|
||||
var childRectSize = new PanelUvSize(orientation, minIndex * itemU, minV);
|
||||
|
||||
child.Arrange(new Rect(new Point(childRectSize.U, childRectSize.V), childSize.ScreenSize));
|
||||
vArr[minIndex] = minV + childUvSize.V;
|
||||
}
|
||||
|
||||
return finalSize;
|
||||
}
|
||||
|
||||
protected override Size MeasureOverride(Size constraint)
|
||||
{
|
||||
var groups = Groups;
|
||||
var orientation = Orientation;
|
||||
var uvConstraint = new PanelUvSize(orientation, constraint);
|
||||
|
||||
var groups = CaculateGroupCount(orientation, uvConstraint);
|
||||
if (groups < 1)
|
||||
{
|
||||
return constraint;
|
||||
}
|
||||
|
||||
var vArr = new double[groups].ToList();
|
||||
var itemU = uvConstraint.U / groups;
|
||||
if (double.IsNaN(itemU) || double.IsInfinity(itemU))
|
||||
{
|
||||
return constraint;
|
||||
}
|
||||
|
||||
if (groups < 1) return constraint;
|
||||
var children = InternalChildren;
|
||||
Size panelSize;
|
||||
|
||||
if (Orientation == Orientation.Horizontal)
|
||||
for (int i = 0, count = children.Count; i < count; i++)
|
||||
{
|
||||
var heightArr = new double[groups].ToList();
|
||||
var itemWidth = constraint.Width / groups;
|
||||
if (double.IsNaN(itemWidth) || double.IsInfinity(itemWidth)) return constraint;
|
||||
|
||||
for (int i = 0, count = children.Count; i < count; i++)
|
||||
var child = children[i];
|
||||
if (child == null)
|
||||
{
|
||||
var child = children[i];
|
||||
if (child == null) continue;
|
||||
|
||||
child.Measure(constraint);
|
||||
var minIndex = heightArr.IndexOf(heightArr.Min());
|
||||
var minY = heightArr[minIndex];
|
||||
child.Arrange(new Rect(new Point(minIndex * itemWidth, minY), new Size(itemWidth, child.DesiredSize.Height)));
|
||||
|
||||
heightArr[minIndex] = minY + child.DesiredSize.Height;
|
||||
continue;
|
||||
}
|
||||
panelSize = new Size(constraint.Width, heightArr.Max());
|
||||
}
|
||||
else
|
||||
{
|
||||
var widthArr = new double[groups].ToList();
|
||||
var itemHeight = constraint.Height / groups;
|
||||
if (double.IsNaN(itemHeight) || double.IsInfinity(itemHeight)) return constraint;
|
||||
|
||||
for (int i = 0, count = children.Count; i < count; i++)
|
||||
{
|
||||
var child = children[i];
|
||||
if (child == null) continue;
|
||||
child.Measure(constraint);
|
||||
|
||||
child.Measure(constraint);
|
||||
var minIndex = widthArr.IndexOf(widthArr.Min());
|
||||
var minX = widthArr[minIndex];
|
||||
child.Arrange(new Rect(new Point(minX, minIndex * itemHeight), new Size(child.DesiredSize.Width, itemHeight)));
|
||||
var sz = new PanelUvSize(orientation, child.DesiredSize);
|
||||
var minIndex = vArr.IndexOf(vArr.Min());
|
||||
var minV = vArr[minIndex];
|
||||
|
||||
widthArr[minIndex] = minX + child.DesiredSize.Width;
|
||||
}
|
||||
panelSize = new Size(widthArr.Max(), constraint.Height);
|
||||
vArr[minIndex] = minV + sz.V;
|
||||
}
|
||||
|
||||
return panelSize;
|
||||
uvConstraint = new PanelUvSize(orientation, new Size(uvConstraint.ScreenSize.Width, vArr.Max()));
|
||||
|
||||
return uvConstraint.ScreenSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user