-
Couldn't load subscription status.
- Fork 725
Description
I am not going to tacklet this as part of
Instead, we'll do it as a follow-on PR.
Recall this conversation: #457 (comment)
Here's the latest Scheme Deep Dive that matches the latest impl in
Scheme Deep Dive
See Drawing for an overview of the drawing system and Configuration for an overview of the configuration system.
Scheme Overview
A Scheme is named a mapping from VisualRoles (e.g. VisualRole.Focus) to Attributes, defining how a View should look based on its purpose (e.g. Menu or Dialog). @Terminal.Gui.SchemeManager.Schemes is a dictionary of Schemes, indexed by name.
A Scheme defines how Views look based on their semantic purpose. The following schemes are supported:
| Scheme Name | Description |
|---|---|
| Base | The base scheme used for most Views. |
| TopLevel | The application Toplevel scheme; used for the Toplevel View. |
| Dialog | The dialog scheme; used for Dialog, MessageBox, and other views dialog-like views. |
| Menu | The menu scheme; used for Terminal.Gui.Menu, MenuBar, and StatusBar. |
| Error | The scheme for showing errors, such as in ErrorQuery. |
@Terminal.Gui.SchemeManager manages the set of available schemes and provides a set of convenience methods for getting the current scheme and for overriding the default values for these schemes.
var scheme = SchemeManager.GetCurrentSchemes () ["TopLevel"];ConfigurationManager can be used to override the default values for these schemes and add additional schemes.
Flexible Scheme Management in Terminal.Gui.View
A View's appearance is primarily determined by its Scheme, which maps semantic VisualRoles (like Normal, Focus, Disabled) to specific Attributes (foreground color, background color, and text style). Terminal.Gui provides a flexible system for managing these schemes:
-
Scheme Inheritance (Default Behavior):
- By default, if a
Viewdoes not have aSchemeexplicitly set, it inherits theSchemefrom itsSuperView(its parent in the view hierarchy). - This cascading behavior allows for consistent styling across related views. If no
SuperViewhas a scheme, (e.g., if the view is a top-level view), it ultimately falls back to the "Base" scheme defined inSchemeManager.GetCurrentSchemes(). - The
GetScheme()method implements this logic:- It first checks if a scheme has been explicitly set via the
_schemefield (see point 2). - If not, and if
SchemeNameis set, it tries to resolve the scheme by name fromSchemeManager. - If still no scheme, it recursively calls
SuperView.GetScheme(). - As a final fallback, it uses
SchemeManager.GetCurrentSchemes()["Base"]!.
- It first checks if a scheme has been explicitly set via the
- By default, if a
-
Explicit Scheme Assignment:
- You can directly assign a
Schemeobject to aViewusing theView.Schemeproperty (which callsSetScheme(value)). This overrides any inherited scheme. TheHasSchemeproperty will then returntrue. - Alternatively, you can set the
View.SchemeNameproperty to the name of a scheme registered inSchemeManager. IfSchemeitself hasn't been directly set,GetScheme()will useSchemeNameto look up the scheme. This is useful for declarative configurations (e.g., from a JSON file). - The
SetScheme(Scheme? scheme)method updates the internal_schemefield. If the new scheme is different from the current one, it marks the view for redraw (SetNeedsDraw()) to reflect the visual change. It also handles a special case forBorderto ensure its scheme is updated if itHasScheme.
- You can directly assign a
-
Event-Driven Customization:
The scheme resolution and application process includes events that allow for fine-grained control and customization:-
GettingSchemeEvent (View.Scheme.cs):- This event is raised within
GetScheme()before the default logic (inheritance,SchemeNamelookup, or explicit_schemeusage) fully determines the scheme. - Subscribers (which could be the
SuperView, aSubView, or any other interested component) can handle this event. - In the event handler, you can:
- Modify the scheme: Set
args.NewSchemeto a differentSchemeobject. - Cancel default resolution: Set
args.Cancel = true. If canceled, theSchemeprovided inargs.NewScheme(which might have been modified by the handler) is returned directly byGetScheme().
- Modify the scheme: Set
- The
OnGettingScheme(out Scheme? scheme)virtual method is called first, allowing derived classes to provide a scheme directly.
- This event is raised within
-
SettingSchemeEvent (View.Scheme.cs):- This event is raised within
SetScheme(Scheme? scheme)before the_schemefield is actually updated. - Subscribers can cancel the scheme change by setting
args.Cancel = truein the event handler. - The
OnSettingScheme(in Scheme? scheme)virtual method is called first, allowing derived classes to prevent the scheme from being set.
- This event is raised within
-
-
Retrieving and Applying Attributes for Visual Roles (
View.Attribute.cs):
Once aViewhas determined its activeScheme(viaGetScheme()), it uses this scheme to get specificAttributes for rendering different parts of itself based on theirVisualRole.-
GetAttributeForRole(VisualRole role):- This method first retrieves the base
Attributefor the givenrolefrom theView's currentScheme(GetScheme()!.GetAttributeForRole(role)). - It then raises the
GettingAttributeForRoleevent (and calls theOnGettingAttributeForRolevirtual method). - Subscribers to
GettingAttributeForRolecan:- Modify the attribute: Change the
args.NewValue(which is passed byrefasschemeAttributeto the event). - Cancel default behavior: Set
args.Cancel = true. The (potentially modified)args.NewValueis then returned.
- Modify the attribute: Change the
- Crucially, if the
ViewisEnabled == falseand the requestedroleis notVisualRole.Disabled, this method will recursively call itself to get theAttributeforVisualRole.Disabled. This ensures disabled views use their designated disabled appearance.
- This method first retrieves the base
-
SetAttributeForRole(VisualRole role):- This method is used to tell the
ConsoleDriverwhichAttributeto use for subsequent drawing operations (likeAddRuneorAddStr). - It first determines the appropriate
Attributefor therolefrom the currentScheme(similar toGetAttributeForRole). - It then raises the
SettingAttributeForRoleevent (and callsOnSettingAttributeForRole). - Subscribers can modify the
schemeAttribute(viaargs.NewValue) or cancel the operation (args.Cancel = true). - If not canceled, it calls
Driver.SetAttribute(schemeAttribute).
- This method is used to tell the
-
SetAttribute(Attribute attribute):- This is a more direct way to set the driver's current attribute, bypassing the scheme and role system. It's generally preferred to use
SetAttributeForRoleto maintain consistency with theScheme.
- This is a more direct way to set the driver's current attribute, bypassing the scheme and role system. It's generally preferred to use
-
Impact of SuperViews and SubViews via Events
-
SuperView Influence: A
SuperViewcan subscribe to itsSubView'sGettingSchemeorGettingAttributeForRoleevents. This would allow aSuperViewto dynamically alter how its children determine their schemes or specific attributes, perhaps based on theSuperView's state or other application logic. For example, a container view might want all its children to adopt a slightly modified version of its own scheme under certain conditions. -
SubView Influence (Less Common for Scheme of Parent): While a
SubViewcould subscribe to itsSuperView's scheme events, this is less typical for influencing theSuperView's own scheme. It's more common for aSubViewto react to changes in itsSuperView's scheme if needed, or to manage its own scheme independently. -
General Event Usage: These events are powerful for scenarios where:
- A specific
Viewinstance needs a unique, dynamically calculated appearance that isn't easily captured by a staticSchemeobject. - External logic needs to intercept and modify appearance decisions.
- Derived
Viewclasses want to implement custom scheme or attribute resolution logic by overriding theOn...methods.
- A specific
In summary, Terminal.Gui offers a layered approach to scheme management: straightforward inheritance and explicit setting for common cases, and a robust event system for advanced customization and dynamic control over how views derive and apply their visual attributes. This allows developers to achieve a wide range of visual styles and behaviors.
Proposal
Change the schemes to these:
| Scheme Name | Description |
|---|---|
| Base | The foundational scheme used for most View instances. All other schemes inherit from this. |
| Form | Used for form-like Views such as Dialog, MessageBox, and Wizard. |
| Command | Used by Menu, MenuBar, and StatusBar to style command surfaces. |
| Error | Used to indicate error states, such as ErrorQuery or failed validations. |
| Popover | Used for lightweight, transient UI like hover text, help popups, or dropdowns. |
Metadata
Metadata
Assignees
Labels
Type
Projects
Status