Mercurial > minori
comparison src/sys/osx/dark_theme.cc @ 275:22f9aacf6ac1
osx: don't dereference NULL pointers
fail
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 18 Apr 2024 19:23:31 -0400 |
parents | f31305b9f60a |
children |
comparison
equal
deleted
inserted
replaced
274:f6a756c19bfb | 275:22f9aacf6ac1 |
---|---|
20 | 20 |
21 bool RetrieveAppearanceNames() { | 21 bool RetrieveAppearanceNames() { |
22 CFBundleRef appkit_bundle = CFBundleGetBundleWithIdentifier(kAppKitBundleID); | 22 CFBundleRef appkit_bundle = CFBundleGetBundleWithIdentifier(kAppKitBundleID); |
23 if (!appkit_bundle) | 23 if (!appkit_bundle) |
24 return false; | 24 return false; |
25 | |
26 auto aqua_appearance = reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameAqua"))); | |
27 if (!aqua_appearance) | |
28 return false; | |
29 NSAppearanceNameAqua = *aqua_appearance; | |
25 | 30 |
26 NSAppearanceNameAqua = | 31 auto dark_aqua_appearance = reinterpret_cast<CFStringRef*>( |
27 *reinterpret_cast<CFStringRef*>(CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameAqua"))); | 32 CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameDarkAqua"))); |
28 if (!NSAppearanceNameAqua) | 33 if (!dark_aqua_appearance) |
29 return false; | 34 return false; |
30 | 35 NSAppearanceNameDarkAqua = *dark_aqua_appearance; |
31 NSAppearanceNameDarkAqua = *reinterpret_cast<CFStringRef*>( | |
32 CFBundleGetDataPointerForName(appkit_bundle, CFSTR("NSAppearanceNameDarkAqua"))); | |
33 if (!NSAppearanceNameDarkAqua) | |
34 return false; | |
35 | 36 |
36 return true; | 37 return true; |
37 } | 38 } |
38 | 39 |
39 bool DarkThemeAvailable() { | 40 bool DarkThemeAvailable() { |
40 return (__builtin_available(macOS 10.14, *)); | 41 if (__builtin_available(macOS 10.14, *)) { |
42 return true; | |
43 } else { | |
44 return false; | |
45 } | |
41 } | 46 } |
42 | 47 |
43 bool IsInDarkTheme() { | 48 bool IsInDarkTheme() { |
44 if (!DarkThemeAvailable()) | 49 if (!DarkThemeAvailable()) |
45 return false; | 50 return false; |
54 return CFArrayCreate(NULL, reinterpret_cast<const void**>(refs), 2, &kCFTypeArrayCallBacks); | 59 return CFArrayCreate(NULL, reinterpret_cast<const void**>(refs), 2, &kCFTypeArrayCallBacks); |
55 }(); | 60 }(); |
56 | 61 |
57 // NSApplication* app = [NSApplication sharedApplication]; | 62 // NSApplication* app = [NSApplication sharedApplication]; |
58 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | 63 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); |
64 if (!app) | |
65 return false; | |
59 | 66 |
60 // NSAppearance* effectiveAppearance = [app effectiveAppearance]; | 67 // NSAppearance* effectiveAppearance = [app effectiveAppearance]; |
61 const id effectiveAppearance = obj_send(app, sel_getUid("effectiveAppearance")); | 68 const id effectiveAppearance = obj_send(app, sel_getUid("effectiveAppearance")); |
62 if (!effectiveAppearance) { | 69 if (!effectiveAppearance) { |
63 CFRelease(array); | 70 CFRelease(array); |
84 if (!RetrieveAppearanceNames()) | 91 if (!RetrieveAppearanceNames()) |
85 return false; | 92 return false; |
86 | 93 |
87 // NSApplication* app = [NSApplication sharedApplication]; | 94 // NSApplication* app = [NSApplication sharedApplication]; |
88 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | 95 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); |
96 if (!app) | |
97 return false; | |
89 | 98 |
90 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua]; | 99 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua]; |
91 const id appearance = | 100 const id appearance = |
92 cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameDarkAqua); | 101 cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameDarkAqua); |
93 if (!appearance) | 102 if (!appearance) |
107 if (!RetrieveAppearanceNames()) | 116 if (!RetrieveAppearanceNames()) |
108 return false; | 117 return false; |
109 | 118 |
110 // NSApplication* app = [NSApplication sharedApplication]; | 119 // NSApplication* app = [NSApplication sharedApplication]; |
111 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | 120 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); |
121 if (!app) | |
122 return false; | |
112 | 123 |
113 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua]; | 124 // NSAppearance* appearance = [NSAppearance appearanceNamed: NSAppearanceNameDarkAqua]; |
114 const id appearance = cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameAqua); | 125 const id appearance = cls_send(objc_getClass("NSAppearance"), sel_getUid("appearanceNamed:"), NSAppearanceNameAqua); |
115 if (!appearance) | 126 if (!appearance) |
116 return false; | 127 return false; |
124 if (!DarkThemeAvailable()) | 135 if (!DarkThemeAvailable()) |
125 return; | 136 return; |
126 | 137 |
127 // NSApplication* app = [NSApplication sharedApplication]; | 138 // NSApplication* app = [NSApplication sharedApplication]; |
128 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); | 139 const id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); |
140 if (!app) | |
141 return; | |
129 | 142 |
130 // [app setAppearance: null]; | 143 // [app setAppearance: null]; |
131 obj_send(app, sel_getUid("setAppearance:"), nullptr); | 144 obj_send(app, sel_getUid("setAppearance:"), nullptr); |
132 } | 145 } |
133 | 146 |