FLEXShortcutsFactory+Defaults.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. }
  79. @end
  80. #pragma mark - View Controllers
  81. @implementation FLEXShortcutsFactory (ViewControllers)
  82. + (void)load { FLEX_EXIT_IF_TESTING()
  83. // toolbarItems is not really a property, make it one
  84. FLEXRuntimeUtilityTryAddObjectProperty(3, toolbarItems, UIViewController.class, NSArray);
  85. // UIViewController
  86. self.append
  87. .properties(@[
  88. @"viewIfLoaded", @"title", @"navigationItem", @"toolbarItems", @"tabBarItem",
  89. @"childViewControllers", @"navigationController", @"tabBarController", @"splitViewController",
  90. @"parentViewController", @"presentedViewController", @"presentingViewController",
  91. ]).methods(@[@"view"]).forClass(UIViewController.class);
  92. }
  93. @end
  94. #pragma mark - UIImage
  95. @implementation FLEXShortcutsFactory (UIImage)
  96. + (void)load { FLEX_EXIT_IF_TESTING()
  97. self.append.methods(@[
  98. @"CGImage", @"CIImage"
  99. ]).properties(@[
  100. @"scale", @"size", @"capInsets",
  101. @"alignmentRectInsets", @"duration", @"images"
  102. ]).forClass(UIImage.class);
  103. if (@available(iOS 13, *)) {
  104. self.append.properties(@[@"symbolImage"]);
  105. }
  106. }
  107. @end
  108. #pragma mark - NSBundle
  109. @implementation FLEXShortcutsFactory (NSBundle)
  110. + (void)load { FLEX_EXIT_IF_TESTING()
  111. self.append.properties(@[
  112. @"bundleIdentifier", @"principalClass",
  113. @"infoDictionary", @"bundlePath",
  114. @"executablePath", @"loaded"
  115. ]).forClass(NSBundle.class);
  116. }
  117. @end
  118. #pragma mark - Classes
  119. @implementation FLEXShortcutsFactory (Classes)
  120. + (void)load { FLEX_EXIT_IF_TESTING()
  121. self.append.classMethods(@[@"new", @"alloc"]).forClass(NSObject.flex_metaclass);
  122. }
  123. @end
  124. #pragma mark - Activities
  125. @implementation FLEXShortcutsFactory (Activities)
  126. + (void)load { FLEX_EXIT_IF_TESTING()
  127. // Property was added in iOS 10 but we want it on iOS 9 too
  128. FLEXRuntimeUtilityTryAddNonatomicProperty(9, item, UIActivityItemProvider.class, id, PropertyKey(ReadOnly));
  129. self.append.properties(@[
  130. @"item", @"placeholderItem", @"activityType"
  131. ]).forClass(UIActivityItemProvider.class);
  132. self.append.properties(@[
  133. @"activityItems", @"applicationActivities", @"excludedActivityTypes", @"completionHandler"
  134. ]).forClass(UIActivityViewController.class);
  135. }
  136. @end
  137. #pragma mark - Blocks
  138. @implementation FLEXShortcutsFactory (Blocks)
  139. + (void)load { FLEX_EXIT_IF_TESTING()
  140. self.append.methods(@[@"invoke"]).forClass(NSClassFromString(@"NSBlock"));
  141. }
  142. @end
  143. #pragma mark - Foundation
  144. @implementation FLEXShortcutsFactory (Foundation)
  145. + (void)load { FLEX_EXIT_IF_TESTING()
  146. self.append.properties(@[
  147. @"configuration", @"delegate", @"delegateQueue", @"sessionDescription",
  148. ]).methods(@[
  149. @"dataTaskWithURL:", @"finishTasksAndInvalidate", @"invalidateAndCancel",
  150. ]).forClass(NSURLSession.class);
  151. self.append.methods(@[
  152. @"cachedResponseForRequest:", @"storeCachedResponse:forRequest:",
  153. @"storeCachedResponse:forDataTask:", @"removeCachedResponseForRequest:",
  154. @"removeCachedResponseForDataTask:", @"removeCachedResponsesSinceDate:",
  155. @"removeAllCachedResponses",
  156. ]).forClass(NSURLCache.class);
  157. self.append.methods(@[
  158. @"postNotification:", @"postNotificationName:object:userInfo:",
  159. @"addObserver:selector:name:object:", @"removeObserver:",
  160. @"removeObserver:name:object:",
  161. ]).forClass(NSNotificationCenter.class);
  162. // NSTimeZone class properties aren't real properties
  163. FLEXRuntimeUtilityTryAddObjectProperty(2, localTimeZone, NSTimeZone.flex_metaclass, NSTimeZone);
  164. FLEXRuntimeUtilityTryAddObjectProperty(2, systemTimeZone, NSTimeZone.flex_metaclass, NSTimeZone);
  165. FLEXRuntimeUtilityTryAddObjectProperty(2, defaultTimeZone, NSTimeZone.flex_metaclass, NSTimeZone);
  166. FLEXRuntimeUtilityTryAddObjectProperty(2, knownTimeZoneNames, NSTimeZone.flex_metaclass, NSArray);
  167. FLEXRuntimeUtilityTryAddObjectProperty(2, abbreviationDictionary, NSTimeZone.flex_metaclass, NSDictionary);
  168. self.append.classMethods(@[
  169. @"timeZoneWithName:", @"timeZoneWithAbbreviation:", @"timeZoneForSecondsFromGMT:", @"", @"", @"",
  170. ]).forClass(NSTimeZone.flex_metaclass);
  171. self.append.classProperties(@[
  172. @"defaultTimeZone", @"systemTimeZone", @"localTimeZone"
  173. ]).forClass(NSTimeZone.class);
  174. // self.append.<#type#>(@[@"<#value#>"]).forClass(NSURLSession.class);
  175. //
  176. //
  177. // self.append.<#type#>(@[@"<#value#>"]).forClass(NSURLSession.class);
  178. //
  179. //
  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. @end