Implement INotifyCollectionChanged and IDictionary by delegating to ColorSchemes

This commit is contained in:
Brandon Thetford
2024-02-15 19:49:09 -07:00
parent e4024a5fc3
commit 3cd0832cb0

View File

@@ -1,4 +1,6 @@
#nullable enable
using System.Collections;
using System.Collections.Specialized;
using System.Text.Json.Serialization;
namespace Terminal.Gui;
@@ -7,7 +9,7 @@ namespace Terminal.Gui;
/// Holds the <see cref="ColorScheme"/>s that define the <see cref="Attribute"/>s that are used by views to render
/// themselves.
/// </summary>
public static class Colors
public sealed class Colors : INotifyCollectionChanged, IDictionary<string, ColorScheme?>
{
static Colors ()
{
@@ -60,11 +62,106 @@ public static class Colors
/// </para>
/// </remarks>
[SerializableConfigurationProperty (Scope = typeof (ThemeScope), OmitClassName = true)]
[JsonConverter (typeof (DictionaryJsonConverter<ColorScheme>))]
public static Dictionary<string, ColorScheme> ColorSchemes { get; private set; }
[JsonConverter (typeof (DictionaryJsonConverter<ColorScheme?>))]
public static Dictionary<string, ColorScheme?> ColorSchemes { get; }
/// <inheritdoc/>
public IEnumerator<KeyValuePair<string, ColorScheme?>> GetEnumerator () { return ColorSchemes.GetEnumerator (); }
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator () { return GetEnumerator (); }
/// <inheritdoc/>
public void Add (KeyValuePair<string, ColorScheme?> item)
{
ColorSchemes.Add (item.Key, item.Value);
CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, item));
}
/// <inheritdoc/>
public void Clear ()
{
ColorSchemes.Clear ();
CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Reset));
}
/// <inheritdoc/>
public bool Contains (KeyValuePair<string, ColorScheme?> item) { return ColorSchemes.Contains (item); }
/// <inheritdoc/>
public void CopyTo (KeyValuePair<string, ColorScheme?> [] array, int arrayIndex) { ((ICollection)ColorSchemes).CopyTo (array, arrayIndex); }
/// <inheritdoc/>
public bool Remove (KeyValuePair<string, ColorScheme?> item)
{
if (ColorSchemes.Remove (item.Key))
{
CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, item));
return true;
}
return false;
}
/// <inheritdoc/>
public int Count => ColorSchemes.Count;
/// <inheritdoc/>
public bool IsReadOnly => false;
/// <inheritdoc/>
public void Add (string key, ColorScheme? value) { Add (new (key, value)); }
/// <inheritdoc/>
public bool ContainsKey (string key) { return ColorSchemes.ContainsKey (key); }
/// <inheritdoc/>
public bool Remove (string key)
{
if (ColorSchemes.Remove (key))
{
CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, key));
return true;
}
return false;
}
/// <inheritdoc/>
public bool TryGetValue (string key, out ColorScheme? value) { return ColorSchemes.TryGetValue (key, out value); }
/// <inheritdoc/>
public ColorScheme? this [string key]
{
get => ColorSchemes [key];
set
{
if (ColorSchemes.TryAdd (key, value))
{
CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, new KeyValuePair<string, ColorScheme?> (key, value)));
}
else
{
ColorScheme? oldValue = ColorSchemes [key];
ColorSchemes [key] = value;
CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Replace, value, oldValue));
}
}
}
/// <inheritdoc/>
public ICollection<string> Keys => ColorSchemes.Keys;
/// <inheritdoc/>
public ICollection<ColorScheme?> Values => ColorSchemes.Values;
/// <inheritdoc/>
public event NotifyCollectionChangedEventHandler? CollectionChanged;
/// <summary>Resets the <see cref="ColorSchemes"/> dictionary to the default values.</summary>
public static Dictionary<string, ColorScheme> Reset ()
public static Dictionary<string, ColorScheme?> Reset ()
{
ColorSchemes.Clear ();
ColorSchemes.Add ("TopLevel", new ());