Recent

Author Topic: How to detect if macOS changed Theme (Dark vs Light) while running?  (Read 8707 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 652
  • Tweaking4All.com
    • Tweaking4All
Re: How to detect if macOS changed Theme (Dark vs Light) while running?
« Reply #15 on: August 31, 2020, 11:27:28 am »
Not sure how others do this, but I check for Dark Theme during the Form's onPaint event - so the application can change certain things (for example controls that do not respond well to a theme change, or controls that use non-standard colors). This works for all my apps just fine. Well, except for Windows, since Lazarus doesn't change colors when you change theme in Windows.
If this is the correct way to do this; maybe we can add this as a tip to the Wiki page?

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: How to detect if macOS changed Theme (Dark vs Light) while running?
« Reply #16 on: September 01, 2020, 04:18:13 am »
If this is the correct way to do this; maybe we can add this as a tip to the Wiki page?

I think it's a hack ;)

You mentioned the "correct" method in your earlier 2018 post in this thread... except you didn't manage to get it working :( It's beyond me too...

In which case, a hack may be better than nothing :)

Wallaby

  • Jr. Member
  • **
  • Posts: 73
Re: How to detect if macOS changed Theme (Dark vs Light) while running?
« Reply #17 on: November 24, 2022, 12:32:27 am »
I know it's an old thread, but I faced the same problem. I needed to adjust certain custom colours when macOS switches to Dark or Light theme.

Here is the code that will receive the event whenever the theme is changed, manually or automatically:

Code: Pascal  [Select][+][-]
  1. unit Core.MacOS.DarkMode;
  2.  
  3. {$MODESWITCH OBJECTIVEC1}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, CocoaAll, MacOsAll;
  9.  
  10. implementation
  11.  
  12. type
  13.   TThemeChangedNotification = objcclass(NSObject)
  14.     procedure ThemeChangedNotification(notification: NSNotification);
  15.       message 'ThemeChangedNotification:';
  16.   end;
  17.  
  18. procedure TThemeChangedNotification.ThemeChangedNotification(
  19.   notification: NSNotification);
  20. begin
  21.   // This will be triggered upon theme change
  22. end;
  23.  
  24. var
  25.   ThemeChangedNotification: TThemeChangedNotification;
  26.   DistributedNotificationCenter: NSDistributedNotificationCenter;
  27.  
  28. initialization
  29.   ThemeChangedNotification := TThemeChangedNotification.alloc.init;
  30.   ThemeChangedNotification.retain;
  31.   DistributedNotificationCenter := NSDistributedNotificationCenter.defaultCenter;
  32.   if Assigned(DistributedNotificationCenter) then
  33.     DistributedNotificationCenter.addObserver_selector_name_object(
  34.       ThemeChangedNotification, ObjCSelector('ThemeChangedNotification:'),
  35.       NSSTR('AppleInterfaceThemeChangedNotification'), nil);
  36.  
  37. finalization
  38.   ThemeChangedNotification.release;
  39.  
  40. end.

 

TinyPortal © 2005-2018