Mercurial > minori
comparison src/sys/osx/dark_theme.cc @ 202:71832ffe425a
animia: re-add kvm fd source
this is all being merged from my wildly out-of-date laptop. SORRY!
in other news, I edited the CI file to install the wayland client
as well, so the linux CI build might finally get wayland stuff.
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Tue, 02 Jan 2024 06:05:06 -0500 |
| parents | f0ff06a45c42 |
| children | 862d0d8619f6 |
comparison
equal
deleted
inserted
replaced
| 201:8f6f8dd2eb23 | 202:71832ffe425a |
|---|---|
| 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 | |
| 8 #include <QOperatingSystemVersion> | |
| 9 | |
| 10 namespace osx { | |
| 11 | |
| 12 typedef id (*object_message_send)(id, SEL, ...); | |
| 13 typedef id (*class_message_send)(Class, SEL, ...); | |
| 14 | |
| 15 static const object_message_send obj_send = reinterpret_cast<object_message_send>(objc_msgSend); | |
| 16 static const class_message_send cls_send = reinterpret_cast<class_message_send>(objc_msgSend); | |
| 17 | |
| 18 static CFStringRef NSAppearanceNameAqua = nullptr; | |
| 19 static CFStringRef NSAppearanceNameDarkAqua = nullptr; | |
| 20 | |
| 21 static const CFStringRef kAppKitBundleID = CFSTR("com.apple.AppKit"); | |
| 22 | |
| 23 bool RetrieveAppearanceNames() { | |
| 24 CFBundleRef appkit_bundle = CFBundleGetBundleWithIdentifier(kAppKitBundleID); | |
| 25 if (!appkit_bundle) | |
| 26 return false; | |
| 27 | |
| 28 NSAppearanceNameAqua = *reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameAqua"))); | |
| 29 if (!NSAppearanceNameAqua) | |
| 30 return false; | |
| 31 | |
| 32 NSAppearanceNameDarkAqua = *reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameDarkAqua"))); | |
| 33 if (!NSAppearanceNameDarkAqua) | |
| 34 return false; | |
| 35 | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 bool DarkThemeAvailable() { | |
| 40 return (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave); | |
| 41 } | |
| 42 | |
| 43 bool IsInDarkTheme() { | |
| 44 if (!DarkThemeAvailable()) | |
| 45 return false; | |
| 46 | |
| 47 if (!NSAppearanceNameAqua || !NSAppearanceNameDarkAqua) | |
| 48 if (!RetrieveAppearanceNames()) | |
| 49 return false; | |
| 50 | |
| 51 // NSArray* array = @[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]; | |
| 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 |
