forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGridSplitter.cs
More file actions
250 lines (210 loc) · 8.11 KB
/
Copy pathGridSplitter.cs
File metadata and controls
250 lines (210 loc) · 8.11 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// 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.
using System;
using System.Reflection;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.ApplicationModel.Resources;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// Represents the control that redistributes space between columns or rows of a Grid control.
/// </summary>
public partial class GridSplitter : Control
{
internal const int GripperCustomCursorDefaultResource = -1;
internal static readonly CoreCursor ColumnsSplitterCursor = new CoreCursor(CoreCursorType.SizeWestEast, 1);
internal static readonly CoreCursor RowSplitterCursor = new CoreCursor(CoreCursorType.SizeNorthSouth, 1);
internal CoreCursor PreviousCursor { get; set; }
private GridResizeDirection _resizeDirection;
private GridResizeBehavior _resizeBehavior;
private GripperHoverWrapper _hoverWrapper;
private TextBlock _gripperDisplay;
private bool _pressed = false;
private bool _dragging = false;
private bool _pointerEntered = false;
/// <summary>
/// Gets the target parent grid from level
/// </summary>
private FrameworkElement TargetControl
{
get
{
if (ParentLevel == 0)
{
return this;
}
var parent = Parent;
for (int i = 2; i < ParentLevel; i++)
{
var frameworkElement = parent as FrameworkElement;
if (frameworkElement != null)
{
parent = frameworkElement.Parent;
}
}
return parent as FrameworkElement;
}
}
/// <summary>
/// Gets GridSplitter Container Grid
/// </summary>
private Grid Resizable => TargetControl?.Parent as Grid;
/// <summary>
/// Gets the current Column definition of the parent Grid
/// </summary>
private ColumnDefinition CurrentColumn
{
get
{
if (Resizable == null)
{
return null;
}
var gridSplitterTargetedColumnIndex = GetTargetedColumn();
if ((gridSplitterTargetedColumnIndex >= 0)
&& (gridSplitterTargetedColumnIndex < Resizable.ColumnDefinitions.Count))
{
return Resizable.ColumnDefinitions[gridSplitterTargetedColumnIndex];
}
return null;
}
}
/// <summary>
/// Gets the Sibling Column definition of the parent Grid
/// </summary>
private ColumnDefinition SiblingColumn
{
get
{
if (Resizable == null)
{
return null;
}
var gridSplitterSiblingColumnIndex = GetSiblingColumn();
if ((gridSplitterSiblingColumnIndex >= 0)
&& (gridSplitterSiblingColumnIndex < Resizable.ColumnDefinitions.Count))
{
return Resizable.ColumnDefinitions[gridSplitterSiblingColumnIndex];
}
return null;
}
}
/// <summary>
/// Gets the current Row definition of the parent Grid
/// </summary>
private RowDefinition CurrentRow
{
get
{
if (Resizable == null)
{
return null;
}
var gridSplitterTargetedRowIndex = GetTargetedRow();
if ((gridSplitterTargetedRowIndex >= 0)
&& (gridSplitterTargetedRowIndex < Resizable.RowDefinitions.Count))
{
return Resizable.RowDefinitions[gridSplitterTargetedRowIndex];
}
return null;
}
}
/// <summary>
/// Gets the Sibling Row definition of the parent Grid
/// </summary>
private RowDefinition SiblingRow
{
get
{
if (Resizable == null)
{
return null;
}
var gridSplitterSiblingRowIndex = GetSiblingRow();
if ((gridSplitterSiblingRowIndex >= 0)
&& (gridSplitterSiblingRowIndex < Resizable.RowDefinitions.Count))
{
return Resizable.RowDefinitions[gridSplitterSiblingRowIndex];
}
return null;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="GridSplitter"/> class.
/// </summary>
public GridSplitter()
{
DefaultStyleKey = typeof(GridSplitter);
Loaded += GridSplitter_Loaded;
string automationName = "WCT_GridSplitter_AutomationName".GetLocalized("Microsoft.Toolkit.Uwp.UI.Controls/Resources");
AutomationProperties.SetName(this, automationName);
}
/// <inheritdoc />
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
// Unhook registered events
Loaded -= GridSplitter_Loaded;
PointerEntered -= GridSplitter_PointerEntered;
PointerExited -= GridSplitter_PointerExited;
PointerPressed -= GridSplitter_PointerPressed;
PointerReleased -= GridSplitter_PointerReleased;
ManipulationStarted -= GridSplitter_ManipulationStarted;
ManipulationCompleted -= GridSplitter_ManipulationCompleted;
_hoverWrapper?.UnhookEvents();
// Register Events
Loaded += GridSplitter_Loaded;
PointerEntered += GridSplitter_PointerEntered;
PointerExited += GridSplitter_PointerExited;
PointerPressed += GridSplitter_PointerPressed;
PointerReleased += GridSplitter_PointerReleased;
ManipulationStarted += GridSplitter_ManipulationStarted;
ManipulationCompleted += GridSplitter_ManipulationCompleted;
_hoverWrapper?.UpdateHoverElement(Element);
ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
}
private void GridSplitter_PointerReleased(object sender, PointerRoutedEventArgs e)
{
_pressed = false;
VisualStateManager.GoToState(this, _pointerEntered ? "PointerOver" : "Normal", true);
}
private void GridSplitter_PointerPressed(object sender, PointerRoutedEventArgs e)
{
_pressed = true;
VisualStateManager.GoToState(this, "Pressed", true);
}
private void GridSplitter_PointerExited(object sender, PointerRoutedEventArgs e)
{
_pointerEntered = false;
if (!_pressed && !_dragging)
{
VisualStateManager.GoToState(this, "Normal", true);
}
}
private void GridSplitter_PointerEntered(object sender, PointerRoutedEventArgs e)
{
_pointerEntered = true;
if (!_pressed && !_dragging)
{
VisualStateManager.GoToState(this, "PointerOver", true);
}
}
private void GridSplitter_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
_dragging = false;
_pressed = false;
VisualStateManager.GoToState(this, _pointerEntered ? "PointerOver" : "Normal", true);
}
private void GridSplitter_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
_dragging = true;
VisualStateManager.GoToState(this, "Pressed", true);
}
}
}