annotate src/sys/osx/dark_theme.cc @ 229:adc20fa321c1

theme: force Fusion style on platforms other than Win32 or OS X I was reluctant to do this, but most of the other styles just look like pure shite regardless of whether I force a stylesheet on them or not. KDE's style is actually hilariously bad paired with my stylesheet, so I've decided to also make the stylesheet Windows-specific as well, because that's really the only platform where it makes sense in the first place.
author Paper <paper@paper.us.eu.org>
date Wed, 10 Jan 2024 21:23:57 -0500
parents f0ff06a45c42
children 862d0d8619f6
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
196
f0ff06a45c42 date: use std::optional for values
Paper <mrpapersonic@gmail.com>
parents: 195
diff changeset
51 // NSArray* array = @[NSAppearanceNameAqua, NSAppearanceNameDarkAqua];
194
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 CFArrayRef array = []() -> CFArrayRef {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 CFStringRef refs[] = {NSAppearanceNameAqua, NSAppearanceNameDarkAqua};
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 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
55 }();
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 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
59
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 // NSAppearance* effectiveAppearance = [app effectiveAppearance];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 const id effectiveAppearance = obj_send(app, sel_getUid("effectiveAppearance"));
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 if (!effectiveAppearance) {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 CFRelease(array);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 return false;
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
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 // NSAppearance* appearance = [effectiveAppearance bestMatchFromAppearancesWithNames: array];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 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
69
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 CFRelease(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 if (!appearance)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 return CFEqual(appearance, NSAppearanceNameDarkAqua);
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
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 bool SetToDarkTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 // 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
80 if (!DarkThemeAvailable())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 if (!RetrieveAppearanceNames())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 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
89
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 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
92 if (!appearance)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 // [app setAppearance: appearance];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 obj_send(app, sel_getUid("setAppearance:"), appearance);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 return true;
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
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 bool SetToLightTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101 // 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
102 if (!DarkThemeAvailable())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 if (!RetrieveAppearanceNames())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 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
111
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 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
114 if (!appearance)
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115 return false;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
117 // [app setAppearance: appearance];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
118 obj_send(app, sel_getUid("setAppearance:"), appearance);
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
119 return true;
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
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
122 void SetToAutoTheme() {
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
123 if (!DarkThemeAvailable())
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
124 return;
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
125
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 // NSApplication* app = [NSApplication sharedApplication];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127 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
128
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 // [app setAppearance: null];
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
130 obj_send(app, sel_getUid("setAppearance:"), nullptr);
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
8548dc425697 sys/osx: remove all objective-c++ stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133 } // namespace osx