add RelativePanel

This commit is contained in:
NaBian committed 2019-12-10 00:41:49 +08:00
1 parent af877ffc91
commit fc1fa6fcf7
6 files changed
+482 -106

No files matched your search

@@ -0,0 +1,96 @@
using System;
using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
using HandyControl.Tools.Extension;
namespace HandyControl.Controls
{
public class Col : ContentControl
{
public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register(
"Layout", typeof(ColLayout), typeof(Col), new PropertyMetadata(default(ColLayout)));
public ColLayout Layout
{
get => (ColLayout)GetValue(LayoutProperty);
set => SetValue(LayoutProperty, value);
}
public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register(
"Offset", typeof(int), typeof(Col), new PropertyMetadata(ValueBoxes.Int0Box));
public int Offset
{
get => (int)GetValue(OffsetProperty);
set => SetValue(OffsetProperty, value);
}
public static readonly DependencyProperty SpanProperty = DependencyProperty.Register(
"Span", typeof(int), typeof(Col), new PropertyMetadata(24), OnSpanValidate);
private static bool OnSpanValidate(object value)
{
var v = (int)value;
return v >= 1 || v <= 24;
}
public int Span
{
get => (int) GetValue(SpanProperty);
set => SetValue(SpanProperty, value);
}
public static readonly DependencyProperty IsFixedProperty = DependencyProperty.Register(
"IsFixed", typeof(bool), typeof(Col), new PropertyMetadata(ValueBoxes.FalseBox));
public bool IsFixed
{
get => (bool) GetValue(IsFixedProperty);
set => SetValue(IsFixedProperty, value);
}
internal int GetLayoutCellCount(ColLayoutStatus status)
{
var result = 0;
if (Layout != null)
{
if (!IsFixed)
{
switch (status)
{
case ColLayoutStatus.Xs:
result = Layout.Xs;
break;
case ColLayoutStatus.Sm:
result = Layout.Sm;
break;
case ColLayoutStatus.Md:
result = Layout.Md;
break;
case ColLayoutStatus.Lg:
result = Layout.Lg;
break;
case ColLayoutStatus.Xl:
result = Layout.Xl;
break;
case ColLayoutStatus.Xxl:
result = Layout.Xxl;
break;
case ColLayoutStatus.Auto:
break;
default:
throw new ArgumentOutOfRangeException(nameof(status), status, null);
}
}
}
else
{
result = Span;
}
return result;
}
}
}
@@ -0,0 +1,96 @@
using HandyControl.Tools.Extension;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
using HandyControl.Tools;
namespace HandyControl.Controls
{
public class Row : Panel
{
private ColLayoutStatus _layoutStatus;
private double _maxChildDesiredHeight;
private double _totalAutoWidth;
public static readonly DependencyProperty GutterProperty = DependencyProperty.Register(
"Gutter", typeof(double), typeof(Row), new PropertyMetadata(ValueBoxes.Double0Box, null, OnGutterCoerce), ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
private static object OnGutterCoerce(DependencyObject d, object basevalue) => ValidateHelper.IsInRangeOfPosDoubleIncludeZero(basevalue) ? basevalue : .0;
public double Gutter
{
get => (double)GetValue(GutterProperty);
set => SetValue(GutterProperty, value);
}
protected override Size MeasureOverride(Size constraint)
{
var totalCellCount = 0;
var totalRowCount = 1;
var gutterHalf = Gutter / 2;
_totalAutoWidth = 0;
foreach (var child in InternalChildren.OfType<Col>())
{
child.Margin = new Thickness(gutterHalf);
child.Measure(constraint);
var childDesiredSize = child.DesiredSize;
if (_maxChildDesiredHeight < childDesiredSize.Height)
{
_maxChildDesiredHeight = childDesiredSize.Height;
}
var cellCount = child.GetLayoutCellCount(_layoutStatus);
totalCellCount += cellCount;
if (totalCellCount > ColLayout.ColMaxCellCount)
{
totalCellCount = cellCount;
totalRowCount++;
}
if (cellCount == 0 || child.IsFixed)
{
_totalAutoWidth += childDesiredSize.Width;
}
}
return new Size(0, _maxChildDesiredHeight * totalRowCount - Gutter);
}
protected override Size ArrangeOverride(Size finalSize)
{
var totalCellCount = 0;
var gutterHalf = Gutter / 2;
var itemWidth = (finalSize.Width - _totalAutoWidth + Gutter) / ColLayout.ColMaxCellCount;
var childBounds = new Rect(-gutterHalf, -gutterHalf, 0, _maxChildDesiredHeight);
_layoutStatus = ColLayout.GetLayoutStatus(finalSize.Width);
foreach (var child in InternalChildren.OfType<Col>())
{
var cellCount = child.GetLayoutCellCount(_layoutStatus);
totalCellCount += cellCount;
var childWidth = cellCount > 0 ? cellCount * itemWidth : child.DesiredSize.Width;
childBounds.Width = childWidth;
childBounds.X += child.Offset * itemWidth;
if (totalCellCount > ColLayout.ColMaxCellCount)
{
childBounds.X = -gutterHalf;
childBounds.Y += _maxChildDesiredHeight;
totalCellCount = cellCount;
}
child.Arrange(childBounds);
childBounds.X += childWidth;
}
return finalSize;
}
}
}