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.

104 lines
3.5KB

  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. %group Adaptive
  9. // We then hook the class in this case Zenith's grabber view is called “ZNGrabberAccessoryView”
  10. %hook ZNGrabberAccessoryView
  11. // this is called when iOS 13's dark mode is enabled!
  12. -(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  13. %orig(previousTraitCollection);
  14. if (kEnabled) {
  15. // if the tweak is enabled and the version is iOS 13 or later run our code
  16. if (@available(iOS 13, *)) {
  17. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  18. [self setBackgroundColor:kColor1];
  19. }
  20. else {
  21. [self setBackgroundColor:kLightColor];
  22. }
  23. }
  24. }
  25. else {
  26. %orig(previousTraitCollection);
  27. }
  28. }
  29. // the method we modify is this method that is called from UIImageView to set the backgroundColor of the image view.
  30. // Since the grabber view is of type UIImageView we can modify this method :)
  31. -(void)setBackgroundColor:(UIColor *)backgroundColor {
  32. %orig;
  33. if (kEnabled) {
  34. NSString* colourString = NULL;
  35. NSDictionary* preferencesDictionary = [NSDictionary dictionaryWithContentsOfFile: @"/var/mobile/Library/Preferences/com.mac-user669.zenithdark.plist"];
  36. if(preferencesDictionary)
  37. {
  38. colourString = [preferencesDictionary objectForKey: @"kCustomDarkColor"];
  39. }
  40. UIColor* darkColor = [SparkColourPickerUtils colourWithString: colourString withFallback: @"#000000:0.44"];
  41. // by default have our tweak overide this.
  42. if (@available(iOS 13, *)) {
  43. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  44. %orig(darkColor);
  45. }
  46. }
  47. else {
  48. %orig;
  49. }
  50. }
  51. }
  52. %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
  53. %end
  54. static void loadPrefs() { // Load preferences to make sure changes are written to the plist
  55. // Thanks to skittyblock!
  56. CFArrayRef keyList = CFPreferencesCopyKeyList(CFSTR("com.mac-user669.zenithdark"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  57. if(keyList) {
  58. prefs = (NSMutableDictionary *)CFPreferencesCopyMultiple(keyList, CFSTR("com.mac-user669.zenithdark"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
  59. CFRelease(keyList);
  60. } else {
  61. prefs = nil;
  62. }
  63. if (!prefs) {
  64. prefs = [NSMutableDictionary dictionaryWithContentsOfFile:PLIST_PATH];
  65. }
  66. kEnabled = [([prefs objectForKey:@"kEnabled"] ?: @(YES)) boolValue]; //our preference values that write to a plist file when a user selects somethings
  67. }
  68. // Thanks to skittyblock!
  69. static void PreferencesChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
  70. loadPrefs();
  71. }
  72. %ctor { // Our constructor
  73. loadPrefs(); // Load our prefs
  74. if (kEnabled) { // If enabled
  75. %init(Adaptive); // Enable the group "Adaptive"
  76. }
  77. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback) PreferencesChangedCallback, CFSTR("com.mac-user669.zenithdark.prefschanged"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
  78. 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.
  79. }