Skip to content

Commit f4d4bdd

Browse files
committed
fixed #784 and optimized MeasureChild method
1 parent f7ede33 commit f4d4bdd

1 file changed

Lines changed: 40 additions & 38 deletions

File tree

src/Shared/HandyControl_Shared/Controls/Panel/RelativePanel.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void SetAlignLeftWithPanel(DependencyObject element, bool value)
2727
=> element.SetValue(AlignLeftWithPanelProperty, ValueBoxes.BooleanBox(value));
2828

2929
public static bool GetAlignLeftWithPanel(DependencyObject element)
30-
=> (bool) element.GetValue(AlignLeftWithPanelProperty);
30+
=> (bool)element.GetValue(AlignLeftWithPanelProperty);
3131

3232
public static readonly DependencyProperty AlignTopWithPanelProperty = DependencyProperty.RegisterAttached(
3333
"AlignTopWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
@@ -36,7 +36,7 @@ public static void SetAlignTopWithPanel(DependencyObject element, bool value)
3636
=> element.SetValue(AlignTopWithPanelProperty, ValueBoxes.BooleanBox(value));
3737

3838
public static bool GetAlignTopWithPanel(DependencyObject element)
39-
=> (bool) element.GetValue(AlignTopWithPanelProperty);
39+
=> (bool)element.GetValue(AlignTopWithPanelProperty);
4040

4141
public static readonly DependencyProperty AlignRightWithPanelProperty = DependencyProperty.RegisterAttached(
4242
"AlignRightWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
@@ -45,7 +45,7 @@ public static void SetAlignRightWithPanel(DependencyObject element, bool value)
4545
=> element.SetValue(AlignRightWithPanelProperty, ValueBoxes.BooleanBox(value));
4646

4747
public static bool GetAlignRightWithPanel(DependencyObject element)
48-
=> (bool) element.GetValue(AlignRightWithPanelProperty);
48+
=> (bool)element.GetValue(AlignRightWithPanelProperty);
4949

5050
public static readonly DependencyProperty AlignBottomWithPanelProperty = DependencyProperty.RegisterAttached(
5151
"AlignBottomWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
@@ -54,7 +54,7 @@ public static void SetAlignBottomWithPanel(DependencyObject element, bool value)
5454
=> element.SetValue(AlignBottomWithPanelProperty, ValueBoxes.BooleanBox(value));
5555

5656
public static bool GetAlignBottomWithPanel(DependencyObject element)
57-
=> (bool) element.GetValue(AlignBottomWithPanelProperty);
57+
=> (bool)element.GetValue(AlignBottomWithPanelProperty);
5858

5959
#endregion
6060

@@ -68,7 +68,7 @@ public static void SetAlignLeftWith(DependencyObject element, UIElement value)
6868

6969
[TypeConverter(typeof(NameReferenceConverter))]
7070
public static UIElement GetAlignLeftWith(DependencyObject element)
71-
=> (UIElement) element.GetValue(AlignLeftWithProperty);
71+
=> (UIElement)element.GetValue(AlignLeftWithProperty);
7272

7373
public static readonly DependencyProperty AlignTopWithProperty = DependencyProperty.RegisterAttached(
7474
"AlignTopWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -78,7 +78,7 @@ public static void SetAlignTopWith(DependencyObject element, UIElement value)
7878

7979
[TypeConverter(typeof(NameReferenceConverter))]
8080
public static UIElement GetAlignTopWith(DependencyObject element)
81-
=> (UIElement) element.GetValue(AlignTopWithProperty);
81+
=> (UIElement)element.GetValue(AlignTopWithProperty);
8282

8383
public static readonly DependencyProperty AlignRightWithProperty = DependencyProperty.RegisterAttached(
8484
"AlignRightWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -88,7 +88,7 @@ public static void SetAlignRightWith(DependencyObject element, UIElement value)
8888

8989
[TypeConverter(typeof(NameReferenceConverter))]
9090
public static UIElement GetAlignRightWith(DependencyObject element)
91-
=> (UIElement) element.GetValue(AlignRightWithProperty);
91+
=> (UIElement)element.GetValue(AlignRightWithProperty);
9292

9393
public static readonly DependencyProperty AlignBottomWithProperty = DependencyProperty.RegisterAttached(
9494
"AlignBottomWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -98,7 +98,7 @@ public static void SetAlignBottomWith(DependencyObject element, UIElement value)
9898

9999
[TypeConverter(typeof(NameReferenceConverter))]
100100
public static UIElement GetAlignBottomWith(DependencyObject element)
101-
=> (UIElement) element.GetValue(AlignBottomWithProperty);
101+
=> (UIElement)element.GetValue(AlignBottomWithProperty);
102102

103103
#endregion
104104

@@ -112,7 +112,7 @@ public static void SetLeftOf(DependencyObject element, UIElement value)
112112

113113
[TypeConverter(typeof(NameReferenceConverter))]
114114
public static UIElement GetLeftOf(DependencyObject element)
115-
=> (UIElement) element.GetValue(LeftOfProperty);
115+
=> (UIElement)element.GetValue(LeftOfProperty);
116116

117117
public static readonly DependencyProperty AboveProperty = DependencyProperty.RegisterAttached(
118118
"Above", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -122,7 +122,7 @@ public static void SetAbove(DependencyObject element, UIElement value)
122122

123123
[TypeConverter(typeof(NameReferenceConverter))]
124124
public static UIElement GetAbove(DependencyObject element)
125-
=> (UIElement) element.GetValue(AboveProperty);
125+
=> (UIElement)element.GetValue(AboveProperty);
126126

127127
public static readonly DependencyProperty RightOfProperty = DependencyProperty.RegisterAttached(
128128
"RightOf", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -132,7 +132,7 @@ public static void SetRightOf(DependencyObject element, UIElement value)
132132

133133
[TypeConverter(typeof(NameReferenceConverter))]
134134
public static UIElement GetRightOf(DependencyObject element)
135-
=> (UIElement) element.GetValue(RightOfProperty);
135+
=> (UIElement)element.GetValue(RightOfProperty);
136136

137137
public static readonly DependencyProperty BelowProperty = DependencyProperty.RegisterAttached(
138138
"Below", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -142,7 +142,7 @@ public static void SetBelow(DependencyObject element, UIElement value)
142142

143143
[TypeConverter(typeof(NameReferenceConverter))]
144144
public static UIElement GetBelow(DependencyObject element)
145-
=> (UIElement) element.GetValue(BelowProperty);
145+
=> (UIElement)element.GetValue(BelowProperty);
146146

147147
#endregion
148148

@@ -155,7 +155,7 @@ public static void SetAlignHorizontalCenterWithPanel(DependencyObject element, b
155155
=> element.SetValue(AlignHorizontalCenterWithPanelProperty, ValueBoxes.BooleanBox(value));
156156

157157
public static bool GetAlignHorizontalCenterWithPanel(DependencyObject element)
158-
=> (bool) element.GetValue(AlignHorizontalCenterWithPanelProperty);
158+
=> (bool)element.GetValue(AlignHorizontalCenterWithPanelProperty);
159159

160160
public static readonly DependencyProperty AlignVerticalCenterWithPanelProperty = DependencyProperty.RegisterAttached(
161161
"AlignVerticalCenterWithPanel", typeof(bool), typeof(RelativePanel), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));
@@ -164,7 +164,7 @@ public static void SetAlignVerticalCenterWithPanel(DependencyObject element, boo
164164
=> element.SetValue(AlignVerticalCenterWithPanelProperty, ValueBoxes.BooleanBox(value));
165165

166166
public static bool GetAlignVerticalCenterWithPanel(DependencyObject element)
167-
=> (bool) element.GetValue(AlignVerticalCenterWithPanelProperty);
167+
=> (bool)element.GetValue(AlignVerticalCenterWithPanelProperty);
168168

169169
public static readonly DependencyProperty AlignHorizontalCenterWithProperty = DependencyProperty.RegisterAttached(
170170
"AlignHorizontalCenterWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -174,7 +174,7 @@ public static void SetAlignHorizontalCenterWith(DependencyObject element, UIElem
174174

175175
[TypeConverter(typeof(NameReferenceConverter))]
176176
public static UIElement GetAlignHorizontalCenterWith(DependencyObject element)
177-
=> (UIElement) element.GetValue(AlignHorizontalCenterWithProperty);
177+
=> (UIElement)element.GetValue(AlignHorizontalCenterWithProperty);
178178

179179
public static readonly DependencyProperty AlignVerticalCenterWithProperty = DependencyProperty.RegisterAttached(
180180
"AlignVerticalCenterWith", typeof(UIElement), typeof(RelativePanel), new FrameworkPropertyMetadata(default(UIElement), FrameworkPropertyMetadataOptions.AffectsRender));
@@ -184,7 +184,7 @@ public static void SetAlignVerticalCenterWith(DependencyObject element, UIElemen
184184

185185
[TypeConverter(typeof(NameReferenceConverter))]
186186
public static UIElement GetAlignVerticalCenterWith(DependencyObject element)
187-
=> (UIElement) element.GetValue(AlignVerticalCenterWithProperty);
187+
=> (UIElement)element.GetValue(AlignVerticalCenterWithProperty);
188188

189189
#endregion
190190

@@ -218,7 +218,7 @@ protected override Size MeasureOverride(Size availableSize)
218218
#region Calc AvailableSize
219219

220220
_childGraph.Reset(false);
221-
var boundingSize = _childGraph.GetBoundingSize(Width.IsNaN(), Height.IsNaN());
221+
var boundingSize = _childGraph.GetBoundingSize(Width.IsNaN(), Height.IsNaN(), availableSize);
222222
_childGraph.Reset();
223223
_childGraph.Measure(boundingSize);
224224
return boundingSize;
@@ -454,6 +454,8 @@ private void Measure(IEnumerable<GraphNode> nodes, HashSet<DependencyObject> set
454454

455455
private void MeasureChild(GraphNode node)
456456
{
457+
var availableSize = AvailableSize;
458+
457459
var child = node.Element;
458460
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
459461
node.OriginDesiredSize = child.DesiredSize;
@@ -502,10 +504,10 @@ private void MeasureChild(GraphNode node)
502504

503505
#region Measure
504506

505-
var availableHeight = AvailableSize.Height - node.Top - node.Bottom;
507+
var availableHeight = availableSize.Height - node.Top - node.Bottom;
506508
if (availableHeight.IsNaN())
507509
{
508-
availableHeight = AvailableSize.Height;
510+
availableHeight = availableSize.Height;
509511

510512
if (!node.Top.IsNaN() && node.Bottom.IsNaN())
511513
{
@@ -517,10 +519,10 @@ private void MeasureChild(GraphNode node)
517519
}
518520
}
519521

520-
var availableWidth = AvailableSize.Width - node.Left - node.Right;
522+
var availableWidth = availableSize.Width - node.Left - node.Right;
521523
if (availableWidth.IsNaN())
522524
{
523-
availableWidth = AvailableSize.Width;
525+
availableWidth = availableSize.Width;
524526

525527
if (!node.Left.IsNaN() && node.Right.IsNaN())
526528
{
@@ -558,7 +560,7 @@ private void MeasureChild(GraphNode node)
558560

559561
if (node.Left.IsNaN())
560562
{
561-
node.Left = AvailableSize.Width - node.RightOfNode.Right;
563+
node.Left = availableSize.Width - node.RightOfNode.Right;
562564
}
563565
}
564566

@@ -571,7 +573,7 @@ private void MeasureChild(GraphNode node)
571573

572574
if (node.Top.IsNaN())
573575
{
574-
node.Top = AvailableSize.Height - node.BelowNode.Bottom;
576+
node.Top = availableSize.Height - node.BelowNode.Bottom;
575577
}
576578
}
577579

@@ -581,8 +583,8 @@ private void MeasureChild(GraphNode node)
581583

582584
if (node.AlignHorizontalCenterWith != null)
583585
{
584-
var halfWidthLeft = (AvailableSize.Width + node.AlignHorizontalCenterWith.Left - node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
585-
var halfWidthRight = (AvailableSize.Width - node.AlignHorizontalCenterWith.Left + node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
586+
var halfWidthLeft = (availableSize.Width + node.AlignHorizontalCenterWith.Left - node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
587+
var halfWidthRight = (availableSize.Width - node.AlignHorizontalCenterWith.Left + node.AlignHorizontalCenterWith.Right - childSize.Width) * 0.5;
586588

587589
if (node.Left.IsNaN()) node.Left = halfWidthLeft;
588590
else node.Left = (node.Left + halfWidthLeft) * 0.5;
@@ -593,8 +595,8 @@ private void MeasureChild(GraphNode node)
593595

594596
if (node.AlignVerticalCenterWith != null)
595597
{
596-
var halfHeightTop = (AvailableSize.Height + node.AlignVerticalCenterWith.Top - node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
597-
var halfHeightBottom = (AvailableSize.Height - node.AlignVerticalCenterWith.Top + node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
598+
var halfHeightTop = (availableSize.Height + node.AlignVerticalCenterWith.Top - node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
599+
var halfHeightBottom = (availableSize.Height - node.AlignVerticalCenterWith.Top + node.AlignVerticalCenterWith.Bottom - childSize.Height) * 0.5;
598600

599601
if (node.Top.IsNaN()) node.Top = halfHeightTop;
600602
else node.Top = (node.Top + halfHeightTop) * 0.5;
@@ -609,7 +611,7 @@ private void MeasureChild(GraphNode node)
609611

610612
if (GetAlignHorizontalCenterWithPanel(child))
611613
{
612-
var halfSubWidth = (AvailableSize.Width - childSize.Width) * 0.5;
614+
var halfSubWidth = (availableSize.Width - childSize.Width) * 0.5;
613615

614616
if (node.Left.IsNaN()) node.Left = halfSubWidth;
615617
else node.Left = (node.Left + halfSubWidth) * 0.5;
@@ -620,7 +622,7 @@ private void MeasureChild(GraphNode node)
620622

621623
if (GetAlignVerticalCenterWithPanel(child))
622624
{
623-
var halfSubHeight = (AvailableSize.Height - childSize.Height) * 0.5;
625+
var halfSubHeight = (availableSize.Height - childSize.Height) * 0.5;
624626

625627
if (node.Top.IsNaN()) node.Top = halfSubHeight;
626628
else node.Top = (node.Top + halfSubHeight) * 0.5;
@@ -634,37 +636,37 @@ private void MeasureChild(GraphNode node)
634636
if (node.Left.IsNaN())
635637
{
636638
if (!node.Right.IsNaN())
637-
node.Left = AvailableSize.Width - node.Right - childSize.Width;
639+
node.Left = availableSize.Width - node.Right - childSize.Width;
638640
else
639641
{
640642
node.Left = 0;
641-
node.Right = AvailableSize.Width - childSize.Width;
643+
node.Right = availableSize.Width - childSize.Width;
642644
}
643645
}
644646
else if (!node.Left.IsNaN() && node.Right.IsNaN())
645647
{
646-
node.Right = AvailableSize.Width - node.Left - childSize.Width;
648+
node.Right = availableSize.Width - node.Left - childSize.Width;
647649
}
648650

649651
if (node.Top.IsNaN())
650652
{
651653
if (!node.Bottom.IsNaN())
652-
node.Top = AvailableSize.Height - node.Bottom - childSize.Height;
654+
node.Top = availableSize.Height - node.Bottom - childSize.Height;
653655
else
654656
{
655657
node.Top = 0;
656-
node.Bottom = AvailableSize.Height - childSize.Height;
658+
node.Bottom = availableSize.Height - childSize.Height;
657659
}
658660
}
659661
else if (!node.Top.IsNaN() && node.Bottom.IsNaN())
660662
{
661-
node.Bottom = AvailableSize.Height - node.Top - childSize.Height;
663+
node.Bottom = availableSize.Height - node.Top - childSize.Height;
662664
}
663665

664666
node.Measured = true;
665667
}
666668

667-
public Size GetBoundingSize(bool calcWidth, bool calcHeight)
669+
public Size GetBoundingSize(bool calcWidth, bool calcHeight, Size availableSize)
668670
{
669671
var boundingSize = new Size();
670672

@@ -675,8 +677,8 @@ public Size GetBoundingSize(bool calcWidth, bool calcHeight)
675677
boundingSize.Height = Math.Max(boundingSize.Height, size.Height);
676678
}
677679

678-
boundingSize.Width = calcWidth ? boundingSize.Width : AvailableSize.Width;
679-
boundingSize.Height = calcHeight ? boundingSize.Height : AvailableSize.Height;
680+
boundingSize.Width = calcWidth ? Math.Max(availableSize.Width, boundingSize.Width) : availableSize.Width;
681+
boundingSize.Height = calcHeight ? Math.Max(availableSize.Height, boundingSize.Height) : availableSize.Height;
680682
return boundingSize;
681683
}
682684
}

0 commit comments

Comments
 (0)