Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ViewSwitcherSelectionState.cs
1using System;
2
4{
5 internal sealed class ViewSwitcherSelectionState
6 {
7 private bool updatingFromIndex;
8 private bool updatingFromItem;
9 private bool updatingFromStateKey;
10
11 public ViewSwitcherSelectionState()
12 {
13 this.SelectedIndex = -1;
14 this.SelectedItem = null;
15 this.SelectedStateKey = null;
16 this.IndexBehavior = ViewSwitcherSelectedIndexBehavior.Clamp;
17 }
18
19 public int SelectedIndex { get; private set; }
20
21 public object? SelectedItem { get; private set; }
22
23 public string? SelectedStateKey { get; private set; }
24
25 public ViewSwitcherSelectedIndexBehavior IndexBehavior { get; set; }
26
27 public bool ShouldIgnore(ViewSwitcherSelectionChangeSource source)
28 {
29 return source switch
30 {
31 ViewSwitcherSelectionChangeSource.Index => this.updatingFromIndex,
32 ViewSwitcherSelectionChangeSource.Item => this.updatingFromItem,
33 ViewSwitcherSelectionChangeSource.StateKey => this.updatingFromStateKey,
34 _ => false
35 };
36 }
37
38 public SelectionStateUpdateScope BeginUpdate(ViewSwitcherSelectionChangeSource source)
39 {
40 this.SetUpdating(source, true);
41 return new SelectionStateUpdateScope(this, source);
42 }
43
44 public int NormalizeIndex(int requestedIndex, int totalCount)
45 {
46 if (requestedIndex < 0)
47 return -1;
48
49 if (totalCount <= 0)
50 {
51 switch (this.IndexBehavior)
52 {
54 throw new ArgumentOutOfRangeException(nameof(requestedIndex), requestedIndex, "No items are available.");
56 return this.SelectedIndex;
57 default:
58 return -1;
59 }
60 }
61
62 if (requestedIndex < totalCount)
63 return requestedIndex;
64
65 switch (this.IndexBehavior)
66 {
68 throw new ArgumentOutOfRangeException(nameof(requestedIndex), requestedIndex, "Requested index is outside the available range.");
70 return this.SelectedIndex;
72 {
73 int remainder = requestedIndex % totalCount;
74 if (remainder < 0)
75 remainder += totalCount;
76 return remainder;
77 }
79 default:
80 return totalCount - 1;
81 }
82 }
83
84 public void UpdateSnapshot(int index, object? item, string? stateKey)
85 {
86 this.SelectedIndex = index;
87 this.SelectedItem = item;
88 this.SelectedStateKey = stateKey;
89 }
90
91 public void Reset()
92 {
93 this.SelectedIndex = -1;
94 this.SelectedItem = null;
95 this.SelectedStateKey = null;
96 }
97
98 private void SetUpdating(ViewSwitcherSelectionChangeSource source, bool isUpdating)
99 {
100 switch (source)
101 {
102 case ViewSwitcherSelectionChangeSource.Index:
103 this.updatingFromIndex = isUpdating;
104 break;
105 case ViewSwitcherSelectionChangeSource.Item:
106 this.updatingFromItem = isUpdating;
107 break;
108 case ViewSwitcherSelectionChangeSource.StateKey:
109 this.updatingFromStateKey = isUpdating;
110 break;
111 }
112 }
113
114 public readonly struct SelectionStateUpdateScope : IDisposable
115 {
116 private readonly ViewSwitcherSelectionState owner;
117 private readonly ViewSwitcherSelectionChangeSource source;
118
119 public SelectionStateUpdateScope(ViewSwitcherSelectionState owner, ViewSwitcherSelectionChangeSource source)
120 {
121 this.owner = owner;
122 this.source = source;
123 }
124
125 public void Dispose()
126 {
127 this.owner.SetUpdating(this.source, false);
128 }
129 }
130 }
131}
Definition: ImplTypes.g.cs:58
ViewSwitcherSelectedIndexBehavior
Determines how ViewSwitcher handles attempts to set a selection outside the available range.