Skip to content

Instantly share code, notes, and snippets.

@DelphiWorlds
Created June 27, 2024 12:50
Show Gist options
  • Save DelphiWorlds/47324275b9724020cfdc4e144ceb8504 to your computer and use it in GitHub Desktop.
Save DelphiWorlds/47324275b9724020cfdc4e144ceb8504 to your computer and use it in GitHub Desktop.
Example of handling theme changes on mobile
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Messaging,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Platform;
type
TForm1 = class(TForm)
Label1: TLabel;
private
FTheme: TSystemThemeKind;
procedure CheckTheme(const ATheme: TSystemThemeKind; const AForce: Boolean = False);
procedure SystemAppearanceChangedMessageHandler(const Sender: TObject; const AMsg: TMessage);
procedure UpdateTheme;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
TMessageManager.DefaultManager.SubscribeToMessage(TSystemAppearanceChangedMessage, SystemAppearanceChangedMessageHandler);
UpdateTheme;
end;
destructor TForm1.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TSystemAppearanceChangedMessage, SystemAppearanceChangedMessageHandler);
inherited;
end;
procedure TForm1.UpdateTheme;
var
LService: IFMXSystemAppearanceService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXSystemAppearanceService, LService) then
CheckTheme(LService.ThemeKind, True);
end;
procedure TForm1.SystemAppearanceChangedMessageHandler(const Sender: TObject; const AMsg: TMessage);
begin
CheckTheme(TSystemAppearanceChangedMessage(AMsg).Value.ThemeKind);
end;
procedure TForm1.CheckTheme(const ATheme: TSystemThemeKind; const AForce: Boolean = False);
begin
if AForce or (FTheme <> ATheme) then
begin
FTheme := ATheme;
case FTheme of
TSystemThemeKind.Unspecified:
Label1.Text := 'Unknown Theme';
TSystemThemeKind.Light:
Label1.Text := 'Light Theme';
TSystemThemeKind.Dark:
Label1.Text := 'Dark Theme';
end;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment