/
GridRange.cs
228 lines (193 loc) · 8.47 KB
/
GridRange.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
namespace DevZest.Data.Presenters.Primitives
{
/// <summary>
/// Represents a range of grid cells.
/// </summary>
public struct GridRange
{
internal GridRange(GridColumn left, GridRow top)
: this(left, top, left, top)
{
}
internal GridRange(GridColumn left, GridRow top, GridColumn right, GridRow bottom)
{
Debug.Assert(left != null);
Debug.Assert(top != null && top.Template == left.Template);
Debug.Assert(right != null && right.Template == top.Template);
Debug.Assert(bottom != null && bottom.Template == right.Template);
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
/// <summary>
/// Gets the left grid column.
/// </summary>
public readonly GridColumn Left;
/// <summary>
/// Gets the top grid row.
/// </summary>
public readonly GridRow Top;
/// <summary>
/// Gets the right grid column.
/// </summary>
public readonly GridColumn Right;
/// <summary>
/// Gets the bottom grid row.
/// </summary>
public readonly GridRow Bottom;
internal Template Template
{
get { return Left == null ? null : Left.Template; }
}
/// <summary>
/// Gets a value indicates whether this <see cref="GridRange"/> is empty.
/// </summary>
public bool IsEmpty
{
get { return Template == null; }
}
/// <summary>
/// Determines whether this <see cref="GridRange"/> contains other <see cref="GridRange"/>.
/// </summary>
/// <param name="gridRange">The other <see cref="GridRange"/>.</param>
/// <returns><see langword="true"/> if this <see cref="GridRange"/> contains other <see cref="GridRange"/>, otherwise <see langword="false"/>.</returns>
public bool Contains(GridRange gridRange)
{
if (IsEmpty || Template != gridRange.Template)
return false;
return Left.Ordinal <= gridRange.Left.Ordinal && Right.Ordinal >= gridRange.Right.Ordinal
&& Top.Ordinal <= gridRange.Top.Ordinal && Bottom.Ordinal >= gridRange.Bottom.Ordinal;
}
/// <summary>
/// Determines whether this <see cref="GridRange"/> contains specified <see cref="GridColumn"/>.
/// </summary>
/// <param name="gridColumn">The specified <see cref="GridColumn"/>.</param>
/// <returns><see langword="true"/> if this <see cref="GridRange"/> contains specified <see cref="GridColumn"/>, otherwise <see langword="false"/>.</returns>
public bool Contains(GridColumn gridColumn)
{
if (IsEmpty || Template != gridColumn.Template)
return false;
return Contains(Left, Right, gridColumn);
}
/// <summary>
/// Determines whether this <see cref="GridRange"/> contains specified <see cref="GridRow"/>.
/// </summary>
/// <param name="gridRow">The specified <see cref="GridRow"/>.</param>
/// <returns><see langword="true"/> if this <see cref="GridRange"/> contains specified <see cref="GridRow"/>, otherwise <see langword="false"/>.</returns>
public bool Contains(GridRow gridRow)
{
if (IsEmpty || Template != gridRow.Template)
return false;
return Contains(Top, Bottom, gridRow);
}
/// <summary>
/// Determines whether this <see cref="GridRange"/> intersects other <see cref="GridRange"/>.
/// </summary>
/// <param name="gridRange">The other <see cref="GridRange"/>.</param>
/// <returns><see langword="true"/> if this <see cref="GridRange"/> intersects other <see cref="GridRange"/>, otherwise <see langword="false"/>.</returns>
public bool IntersectsWith(GridRange gridRange)
{
if (IsEmpty || Template != gridRange.Template)
return false;
return gridRange.Left.Ordinal <= this.Right.Ordinal
&& gridRange.Right.Ordinal >= this.Left.Ordinal
&& gridRange.Top.Ordinal <= this.Bottom.Ordinal
&& gridRange.Bottom.Ordinal >= this.Top.Ordinal;
}
/// <summary>
/// Creates a new range defined as the union of this and the specified range.
/// </summary>
/// <param name="gridRange">The specified range.</param>
/// <returns>The new range.</returns>
public GridRange Union(GridRange gridRange)
{
if (gridRange.IsEmpty)
return this;
if (this.IsEmpty)
return gridRange;
if (Template != gridRange.Template)
throw new ArgumentException(DiagnosticMessages.GridRange_InvalidOwner, nameof(gridRange));
return new GridRange(
Left.Ordinal < gridRange.Left.Ordinal ? Left : gridRange.Left,
Top.Ordinal < gridRange.Top.Ordinal ? Top : gridRange.Top,
Right.Ordinal > gridRange.Right.Ordinal ? Right : gridRange.Right,
Bottom.Ordinal > gridRange.Bottom.Ordinal ? Bottom : gridRange.Bottom);
}
internal IConcatList<GridColumn> FilterColumns(Func<GridColumn, bool> predict, Action<GridColumn> action = null)
{
Debug.Assert(!IsEmpty);
return Template.InternalGridColumns.Filter(this, predict, action);
}
internal IConcatList<GridRow> FilterRows(Func<GridRow, bool> predict, Action<GridRow> action = null)
{
Debug.Assert(!IsEmpty);
return Template.InternalGridRows.Filter(this, predict, action);
}
internal Point MeasuredPosition
{
get { return new Point(Left == null ? 0 : Left.StartOffset, Top == null ? 0 : Top.StartOffset); }
}
internal Size MeasuredSize
{
get { return new Size(MeasuredWidth, MeasuredHeight); }
}
internal double MeasuredWidth
{
get { return IsEmpty ? 0 : Template.InternalGridColumns.GetMeasuredLength(ColumnSpan); }
}
internal double GetMeasuredWidth(Func<GridColumn, bool> predict)
{
return IsEmpty ? 0 : Template.InternalGridColumns.GetMeasuredLength(ColumnSpan, predict);
}
internal double MeasuredHeight
{
get { return IsEmpty? 0 : Template.InternalGridRows.GetMeasuredLength(RowSpan); }
}
internal double GetMeasuredHeight(Func<GridRow, bool> predict)
{
return IsEmpty ? 0 : Template.InternalGridRows.GetMeasuredLength(RowSpan, predict);
}
/// <inheritdoc/>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "[({0},{1}),({2},{3})]",
Left == null ? string.Empty : Left.Ordinal.ToString(CultureInfo.InvariantCulture),
Top == null ? string.Empty : Top.Ordinal.ToString(CultureInfo.InvariantCulture),
Right == null ? string.Empty : Right.Ordinal.ToString(CultureInfo.InvariantCulture),
Bottom == null ? string.Empty : Bottom.Ordinal.ToString(CultureInfo.InvariantCulture));
}
internal bool HorizontallyIntersectsWith(int gridOrdinal)
{
return gridOrdinal > Left.Ordinal && gridOrdinal < Right.Ordinal;
}
internal bool VerticallyIntersectsWith(int gridOrdinal)
{
return gridOrdinal > Top.Ordinal && gridOrdinal < Bottom.Ordinal;
}
private static bool Contains<T>(T headTrack, T tailTrack, T gridTrack)
where T : GridTrack
{
return gridTrack.Ordinal >= headTrack.Ordinal && gridTrack.Ordinal <= tailTrack.Ordinal;
}
internal GridSpan<GridColumn> ColumnSpan
{
get { return new GridSpan<GridColumn>(Left, Right); }
}
internal GridSpan<GridRow> RowSpan
{
get { return new GridSpan<GridRow>(Top, Bottom); }
}
internal Point GetRelativePosition(GridRange innerGridRange)
{
Debug.Assert(Contains(innerGridRange));
var originPosition = MeasuredPosition;
var position = innerGridRange.MeasuredPosition;
return new Point(position.X - originPosition.X, position.Y - originPosition.Y);
}
}
}