-
Notifications
You must be signed in to change notification settings - Fork 23
/
nsappkitext.pas
82 lines (61 loc) · 1.92 KB
/
nsappkitext.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
unit nsappkitext;
{$mode objfpc}{$H+}
{$modeswitch objectivec2}
interface
uses
CocoaAll, LCLType,Classes, SysUtils, Controls, LCLClasses;
procedure setThemeMode(Owner: TComponent; isDarkMode: boolean);
function isDarkModeSupported: boolean;
implementation
function ComponentToNSWindow(Owner: TComponent): NSWindow;
var
obj : NSObject;
begin
Result := nil;
if not Assigned(Owner) or not (Owner is TWinControl) then Exit;
obj := NSObject(TWinControl(Owner).Handle);
if not Assigned(obj) then Exit;
if obj.respondsToSelector(ObjCSelector('window')) then
Result := objc_msgSend(obj, ObjCSelector('window'));
end;
const
macOSNSAppearanceNameAqua = 'NSAppearanceNameAqua';
DefaultAppearance = macOSNSAppearanceNameAqua;
macOSNSAppearanceNameVibrantDark = 'NSAppearanceNameVibrantDark';
macOSNSAppearanceNameVibrantLight = 'NSAppearanceNameVibrantLight';
function UpdateAppearance(Owner: TComponent; const AAppearance: String): Boolean;
var
cls : id;
ap : string;
apr : id;
win : NSWindow;
begin
Result := false;
win := ComponentToNSWindow(Owner);
if not Assigned(win) then Exit;
if AAppearance = ''
then ap := DefaultAppearance
else ap := AAppearance;
cls := NSClassFromString( NSSTR('NSAppearance'));
if not Assigned(cls) then Exit; // not suppored in OSX version
apr := objc_msgSend(cls, ObjCSelector('appearanceNamed:'), NSSTR(@ap[1]));
if not Assigned(apr) then Exit;
if win.respondsToSelector(ObjCSelector('setAppearance:')) then
begin
objc_msgSend(win, ObjCSelector('setAppearance:'), apr);
Result := true;
end;
end;
procedure setThemeMode(Owner: TComponent; isDarkMode: boolean);
begin
if (isDarkMode) then
UpdateAppearance(Owner, macOSNSAppearanceNameVibrantDark)
else
UpdateAppearance(Owner, DefaultAppearance);
//
end;
function isDarkModeSupported: boolean;
begin
result := Assigned(NSClassFromString( NSSTR('NSAppearance')));
end;
end.