1
+ using System ;
2
+ using Avalonia ;
3
+ using Avalonia . Controls ;
4
+
5
+ namespace AvaloniaStyles . Controls ;
6
+
7
+ public class ThreeSidesPanel : Panel
8
+ {
9
+ protected override Size MeasureOverride ( Size availableSize )
10
+ {
11
+ double totalWidth = 0 ;
12
+ double maxHeight = 0 ;
13
+ foreach ( var child in Children )
14
+ {
15
+ child . Measure ( availableSize ) ;
16
+ maxHeight = Math . Max ( maxHeight , child . DesiredSize . Height ) ;
17
+ totalWidth += child . DesiredSize . Width ;
18
+ }
19
+ return new Size ( totalWidth , maxHeight ) ;
20
+ }
21
+
22
+ protected override Size ArrangeOverride ( Size finalSize )
23
+ {
24
+ double leftDesiredWidth = 0 ;
25
+ double rightDesiredWidth = 0 ;
26
+ double centerDesiredWidth = 0 ;
27
+ Control ? leftChild = null , centerChild = null , rightChild = null ;
28
+ for ( var index = 0 ; index < Children . Count ; index ++ )
29
+ {
30
+ var child = Children [ index ] ;
31
+ var desiredWidth = child . DesiredSize . Width ;
32
+ if ( index == 0 )
33
+ {
34
+ leftDesiredWidth = desiredWidth ;
35
+ leftChild = child ;
36
+ }
37
+ else if ( index == 1 )
38
+ {
39
+ rightDesiredWidth = desiredWidth ;
40
+ rightChild = child ;
41
+ }
42
+ else if ( index == 2 )
43
+ {
44
+ centerDesiredWidth = 400 ; //desiredWidth;
45
+ centerChild = child ;
46
+ }
47
+ }
48
+
49
+ var totalWidth = finalSize . Width ;
50
+
51
+ var leftSpaceLeft = Math . Max ( 0 , totalWidth / 2 - leftDesiredWidth ) ;
52
+ var rightSpaceLeft = Math . Max ( 0 , totalWidth / 2 - rightDesiredWidth ) ;
53
+
54
+ var spaceLeft = leftSpaceLeft + rightSpaceLeft ;
55
+ centerDesiredWidth = Math . Min ( spaceLeft , centerDesiredWidth ) ;
56
+
57
+ leftDesiredWidth = Math . Min ( totalWidth - rightDesiredWidth , leftDesiredWidth ) ;
58
+
59
+ leftChild ? . Arrange ( new Rect ( 0 , 0 , leftDesiredWidth , finalSize . Height ) ) ;
60
+ var centerRect = new Rect ( Math . Max ( leftDesiredWidth , finalSize . Width / 2 - centerDesiredWidth / 2 ) , 0 ,
61
+ centerDesiredWidth , finalSize . Height ) ;
62
+ var rightRect = new Rect ( finalSize . Width - rightDesiredWidth , 0 , rightDesiredWidth , finalSize . Height ) ;
63
+ var overflowRight = Math . Max ( 0 , centerRect . Right - rightRect . Left ) ;
64
+ centerRect = new Rect ( centerRect . X - overflowRight , centerRect . Y , centerRect . Width , centerRect . Height ) ;
65
+ centerChild ? . Arrange ( centerRect ) ;
66
+ rightChild ? . Arrange ( rightRect ) ;
67
+
68
+ return finalSize ;
69
+ }
70
+ }
0 commit comments