diff --git a/Terminal.Gui/Drawing/ColorScheme.Colors.cs b/Terminal.Gui/Drawing/ColorScheme.Colors.cs index 57dd4ad27..056298a47 100644 --- a/Terminal.Gui/Drawing/ColorScheme.Colors.cs +++ b/Terminal.Gui/Drawing/ColorScheme.Colors.cs @@ -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 s that define the s that are used by views to render /// themselves. /// -public static class Colors +public sealed class Colors : INotifyCollectionChanged, IDictionary { static Colors () { @@ -60,11 +62,106 @@ public static class Colors /// /// [SerializableConfigurationProperty (Scope = typeof (ThemeScope), OmitClassName = true)] - [JsonConverter (typeof (DictionaryJsonConverter))] - public static Dictionary ColorSchemes { get; private set; } + [JsonConverter (typeof (DictionaryJsonConverter))] + public static Dictionary ColorSchemes { get; } + + /// + public IEnumerator> GetEnumerator () { return ColorSchemes.GetEnumerator (); } + + /// + IEnumerator IEnumerable.GetEnumerator () { return GetEnumerator (); } + + /// + public void Add (KeyValuePair item) + { + ColorSchemes.Add (item.Key, item.Value); + CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, item)); + } + + /// + public void Clear () + { + ColorSchemes.Clear (); + CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Reset)); + } + + /// + public bool Contains (KeyValuePair item) { return ColorSchemes.Contains (item); } + + /// + public void CopyTo (KeyValuePair [] array, int arrayIndex) { ((ICollection)ColorSchemes).CopyTo (array, arrayIndex); } + + /// + public bool Remove (KeyValuePair item) + { + if (ColorSchemes.Remove (item.Key)) + { + CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, item)); + + return true; + } + + return false; + } + + /// + public int Count => ColorSchemes.Count; + + /// + public bool IsReadOnly => false; + + /// + public void Add (string key, ColorScheme? value) { Add (new (key, value)); } + + /// + public bool ContainsKey (string key) { return ColorSchemes.ContainsKey (key); } + + /// + public bool Remove (string key) + { + if (ColorSchemes.Remove (key)) + { + CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, key)); + + return true; + } + + return false; + } + + /// + public bool TryGetValue (string key, out ColorScheme? value) { return ColorSchemes.TryGetValue (key, out value); } + + /// + public ColorScheme? this [string key] + { + get => ColorSchemes [key]; + set + { + if (ColorSchemes.TryAdd (key, value)) + { + CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, new KeyValuePair (key, value))); + } + else + { + ColorScheme? oldValue = ColorSchemes [key]; + ColorSchemes [key] = value; + CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Replace, value, oldValue)); + } + } + } + + /// + public ICollection Keys => ColorSchemes.Keys; + + /// + public ICollection Values => ColorSchemes.Values; + + /// + public event NotifyCollectionChangedEventHandler? CollectionChanged; /// Resets the dictionary to the default values. - public static Dictionary Reset () + public static Dictionary Reset () { ColorSchemes.Clear (); ColorSchemes.Add ("TopLevel", new ());