annotate src/sys/osx/dark_theme.cc @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents f0ff06a45c42
children f31305b9f60a
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
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
3 #include <objc/message.h>
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <objc/runtime.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
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
28 NSAppearanceNameAqua =
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
29 *reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameAqua")));
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 if (!NSAppearanceNameAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
33 NSAppearanceNameDarkAqua = *reinterpret_cast<CFStringRef*>(
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
34 CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameDarkAqua")));
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 if (!NSAppearanceNameDarkAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 return false;
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 return true;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 }
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 bool DarkThemeAvailable() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 return (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 }
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 bool IsInDarkTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 if (!DarkThemeAvailable())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 if (!RetrieveAppearanceNames())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 195
diff changeset
53 // NSArray* array = @[NSAppearanceNameAqua, NSAppearanceNameDarkAqua];
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 CFArrayRef array = []() -> CFArrayRef {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 CFStringRef refs[] = {NSAppearanceNameAqua, NSAppearanceNameDarkAqua};
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 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
57 }();
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 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 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
61
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 // NSAppearance* effectiveAppearance = [app effectiveAppearance];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 const id effectiveAppearance = obj_send(app, sel_getUid("effectiveAppearance"));
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 if (!effectiveAppearance) {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 CFRelease(array);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 }
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 // NSAppearance* appearance = [effectiveAppearance bestMatchFromAppearancesWithNames: array];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 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
71
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 CFRelease(array);
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 if (!appearance)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 return false;
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 return CFEqual(appearance, NSAppearanceNameDarkAqua);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 }
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 bool SetToDarkTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 // 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
82 if (!DarkThemeAvailable())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 if (!RetrieveAppearanceNames())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 return false;
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 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 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
91
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua];
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
93 const id appearance =
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 196
diff changeset
94 cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameDarkAqua);
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 if (!appearance)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 return false;
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 // [app setAppearance: appearance];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 obj_send(app, sel_getUid("setAppearance:"), appearance);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 return true;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 }
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 bool SetToLightTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 // 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
105 if (!DarkThemeAvailable())
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 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 if (!RetrieveAppearanceNames())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 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
114
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116 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
117 if (!appearance)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
118 return false;
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 // [app setAppearance: appearance];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
121 obj_send(app, sel_getUid("setAppearance:"), appearance);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
122 return true;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
123 }
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 void SetToAutoTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 if (!DarkThemeAvailable())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127 return;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
128
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
130 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
131
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132 // [app setAppearance: null];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133 obj_send(app, sel_getUid("setAppearance:"), nullptr);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
134 }
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
135
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
136 } // namespace osx