Mercurial > minori
comparison src/sys/osx/dark_theme.cc @ 194:8548dc425697
sys/osx: remove all objective-c++ stuff
mmmm :)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 07 Dec 2023 03:17:05 -0500 |
parents | |
children | 975a3f0965e2 |
comparison
equal
deleted
inserted
replaced
193:0ad2507c3e60 | 194:8548dc425697 |
---|---|
1 #include "sys/osx/dark_theme.h" | |
2 | |
3 #include <objc/runtime.h> | |
4 #include <objc/message.h> | |
5 | |
6 #include <CoreFoundation/CoreFoundation.h> | |
7 #include <AvailabilityMacros.h> | |
8 | |
9 #include <QOperatingSystemVersion> | |
10 | |
11 namespace osx { | |
12 | |
13 typedef id (*object_message_send)(id, SEL, ...); | |
14 typedef id (*class_message_send)(Class, SEL, ...); | |
15 | |
16 static const object_message_send obj_send = reinterpret_cast<object_message_send>(objc_msgSend); | |
17 static const class_message_send cls_send = reinterpret_cast<class_message_send>(objc_msgSend); | |
18 | |
19 static CFStringRef NSAppearanceNameAqua = nullptr; | |
20 static CFStringRef NSAppearanceNameDarkAqua = nullptr; | |
21 | |
22 static const CFStringRef kLaunchServicesBundleID = CFSTR("com.apple.AppKit"); | |
23 | |
24 bool RetrieveAppearanceNames() { | |
25 CFBundleRef appkit_bundle = CFBundleGetBundleWithIdentifier(kLaunchServicesBundleID); | |
26 if (!appkit_bundle) | |
27 return false; | |
28 | |
29 NSAppearanceNameAqua = *reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameAqua"))); | |
30 if (!NSAppearanceNameAqua) | |
31 return false; | |
32 | |
33 NSAppearanceNameDarkAqua = *reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameDarkAqua"))); | |
34 if (!NSAppearanceNameDarkAqua) | |
35 return false; | |
36 | |
37 return true; | |
38 } | |
39 | |
40 bool DarkThemeAvailable() { | |
41 return (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave); | |
42 } | |
43 | |
44 bool IsInDarkTheme() { | |
45 if (!DarkThemeAvailable()) | |
46 return false; | |
47 | |
48 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua) | |
49 if (!RetrieveAppearanceNames()) | |
50 return false; | |
51 | |
52 CFArrayRef array = []() -> CFArrayRef { | |
53 CFStringRef refs[] = {NSAppearanceNameAqua, NSAppearanceNameDarkAqua}; | |
54 return CFArrayCreate(NULL, reinterpret_cast<const void**>(refs), 2, &kCFTypeArrayCallBacks); | |
55 }(); | |
56 | |
57 // NSApplication* app = [NSApplication sharedApplication]; | |
58 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | |
59 | |
60 // NSAppearance* effectiveAppearance = [app effectiveAppearance]; | |
61 const id effectiveAppearance = obj_send(app, sel_getUid("effectiveAppearance")); | |
62 if (!effectiveAppearance) { | |
63 CFRelease(array); | |
64 return false; | |
65 } | |
66 | |
67 // NSAppearance* appearance = [effectiveAppearance bestMatchFromAppearancesWithNames: array]; | |
68 const id appearance = obj_send(effectiveAppearance, sel_getUid("bestMatchFromAppearancesWithNames:"), array); | |
69 | |
70 CFRelease(array); | |
71 | |
72 if (!appearance) | |
73 return false; | |
74 | |
75 return CFEqual(appearance, NSAppearanceNameDarkAqua); | |
76 } | |
77 | |
78 bool SetToDarkTheme() { | |
79 // https://stackoverflow.com/questions/55925862/how-can-i-set-my-os-x-application-theme-in-code | |
80 if (!DarkThemeAvailable()) | |
81 return false; | |
82 | |
83 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua) | |
84 if (!RetrieveAppearanceNames()) | |
85 return false; | |
86 | |
87 // NSApplication* app = [NSApplication sharedApplication]; | |
88 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | |
89 | |
90 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua]; | |
91 const id appearance = cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameDarkAqua); | |
92 if (!appearance) | |
93 return false; | |
94 | |
95 // [app setAppearance: appearance]; | |
96 obj_send(app, sel_getUid("setAppearance:"), appearance); | |
97 return true; | |
98 } | |
99 | |
100 bool SetToLightTheme() { | |
101 // https://stackoverflow.com/questions/55925862/how-can-i-set-my-os-x-application-theme-in-code | |
102 if (!DarkThemeAvailable()) | |
103 return false; | |
104 | |
105 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua) | |
106 if (!RetrieveAppearanceNames()) | |
107 return false; | |
108 | |
109 // NSApplication* app = [NSApplication sharedApplication]; | |
110 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | |
111 | |
112 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua]; | |
113 const id appearance = cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameAqua); | |
114 if (!appearance) | |
115 return false; | |
116 | |
117 // [app setAppearance: appearance]; | |
118 obj_send(app, sel_getUid("setAppearance:"), appearance); | |
119 return true; | |
120 } | |
121 | |
122 void SetToAutoTheme() { | |
123 if (!DarkThemeAvailable()) | |
124 return; | |
125 | |
126 // NSApplication* app = [NSApplication sharedApplication]; | |
127 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | |
128 | |
129 // [app setAppearance: null]; | |
130 obj_send(app, sel_getUid("setAppearance:"), nullptr); | |
131 } | |
132 | |
133 } // namespace osx |