-
Notifications
You must be signed in to change notification settings - Fork 80
/
GridSplitter.Events.cs
169 lines (146 loc) · 6.05 KB
/
GridSplitter.Events.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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.
namespace CommunityToolkit.WinUI.Controls;
public partial class GridSplitter
{
/// <inheritdoc/>
protected override void OnLoaded(RoutedEventArgs e)
{
_resizeDirection = GetResizeDirection();
Orientation = _resizeDirection == GridResizeDirection.Rows ?
Orientation.Horizontal : Orientation.Vertical;
_resizeBehavior = GetResizeBehavior();
}
private double _currentSize;
private double _siblingSize;
/// <inheritdoc />
protected override void OnDragStarting()
{
_resizeDirection = GetResizeDirection();
Orientation = _resizeDirection == GridResizeDirection.Rows ?
Orientation.Horizontal : Orientation.Vertical;
_resizeBehavior = GetResizeBehavior();
// Record starting points
if (Orientation == Orientation.Horizontal)
{
_currentSize = CurrentRow?.ActualHeight ?? -1;
_siblingSize = SiblingRow?.ActualHeight ?? -1;
}
else
{
_currentSize = CurrentColumn?.ActualWidth ?? -1;
_siblingSize = SiblingColumn?.ActualWidth ?? -1;
}
}
/// <inheritdoc/>
protected override bool OnDragVertical(double verticalChange)
{
if (CurrentRow == null || SiblingRow == null || Resizable == null)
{
return false;
}
var currentChange = _currentSize + verticalChange;
var siblingChange = _siblingSize + (verticalChange * -1); // sibling moves opposite
// if current row has fixed height then resize it
if (!IsStarRow(CurrentRow))
{
// No need to check for the row Min height because it is automatically respected
return SetRowHeight(CurrentRow, currentChange, GridUnitType.Pixel);
}
// if sibling row has fixed width then resize it
else if (!IsStarRow(SiblingRow))
{
// Would adding to this column make the current column violate the MinWidth?
if (IsValidRowHeight(CurrentRow, currentChange) == false)
{
return false;
}
return SetRowHeight(SiblingRow, siblingChange, GridUnitType.Pixel);
}
// if both row haven't fixed height (auto *)
else
{
// change current row height to the new height with respecting the auto
// change sibling row height to the new height relative to current row
// respect the other star row height by setting it's height to it's actual height with stars
// We need to validate current and sibling height to not cause any unexpected behavior
if (!IsValidRowHeight(CurrentRow, currentChange) ||
!IsValidRowHeight(SiblingRow, siblingChange))
{
return false;
}
foreach (var rowDefinition in Resizable.RowDefinitions)
{
if (rowDefinition == CurrentRow)
{
SetRowHeight(CurrentRow, currentChange, GridUnitType.Star);
}
else if (rowDefinition == SiblingRow)
{
SetRowHeight(SiblingRow, siblingChange, GridUnitType.Star);
}
else if (IsStarRow(rowDefinition))
{
rowDefinition.Height = new GridLength(rowDefinition.ActualHeight, GridUnitType.Star);
}
}
return true;
}
}
/// <inheritdoc/>
protected override bool OnDragHorizontal(double horizontalChange)
{
if (CurrentColumn == null || SiblingColumn == null || Resizable == null)
{
return false;
}
var currentChange = _currentSize + horizontalChange;
var siblingChange = _siblingSize + (horizontalChange * -1); // sibling moves opposite
// if current column has fixed width then resize it
if (!IsStarColumn(CurrentColumn))
{
// No need to check for the Column Min width because it is automatically respected
return SetColumnWidth(CurrentColumn, currentChange, GridUnitType.Pixel);
}
// if sibling column has fixed width then resize it
else if (!IsStarColumn(SiblingColumn))
{
// Would adding to this column make the current column violate the MinWidth?
if (IsValidColumnWidth(CurrentColumn, currentChange) == false)
{
return false;
}
return SetColumnWidth(SiblingColumn, siblingChange, GridUnitType.Pixel);
}
// if both column haven't fixed width (auto *)
else
{
// change current column width to the new width with respecting the auto
// change sibling column width to the new width relative to current column
// respect the other star column width by setting it's width to it's actual width with stars
// We need to validate current and sibling width to not cause any unexpected behavior
if (!IsValidColumnWidth(CurrentColumn, currentChange) ||
!IsValidColumnWidth(SiblingColumn, siblingChange))
{
return false;
}
foreach (var columnDefinition in Resizable.ColumnDefinitions)
{
if (columnDefinition == CurrentColumn)
{
SetColumnWidth(CurrentColumn, currentChange, GridUnitType.Star);
}
else if (columnDefinition == SiblingColumn)
{
SetColumnWidth(SiblingColumn, siblingChange, GridUnitType.Star);
}
else if (IsStarColumn(columnDefinition))
{
columnDefinition.Width = new GridLength(columnDefinition.ActualWidth, GridUnitType.Star);
}
}
return true;
}
}
}