FLEXShortcutsFactory+Defaults.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // FLEXShortcutsFactory+Defaults.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/29/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXShortcutsFactory+Defaults.h"
  9. #import "FLEXShortcut.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "NSObject+FLEX_Reflection.h"
  12. #pragma mark - UIApplication
  13. @implementation FLEXShortcutsFactory (UIApplication)
  14. + (void)load { FLEX_EXIT_IF_TESTING()
  15. self.append.classProperties(@[@"sharedApplication"]).forClass(UIApplication.flex_metaclass);
  16. self.append.properties(@[
  17. @"delegate", @"keyWindow", @"windows"
  18. ]).forClass(UIApplication.class);
  19. if (@available(iOS 13, *)) {
  20. self.append.properties(@[
  21. @"connectedScenes", @"openSessions", @"supportsMultipleScenes"
  22. ]).forClass(UIApplication.class);
  23. }
  24. }
  25. @end
  26. #pragma mark - Views
  27. @implementation FLEXShortcutsFactory (Views)
  28. + (void)load { FLEX_EXIT_IF_TESTING()
  29. // A quirk of UIView and some other classes: a lot of the `@property`s are
  30. // not actually properties from the perspective of the runtime.
  31. //
  32. // We add these properties to the class at runtime if they haven't been added yet.
  33. // This way, we can use our property editor to access and change them.
  34. // The property attributes match the declared attributes in their headers.
  35. // UIView, public
  36. Class UIView_ = UIView.class;
  37. FLEXRuntimeUtilityTryAddNonatomicProperty(2, frame, UIView_, CGRect);
  38. FLEXRuntimeUtilityTryAddNonatomicProperty(2, alpha, UIView_, CGFloat);
  39. FLEXRuntimeUtilityTryAddNonatomicProperty(2, clipsToBounds, UIView_, BOOL);
  40. FLEXRuntimeUtilityTryAddNonatomicProperty(2, opaque, UIView_, BOOL, PropertyKeyGetter(isOpaque));
  41. FLEXRuntimeUtilityTryAddNonatomicProperty(2, hidden, UIView_, BOOL, PropertyKeyGetter(isHidden));
  42. FLEXRuntimeUtilityTryAddObjectProperty(2, backgroundColor, UIView_, UIColor, PropertyKey(Copy));
  43. FLEXRuntimeUtilityTryAddObjectProperty(6, constraints, UIView_, NSArray, PropertyKey(ReadOnly));
  44. FLEXRuntimeUtilityTryAddObjectProperty(2, subviews, UIView_, NSArray, PropertyKey(ReadOnly));
  45. FLEXRuntimeUtilityTryAddObjectProperty(2, superview, UIView_, UIView, PropertyKey(ReadOnly));
  46. // UIButton, private
  47. FLEXRuntimeUtilityTryAddObjectProperty(2, font, UIButton.class, UIFont, PropertyKey(ReadOnly));
  48. // Only available since iOS 3.2, but we never supported iOS 3, so who cares
  49. NSArray *ivars = @[@"_gestureRecognizers"];
  50. NSArray *methods = @[@"sizeToFit", @"setNeedsLayout", @"removeFromSuperview"];
  51. // UIView
  52. self.append.ivars(ivars).methods(methods).properties(@[
  53. @"frame", @"bounds", @"center", @"transform",
  54. @"backgroundColor", @"alpha", @"opaque", @"hidden",
  55. @"clipsToBounds", @"userInteractionEnabled", @"layer",
  56. @"superview", @"subviews"
  57. ]).forClass(UIView.class);
  58. // UILabel
  59. self.append.ivars(ivars).methods(methods).properties(@[
  60. @"text", @"attributedText", @"font", @"frame",
  61. @"textColor", @"textAlignment", @"numberOfLines",
  62. @"lineBreakMode", @"enabled", @"backgroundColor",
  63. @"alpha", @"hidden", @"preferredMaxLayoutWidth",
  64. @"superview", @"subviews"
  65. ]).forClass(UILabel.class);
  66. // UIWindow
  67. self.append.ivars(ivars).properties(@[
  68. @"rootViewController", @"windowLevel", @"keyWindow",
  69. @"frame", @"bounds", @"center", @"transform",
  70. @"backgroundColor", @"alpha", @"opaque", @"hidden",
  71. @"clipsToBounds", @"userInteractionEnabled", @"layer",
  72. @"subviews"
  73. ]).forClass(UIWindow.class);
  74. if (@available(iOS 13, *)) {
  75. self.append.properties(@[@"windowScene"]).forClass(UIWindow.class);
  76. }
  77. ivars = @[@"_targetActions", @"_gestureRecognizers"];
  78. // Property was added in iOS 10 but we want it on iOS 9 too
  79. FLEXRuntimeUtilityTryAddObjectProperty(9, allTargets, UIControl.class, NSArray, PropertyKey(ReadOnly));
  80. // UIControl
  81. self.append.ivars(ivars).methods(methods).properties(@[
  82. @"enabled", @"allTargets", @"frame",
  83. @"backgroundColor", @"hidden", @"clipsToBounds",
  84. @"userInteractionEnabled", @"superview", @"subviews"
  85. ]).forClass(UIControl.class);
  86. // UIButton
  87. self.append.ivars(ivars).properties(@[
  88. @"titleLabel", @"font", @"imageView", @"tintColor",
  89. @"currentTitle", @"currentImage", @"enabled", @"frame",
  90. @"superview", @"subviews"
  91. ]).forClass(UIButton.class);
  92. // UIImageView
  93. self.append.properties(@[
  94. @"image", @"animationImages", @"frame", @"bounds", @"center",
  95. @"transform", @"alpha", @"hidden", @"clipsToBounds",
  96. @"userInteractionEnabled", @"layer", @"superview", @"subviews",
  97. ]).forClass(UIImageView.class);
  98. }
  99. @end
  100. #pragma mark - View Controllers
  101. @implementation FLEXShortcutsFactory (ViewControllers)
  102. + (void)load { FLEX_EXIT_IF_TESTING()
  103. // toolbarItems is not really a property, make it one
  104. FLEXRuntimeUtilityTryAddObjectProperty(3, toolbarItems, UIViewController.class, NSArray);
  105. // UIViewController
  106. self.append
  107. .properties(@[
  108. @"viewIfLoaded", @"title", @"navigationItem", @"toolbarItems", @"tabBarItem",
  109. @"childViewControllers", @"navigationController", @"tabBarController", @"splitViewController",
  110. @"parentViewController", @"presentedViewController", @"presentingViewController",
  111. ]).methods(@[@"view"]).forClass(UIViewController.class);
  112. }
  113. @end
  114. #pragma mark - UIImage
  115. @implementation FLEXShortcutsFactory (UIImage)
  116. + (void)load { FLEX_EXIT_IF_TESTING()
  117. self.append.methods(@[
  118. @"CGImage", @"CIImage"
  119. ]).properties(@[
  120. @"scale", @"size", @"capInsets",
  121. @"alignmentRectInsets", @"duration", @"images"
  122. ]).forClass(UIImage.class);
  123. if (@available(iOS 13, *)) {
  124. self.append.properties(@[@"symbolImage"]).forClass(UIImage.class);
  125. }
  126. }
  127. @end
  128. #pragma mark - NSBundle
  129. @implementation FLEXShortcutsFactory (NSBundle)
  130. + (void)load { FLEX_EXIT_IF_TESTING()
  131. self.append.properties(@[
  132. @"bundleIdentifier", @"principalClass",
  133. @"infoDictionary", @"bundlePath",
  134. @"executablePath", @"loaded"
  135. ]).forClass(NSBundle.class);
  136. }
  137. @end
  138. #pragma mark - Classes
  139. @implementation FLEXShortcutsFactory (Classes)
  140. + (void)load { FLEX_EXIT_IF_TESTING()
  141. self.append.classMethods(@[@"new", @"alloc"]).forClass(NSObject.flex_metaclass);
  142. }
  143. @end
  144. #pragma mark - Activities
  145. @implementation FLEXShortcutsFactory (Activities)
  146. + (void)load { FLEX_EXIT_IF_TESTING()
  147. // Property was added in iOS 10 but we want it on iOS 9 too
  148. FLEXRuntimeUtilityTryAddNonatomicProperty(9, item, UIActivityItemProvider.class, id, PropertyKey(ReadOnly));
  149. self.append.properties(@[
  150. @"item", @"placeholderItem", @"activityType"
  151. ]).forClass(UIActivityItemProvider.class);
  152. self.append.properties(@[
  153. @"activityItems", @"applicationActivities", @"excludedActivityTypes", @"completionHandler"
  154. ]).forClass(UIActivityViewController.class);
  155. }
  156. @end
  157. #pragma mark - Blocks
  158. @implementation FLEXShortcutsFactory (Blocks)
  159. + (void)load { FLEX_EXIT_IF_TESTING()
  160. self.append.methods(@[@"invoke"]).forClass(NSClassFromString(@"NSBlock"));
  161. }
  162. @end
  163. #pragma mark - Foundation
  164. @implementation FLEXShortcutsFactory (Foundation)
  165. + (void)load { FLEX_EXIT_IF_TESTING()
  166. self.append.properties(@[
  167. @"configuration", @"delegate", @"delegateQueue", @"sessionDescription",
  168. ]).methods(@[
  169. @"dataTaskWithURL:", @"finishTasksAndInvalidate", @"invalidateAndCancel",
  170. ]).forClass(NSURLSession.class);
  171. self.append.methods(@[
  172. @"cachedResponseForRequest:", @"storeCachedResponse:forRequest:",
  173. @"storeCachedResponse:forDataTask:", @"removeCachedResponseForRequest:",
  174. @"removeCachedResponseForDataTask:", @"removeCachedResponsesSinceDate:",
  175. @"removeAllCachedResponses",
  176. ]).forClass(NSURLCache.class);
  177. self.append.methods(@[
  178. @"postNotification:", @"postNotificationName:object:userInfo:",
  179. @"addObserver:selector:name:object:", @"removeObserver:",
  180. @"removeObserver:name:object:",
  181. ]).forClass(NSNotificationCenter.class);
  182. // NSTimeZone class properties aren't real properties
  183. FLEXRuntimeUtilityTryAddObjectProperty(2, localTimeZone, NSTimeZone.flex_metaclass, NSTimeZone);
  184. FLEXRuntimeUtilityTryAddObjectProperty(2, systemTimeZone, NSTimeZone.flex_metaclass, NSTimeZone);
  185. FLEXRuntimeUtilityTryAddObjectProperty(2, defaultTimeZone, NSTimeZone.flex_metaclass, NSTimeZone);
  186. FLEXRuntimeUtilityTryAddObjectProperty(2, knownTimeZoneNames, NSTimeZone.flex_metaclass, NSArray);
  187. FLEXRuntimeUtilityTryAddObjectProperty(2, abbreviationDictionary, NSTimeZone.flex_metaclass, NSDictionary);
  188. self.append.classMethods(@[
  189. @"timeZoneWithName:", @"timeZoneWithAbbreviation:", @"timeZoneForSecondsFromGMT:", @"", @"", @"",
  190. ]).forClass(NSTimeZone.flex_metaclass);
  191. self.append.classProperties(@[
  192. @"defaultTimeZone", @"systemTimeZone", @"localTimeZone"
  193. ]).forClass(NSTimeZone.class);
  194. }
  195. @end