-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DateTimePickerPanel.cs
570 lines (497 loc) · 19.7 KB
/
DateTimePickerPanel.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
using System;
using System.Globalization;
using System.Linq;
using Avalonia.Input;
using Avalonia.VisualTree;
namespace Avalonia.Controls.Primitives
{
public enum DateTimePickerPanelType
{
Year,
Month,
Day,
Hour,
Minute,
TimePeriod //AM or PM
}
public class DateTimePickerPanel : Panel, ILogicalScrollable
{
/// <summary>
/// Defines the <see cref="ItemHeight"/> property
/// </summary>
public static readonly StyledProperty<double> ItemHeightProperty =
AvaloniaProperty.Register<DateTimePickerPanel, double>(nameof(ItemHeight), 40.0);
/// <summary>
/// Defines the <see cref="PanelType"/> property
/// </summary>
public static readonly StyledProperty<DateTimePickerPanelType> PanelTypeProperty =
AvaloniaProperty.Register<DateTimePickerPanel, DateTimePickerPanelType>(nameof(PanelType));
/// <summary>
/// Defines the <see cref="ItemFormat"/> property
/// </summary>
public static readonly StyledProperty<string> ItemFormatProperty =
AvaloniaProperty.Register<DateTimePickerPanel, string>(nameof(ItemFormat), "yyyy");
/// <summary>
/// Defines the <see cref="ShouldLoop"/> property
/// </summary>
public static readonly StyledProperty<bool> ShouldLoopProperty =
AvaloniaProperty.Register<DateTimePickerPanel, bool>(nameof(ShouldLoop));
//Backing fields for properties
private int _minimumValue = 1;
private int _maximumValue = 2;
private int _selectedValue = 1;
private int _increment = 1;
//Helper fields
private int _selectedIndex = 0;
private int _totalItems;
private int _numItemsAboveBelowSelected;
private int _range;
private double _extentOne;
private Size _extent;
private Vector _offset;
private bool _hasInit;
private bool _suppressUpdateOffset;
private ListBoxItem? _pressedItem;
public DateTimePickerPanel()
{
FormatDate = DateTime.Now;
AddHandler(ListBoxItem.PointerPressedEvent, OnItemPointerDown, Avalonia.Interactivity.RoutingStrategies.Bubble);
AddHandler(ListBoxItem.PointerReleasedEvent, OnItemPointerUp, Avalonia.Interactivity.RoutingStrategies.Bubble);
}
static DateTimePickerPanel()
{
FocusableProperty.OverrideDefaultValue<DateTimePickerPanel>(true);
AffectsMeasure<DateTimePickerPanel>(ItemHeightProperty);
}
/// <summary>
/// Gets or sets what this panel displays in date or time units
/// </summary>
public DateTimePickerPanelType PanelType
{
get => GetValue(PanelTypeProperty);
set => SetValue(PanelTypeProperty, value);
}
/// <summary>
/// Gets or sets the height of each item
/// </summary>
public double ItemHeight
{
get => GetValue(ItemHeightProperty);
set => SetValue(ItemHeightProperty, value);
}
/// <summary>
/// Gets or sets the string format for the items, using standard
/// .net DateTime or TimeSpan formatting. Format must match panel type
/// </summary>
public string ItemFormat
{
get => GetValue(ItemFormatProperty);
set => SetValue(ItemFormatProperty, value);
}
/// <summary>
/// Gets or sets whether the panel should loop
/// </summary>
public bool ShouldLoop
{
get => GetValue(ShouldLoopProperty);
set => SetValue(ShouldLoopProperty, value);
}
/// <summary>
/// Gets or sets the minimum value
/// </summary>
public int MinimumValue
{
get => _minimumValue;
set
{
if (value > MaximumValue)
throw new InvalidOperationException("Minimum cannot be greater than Maximum");
_minimumValue = value;
UpdateHelperInfo();
var sel = CoerceSelected(SelectedValue);
if (sel != SelectedValue)
SelectedValue = sel;
UpdateItems();
InvalidateArrange();
RaiseScrollInvalidated(EventArgs.Empty);
}
}
/// <summary>
/// Gets or sets the maximum value
/// </summary>
public int MaximumValue
{
get => _maximumValue;
set
{
if (value < MinimumValue)
throw new InvalidOperationException("Maximum cannot be less than Minimum");
_maximumValue = value;
UpdateHelperInfo();
var sel = CoerceSelected(SelectedValue);
if (sel != SelectedValue)
SelectedValue = sel;
UpdateItems();
InvalidateArrange();
RaiseScrollInvalidated(EventArgs.Empty);
}
}
/// <summary>
/// Gets or sets the selected value
/// </summary>
public int SelectedValue
{
get => _selectedValue;
set
{
if (value > MaximumValue || value < MinimumValue)
throw new ArgumentOutOfRangeException("SelectedValue");
var sel = CoerceSelected(value);
_selectedValue = sel;
_selectedIndex = (value - MinimumValue) / Increment;
if (!ShouldLoop)
CreateOrDestroyItems(Children);
if (!_suppressUpdateOffset)
_offset = new Vector(0, ShouldLoop ? _selectedIndex * ItemHeight + (_extentOne * 50) :
_selectedIndex * ItemHeight);
UpdateItems();
InvalidateArrange();
RaiseScrollInvalidated(EventArgs.Empty);
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
}
/// <summary>
/// Gets or sets the increment
/// </summary>
public int Increment
{
get => _increment;
set
{
if (value <= 0 || value > _range)
throw new ArgumentOutOfRangeException("Increment");
_increment = value;
UpdateHelperInfo();
var sel = CoerceSelected(SelectedValue);
if (sel != SelectedValue)
SelectedValue = sel;
UpdateItems();
InvalidateArrange();
RaiseScrollInvalidated(EventArgs.Empty);
}
}
//Used to help format the date (if applicable), for ex.,
//if we're want to display the day of week, we need context
//for the month/year, this is our context
internal DateTime FormatDate { get; set; }
public Vector Offset
{
get => _offset;
set
{
var old = _offset;
_offset = value;
var dy = _offset.Y - old.Y;
var children = Children;
if (dy > 0) // Scroll Down
{
int numCountsToMove = 0;
for (int i = 0; i < children.Count; i++)
{
if (children[i].Bounds.Bottom - dy < 0)
numCountsToMove++;
else
break;
}
children.MoveRange(0, numCountsToMove, children.Count);
var scrollHeight = _extent.Height - Viewport.Height;
if (ShouldLoop && value.Y >= scrollHeight - _extentOne)
_offset = new Vector(0, value.Y - (_extentOne * 50));
}
else if (dy < 0) // Scroll Up
{
int numCountsToMove = 0;
for (int i = children.Count - 1; i >= 0; i--)
{
if (children[i].Bounds.Top - dy > Bounds.Height)
numCountsToMove++;
else
break;
}
children.MoveRange(children.Count - numCountsToMove, numCountsToMove, 0);
if (ShouldLoop && value.Y < _extentOne)
_offset = new Vector(0, value.Y + (_extentOne * 50));
}
//Setting selection will handle all invalidation
var newSel = (Offset.Y / ItemHeight) % _totalItems;
_suppressUpdateOffset = true;
SelectedValue = (int)newSel * Increment + MinimumValue;
_suppressUpdateOffset = false;
}
}
public bool CanHorizontallyScroll { get => false; set { } }
public bool CanVerticallyScroll { get => true; set { } }
public bool IsLogicalScrollEnabled => true;
public Size ScrollSize => new Size(0, ItemHeight);
public Size PageScrollSize => new Size(0, ItemHeight * 4);
public Size Extent => _extent;
public Size Viewport => new Size(0, ItemHeight);
public event EventHandler? ScrollInvalidated;
public event EventHandler? SelectionChanged;
protected override Size MeasureOverride(Size availableSize)
{
if (double.IsInfinity(availableSize.Width) ||
double.IsInfinity(availableSize.Height))
throw new InvalidOperationException("Panel must have finite height");
if (!_hasInit)
UpdateHelperInfo();
double initY = (availableSize.Height / 2.0) - (ItemHeight / 2.0);
_numItemsAboveBelowSelected = (int)Math.Ceiling(initY / ItemHeight) + 1;
var children = Children;
CreateOrDestroyItems(children);
for (int i = 0; i < children.Count; i++)
children[i].Measure(availableSize);
if (!_hasInit)
{
UpdateItems();
RaiseScrollInvalidated(EventArgs.Empty);
_hasInit = true;
}
return availableSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
if (Children.Count == 0)
return base.ArrangeOverride(finalSize);
var itemHgt = ItemHeight;
var children = Children;
Rect rc;
double initY = (finalSize.Height / 2.0) - (itemHgt / 2.0);
if (ShouldLoop)
{
var currentSet = Math.Truncate(Offset.Y / _extentOne);
initY += (_extentOne * currentSet) + (_selectedIndex - _numItemsAboveBelowSelected) * ItemHeight;
for (int i = 0; i < children.Count; i++)
{
rc = new Rect(0, initY - Offset.Y, finalSize.Width, itemHgt);
children[i].Arrange(rc);
initY += itemHgt;
}
}
else
{
var first = Math.Max(0, (_selectedIndex - _numItemsAboveBelowSelected));
for (int i = 0; i < children.Count; i++)
{
rc = new Rect(0, (initY + first * itemHgt) - Offset.Y, finalSize.Width, itemHgt);
children[i].Arrange(rc);
initY += itemHgt;
}
}
return finalSize;
}
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Up:
ScrollUp();
e.Handled = true;
break;
case Key.Down:
ScrollDown();
e.Handled = true;
break;
case Key.PageUp:
ScrollUp(4);
e.Handled = true;
break;
case Key.PageDown:
ScrollDown(4);
e.Handled = true;
break;
}
base.OnKeyDown(e);
}
/// <summary>
/// Refreshes the content of the visible items
/// </summary>
public void RefreshItems()
{
UpdateItems();
}
/// <summary>
/// Scrolls up the specified number of items
/// </summary>
public void ScrollUp(int numItems = 1)
{
var newY = Math.Max(Offset.Y - (numItems * ItemHeight), 0);
Offset = new Vector(0, newY);
}
/// <summary>
/// Scrolls down the specified number of items
/// </summary>
public void ScrollDown(int numItems = 1)
{
var scrollHeight = _extent.Height - Viewport.Height;
var newY = Math.Min(Offset.Y + (numItems * ItemHeight), scrollHeight);
Offset = new Vector(0, newY);
}
/// <summary>
/// Updates helper fields used in various calculations
/// </summary>
private void UpdateHelperInfo()
{
_range = _maximumValue - _minimumValue + 1;
_totalItems = (int)Math.Ceiling((double)_range / _increment);
var itemHgt = ItemHeight;
//If looping, measure 100x as many items as we actually have
_extent = new Size(0, ShouldLoop ? _totalItems * itemHgt * 100 : _totalItems * itemHgt);
//Height of 1 "set" of items
_extentOne = _totalItems * itemHgt;
_offset = new Vector(0, ShouldLoop ? _extentOne * 50 + _selectedIndex * itemHgt : _selectedIndex * itemHgt);
}
/// <summary>
/// Ensures enough containers are visible in the viewport
/// </summary>
/// <param name="children"></param>
private void CreateOrDestroyItems(Controls children)
{
int totalItemsInViewport = _numItemsAboveBelowSelected * 2 + 1;
if (!ShouldLoop)
{
int numItemAboveSelect = _numItemsAboveBelowSelected;
if (_selectedIndex - _numItemsAboveBelowSelected < 0)
numItemAboveSelect = _selectedIndex;
int numItemBelowSelect = _numItemsAboveBelowSelected;
if (_selectedIndex + _numItemsAboveBelowSelected >= _totalItems)
numItemBelowSelect = _totalItems - _selectedIndex - 1;
totalItemsInViewport = numItemBelowSelect + numItemAboveSelect + 1;
}
while (children.Count < totalItemsInViewport)
{
children.Add(new ListBoxItem
{
Height = ItemHeight,
Classes = new Classes("DateTimePickerItem", $"{PanelType}Item"),
VerticalContentAlignment = Avalonia.Layout.VerticalAlignment.Center,
Focusable = false
});
}
if (children.Count > totalItemsInViewport)
{
var numToRemove = children.Count - totalItemsInViewport;
children.RemoveRange(children.Count - numToRemove, numToRemove);
}
}
/// <summary>
/// Updates item content based on the current selection
/// and the panel type
/// </summary>
private void UpdateItems()
{
var children = Children;
var min = MinimumValue;
var panelType = PanelType;
var selected = SelectedValue;
var max = MaximumValue;
int first;
if (ShouldLoop)
{
first = (_selectedIndex - _numItemsAboveBelowSelected) % _totalItems;
first = first < 0 ? min + (first + _totalItems) * Increment : min + first * Increment;
}
else
{
first = min + Math.Max(0, _selectedIndex - _numItemsAboveBelowSelected) * Increment;
}
for (int i = 0; i < children.Count; i++)
{
ListBoxItem item = (ListBoxItem)children[i];
item.Content = FormatContent(first, panelType);
item.Tag = first;
item.IsSelected = first == selected;
first += Increment;
if (first > max)
first = min;
}
}
private string FormatContent(int value, DateTimePickerPanelType panelType)
{
switch (panelType)
{
case DateTimePickerPanelType.Year:
return new DateTime(value, 1, 1).ToString(ItemFormat);
case DateTimePickerPanelType.Month:
return new DateTime(FormatDate.Year, value, 1).ToString(ItemFormat);
case DateTimePickerPanelType.Day:
return new DateTime(FormatDate.Year, FormatDate.Month, value).ToString(ItemFormat);
case DateTimePickerPanelType.Hour:
return new TimeSpan(value, 0, 0).ToString(ItemFormat);
case DateTimePickerPanelType.Minute:
return new TimeSpan(0, value, 0).ToString(ItemFormat);
case DateTimePickerPanelType.TimePeriod:
return value == MinimumValue ? CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator :
CultureInfo.CurrentCulture.DateTimeFormat.PMDesignator;
default:
return "";
}
}
/// <summary>
/// Ensures the <see cref="SelectedValue"/> is within the bounds and
/// follows the current Increment
/// </summary>
private int CoerceSelected(int newValue)
{
if (newValue < MinimumValue)
return MinimumValue;
if (newValue > MaximumValue)
return MaximumValue;
if (newValue % Increment != 0)
{
var items = Enumerable.Range(MinimumValue, MaximumValue + 1).Where(i => i % Increment == 0).ToList();
var nearest = items.Aggregate((x, y) => Math.Abs(x - newValue) > Math.Abs(y - newValue) ? y : x);
return items.IndexOf(nearest) * Increment;
}
return newValue;
}
private void OnItemPointerDown(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed &&
e.Source is IVisual source)
{
_pressedItem = GetItemFromSource(source);
e.Handled = true;
}
}
private void OnItemPointerUp(object? sender, PointerReleasedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.PointerUpdateKind == PointerUpdateKind.LeftButtonReleased &&
_pressedItem != null &&
e.Source is IVisual source &&
GetItemFromSource(source) is ListBoxItem item &&
item.Tag is int tag)
{
SelectedValue = tag;
_pressedItem = null;
e.Handled = true;
}
}
//Helper to get ListBoxItem from pointerevent source
private ListBoxItem? GetItemFromSource(IVisual src)
{
var item = src;
while (item != null && !(item is ListBoxItem))
{
item = item.VisualParent;
}
return (ListBoxItem?)item;
}
public bool BringIntoView(IControl target, Rect targetRect) { return false; }
public IControl? GetControlInDirection(NavigationDirection direction, IControl? from) { return null; }
public void RaiseScrollInvalidated(EventArgs e)
{
ScrollInvalidated?.Invoke(this, e);
}
}
}