Dark tabs for Zenith! Mirror of https://github.com/mac-user669/ZenithDark
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 satır
3.9KB

  1. #import "ZenithDark.h"
  2. /*
  3. Dark Mode for Zenith's Grabber view!
  4. Copyright 2020 J.K. Hayslip (@iKilledAppl3) & ToxicAppl3 INSDC/iKilledAppl3 LLC.
  5. All code was written for learning purposes and credit must be given to the original author.
  6. Written for Cooper Hull, @mac-user669.
  7. */
  8. BOOL kEnabled;
  9. BOOL kCustomDarkColorEnabled;
  10. BOOL kCustomLightColorEnabled;
  11. inline NSString *StringForPreferenceKey(NSString *key) {
  12. NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:PLIST_PATH] ? : [NSDictionary new];
  13. return prefs[key];
  14. }
  15. %group TweakGroup
  16. %hook ZNGrabberAccessoryView
  17. // this is called when iOS 13's dark mode is enabled!
  18. -(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  19. %orig(previousTraitCollection);
  20. if (@available(iOS 13, *)) {
  21. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  22. [self setBackgroundColor:[UIColor cscp_colorFromHexString:StringForPreferenceKey(@"kDarkCustomColor")]];
  23. }
  24. else {
  25. if (kCustomLightColorEnabled) {
  26. [self setBackgroundColor:[UIColor cscp_colorFromHexString:StringForPreferenceKey(@"kLightCustomColor")]];
  27. }
  28. else {
  29. [self setBackgroundColor:kLightModeColor];
  30. }
  31. }
  32. }
  33. }
  34. // the method we modify is this method that is called from UIImageView to set the backgroundColor of the image view.
  35. // Since the grabber view is of type UIImageView we can modify this method :)
  36. -(void)setBackgroundColor:(UIColor *)backgroundColor {
  37. // by default have our tweak overide this.
  38. if (@available(iOS 13, *)) {
  39. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  40. if (kCustomDarkColorEnabled) {
  41. %orig([UIColor cscp_colorFromHexString:StringForPreferenceKey(@"kDarkCustomColor")]);
  42. }
  43. else {
  44. %orig(kDarkModeColor);
  45. }
  46. }
  47. else {
  48. %orig;
  49. }
  50. }
  51. }
  52. // we need to make sure we tell theos that we are finished hooking this class not doing so with cause the end of the world :P
  53. %end
  54. %end
  55. static void loadPrefs() { // Load preferences to make sure changes are written to the plist
  56. // Thanks to skittyblock!
  57. CFArrayRef keyList = CFPreferencesCopyKeyList(CFSTR("com.mac-user669.zenithdark"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  58. if(keyList) {
  59. prefs = (NSMutableDictionary *)CFPreferencesCopyMultiple(keyList, CFSTR("com.mac-user669.zenithdark"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  60. CFRelease(keyList);
  61. } else {
  62. prefs = nil;
  63. }
  64. if (!prefs) {
  65. prefs = [NSMutableDictionary dictionaryWithContentsOfFile:PLIST_PATH];
  66. }
  67. kEnabled = [([prefs objectForKey:@"kEnabled"] ?: @(YES)) boolValue]; //our preference values that write to a plist file when a user selects somethings
  68. kCustomDarkColorEnabled = [([prefs objectForKey:@"kCustomDarkColorEnabled"] ?: @(NO)) boolValue]; //our preference values that write to a plist file when a user selects somethings
  69. kCustomLightColorEnabled = [([prefs objectForKey:@"kCustomLightColorEnabled"] ?: @(NO)) boolValue]; //our preference values that write to a plist file when a user selects somethings
  70. }
  71. // Thanks to skittyblock!
  72. static void PreferencesChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  73. loadPrefs();
  74. }
  75. %ctor { // Our constructor
  76. loadPrefs(); // Load our prefs
  77. if (kEnabled) { // If enabled
  78. %init(TweakGroup); // Enable the group "TweakGroup"
  79. }
  80. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback) PreferencesChangedCallback, CFSTR("com.mac-user669.zenithdark.prefschanged"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  81. dlopen ("/Library/MobileSubstrate/DynamicLibraries/Zenith.dylib", RTLD_NOW); // We use this to make sure we load Zenith's dynamic library at runtime so we can modify it with our tweak.
  82. }