#nullable enable using System.ComponentModel; namespace Terminal.Gui.ViewBase; public partial class View { #region Get /// Gets the current used by . /// The current attribute. public Attribute GetCurrentAttribute () { return Driver?.GetAttribute () ?? Attribute.Default; } /// /// Gets the associated with a specified /// from the . /// /// Raises / /// which can cancel the default behavior, and optionally change the attribute in the event args. /// /// /// If is , /// will be used instead of . /// To override this behavior use / /// to cancel the method, and return a different attribute. /// /// /// If is not and is , /// the will be used instead of . /// To override this behavior use / /// to cancel the method, and return a different attribute. /// /// /// The semantic describing the element being rendered. /// The corresponding from the . public Attribute GetAttributeForRole (VisualRole role) { Attribute schemeAttribute = GetScheme ()!.GetAttributeForRole (role); if (OnGettingAttributeForRole (role, ref schemeAttribute)) { // The implementation may have changed the attribute return schemeAttribute; } VisualRoleEventArgs args = new (role, newValue: ref schemeAttribute, currentValue: ref schemeAttribute); GettingAttributeForRole?.Invoke (this, args); if (args.Cancel) { // A handler may have changed the attribute return args.NewValue; } if (HighlightStyle != HighlightStyle.None) { if (MouseHovering && HighlightStyle.HasFlag (HighlightStyle.Hover) && role != VisualRole.Highlight && role != VisualRole.Disabled) { schemeAttribute = GetAttributeForRole (VisualRole.Highlight); } } return Enabled || role == VisualRole.Disabled ? schemeAttribute : GetAttributeForRole (VisualRole.Disabled); } /// /// Called when the Attribute for a is being retrieved. /// Implementations can /// return to stop further processing and optionally set the in the /// event args to a different value. /// /// /// The current value of the Attribute for the VisualRole. This by-ref value can be changed /// protected virtual bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute) { return false; } /// /// Raised when the Attribute for a is being retrieved. /// Handlers should check if /// has been set to and do nothing if so. If Cancel is /// a handler can set it to to stop further processing optionally change the /// `CurrentValue` in the event args to a different value. /// public event EventHandler? GettingAttributeForRole; #endregion Get #region Set /// /// Selects the specified Attribute /// as the Attribute to use for subsequent calls to and . /// /// THe Attribute to set. /// The previously set Attribute. public Attribute SetAttribute (Attribute attribute) { return Driver?.SetAttribute (attribute) ?? Attribute.Default; } /// /// Selects the Attribute associated with the specified /// as the Attribute to use for subsequent calls to and . /// /// Calls to get the Attribute associated with the specified role, which will /// raise /. /// /// /// The semantic describing the element being rendered. /// The previously set Attribute. public Attribute? SetAttributeForRole (VisualRole role) { Attribute schemeAttribute = GetAttributeForRole (role); Attribute currentAttribute = GetCurrentAttribute (); return SetAttribute (schemeAttribute); } #endregion Set }