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.

129 lines
4.1KB

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