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

PropertyTypeDescription
PaletteTThemePaletteCurrent color palette with primary, secondary, background, text colors.
StyleDefsTObjectDictionaryComponent-specific style definitions keyed by component type.

TThemePalette

PropertyTypeDescription
PrimaryColorSTRINGPrimary brand color (hex).
SecondaryColorSTRINGSecondary accent color.
PrimaryBackgroundSTRINGMain background color.
SecondaryBackgroundSTRINGSecondary/card background.
PrimaryTextSTRINGMain text color.
SecondaryTextSTRINGMuted/secondary text.
BorderColorSTRINGDefault 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.