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) { // Get the base attribute // If this view doesn't have an explicit scheme or scheme name, defer to SuperView for attribute resolution. // This allows parent views to customize attribute resolution for their children via // OnGettingAttributeForRole/GettingAttributeForRole. // This matches the logic in GetScheme() where SchemeName takes precedence over SuperView inheritance. Attribute schemeAttribute; if (!HasScheme && string.IsNullOrEmpty (SchemeName) && SuperView is { }) { schemeAttribute = SuperView.GetAttributeForRole (role); } else { schemeAttribute = GetScheme ()!.GetAttributeForRole (role); } if (OnGettingAttributeForRole (role, ref schemeAttribute)) { // The implementation may have changed the attribute return schemeAttribute; } VisualRoleEventArgs args = new (role, result: schemeAttribute); GettingAttributeForRole?.Invoke (this, args); if (args is { Handled: true, Result: { } }) { // A handler may have changed the attribute return args.Result.Value; } if (role != VisualRole.Disabled && HighlightStates != MouseState.None) { // The default behavior for HighlightStates of MouseState.Over is to use the Highlight role if (((HighlightStates.HasFlag (MouseState.In) && MouseState.HasFlag (MouseState.In)) || (HighlightStates.HasFlag (MouseState.Pressed) && MouseState.HasFlag (MouseState.Pressed))) && role != VisualRole.Highlight && !HasFocus) { 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 }