#nullable enable
using System.ComponentModel;
namespace Terminal.Gui.ViewBase;
public partial class View
{
private string? _schemeName;
///
/// Gets or sets the name of the Scheme to use for this View. If set, it will override the scheme inherited from the
/// SuperView. If a Scheme was explicitly set ( is ),
/// this property will be ignored.
///
public string? SchemeName
{
get => _schemeName;
set
{
if (_schemeName == value)
{
return;
}
if (OnSettingSchemeName (in _schemeName, ref value))
{
_schemeName = value;
return;
}
StringPropertyEventArgs args = new (in _schemeName, ref value);
SettingSchemeName?.Invoke (this, args);
if (args.Cancel)
{
_schemeName = args.NewString;
return;
}
_schemeName = value;
}
}
///
/// Called when the for the View is to be set.
///
///
///
/// to stop default behavior.
protected virtual bool OnSettingSchemeName (in string? currentName, ref string? newName) { return false; }
/// Raised when the for the View is to be set.
///
/// Set to to stop default behavior.
///
public event EventHandler? SettingSchemeName;
// Both holds the set Scheme and is used to determine if a Scheme has been set or not
private Scheme? _scheme;
///
/// Gets whether a Scheme has been explicitly set for this View, or if it will inherit the Scheme from its
/// .
///
public bool HasScheme => _scheme is { };
///
/// Gets the Scheme for the View. If the Scheme has not been explicitly set (see ), gets
/// 's Scheme.
///
///
public Scheme GetScheme ()
{
if (OnGettingScheme (out Scheme? newScheme))
{
return newScheme!;
}
var args = new SchemeEventArgs (in _scheme, ref newScheme);
GettingScheme?.Invoke (this, args);
if (args.Cancel)
{
return args.NewScheme!;
}
if (!HasScheme && !string.IsNullOrEmpty (SchemeName))
{
return SchemeManager.GetScheme (SchemeName);
}
if (!HasScheme)
{
return SuperView?.GetScheme () ?? SchemeManager.GetScheme (Schemes.Base);
}
return _scheme!;
}
///
/// Called when the for the View is being retrieved. Overrides can return
/// to
/// stop further processing and optionally set to a different value.
///
/// to stop default behavior.
protected virtual bool OnGettingScheme (out Scheme? scheme)
{
scheme = null;
return false;
}
///
/// Raised when the for the View is being retrieved. Overrides can return
/// to
/// stop further processing and optionally set the in the event args to a different value.
///
///
/// Set `Cancel` to to stop default behavior.
///
public event EventHandler? GettingScheme;
///
/// Sets the Scheme for the View. Raises event before setting the scheme.
///
///
/// The scheme to set. If will be
/// .
///
/// if the scheme was set.
public bool SetScheme (Scheme? scheme)
{
if (_scheme == scheme)
{
return false;
}
if (OnSettingScheme (in scheme))
{
return false;
}
var args = new CancelEventArgs ();
SettingScheme?.Invoke (this, args);
if (args.Cancel)
{
return false;
}
_scheme = scheme;
// BUGBUG: This should be in Border.cs somehow
if (Border is { } && Border.LineStyle != LineStyle.None && Border.HasScheme)
{
Border.SetScheme (_scheme);
}
SetNeedsDraw ();
return true;
}
///
/// Called when the for the View is to be set.
///
///
/// to stop default behavior.
protected virtual bool OnSettingScheme (in Scheme? scheme) { return false; }
/// Raised when the for the View is to be set.
///
/// Set to to stop default behavior.
///
public event EventHandler? SettingScheme;
}