forked from transmission-remote-gui/transgui
-
Notifications
You must be signed in to change notification settings - Fork 4
/
macosthemedetect.pas
59 lines (43 loc) · 1.43 KB
/
macosthemedetect.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{adapted from https://forum.lazarus.freepascal.org/index.php/topic,43111.msg461112.html}
unit MacOSThemeDetect;
{$MODESWITCH OBJECTIVEC2}
interface
uses
Classes, SysUtils, CocoaAll, MacOsAll, CocoaUtils;
type
TThemeChangedCallback = procedure () of object;
var
Callback : TThemeChangedCallback;
function IsDarkMode: Boolean;
implementation
type
TThemeChangedNotification = objcclass(NSObject)
procedure ThemeChangedNotification(notification: NSNotification);
message 'ThemeChangedNotification:';
end;
procedure TThemeChangedNotification.ThemeChangedNotification(
notification: NSNotification);
begin
Callback();
end;
function IsDarkMode: Boolean;
var
sMode: string;
begin
sMode := CFStringToStr(CFStringRef(NSApp.effectiveAppearance.name));
Result := Pos('Dark', sMode) > 0;
end;
var
ThemeChangedNotification: TThemeChangedNotification;
DistributedNotificationCenter: NSDistributedNotificationCenter;
initialization
ThemeChangedNotification := TThemeChangedNotification.alloc.init;
ThemeChangedNotification.retain;
DistributedNotificationCenter := NSDistributedNotificationCenter.defaultCenter;
if Assigned(DistributedNotificationCenter) then
DistributedNotificationCenter.addObserver_selector_name_object(
ThemeChangedNotification, ObjCSelector('ThemeChangedNotification:'),
NSSTR('AppleInterfaceThemeChangedNotification'), nil);
finalization
ThemeChangedNotification.release;
end.