5 internal sealed
class ViewSwitcherSelectionState
7 private bool updatingFromIndex;
8 private bool updatingFromItem;
9 private bool updatingFromStateKey;
11 public ViewSwitcherSelectionState()
13 this.SelectedIndex = -1;
14 this.SelectedItem =
null;
15 this.SelectedStateKey =
null;
19 public int SelectedIndex {
get;
private set; }
21 public object? SelectedItem {
get;
private set; }
23 public string? SelectedStateKey {
get;
private set; }
27 public bool ShouldIgnore(ViewSwitcherSelectionChangeSource source)
31 ViewSwitcherSelectionChangeSource.Index => this.updatingFromIndex,
32 ViewSwitcherSelectionChangeSource.Item => this.updatingFromItem,
33 ViewSwitcherSelectionChangeSource.StateKey => this.updatingFromStateKey,
38 public SelectionStateUpdateScope BeginUpdate(ViewSwitcherSelectionChangeSource source)
40 this.SetUpdating(source,
true);
41 return new SelectionStateUpdateScope(
this, source);
44 public int NormalizeIndex(
int requestedIndex,
int totalCount)
46 if (requestedIndex < 0)
51 switch (this.IndexBehavior)
54 throw new ArgumentOutOfRangeException(nameof(requestedIndex), requestedIndex,
"No items are available.");
56 return this.SelectedIndex;
62 if (requestedIndex < totalCount)
63 return requestedIndex;
65 switch (this.IndexBehavior)
68 throw new ArgumentOutOfRangeException(nameof(requestedIndex), requestedIndex,
"Requested index is outside the available range.");
70 return this.SelectedIndex;
73 int remainder = requestedIndex % totalCount;
75 remainder += totalCount;
80 return totalCount - 1;
84 public void UpdateSnapshot(
int index,
object? item,
string? stateKey)
86 this.SelectedIndex = index;
87 this.SelectedItem = item;
88 this.SelectedStateKey = stateKey;
93 this.SelectedIndex = -1;
94 this.SelectedItem =
null;
95 this.SelectedStateKey =
null;
98 private void SetUpdating(ViewSwitcherSelectionChangeSource source,
bool isUpdating)
102 case ViewSwitcherSelectionChangeSource.Index:
103 this.updatingFromIndex = isUpdating;
105 case ViewSwitcherSelectionChangeSource.Item:
106 this.updatingFromItem = isUpdating;
108 case ViewSwitcherSelectionChangeSource.StateKey:
109 this.updatingFromStateKey = isUpdating;
116 private readonly ViewSwitcherSelectionState owner;
117 private readonly ViewSwitcherSelectionChangeSource source;
122 this.source = source;
125 public void Dispose()
127 this.owner.SetUpdating(this.source,
false);
ViewSwitcherSelectedIndexBehavior
Determines how ViewSwitcher handles attempts to set a selection outside the available range.