Repository navigation
Expand file tree
/
Copy pathGroupExpanderStateStore.cs
More file actions
70 lines (59 loc) · 2.56 KB
/
Copy pathGroupExpanderStateStore.cs
File metadata and controls
70 lines (59 loc) · 2.56 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
namespace NETworkManager.Controls
{
public class GroupExpanderStateStore : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Occurs when a single group's expansion state changes via the indexer (not raised by <see cref="LoadState"/>).
/// </summary>
public event EventHandler<GroupExpandedChangedArgs> GroupExpandedChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
/// <summary>
/// Stores the expansion state of each group by its name.
/// </summary>
private readonly Dictionary<string, bool> _states = [];
/// <summary>
/// The indexer to get or set the expansion state of a group by its name.
/// </summary>
/// <param name="groupName">Name of the group.</param>
/// <returns>True if expanded, false if collapsed.</returns>
public bool this[string groupName]
{
get
{
// Default to expanded if not set
if (!_states.TryGetValue(groupName, out var val))
_states[groupName] = val = true;
return val;
}
set
{
if (_states.TryGetValue(groupName, out var existing) && existing == value)
return;
Debug.WriteLine("GroupExpanderStateStore: Setting state of '{0}' to {1}", groupName, value);
_states[groupName] = value;
OnPropertyChanged($"Item[{groupName}]");
GroupExpandedChanged?.Invoke(this, new GroupExpandedChangedArgs(groupName, value));
}
}
/// <summary>
/// Replaces all stored states with the given ones (e.g. bulk-loading previously persisted state).
/// Does not raise <see cref="PropertyChanged"/> or <see cref="GroupExpandedChanged"/> - consumers
/// (e.g. <see cref="GroupExpander"/>) read the current state fresh whenever they bind, so no
/// notification is needed, and this avoids a load-triggers-save feedback loop.
/// </summary>
public void LoadState(IReadOnlyDictionary<string, bool> state)
{
_states.Clear();
if (state == null)
return;
foreach (var kvp in state)
_states[kvp.Key] = kvp.Value;
}
}
}