|
@@ -11,9 +11,11 @@ namespace Terminal.Gui;
|
|
|
/// </summary>
|
|
|
public sealed class Colors : INotifyCollectionChanged, IDictionary<string, ColorScheme?>
|
|
|
{
|
|
|
+ private static readonly object _lock = new object ();
|
|
|
+
|
|
|
static Colors ()
|
|
|
{
|
|
|
- ColorSchemes = new (5, StringComparer.InvariantCultureIgnoreCase);
|
|
|
+ ColorSchemes = new Dictionary<string, ColorScheme?> (5, StringComparer.InvariantCultureIgnoreCase);
|
|
|
Reset ();
|
|
|
}
|
|
|
|
|
@@ -66,111 +68,195 @@ public sealed class Colors : INotifyCollectionChanged, IDictionary<string, Color
|
|
|
[UsedImplicitly]
|
|
|
public static Dictionary<string, ColorScheme?> ColorSchemes { get; private set; }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public IEnumerator<KeyValuePair<string, ColorScheme?>> GetEnumerator () { return ColorSchemes.GetEnumerator (); }
|
|
|
-
|
|
|
- /// <inheritdoc/>
|
|
|
- IEnumerator IEnumerable.GetEnumerator () { return GetEnumerator (); }
|
|
|
+ /// <summary>
|
|
|
+ /// Raised when the collection changes.
|
|
|
+ /// </summary>
|
|
|
+ public event NotifyCollectionChangedEventHandler? CollectionChanged;
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public void Add (KeyValuePair<string, ColorScheme?> item)
|
|
|
+ /// <inheritdoc />
|
|
|
+ public ColorScheme? this [string key]
|
|
|
{
|
|
|
- ColorSchemes.Add (item.Key, item.Value);
|
|
|
- CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, item));
|
|
|
+ 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<string, ColorScheme?> (key, value)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public void Clear ()
|
|
|
+ /// <inheritdoc />
|
|
|
+ public int Count
|
|
|
{
|
|
|
- ColorSchemes.Clear ();
|
|
|
- CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Reset));
|
|
|
+ get
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ return ColorSchemes.Count;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /// <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 IsReadOnly => false;
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public bool Remove (KeyValuePair<string, ColorScheme?> item)
|
|
|
+ /// <inheritdoc />
|
|
|
+ public ICollection<string> Keys
|
|
|
{
|
|
|
- if (ColorSchemes.Remove (item.Key))
|
|
|
+ get
|
|
|
{
|
|
|
- CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, item));
|
|
|
-
|
|
|
- return true;
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ return new List<string> (ColorSchemes.Keys);
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- return false;
|
|
|
+ /// <inheritdoc />
|
|
|
+ public ICollection<ColorScheme?> Values
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ return new List<ColorScheme?> (ColorSchemes.Values);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public int Count => ColorSchemes.Count;
|
|
|
+ /// <inheritdoc />
|
|
|
+ public void Add (KeyValuePair<string, ColorScheme?> item)
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ ColorSchemes.Add (item.Key, item.Value);
|
|
|
+ CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public bool IsReadOnly => false;
|
|
|
+ /// <inheritdoc />
|
|
|
+ public void Add (string key, ColorScheme? value)
|
|
|
+ {
|
|
|
+ Add (new KeyValuePair<string, ColorScheme?> (key, value));
|
|
|
+ }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public void Add (string key, ColorScheme? value) { Add (new (key, value)); }
|
|
|
+ /// <inheritdoc />
|
|
|
+ public void Clear ()
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ ColorSchemes.Clear ();
|
|
|
+ CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public bool ContainsKey (string key) { return ColorSchemes.ContainsKey (key); }
|
|
|
+ /// <inheritdoc />
|
|
|
+ public bool Contains (KeyValuePair<string, ColorScheme?> item)
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ return ColorSchemes.Contains (item);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public bool Remove (string key)
|
|
|
+ /// <inheritdoc />
|
|
|
+ public bool ContainsKey (string key)
|
|
|
{
|
|
|
- if (ColorSchemes.Remove (key))
|
|
|
+ lock (_lock)
|
|
|
{
|
|
|
- CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, key));
|
|
|
+ return ColorSchemes.ContainsKey (key);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return true;
|
|
|
+ public void CopyTo (KeyValuePair<string, ColorScheme?> [] array, int arrayIndex)
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ ((ICollection)ColorSchemes).CopyTo (array, arrayIndex);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- return false;
|
|
|
+ public IEnumerator<KeyValuePair<string, ColorScheme?>> GetEnumerator ()
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ return new List<KeyValuePair<string, ColorScheme?>> (ColorSchemes).GetEnumerator ();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public bool TryGetValue (string key, out ColorScheme? value) { return ColorSchemes.TryGetValue (key, out value); }
|
|
|
+ IEnumerator IEnumerable.GetEnumerator ()
|
|
|
+ {
|
|
|
+ return GetEnumerator ();
|
|
|
+ }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public ColorScheme? this [string key]
|
|
|
+ public bool Remove (KeyValuePair<string, ColorScheme?> item)
|
|
|
{
|
|
|
- get => ColorSchemes [key];
|
|
|
- set
|
|
|
+ lock (_lock)
|
|
|
{
|
|
|
- if (ColorSchemes.TryAdd (key, value))
|
|
|
+ if (ColorSchemes.Remove (item.Key))
|
|
|
{
|
|
|
- CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, new KeyValuePair<string, ColorScheme?> (key, value)));
|
|
|
+ CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item));
|
|
|
+ return true;
|
|
|
}
|
|
|
- else
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Remove (string key)
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ if (ColorSchemes.Remove (key))
|
|
|
{
|
|
|
- ColorScheme? oldValue = ColorSchemes [key];
|
|
|
- ColorSchemes [key] = value;
|
|
|
- CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Replace, value, oldValue));
|
|
|
+ CollectionChanged?.Invoke (this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, key));
|
|
|
+ return true;
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public ICollection<string> Keys => ColorSchemes.Keys;
|
|
|
+ /// <inheritdoc />
|
|
|
+ public bool TryGetValue (string key, out ColorScheme? value)
|
|
|
+ {
|
|
|
+ lock (_lock)
|
|
|
+ {
|
|
|
+ return ColorSchemes.TryGetValue (key, out value);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public ICollection<ColorScheme?> Values => ColorSchemes.Values;
|
|
|
|
|
|
- /// <inheritdoc/>
|
|
|
- public event NotifyCollectionChangedEventHandler? CollectionChanged;
|
|
|
-
|
|
|
- /// <summary>Resets the <see cref="ColorSchemes"/> dictionary to the default values.</summary>
|
|
|
+ /// <summary>
|
|
|
+ /// Resets the <see cref="ColorSchemes"/> dictionary to its default values.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
public static Dictionary<string, ColorScheme?> 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;
|
|
|
+ }
|
|
|
}
|
|
|
-}
|
|
|
+}
|