annotate src/sys/osx/dark_theme.cc @ 195:975a3f0965e2

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