-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathStartViewPanel.cs
executable file
·371 lines (316 loc) · 11.2 KB
/
StartViewPanel.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace TelegramClient.Controls.StartView
{
/// <summary>
/// Implements a custom Panel for the StartView control.
/// </summary>
public class StartViewPanel : Panel
{
private const int SnapThresholdDivisor = 2;
private readonly List<StartViewItem> _visibleChildren = new List<StartViewItem>();
private readonly List<ItemStop> _itemStops = new List<ItemStop>();
private StartView _owner;
private StartViewItem _selectedItem;
internal IList<StartViewItem> VisibleChildren
{
get { return _visibleChildren; }
}
private StartView Owner
{
get { return _owner; }
set
{
if (_owner != value)
{
if (_owner != null)
{
_owner.Panel = null;
}
_owner = value;
if (_owner != null)
{
_owner.Panel = this;
}
}
}
}
/// <summary>
/// Initializes a new instance of the StartViewPanel class.
/// </summary>
public StartViewPanel()
{
SizeChanged += OnSizeChanged;
Unloaded += OnUnloaded;
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
_owner = null;
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
Owner.ItemsWidth = (int)e.NewSize.Width;
}
/// <summary>
/// Handles the measure pass for the control.
/// </summary>
///
/// <returns>
/// Desired size.
/// </returns>
/// <param name="availableSize">Available size.</param>
protected override Size MeasureOverride(Size availableSize)
{
if (_owner == null)
{
FindOwner();
}
Size desiredSize = new Size(0, availableSize.Height);
int childWidth = Owner.ViewportWidth;
int childHeight = (int)Math.Min(availableSize.Height, Owner.ViewportHeight);
Size childSize = new Size(childWidth, childHeight);
_visibleChildren.Clear();
foreach (StartViewItem child in Children)
{
if (child.Visibility == Visibility.Visible)
{
_visibleChildren.Add(child);
child.Measure(childSize);
desiredSize.Width += childWidth;
}
}
return desiredSize;
}
/// <summary>
/// Handles the arrange pass for the control.
/// </summary>
///
/// <returns>
/// Render size.
/// </returns>
/// <param name="finalSize">Final size.</param>
protected override Size ArrangeOverride(Size finalSize)
{
_itemStops.Clear();
double x = 0;
Rect finalRect = new Rect(0, 0, 0, finalSize.Height);
for (int index = 0; index < _visibleChildren.Count; ++index)
{
StartViewItem child = _visibleChildren[index];
finalRect.X = child.StartPosition = (int)x;
_itemStops.Add(new ItemStop(child, child.StartPosition));
finalRect.Width = Owner.ViewportWidth;
child.ItemWidth = (int)finalRect.Width;
child.Arrange(finalRect);
x += finalRect.Width;
}
Owner.RequestAdjustSelection();
return finalSize;
}
private void GetItemsInView(int offset, int viewportWidth, out int leftIndex, out int leftInView, out int centerIndex, out int rightIndex, out int rightInView)
{
leftIndex = leftInView = centerIndex = rightIndex = rightInView = -1;
int count = VisibleChildren.Count;
if (count == 0)
{
return;
}
for (int index = 0; index < count; ++index)
{
StartViewItem child = _visibleChildren[index];
int leftOffset = child.StartPosition + offset;
int rightOffset = leftOffset + child.ItemWidth - 1;
if (leftOffset <= 0 && rightOffset >= 0)
{
leftIndex = index;
leftInView = Math.Min(viewportWidth, child.ItemWidth + leftOffset);
}
if (leftOffset < viewportWidth && rightOffset >= viewportWidth)
{
rightIndex = index;
rightInView = Math.Min(viewportWidth, viewportWidth - leftOffset);
}
if (leftOffset > 0 && rightOffset < viewportWidth)
{
centerIndex = index;
}
if (index == 0 && leftInView == -1)
{
leftInView = leftOffset;
}
if (index == count - 1 && rightInView == -1)
{
rightInView = viewportWidth - rightOffset - 1;
}
}
}
internal void GetStops(int offset, int totalWidth, out ItemStop previous, out ItemStop current, out ItemStop next)
{
next = current = previous = null;
if (VisibleChildren.Count == 0)
{
return;
}
int nextIndex = -1;
int currentIndex = -1;
int previousIndex = -1;
int position = -offset % totalWidth;
int itemStopIndex = 0;
foreach (ItemStop itemStop in _itemStops)
{
if (itemStop.Position < position)
{
previousIndex = itemStopIndex;
}
else if (itemStop.Position > position)
{
nextIndex = itemStopIndex;
break;
}
else if (itemStop.Position == position)
{
currentIndex = itemStopIndex;
}
++itemStopIndex;
}
if (previousIndex != -1)
{
previous = _itemStops[previousIndex];
}
if (currentIndex != -1)
{
current = _itemStops[currentIndex];
}
if (nextIndex != -1)
{
next = _itemStops[nextIndex];
}
}
internal void GetSnapOffset(int offset, int viewportWidth, int direction, out int snapTo, out int newDirection, out StartViewItem newSelection)
{
int snapThreshold = viewportWidth / SnapThresholdDivisor;
snapTo = offset;
newDirection = direction;
newSelection = _selectedItem;
if (VisibleChildren.Count == 0)
{
return;
}
foreach (ItemStop itemStop in _itemStops)
{
if (itemStop.Position == -offset)
{
newSelection = itemStop.Item;
return;
}
}
int leftIndex;
int leftInView;
int centerIndex;
int rightIndex;
int rightInView;
GetItemsInView(offset, viewportWidth, out leftIndex, out leftInView, out centerIndex, out rightIndex, out rightInView);
if (leftIndex == rightIndex && leftIndex != -1)
{
newSelection = _selectedItem = _visibleChildren[leftIndex];
}
else
{
if (leftIndex == -1)
{
leftIndex = _visibleChildren.Count - 1;
}
if (rightIndex == -1)
{
rightIndex = 0;
}
int index;
if (direction < 0)
{
if (rightInView > snapThreshold)
{
index = GetBestIndex(centerIndex, rightIndex, leftIndex);
newDirection = -1;
}
else
{
index = GetBestIndex(leftIndex, centerIndex, rightIndex);
newDirection = 1;
}
}
else if (direction > 0)
{
if (leftInView > snapThreshold)
{
index = StartViewPanel.GetBestIndex(leftIndex, centerIndex, rightIndex);
newDirection = 1;
}
else
{
index = StartViewPanel.GetBestIndex(centerIndex, rightIndex, leftIndex);
newDirection = -1;
}
}
else if (centerIndex != -1)
{
index = centerIndex;
newDirection = -1;
}
else if (leftInView > rightInView)
{
index = leftIndex;
newDirection = -1;
}
else
{
index = rightIndex;
newDirection = 1;
}
_selectedItem = _visibleChildren[index];
snapTo = -_selectedItem.StartPosition;
newSelection = _selectedItem;
}
}
private static int GetBestIndex(int n0, int n1, int n2)
{
if (n0 >= 0)
{
return n0;
}
if (n1 >= 0)
{
return n1;
}
if (n2 >= 0)
{
return n2;
}
throw new InvalidOperationException("No best index.");
}
private void FindOwner()
{
FrameworkElement frameworkElement = this;
StartView owner;
do
{
frameworkElement = (FrameworkElement)VisualTreeHelper.GetParent(frameworkElement);
owner = frameworkElement as StartView;
}
while (frameworkElement != null && owner == null);
Owner = owner;
}
internal class ItemStop
{
public ItemStop(StartViewItem item, int position)
{
Item = item;
Position = position;
}
public int Position { get; private set; }
public StartViewItem Item { get; private set; }
}
}
}