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.

127 lines
4.0KB

  1. #import "ZenithDark.h"
  2. #import <Cephei/HBPreferences.h>
  3. /*
  4. Dark Mode for Zenith's Grabber view!
  5. Copyright 2020 J.K. Hayslip (@iKilledAppl3) & ToxicAppl3 INSDC/iKilledAppl3 LLC.
  6. All code was written for learning purposes and credit must be given to the original author.
  7. Written for Cooper Hull, @(mac-user669).
  8. */
  9. %group Tweak13
  10. // We then hook the class in this case Zenith's grabber view is called “ZNGrabberAccessoryView”
  11. %hook ZNGrabberAccessoryView
  12. // this is called when iOS 13's dark mode is enabled!
  13. -(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  14. %orig(previousTraitCollection);
  15. if (kEnabled) {
  16. // if the tweak is enabled and the version is iOS 13 or later run our code
  17. if (@available(iOS 13, *)) {
  18. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  19. [self setBackgroundColor:kDarkModeColor];
  20. }
  21. else {
  22. [self setBackgroundColor:kLightModeColor];
  23. }
  24. }
  25. }
  26. else {
  27. %orig(previousTraitCollection);
  28. }
  29. }
  30. // the method we modify is this method that is called from UIImageView to set the backgroundColor of the image view.
  31. // Since the grabber view is of type UIImageView we can modify this method :)
  32. -(void)setBackgroundColor:(UIColor *)backgroundColor {
  33. %orig;
  34. if (kEnabled) {
  35. // by default have our tweak overide this.
  36. if (@available(iOS 13, *)) {
  37. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  38. %orig(kDarkModeColor);
  39. }
  40. }
  41. else {
  42. %orig;
  43. }
  44. }
  45. }
  46. %end // 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
  47. %end
  48. static BOOL ios13;
  49. %group Tweak12
  50. //We then hook the class in this case Zenith's grabber view is called “ZNGrabberAccessoryView”
  51. %hook ZNGrabberAccessoryView
  52. // The method we then modify is this method that is called from UIImageView to set the backgroundColor of the image view.
  53. // Since the grabber view is of type UIImageView we can modify this method :)
  54. -(void)setBackgroundColor:(UIColor *)backgroundColor {
  55. //call the original function then pass our custom argument to the backgroundColor argument as shown below.
  56. %orig(kDarkModeColor);
  57. }
  58. // 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
  59. %end
  60. %end
  61. static void loadPrefs() { // Load preferences to make sure changes are written to the plist
  62. // Thanks to skittyblock!
  63. CFArrayRef keyList = CFPreferencesCopyKeyList(CFSTR("com.mac-user669.zenithdark"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  64. if(keyList) {
  65. prefs = (NSMutableDictionary *)CFPreferencesCopyMultiple(keyList, CFSTR("com.mac-user669.zenithdark"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  66. CFRelease(keyList);
  67. } else {
  68. prefs = nil;
  69. }
  70. if (!prefs) {
  71. prefs = [NSMutableDictionary dictionaryWithContentsOfFile:PLIST_PATH];
  72. }
  73. kEnabled = [([prefs objectForKey:@"kEnabled"] ?: @(YES)) boolValue]; //our preference values that write to a plist file when a user selects somethings
  74. }
  75. // Thanks to skittyblock!
  76. static void PreferencesChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  77. loadPrefs();
  78. }
  79. %ctor { // Our constructor
  80. loadPrefs(); // Load our prefs
  81. if (kEnabled) { // If enabled
  82. if (@available(iOS 13, *)) { // If the device is running iOS 13
  83. ios13 = YES; // Set "iOS13" to "YES"
  84. %init(Tweak13); // Enable the group "Tweak13"
  85. } else {
  86. ios13 = NO; // Set "iOS13" to "NO"
  87. %init(Tweak12); // Enable the group "Tweak12"
  88. }
  89. }
  90. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback) PreferencesChangedCallback, CFSTR("com.mac-user669.zenithdark.prefschanged"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  91. 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.
  92. }