FLEXShortcutsFactory+Defaults.m 8.9 KB

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