TThemeManager
Central theme management component. Manages color palettes and component style definitions. Notifies IThemeSubscriber components when theme changes.
CoreThemeObserver Pattern
Overview
TThemeManager is a non-visual component that manages application theming. Components implementing IThemeSubscriber can subscribe to receive automatic style updates when the theme changes.
Key Properties
| Property | Type | Description |
|---|---|---|
| Palette | TThemePalette | Current color palette with primary, secondary, background, text colors. |
| StyleDefs | TObjectDictionary | Component-specific style definitions keyed by component type. |
TThemePalette
| Property | Type | Description |
|---|---|---|
| PrimaryColor | STRING | Primary brand color (hex). |
| SecondaryColor | STRING | Secondary accent color. |
| PrimaryBackground | STRING | Main background color. |
| SecondaryBackground | STRING | Secondary/card background. |
| PrimaryText | STRING | Main text color. |
| SecondaryText | STRING | Muted/secondary text. |
| BorderColor | STRING | Default border color. |
Key Methods
PROCEDURE Subscribe(Subscriber: IThemeSubscriber);
Registers a component to receive theme updates.
PROCEDURE Unsubscribe(Subscriber: IThemeSubscriber);
Removes a component from receiving theme updates.
PROCEDURE NotifySubscribers;
Broadcasts current theme to all subscribers. Called automatically when theme changes.
FUNCTION GetStyleDef(ComponentType: STRING): TComponentStyleDef;
Returns style definition for a specific component type (e.g., 'button', 'card', 'header').
IThemeSubscriber Interface
IThemeSubscriber = INTERFACE
PROCEDURE ApplyThemeStyles(ThemeManager: TThemeManager);
FUNCTION GetSubscriberComponentType: STRING;
END;
Usage Pattern
// In component's Loaded method:
ThemeManagerInstance := FindThemeManager(Self);
IF Assigned(ThemeManagerInstance) THEN
ThemeManagerInstance.Subscribe(Self);
// In component's ApplyThemeStyles:
PROCEDURE TMyCard.ApplyThemeStyles(ThemeManager: TThemeManager);
VAR
CardStyle: TComponentStyleDef;
BEGIN
CardStyle := ThemeManager.GetStyleDef('card');
IF Assigned(CardStyle) THEN
TDom.SetStyle(ElementHandle, 'background-color', CardStyle.BackgroundColor);
END;
FindThemeManager: Use the helper function
FindThemeManager(Self) to locate the ThemeManager in the component hierarchy. It searches up through parent components.