Files
Terminal.Gui/Terminal.Gui/Configuration/Scope.cs
dodexahedron 34bef2c839 Fixes #3242 - Replaces simple null checks (#3248)
* Replace all 342 `== null` with `is null`

* Replace 354 `!= null` with `is { }`

* Wrap these in conditionals since they break tests against Release configuration

The members they depend on do not exist in Release configuration

* Split these up and dispose properly

This test needs to be revisited for several reasons at some point.

* Fix release configuration tests

* Declare interface these already support

* Annotate constructor properly and use throw helper

* Move class to its own file

* Rename these files so they nest in the solution explorer

* Make this a record type and remove now-redundant/illegal members

* Reference passing to avoid some struct copies

* Simplify this

* Carry reference passing through as appropriate

* Turn this into a record struct

* Remove unused internal constructor and its test

It was only used by that test.

* Simplify this constructor

* This should be a property

* Simplify constructor

* Simplify GetHashCode

* Mark this ignored just in case

* Missed a couple of opportunities for reference passing

* record struct already does this by value

* Remove unused class

* Simplify the type initializer and Reset method

* Implement INotifyCollectionChanged and IDictionary by delegating to ColorSchemes

* Fix for reflection-based configuration

* Make CI  build happy by disambiguiating this attribute
2024-02-16 16:46:25 -07:00

82 lines
3.1 KiB
C#

#nullable enable
using System.Reflection;
namespace Terminal.Gui;
/// <summary>
/// Defines a configuration settings scope. Classes that inherit from this abstract class can be used to define
/// scopes for configuration settings. Each scope is a JSON object that contains a set of configuration settings.
/// </summary>
public class Scope<T> : Dictionary<string, ConfigProperty>
{ //, IScope<Scope<T>> {
/// <summary>Crates a new instance.</summary>
public Scope () : base (StringComparer.InvariantCultureIgnoreCase)
{
foreach (KeyValuePair<string, ConfigProperty> p in GetScopeProperties ())
{
Add (p.Key, new ConfigProperty { PropertyInfo = p.Value.PropertyInfo, PropertyValue = null });
}
}
/// <summary>Retrieves the values of the properties of this scope from their corresponding static properties.</summary>
public void RetrieveValues ()
{
foreach (KeyValuePair<string, ConfigProperty> p in this.Where (cp => cp.Value.PropertyInfo is { }))
{
p.Value.RetrieveValue ();
}
}
/// <summary>Updates this instance from the specified source scope.</summary>
/// <param name="source"></param>
/// <returns>The updated scope (this).</returns>
public Scope<T>? Update (Scope<T> source)
{
foreach (KeyValuePair<string, ConfigProperty> prop in source)
{
if (ContainsKey (prop.Key))
{
this [prop.Key].PropertyValue = this [prop.Key].UpdateValueFrom (prop.Value.PropertyValue!);
}
else
{
this [prop.Key].PropertyValue = prop.Value.PropertyValue;
}
}
return this;
}
/// <summary>Applies the values of the properties of this scope to their corresponding static properties.</summary>
/// <returns></returns>
internal virtual bool Apply ()
{
var set = false;
foreach (KeyValuePair<string, ConfigProperty> p in this.Where (
t => t.Value != null
&& t.Value.PropertyValue != null
))
{
if (p.Value.Apply ())
{
set = true;
}
}
return set;
}
private IEnumerable<KeyValuePair<string, ConfigProperty>> GetScopeProperties ()
{
return _allConfigProperties!.Where (
cp =>
(cp.Value.PropertyInfo?.GetCustomAttribute (
typeof (SerializableConfigurationProperty)
)
as SerializableConfigurationProperty)?.Scope
== GetType ()
);
}
}