forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagerControl.cpp
803 lines (744 loc) · 31 KB
/
PagerControl.cpp
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "common.h"
#include "Vector.h"
#include "PagerControl.h"
#include "RuntimeProfiler.h"
#include "ResourceAccessor.h"
#include "PagerControlSelectedIndexChangedEventArgs.h"
#include "PagerControlTemplateSettings.h"
#include <PagerControlAutomationPeer.h>
constexpr auto c_numberBoxVisibleVisualState = L"NumberBoxVisible"sv;
constexpr auto c_comboBoxVisibleVisualState = L"ComboBoxVisible"sv;
constexpr auto c_numberPanelVisibleVisualState = L"NumberPanelVisible"sv;
constexpr auto c_firstPageButtonVisibleVisualState = L"FirstPageButtonVisible"sv;
constexpr auto c_firstPageButtonNotVisibleVisualState = L"FirstPageButtonCollapsed"sv;
constexpr auto c_firstPageButtonHiddenVisualState = L"FirstPageButtonHidden"sv;
constexpr auto c_firstPageButtonEnabledVisualState = L"FirstPageButtonEnabled"sv;
constexpr auto c_firstPageButtonDisabledVisualState = L"FirstPageButtonDisabled"sv;
constexpr auto c_previousPageButtonVisibleVisualState = L"PreviousPageButtonVisible"sv;
constexpr auto c_previousPageButtonNotVisibleVisualState = L"PreviousPageButtonCollapsed"sv;
constexpr auto c_previousPageButtonHiddenVisualState = L"PreviousPageButtonHidden"sv;
constexpr auto c_previousPageButtonEnabledVisualState = L"PreviousPageButtonEnabled"sv;
constexpr auto c_previousPageButtonDisabledVisualState = L"PreviousPageButtonDisabled"sv;
constexpr auto c_nextPageButtonVisibleVisualState = L"NextPageButtonVisible"sv;
constexpr auto c_nextPageButtonNotVisibleVisualState = L"NextPageButtonCollapsed"sv;
constexpr auto c_nextPageButtonHiddenVisualState = L"NextPageButtonHidden"sv;
constexpr auto c_nextPageButtonEnabledVisualState = L"NextPageButtonEnabled"sv;
constexpr auto c_nextPageButtonDisabledVisualState = L"NextPageButtonDisabled"sv;
constexpr auto c_lastPageButtonVisibleVisualState = L"LastPageButtonVisible"sv;
constexpr auto c_lastPageButtonNotVisibleVisualState = L"LastPageButtonCollapsed"sv;
constexpr auto c_lastPageButtonHiddenVisualState = L"LastPageButtonHidden"sv;
constexpr auto c_lastPageButtonEnabledVisualState = L"LastPageButtonEnabled"sv;
constexpr auto c_lastPageButtonDisabledVisualState = L"LastPageButtonDisabled"sv;
constexpr auto c_finiteItemsModeState = L"FiniteItems"sv;
constexpr auto c_infiniteItemsModeState = L"InfiniteItems"sv;
constexpr auto c_rootGridName = L"RootGrid"sv;
constexpr auto c_comboBoxName = L"ComboBoxDisplay"sv;
constexpr auto c_numberBoxName = L"NumberBoxDisplay"sv;
constexpr auto c_numberPanelRepeaterName = L"NumberPanelItemsRepeater"sv;
constexpr auto c_numberPanelIndicatorName = L"NumberPanelCurrentPageIndicator"sv;
constexpr auto c_firstPageButtonName = L"FirstPageButton"sv;
constexpr auto c_previousPageButtonName = L"PreviousPageButton"sv;
constexpr auto c_nextPageButtonName = L"NextPageButton"sv;
constexpr auto c_lastPageButtonName = L"LastPageButton"sv;
static constexpr auto c_numberPanelButtonStyleName = L"PagerControlNumberPanelButtonStyle"sv;
const int c_AutoDisplayModeNumberOfPagesThreshold = 10; // This integer determines when to switch between NumberBoxDisplayMode and ComboBoxDisplayMode
const int c_infiniteModeComboBoxItemsIncrement = 100;
/* Common functions */
PagerControl::PagerControl()
{
__RP_Marker_ClassById(RuntimeProfiler::ProfId_PagerControl);
m_comboBoxEntries = winrt::make<Vector<IInspectable>>().as<winrt::IObservableVector<IInspectable>>();
m_numberPanelElements = winrt::make<Vector<IInspectable>>().as<winrt::IObservableVector<IInspectable>>();
const auto templateSettings = winrt::make<PagerControlTemplateSettings>();
templateSettings.SetValue(PagerControlTemplateSettingsProperties::s_PagesProperty, m_comboBoxEntries);
templateSettings.SetValue(PagerControlTemplateSettingsProperties::s_NumberPanelItemsProperty, m_numberPanelElements);
SetValue(s_TemplateSettingsProperty, templateSettings);
SetDefaultStyleKey(this);
}
PagerControl::~PagerControl()
{
m_rootGridKeyDownRevoker.revoke();
m_comboBoxSelectionChangedRevoker.revoke();
m_firstPageButtonClickRevoker.revoke();
m_previousPageButtonClickRevoker.revoke();
m_nextPageButtonClickRevoker.revoke();
m_lastPageButtonClickRevoker.revoke();
}
void PagerControl::OnApplyTemplate()
{
if (PrefixText().empty())
{
PrefixText(ResourceAccessor::GetLocalizedStringResource(SR_PagerControlPrefixTextName));
}
if (SuffixText().empty())
{
SuffixText(ResourceAccessor::GetLocalizedStringResource(SR_PagerControlSuffixTextName));
}
if (const auto rootGrid = GetTemplateChildT<winrt::FrameworkElement>(c_rootGridName, *this))
{
m_rootGridKeyDownRevoker = rootGrid.KeyDown(winrt::auto_revoke, { this, &PagerControl::OnRootGridKeyDown });
}
if (const auto firstPageButton = GetTemplateChildT<winrt::Button>(c_firstPageButtonName, *this))
{
winrt::AutomationProperties::SetName(firstPageButton, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlFirstPageButtonTextName));
m_firstPageButtonClickRevoker = firstPageButton.Click(winrt::auto_revoke, { this, &PagerControl::FirstButtonClicked });
}
if (const auto previousPageButton = GetTemplateChildT<winrt::Button>(c_previousPageButtonName, *this))
{
winrt::AutomationProperties::SetName(previousPageButton, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlPreviousPageButtonTextName));
m_previousPageButtonClickRevoker = previousPageButton.Click(winrt::auto_revoke, { this, &PagerControl::PreviousButtonClicked });
}
if (const auto nextPageButton = GetTemplateChildT<winrt::Button>(c_nextPageButtonName, *this))
{
winrt::AutomationProperties::SetName(nextPageButton, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlNextPageButtonTextName));
m_nextPageButtonClickRevoker = nextPageButton.Click(winrt::auto_revoke, { this, &PagerControl::NextButtonClicked });
}
if (const auto lastPageButton = GetTemplateChildT<winrt::Button>(c_lastPageButtonName, *this))
{
winrt::AutomationProperties::SetName(lastPageButton, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlLastPageButtonTextName));
m_lastPageButtonClickRevoker = lastPageButton.Click(winrt::auto_revoke, { this, &PagerControl::LastButtonClicked });
}
m_comboBoxSelectionChangedRevoker.revoke();
[this](const winrt::ComboBox comboBox) {
m_comboBox.set(comboBox);
if (comboBox)
{
FillComboBoxCollectionToSize(NumberOfPages());
comboBox.SelectedIndex(SelectedPageIndex() - 1);
winrt::AutomationProperties::SetName(comboBox, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlPageTextName));
m_comboBoxSelectionChangedRevoker = comboBox.SelectionChanged(winrt::auto_revoke, { this, &PagerControl::ComboBoxSelectionChanged });
}
}(GetTemplateChildT<winrt::ComboBox>(c_comboBoxName, *this));
m_numberBoxValueChangedRevoker.revoke();
[this](const winrt::NumberBox numberBox) {
m_numberBox.set(numberBox);
if (numberBox)
{
numberBox.Value(SelectedPageIndex() + 1);
winrt::AutomationProperties::SetName(numberBox, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlPageTextName));
m_numberBoxValueChangedRevoker = numberBox.ValueChanged(winrt::auto_revoke, { this,&PagerControl::NumberBoxValueChanged });
}
}(GetTemplateChildT<winrt::NumberBox>(c_numberBoxName, *this));
m_numberPanelRepeater.set(GetTemplateChildT<winrt::ItemsRepeater>(c_numberPanelRepeaterName, *this));
m_selectedPageIndicator.set(GetTemplateChildT<winrt::FrameworkElement>(c_numberPanelIndicatorName, *this));
// This is for the initial loading of the control
OnDisplayModeChanged();
UpdateOnEdgeButtonVisualStates();
OnNumberOfPagesChanged(0);
// Update button visibilities
OnButtonVisibilityChanged(FirstButtonVisibility(),
c_firstPageButtonVisibleVisualState,
c_firstPageButtonNotVisibleVisualState,
c_firstPageButtonHiddenVisualState,
0);
OnButtonVisibilityChanged(PreviousButtonVisibility(),
c_previousPageButtonVisibleVisualState,
c_previousPageButtonNotVisibleVisualState,
c_previousPageButtonHiddenVisualState,
0);
OnButtonVisibilityChanged(NextButtonVisibility(),
c_nextPageButtonVisibleVisualState,
c_nextPageButtonNotVisibleVisualState,
c_nextPageButtonHiddenVisualState,
NumberOfPages() - 1);
OnButtonVisibilityChanged(LastButtonVisibility(),
c_lastPageButtonVisibleVisualState,
c_lastPageButtonNotVisibleVisualState,
c_lastPageButtonHiddenVisualState,
NumberOfPages() - 1);
OnSelectedPageIndexChange(-1);
}
void PagerControl::OnPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
winrt::IDependencyProperty property = args.Property();
if (this->Template() != nullptr)
{
if (property == FirstButtonVisibilityProperty())
{
OnButtonVisibilityChanged(FirstButtonVisibility(),
c_firstPageButtonVisibleVisualState,
c_firstPageButtonNotVisibleVisualState,
c_firstPageButtonHiddenVisualState,
0);
}
else if (property == PreviousButtonVisibilityProperty())
{
OnButtonVisibilityChanged(PreviousButtonVisibility(),
c_previousPageButtonVisibleVisualState,
c_previousPageButtonNotVisibleVisualState,
c_previousPageButtonHiddenVisualState,
0);
}
else if (property == NextButtonVisibilityProperty())
{
OnButtonVisibilityChanged(NextButtonVisibility(),
c_nextPageButtonVisibleVisualState,
c_nextPageButtonNotVisibleVisualState,
c_nextPageButtonHiddenVisualState,
NumberOfPages() - 1);
}
else if (property == LastButtonVisibilityProperty())
{
OnButtonVisibilityChanged(LastButtonVisibility(),
c_lastPageButtonVisibleVisualState,
c_lastPageButtonNotVisibleVisualState,
c_lastPageButtonHiddenVisualState,
NumberOfPages() - 1);
}
else if (property == DisplayModeProperty())
{
OnDisplayModeChanged();
// Why are we calling this you might ask.
// The reason is that that method only updates what it currently needs to update.
// So when we switch to ComboBox from NumberPanel, the NumberPanel element list might be out of date.
UpdateTemplateSettingElementLists();
}
else if (property == NumberOfPagesProperty())
{
OnNumberOfPagesChanged(winrt::unbox_value<int>(args.OldValue()));
}
else if (property == SelectedPageIndexProperty())
{
OnSelectedPageIndexChange(winrt::unbox_value<int>(args.OldValue()));
}
else if (property == ButtonPanelAlwaysShowFirstLastPageIndexProperty())
{
UpdateNumberPanel(NumberOfPages());
}
}
}
winrt::AutomationPeer PagerControl::OnCreateAutomationPeer()
{
return winrt::make<PagerControlAutomationPeer>(*this);
}
/* Property changed handlers */
void PagerControl::OnDisplayModeChanged()
{
// Cache for performance reasons
const auto displayMode = DisplayMode();
if (displayMode == winrt::PagerControlDisplayMode::ButtonPanel)
{
winrt::VisualStateManager::GoToState(*this, c_numberPanelVisibleVisualState, false);
}
else if (displayMode == winrt::PagerControlDisplayMode::ComboBox)
{
winrt::VisualStateManager::GoToState(*this, c_comboBoxVisibleVisualState, false);
}
else if (displayMode == winrt::PagerControlDisplayMode::NumberBox)
{
winrt::VisualStateManager::GoToState(*this, c_numberBoxVisibleVisualState, false);
}
else
{
UpdateDisplayModeAutoState();
}
}
void PagerControl::UpdateDisplayModeAutoState()
{
const int numberOfPages = NumberOfPages();
if (numberOfPages > -1)
{
winrt::VisualStateManager::GoToState(*this, numberOfPages < c_AutoDisplayModeNumberOfPagesThreshold ?
c_comboBoxVisibleVisualState : c_numberBoxVisibleVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_numberBoxVisibleVisualState, false);
}
}
void PagerControl::OnNumberOfPagesChanged(const int oldValue)
{
m_lastNumberOfPagesCount = oldValue;
const int numberOfPages = NumberOfPages();
if (numberOfPages < SelectedPageIndex() && numberOfPages > -1)
{
SelectedPageIndex(numberOfPages - 1);
}
UpdateTemplateSettingElementLists();
if (DisplayMode() == winrt::PagerControlDisplayMode::Auto)
{
UpdateDisplayModeAutoState();
}
if (numberOfPages > -1)
{
winrt::VisualStateManager::GoToState(*this, c_finiteItemsModeState, false);
if (const auto numberBox = m_numberBox.get())
{
numberBox.Maximum(numberOfPages);
}
}
else
{
winrt::VisualStateManager::GoToState(*this, c_infiniteItemsModeState, false);
if (const auto numberBox = m_numberBox.get())
{
numberBox.Maximum(INFINITY);
}
}
UpdateOnEdgeButtonVisualStates();
}
void PagerControl::OnSelectedPageIndexChange(const int oldValue)
{
// If we don't have any pages, there is nothing we should do.
// Ensure that SelectedPageIndex will end up in the valid range of values
// Special case is NumberOfPages being 0, in that case, don't verify upperbound restrictions
if (SelectedPageIndex() > NumberOfPages() - 1 && NumberOfPages() > 0)
{
SelectedPageIndex(NumberOfPages() - 1);
}
else if (SelectedPageIndex() < 0)
{
SelectedPageIndex(0);
}
// Now handle the value changes
m_lastSelectedPageIndex = oldValue;
if (const auto comboBox = m_comboBox.get())
{
if (SelectedPageIndex() < (int32_t)m_comboBoxEntries.Size())
{
comboBox.SelectedIndex(SelectedPageIndex());
}
}
if (const auto numBox = m_numberBox.get())
{
numBox.Value(SelectedPageIndex() + 1);
}
UpdateOnEdgeButtonVisualStates();
UpdateTemplateSettingElementLists();
if (DisplayMode() == winrt::PagerControlDisplayMode::ButtonPanel)
{
// NumberPanel needs to update based on multiple parameters.
// SelectedPageIndex is one of them, so let's do that now!
UpdateNumberPanel(NumberOfPages());
}
// Fire value property change for UIA
if (const auto peer = winrt::FrameworkElementAutomationPeer::FromElement(*this).try_as<winrt::PagerControlAutomationPeer>())
{
winrt::get_self<PagerControlAutomationPeer>(peer)->RaiseSelectionChanged(m_lastSelectedPageIndex, SelectedPageIndex());
}
RaiseSelectedIndexChanged();
}
void PagerControl::RaiseSelectedIndexChanged()
{
const auto args = winrt::make_self<PagerControlSelectedIndexChangedEventArgs>(m_lastSelectedPageIndex, SelectedPageIndex());
m_selectedIndexChangedEventSource(*this, *args);
}
void PagerControl::OnButtonVisibilityChanged(const winrt::PagerControlButtonVisibility visibility,
const wstring_view visibleStateName,
const wstring_view collapsedStateName,
const wstring_view hiddenStateName,
const int hiddenOnEdgePageCriteria)
{
if (visibility == winrt::PagerControlButtonVisibility::Visible)
{
winrt::VisualStateManager::GoToState(*this, visibleStateName, false);
}
else if (visibility == winrt::PagerControlButtonVisibility::Hidden)
{
winrt::VisualStateManager::GoToState(*this, collapsedStateName, false);
}
else
{
if (SelectedPageIndex() != hiddenOnEdgePageCriteria)
{
winrt::VisualStateManager::GoToState(*this, visibleStateName, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, hiddenStateName, false);
}
}
}
void PagerControl::UpdateTemplateSettingElementLists()
{
// Cache values for performance :)
const auto displayMode = DisplayMode();
const auto numberOfPages = NumberOfPages();
if (displayMode == winrt::PagerControlDisplayMode::ComboBox ||
displayMode == winrt::PagerControlDisplayMode::Auto)
{
if (numberOfPages > -1)
{
FillComboBoxCollectionToSize(numberOfPages);
}
else
{
if (m_comboBoxEntries.Size() < c_infiniteModeComboBoxItemsIncrement)
{
FillComboBoxCollectionToSize(c_infiniteModeComboBoxItemsIncrement);
}
}
}
else if (displayMode == winrt::PagerControlDisplayMode::ButtonPanel)
{
UpdateNumberPanel(numberOfPages);
}
}
void PagerControl::FillComboBoxCollectionToSize(const int numberOfPages)
{
const int currentComboBoxItemsCount = (int32_t)m_comboBoxEntries.Size();
if (currentComboBoxItemsCount <= numberOfPages)
{
// We are increasing the number of pages, so add the missing numbers.
for (int i = currentComboBoxItemsCount; i < numberOfPages; i++)
{
m_comboBoxEntries.Append(winrt::box_value(i + 1));
}
}
else
{
// We are decreasing the number of pages, so remove numbers starting at the end.
for (int i = currentComboBoxItemsCount; i > numberOfPages; i--)
{
m_comboBoxEntries.RemoveAtEnd();
}
}
}
void PagerControl::UpdateOnEdgeButtonVisualStates()
{
// Cache those values as we need them often and accessing a DP is (relatively) expensive
const int selectedPageIndex = SelectedPageIndex();
const int numberOfPages = NumberOfPages();
// Handle disabled/enabled status of buttons
if (selectedPageIndex == 0)
{
winrt::VisualStateManager::GoToState(*this, c_firstPageButtonDisabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_previousPageButtonDisabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_nextPageButtonEnabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_lastPageButtonEnabledVisualState, false);
}
else if (selectedPageIndex >= numberOfPages - 1)
{
winrt::VisualStateManager::GoToState(*this, c_firstPageButtonEnabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_previousPageButtonEnabledVisualState, false);
if (numberOfPages > -1)
{
winrt::VisualStateManager::GoToState(*this, c_nextPageButtonDisabledVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_nextPageButtonEnabledVisualState, false);
}
winrt::VisualStateManager::GoToState(*this, c_lastPageButtonDisabledVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_firstPageButtonEnabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_previousPageButtonEnabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_nextPageButtonEnabledVisualState, false);
winrt::VisualStateManager::GoToState(*this, c_lastPageButtonEnabledVisualState, false);
}
// Handle HiddenOnEdge states
if (FirstButtonVisibility() == winrt::PagerControlButtonVisibility::HiddenOnEdge)
{
if (selectedPageIndex != 0)
{
winrt::VisualStateManager::GoToState(*this, c_firstPageButtonVisibleVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_firstPageButtonHiddenVisualState, false);
}
}
if (PreviousButtonVisibility() == winrt::PagerControlButtonVisibility::HiddenOnEdge)
{
if (selectedPageIndex != 0)
{
winrt::VisualStateManager::GoToState(*this, c_previousPageButtonVisibleVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_previousPageButtonHiddenVisualState, false);
}
}
if (NextButtonVisibility() == winrt::PagerControlButtonVisibility::HiddenOnEdge)
{
if (selectedPageIndex != numberOfPages - 1)
{
winrt::VisualStateManager::GoToState(*this, c_nextPageButtonVisibleVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_nextPageButtonHiddenVisualState, false);
}
}
if (LastButtonVisibility() == winrt::PagerControlButtonVisibility::HiddenOnEdge)
{
if (selectedPageIndex != numberOfPages - 1)
{
winrt::VisualStateManager::GoToState(*this, c_lastPageButtonVisibleVisualState, false);
}
else
{
winrt::VisualStateManager::GoToState(*this, c_lastPageButtonHiddenVisualState, false);
}
}
}
/* NumberPanel logic */
void PagerControl::UpdateNumberPanel(const int numberOfPages)
{
if (numberOfPages < 0)
{
UpdateNumberOfPanelCollectionInfiniteItems();
}
else if (numberOfPages < 8)
{
UpdateNumberPanelCollectionAllItems(numberOfPages);
}
else
{
const auto selectedIndex = SelectedPageIndex();
// Idea: Choose correct "template" based on SelectedPageIndex (x) and NumberOfPages (n)
// 1 2 3 4 5 6 ... n <-- Items
if (selectedIndex < 4)
{
// First four items selected, create following pattern:
// 1 2 3 4 5... n
UpdateNumberPanelCollectionStartWithEllipsis(numberOfPages, selectedIndex);
}
else if (selectedIndex >= numberOfPages - 4)
{
// Last four items selected, create following pattern:
//1 [...] n-4 n-3 n-2 n-1 n
UpdateNumberPanelCollectionEndWithEllipsis(numberOfPages, selectedIndex);
}
else
{
// Neither start or end, so lets do this:
// 1 [...] x-2 x-1 x x+1 x+2 [...] n
// where x > 4 and x < n - 4
UpdateNumberPanelCollectionCenterWithEllipsis(numberOfPages, selectedIndex);
}
}
}
void PagerControl::UpdateNumberOfPanelCollectionInfiniteItems()
{
const int selectedIndex = SelectedPageIndex();
m_numberPanelElements.Clear();
if (selectedIndex < 3)
{
AppendButtonToNumberPanelList(1, 0);
AppendButtonToNumberPanelList(2, 0);
AppendButtonToNumberPanelList(3, 0);
AppendButtonToNumberPanelList(4, 0);
AppendButtonToNumberPanelList(5, 0);
MoveIdentifierToElement(selectedIndex);
}
else
{
AppendButtonToNumberPanelList(1, 0);
AppendEllipsisIconToNumberPanelList();
AppendButtonToNumberPanelList(selectedIndex, 0);
AppendButtonToNumberPanelList(selectedIndex + 1, 0);
AppendButtonToNumberPanelList(selectedIndex + 2, 0);
MoveIdentifierToElement(3);
}
}
void PagerControl::UpdateNumberPanelCollectionAllItems(int numberOfPages)
{
// Check that the NumberOfPages did not change, so we can skip recreating collection
if (m_lastNumberOfPagesCount != numberOfPages)
{
m_numberPanelElements.Clear();
for (int i = 0; i < numberOfPages && i < 7; i++)
{
AppendButtonToNumberPanelList(i + 1, numberOfPages);
}
}
MoveIdentifierToElement(SelectedPageIndex());
}
void PagerControl::UpdateNumberPanelCollectionStartWithEllipsis(int numberOfPages, int selectedIndex)
{
if (m_lastNumberOfPagesCount != numberOfPages)
{
// Do a recalculation as the number of pages changed.
m_numberPanelElements.Clear();
AppendButtonToNumberPanelList(1, numberOfPages);
AppendButtonToNumberPanelList(2, numberOfPages);
AppendButtonToNumberPanelList(3, numberOfPages);
AppendButtonToNumberPanelList(4, numberOfPages);
AppendButtonToNumberPanelList(5, numberOfPages);
if (ButtonPanelAlwaysShowFirstLastPageIndex())
{
AppendEllipsisIconToNumberPanelList();
AppendButtonToNumberPanelList(numberOfPages, numberOfPages);
}
}
// SelectedIndex is definitely the correct index here as we are counting from start
MoveIdentifierToElement(selectedIndex);
}
void PagerControl::UpdateNumberPanelCollectionEndWithEllipsis(int numberOfPages, int selectedIndex)
{
if (m_lastNumberOfPagesCount != numberOfPages)
{
// Do a recalculation as the number of pages changed.
m_numberPanelElements.Clear();
if (ButtonPanelAlwaysShowFirstLastPageIndex())
{
AppendButtonToNumberPanelList(1, numberOfPages);
AppendEllipsisIconToNumberPanelList();
}
AppendButtonToNumberPanelList(numberOfPages - 4, numberOfPages);
AppendButtonToNumberPanelList(numberOfPages - 3, numberOfPages);
AppendButtonToNumberPanelList(numberOfPages - 2, numberOfPages);
AppendButtonToNumberPanelList(numberOfPages - 1, numberOfPages);
AppendButtonToNumberPanelList(numberOfPages, numberOfPages);
}
// We can only be either the last, the second from last or third from last
// => SelectedIndex = NumberOfPages - y with y in {1,2,3}
if (ButtonPanelAlwaysShowFirstLastPageIndex())
{
// Last item sits at index 4.
// SelectedPageIndex for the last page is NumberOfPages - 1.
// => SelectedItem = SelectedIndex - NumberOfPages + 7;
MoveIdentifierToElement(selectedIndex - numberOfPages + 7);
}
else
{
// Last item sits at index 6.
// SelectedPageIndex for the last page is NumberOfPages - 1.
// => SelectedItem = SelectedIndex - NumberOfPages + 5;
MoveIdentifierToElement(selectedIndex - numberOfPages + 5);
}
}
void PagerControl::UpdateNumberPanelCollectionCenterWithEllipsis(int numberOfPages, int selectedIndex)
{
const auto showFirstLastPageIndex = ButtonPanelAlwaysShowFirstLastPageIndex();
if (m_lastNumberOfPagesCount != numberOfPages)
{
// Do a recalculation as the number of pages changed.
m_numberPanelElements.Clear();
if (showFirstLastPageIndex)
{
AppendButtonToNumberPanelList(1, numberOfPages);
AppendEllipsisIconToNumberPanelList();
}
AppendButtonToNumberPanelList(selectedIndex, numberOfPages);
AppendButtonToNumberPanelList(selectedIndex + 1, numberOfPages);
AppendButtonToNumberPanelList(selectedIndex + 2, numberOfPages);
if (showFirstLastPageIndex)
{
AppendEllipsisIconToNumberPanelList();
AppendButtonToNumberPanelList(numberOfPages, numberOfPages);
}
}
// "selectedIndex + 1" is our representation for SelectedIndex.
if (showFirstLastPageIndex)
{
// SelectedIndex + 1 is the fifth element.
// Collections are base zero, so the index to underline is 3.
MoveIdentifierToElement(3);
}
else
{
// SelectedIndex + 1 is the third element.
// Collections are base zero, so the index to underline is 1.
MoveIdentifierToElement(1);
}
}
void PagerControl::MoveIdentifierToElement(int index)
{
if (const auto indicator = m_selectedPageIndicator.get())
{
if (const auto repeater = m_numberPanelRepeater.get())
{
repeater.UpdateLayout();
if (const auto element = repeater.TryGetElement(index).try_as<winrt::FrameworkElement>())
{
const auto boundingRect = element.TransformToVisual(repeater).TransformBounds(winrt::Rect::Rect(0, 0, (float)repeater.ActualWidth(), (float)repeater.ActualHeight()));
winrt::Thickness newMargin;
newMargin.Left = boundingRect.X;
newMargin.Top = 0;
newMargin.Right = 0;
newMargin.Bottom = 0;
indicator.Margin(newMargin);
indicator.Width(element.ActualWidth());
}
}
}
}
void PagerControl::AppendButtonToNumberPanelList(const int pageNumber, const int numberOfPages)
{
winrt::Button button;
button.Content(winrt::box_value(pageNumber));
const auto buttonClickedFunc = [this](auto const& sender, auto const& args) {
if (const auto button = sender.try_as<winrt::Button>())
{
const int unboxedValue = winrt::unbox_value<int>(button.Content());
SelectedPageIndex(unboxedValue - 1);
}
};
button.Click(buttonClickedFunc);
// Set the default style of buttons
button.Style(unbox_value<winrt::Style>(ResourceAccessor::ResourceLookup(*this, box_value(c_numberPanelButtonStyleName))));
winrt::AutomationProperties::SetName(button, ResourceAccessor::GetLocalizedStringResource(SR_PagerControlPageTextName) + L" " + winrt::to_hstring(pageNumber));
winrt::AutomationProperties::SetPositionInSet(button, pageNumber);
winrt::AutomationProperties::SetSizeOfSet(button, numberOfPages);
m_numberPanelElements.Append(button);
}
void PagerControl::AppendEllipsisIconToNumberPanelList()
{
winrt::SymbolIcon ellipsisIcon;
ellipsisIcon.Symbol(winrt::Symbol::More);
m_numberPanelElements.Append(ellipsisIcon);
}
/* Interaction event listeners */
void PagerControl::OnRootGridKeyDown(const winrt::IInspectable& sender, const winrt::KeyRoutedEventArgs& args)
{
if (args.Key() == winrt::VirtualKey::Left || args.Key() == winrt::VirtualKey::GamepadDPadLeft)
{
winrt::FocusManager::TryMoveFocus(winrt::FocusNavigationDirection::Left);
}
else if (args.Key() == winrt::VirtualKey::Right || args.Key() == winrt::VirtualKey::GamepadDPadRight)
{
winrt::FocusManager::TryMoveFocus(winrt::FocusNavigationDirection::Right);
}
}
void PagerControl::ComboBoxSelectionChanged(const winrt::IInspectable& sender, const winrt::SelectionChangedEventArgs& args)
{
if (const auto comboBox = m_comboBox.get())
{
SelectedPageIndex(comboBox.SelectedIndex());
}
}
void PagerControl::NumberBoxValueChanged(const winrt::IInspectable& sender, const winrt::NumberBoxValueChangedEventArgs& args)
{
SelectedPageIndex((int)(args.NewValue()) - 1);
}
void PagerControl::FirstButtonClicked(const IInspectable& sender, const winrt::RoutedEventArgs& e)
{
SelectedPageIndex(0);
if (const auto command = FirstButtonCommand())
{
command.Execute(nullptr);
}
}
void PagerControl::PreviousButtonClicked(const IInspectable& sender, const winrt::RoutedEventArgs& e)
{
// In this method, SelectedPageIndex is always greater than 1.
SelectedPageIndex(SelectedPageIndex() - 1);
if (const auto command = PreviousButtonCommand())
{
command.Execute(nullptr);
}
}
void PagerControl::NextButtonClicked(const IInspectable& sender, const winrt::RoutedEventArgs& e)
{
// In this method, SelectedPageIndex is always less than maximum.
SelectedPageIndex(SelectedPageIndex() + 1);
if (const auto command = NextButtonCommand())
{
command.Execute(nullptr);
}
}
void PagerControl::LastButtonClicked(const IInspectable& sender, const winrt::RoutedEventArgs& e)
{
SelectedPageIndex(NumberOfPages() - 1);
if (const auto command = LastButtonCommand())
{
command.Execute(nullptr);
}
}