diff --git a/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs b/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs index ca14a4986..5c3bc6dba 100644 --- a/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs +++ b/Terminal.Gui/Drawing/Color/ColorScheme.Colors.cs @@ -11,9 +11,11 @@ namespace Terminal.Gui; /// public sealed class Colors : INotifyCollectionChanged, IDictionary { + private static readonly object _lock = new object (); + static Colors () { - ColorSchemes = new (5, StringComparer.InvariantCultureIgnoreCase); + ColorSchemes = new Dictionary (5, StringComparer.InvariantCultureIgnoreCase); Reset (); } @@ -66,111 +68,195 @@ public sealed class Colors : INotifyCollectionChanged, IDictionary ColorSchemes { get; private set; } - /// - 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; - - /// + /// + /// Raised when the collection changes. + /// public event NotifyCollectionChangedEventHandler? CollectionChanged; - /// Resets the dictionary to the default values. + /// + public ColorScheme? this [string key] + { + get + { + lock (_lock) + { + return ColorSchemes [key]; + } + } + set + { + lock (_lock) + { + if (ColorSchemes.ContainsKey (key)) + { + ColorScheme? oldValue = ColorSchemes [key]; + ColorSchemes [key] = value; + CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, value, oldValue)); + } + else + { + ColorSchemes.Add (key, value); + CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, new KeyValuePair (key, value))); + } + } + } + } + + /// + public int Count + { + get + { + lock (_lock) + { + return ColorSchemes.Count; + } + } + } + + /// + public bool IsReadOnly => false; + + /// + public ICollection Keys + { + get + { + lock (_lock) + { + return new List (ColorSchemes.Keys); + } + } + } + + /// + public ICollection Values + { + get + { + lock (_lock) + { + return new List (ColorSchemes.Values); + } + } + } + + /// + public void Add (KeyValuePair item) + { + lock (_lock) + { + ColorSchemes.Add (item.Key, item.Value); + CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item)); + } + } + + /// + public void Add (string key, ColorScheme? value) + { + Add (new KeyValuePair (key, value)); + } + + /// + public void Clear () + { + lock (_lock) + { + ColorSchemes.Clear (); + CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset)); + } + } + + /// + public bool Contains (KeyValuePair item) + { + lock (_lock) + { + return ColorSchemes.Contains (item); + } + } + + /// + public bool ContainsKey (string key) + { + lock (_lock) + { + return ColorSchemes.ContainsKey (key); + } + } + + public void CopyTo (KeyValuePair [] array, int arrayIndex) + { + lock (_lock) + { + ((ICollection)ColorSchemes).CopyTo (array, arrayIndex); + } + } + + public IEnumerator> GetEnumerator () + { + lock (_lock) + { + return new List> (ColorSchemes).GetEnumerator (); + } + } + + IEnumerator IEnumerable.GetEnumerator () + { + return GetEnumerator (); + } + + public bool Remove (KeyValuePair item) + { + lock (_lock) + { + if (ColorSchemes.Remove (item.Key)) + { + CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item)); + return true; + } + return false; + } + } + + public bool Remove (string key) + { + lock (_lock) + { + if (ColorSchemes.Remove (key)) + { + CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, key)); + return true; + } + return false; + } + } + + /// + public bool TryGetValue (string key, out ColorScheme? value) + { + lock (_lock) + { + return ColorSchemes.TryGetValue (key, out value); + } + } + + + /// + /// Resets the dictionary to its default values. + /// + /// public static Dictionary Reset () { - ColorSchemes.Clear (); - ColorSchemes.Add ("TopLevel", new ()); - ColorSchemes.Add ("Base", new ()); - ColorSchemes.Add ("Dialog", new ()); - ColorSchemes.Add ("Menu", new ()); - ColorSchemes.Add ("Error", new ()); - - return ColorSchemes; + lock (_lock) + { + ColorSchemes.Clear (); + ColorSchemes.Add ("TopLevel", new ColorScheme ()); + ColorSchemes.Add ("Base", new ColorScheme ()); + ColorSchemes.Add ("Dialog", new ColorScheme ()); + ColorSchemes.Add ("Menu", new ColorScheme ()); + ColorSchemes.Add ("Error", new ColorScheme ()); + return ColorSchemes; + } } -} +} \ No newline at end of file