FLEXViewShortcuts.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // FLEXViewShortcuts.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 12/11/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXViewShortcuts.h"
  9. #import "FLEXObjectExplorerFactory.h"
  10. #import "FLEXImagePreviewViewController.h"
  11. @interface FLEXViewShortcuts ()
  12. @property (nonatomic, readonly) UIView *view;
  13. @property (nonatomic, readonly) BOOL showsViewControllerRow;
  14. @end
  15. @implementation FLEXViewShortcuts
  16. #pragma mark - Internal
  17. - (UIView *)view {
  18. return self.object;
  19. }
  20. + (UIViewController *)viewControllerForView:(UIView *)view {
  21. NSString *viewDelegate = @"viewDelegate";
  22. if ([view respondsToSelector:NSSelectorFromString(viewDelegate)]) {
  23. return [view valueForKey:viewDelegate];
  24. }
  25. return nil;
  26. }
  27. + (UIViewController *)viewControllerForAncestralView:(UIView *)view {
  28. NSString *_viewControllerForAncestor = @"_viewControllerForAncestor";
  29. if ([view respondsToSelector:NSSelectorFromString(_viewControllerForAncestor)]) {
  30. return [view valueForKey:_viewControllerForAncestor];
  31. }
  32. return nil;
  33. }
  34. - (UIViewController *)viewControllerForView {
  35. return [[self class] viewControllerForView:self.view] ?:
  36. [[self class] viewControllerForAncestralView:self.view];
  37. }
  38. - (UIViewController *)imagePreviewViewController {
  39. if (!CGRectIsEmpty(self.view.bounds)) {
  40. CGSize viewSize = self.view.bounds.size;
  41. UIGraphicsBeginImageContextWithOptions(viewSize, NO, 0.0);
  42. [self.view drawViewHierarchyInRect:CGRectMake(0, 0, viewSize.width, viewSize.height) afterScreenUpdates:YES];
  43. UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
  44. UIGraphicsEndImageContext();
  45. return [FLEXImagePreviewViewController forImage:previewImage];
  46. }
  47. return nil;
  48. }
  49. #pragma mark - Overrides
  50. + (instancetype)forObject:(UIView *)view {
  51. // Views without a superview don't need the "View Controller for Ancestor" row
  52. BOOL hasViewController = [self viewControllerForView:view] != nil;
  53. BOOL hasAncestralVC = [self viewControllerForAncestralView:view] != nil;
  54. NSString *vcRowTitle = hasViewController ? @"View Controller" :
  55. hasAncestralVC ? @"View Controller for Ancestor" : nil;
  56. // These additional rows will appear at the beginning of the shortcuts section.
  57. // The methods below are written in such a way that they will not interfere
  58. // with properties/etc being registered alongside these
  59. FLEXViewShortcuts *shortcuts = [self forObject:view additionalRows:(
  60. vcRowTitle ? @[vcRowTitle, @"Preview"] : @[@"Preview"]
  61. )];
  62. shortcuts->_showsViewControllerRow = hasViewController || hasAncestralVC;
  63. return shortcuts;
  64. }
  65. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  66. switch (row) {
  67. case 0:
  68. if (self.showsViewControllerRow) {
  69. return [FLEXObjectExplorerFactory
  70. explorerViewControllerForObject:[self viewControllerForView]
  71. ];
  72. } else {
  73. return [self imagePreviewViewController];
  74. }
  75. case 1:
  76. if (self.showsViewControllerRow) {
  77. return [self imagePreviewViewController];
  78. }
  79. default:
  80. return [super viewControllerToPushForRow:row];
  81. }
  82. }
  83. #pragma mark - Runtime Adjustment
  84. #define PropertyKey(suffix) kFLEXPropertyAttributeKey##suffix : @""
  85. #define PropertyKeyGetter(getter) kFLEXPropertyAttributeKeyCustomGetter : NSStringFromSelector(@selector(getter))
  86. #define PropertyKeySetter(setter) kFLEXPropertyAttributeKeyCustomSetter : NSStringFromSelector(@selector(setter))
  87. /// Takes: min iOS version, property name, target class, property type, and a list of attributes
  88. #define FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, type, ...) ({ \
  89. if (@available(iOS iOS_atLeast, *)) { \
  90. NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:@{ \
  91. kFLEXPropertyAttributeKeyTypeEncoding : @(type), \
  92. __VA_ARGS__ \
  93. }]; \
  94. [FLEXRuntimeUtility \
  95. tryAddPropertyWithName:#name \
  96. attributes:attrs \
  97. toClass:[cls class] \
  98. ]; \
  99. } \
  100. })
  101. /// Takes: min iOS version, property name, target class, property type, and a list of attributes
  102. #define FLEXRuntimeUtilityTryAddNonatomicProperty(iOS_atLeast, name, cls, type, ...) \
  103. FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, @encode(type), PropertyKey(NonAtomic), __VA_ARGS__);
  104. /// Takes: min iOS version, property name, target class, property type (class name), and a list of attributes
  105. #define FLEXRuntimeUtilityTryAddObjectProperty(iOS_atLeast, name, cls, type, ...) \
  106. FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, FLEXEncodeClass(type), PropertyKey(NonAtomic), __VA_ARGS__);
  107. + (void)load {
  108. // A quirk of UIView and some other classes: a lot of the `@property`s are
  109. // not actually properties from the perspective of the runtime.
  110. //
  111. // We add these properties to the class at runtime if they haven't been added yet.
  112. // This way, we can use our property editor to access and change them.
  113. // The property attributes match the declared attributes in their headers.
  114. // UIView, public
  115. FLEXRuntimeUtilityTryAddNonatomicProperty(2, frame, UIView, CGRect);
  116. FLEXRuntimeUtilityTryAddNonatomicProperty(2, alpha, UIView, CGFloat);
  117. FLEXRuntimeUtilityTryAddNonatomicProperty(2, clipsToBounds, UIView, BOOL);
  118. FLEXRuntimeUtilityTryAddNonatomicProperty(2, opaque, UIView, BOOL, PropertyKeyGetter(isOpaque));
  119. FLEXRuntimeUtilityTryAddNonatomicProperty(2, hidden, UIView, BOOL, PropertyKeyGetter(isHidden));
  120. FLEXRuntimeUtilityTryAddObjectProperty(2, backgroundColor, UIView, UIColor, PropertyKey(Copy));
  121. FLEXRuntimeUtilityTryAddObjectProperty(6, constraints, UIView, NSArray, PropertyKey(ReadOnly));
  122. FLEXRuntimeUtilityTryAddObjectProperty(2, subviews, UIView, NSArray, PropertyKey(ReadOnly));
  123. FLEXRuntimeUtilityTryAddObjectProperty(2, superview, UIView, UIView, PropertyKey(ReadOnly));
  124. // UIButton, private
  125. FLEXRuntimeUtilityTryAddObjectProperty(2, font, UIButton, UIFont, PropertyKey(ReadOnly));
  126. }
  127. @end