#nullable enable
using System.ComponentModel;
namespace Terminal.Gui;
public partial class View
{
// TODO: See https://github.com/gui-cs/Terminal.Gui/issues/4014
// TODO: See https://github.com/gui-cs/Terminal.Gui/issues/4016
// TODO: Enable ability to tell if ColorScheme was explicitly set; ColorScheme, as is, hides this.
internal ColorScheme? _colorScheme;
/// The color scheme for this view, if it is not defined, it returns the 's color scheme.
public virtual ColorScheme? ColorScheme
{
// BUGBUG: This prevents the ability to know if ColorScheme was explicitly set or not.
get => _colorScheme ?? SuperView?.ColorScheme;
set
{
if (_colorScheme == value)
{
return;
}
_colorScheme = value;
// BUGBUG: This should be in Border.cs somehow
if (Border is { } && Border.LineStyle != LineStyle.None && Border.ColorScheme is { })
{
Border.ColorScheme = _colorScheme;
}
SetNeedsDraw ();
}
}
/// Determines the current based on the value.
///
/// if is or
/// if is . If it's
/// overridden can return other values.
///
public virtual Attribute GetFocusColor ()
{
Attribute currAttribute = ColorScheme?.Normal ?? Attribute.Default;
var newAttribute = new Attribute ();
CancelEventArgs args = new (in currAttribute, ref newAttribute);
GettingFocusColor?.Invoke (this, args);
if (args.Cancel)
{
return args.NewValue;
}
ColorScheme? cs = ColorScheme ?? new ();
return Enabled ? GetColor (cs.Focus) : cs.Disabled;
}
///
/// Raised the Focus Color is being retrieved, from . Cancel the event and set the new
/// attribute in the event args to
/// a different value to change the focus color.
///
public event EventHandler>? GettingFocusColor;
/// Determines the current based on the value.
///
/// if is or
/// if is . If it's
/// overridden can return other values.
///
public virtual Attribute GetHotFocusColor ()
{
Attribute currAttribute = ColorScheme?.Normal ?? Attribute.Default;
var newAttribute = new Attribute ();
CancelEventArgs args = new (in currAttribute, ref newAttribute);
GettingHotFocusColor?.Invoke (this, args);
if (args.Cancel)
{
return args.NewValue;
}
ColorScheme? cs = ColorScheme ?? new ();
return Enabled ? GetColor (cs.HotFocus) : cs.Disabled;
}
///
/// Raised the HotFocus Color is being retrieved, from . Cancel the event and set the new
/// attribute in the event args to
/// a different value to change the focus color.
///
public event EventHandler>? GettingHotFocusColor;
/// Determines the current based on the value.
///
/// if is or
/// if is . If it's
/// overridden can return other values.
///
public virtual Attribute GetHotNormalColor ()
{
Attribute currAttribute = ColorScheme?.Normal ?? Attribute.Default;
var newAttribute = new Attribute ();
CancelEventArgs args = new (in currAttribute, ref newAttribute);
GettingHotNormalColor?.Invoke (this, args);
if (args.Cancel)
{
return args.NewValue;
}
ColorScheme? cs = ColorScheme ?? new ();
return Enabled ? GetColor (cs.HotNormal) : cs.Disabled;
}
///
/// Raised the HotNormal Color is being retrieved, from . Cancel the event and set the
/// new attribute in the event args to
/// a different value to change the focus color.
///
public event EventHandler>? GettingHotNormalColor;
/// Determines the current based on the value.
///
/// if is or
/// if is . If it's
/// overridden can return other values.
///
public virtual Attribute GetNormalColor ()
{
Attribute currAttribute = ColorScheme?.Normal ?? Attribute.Default;
var newAttribute = new Attribute ();
CancelEventArgs args = new (in currAttribute, ref newAttribute);
GettingNormalColor?.Invoke (this, args);
if (args.Cancel)
{
return args.NewValue;
}
ColorScheme? cs = ColorScheme ?? new ();
Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
{
disabled = new (disabled.Foreground.GetDarkerColor (), disabled.Background.GetDarkerColor ());
}
return Enabled ? GetColor (cs.Normal) : disabled;
}
///
/// Raised the Normal Color is being retrieved, from . Cancel the event and set the new
/// attribute in the event args to
/// a different value to change the focus color.
///
public event EventHandler>? GettingNormalColor;
///
/// Sets the Normal attribute if the setting process is not canceled. It triggers an event and checks for
/// cancellation before proceeding.
///
public void SetNormalAttribute ()
{
if (OnSettingNormalAttribute ())
{
return;
}
var args = new CancelEventArgs ();
SettingNormalAttribute?.Invoke (this, args);
if (args.Cancel)
{
return;
}
if (ColorScheme is { })
{
SetAttribute (GetNormalColor ());
}
}
///
/// Called when the normal attribute for the View is to be set. This is called before the View is drawn.
///
/// to stop default behavior.
protected virtual bool OnSettingNormalAttribute () { return false; }
/// Raised when the normal attribute for the View is to be set. This is raised before the View is drawn.
///
/// Set to to stop default behavior.
///
public event EventHandler? SettingNormalAttribute;
private Attribute GetColor (Attribute inputAttribute)
{
Attribute attr = inputAttribute;
if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
{
attr = new (attr.Foreground.GetDarkerColor (), attr.Background.GetDarkerColor ());
}
return attr;
}
}