FLEXViewShortcuts.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "FLEXShortcut.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. #import "FLEXImagePreviewViewController.h"
  13. @interface FLEXViewShortcuts ()
  14. @property (nonatomic, readonly) UIView *view;
  15. @end
  16. @implementation FLEXViewShortcuts
  17. #pragma mark - Internal
  18. - (UIView *)view {
  19. return self.object;
  20. }
  21. + (UIViewController *)viewControllerForView:(UIView *)view {
  22. NSString *viewDelegate = @"viewDelegate";
  23. if ([view respondsToSelector:NSSelectorFromString(viewDelegate)]) {
  24. return [view valueForKey:viewDelegate];
  25. }
  26. return nil;
  27. }
  28. + (UIViewController *)viewControllerForAncestralView:(UIView *)view {
  29. NSString *_viewControllerForAncestor = @"_viewControllerForAncestor";
  30. if ([view respondsToSelector:NSSelectorFromString(_viewControllerForAncestor)]) {
  31. return [view valueForKey:_viewControllerForAncestor];
  32. }
  33. return nil;
  34. }
  35. + (UIViewController *)nearestViewControllerForView:(UIView *)view {
  36. return [self viewControllerForView:view] ?: [self viewControllerForAncestralView:view];
  37. }
  38. #pragma mark - Overrides
  39. + (instancetype)forObject:(UIView *)view {
  40. // In the past, FLEX would not hold a strong reference to something like this.
  41. // After using FLEX for so long, I am certain it is more useful to eagerly
  42. // reference something as useful as a view controller so that the reference
  43. // is not lost and swept out from under you before you can access it.
  44. //
  45. // The alternative here is to use a future in place of `controller` which would
  46. // dynamically grab a reference to the view controller. 99% of the time, however,
  47. // it is not all that useful. If you need it to refresh, you can simply go back
  48. // and go forward again and it will show if the view controller is nil or changed.
  49. UIViewController *controller = [FLEXViewShortcuts nearestViewControllerForView:view];
  50. return [self forObject:view additionalRows:@[
  51. [FLEXActionShortcut title:@"Nearest View Controller"
  52. subtitle:^NSString *(id view) {
  53. return [FLEXRuntimeUtility safeDescriptionForObject:controller];
  54. }
  55. viewer:^UIViewController *(id view) {
  56. return [FLEXObjectExplorerFactory explorerViewControllerForObject:controller];
  57. }
  58. accessoryType:^UITableViewCellAccessoryType(id view) {
  59. return controller ? UITableViewCellAccessoryDisclosureIndicator : 0;
  60. }
  61. ],
  62. [FLEXActionShortcut title:@"Preview Image" subtitle:nil
  63. viewer:^UIViewController *(id view) {
  64. return [FLEXImagePreviewViewController previewForView:view];
  65. }
  66. accessoryType:^UITableViewCellAccessoryType(id view) {
  67. return UITableViewCellAccessoryDisclosureIndicator;
  68. }
  69. ]
  70. ]];
  71. }
  72. #pragma mark - Runtime Adjustment
  73. #define PropertyKey(suffix) kFLEXPropertyAttributeKey##suffix : @""
  74. #define PropertyKeyGetter(getter) kFLEXPropertyAttributeKeyCustomGetter : NSStringFromSelector(@selector(getter))
  75. #define PropertyKeySetter(setter) kFLEXPropertyAttributeKeyCustomSetter : NSStringFromSelector(@selector(setter))
  76. /// Takes: min iOS version, property name, target class, property type, and a list of attributes
  77. #define FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, type, ...) ({ \
  78. if (@available(iOS iOS_atLeast, *)) { \
  79. NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:@{ \
  80. kFLEXPropertyAttributeKeyTypeEncoding : @(type), \
  81. __VA_ARGS__ \
  82. }]; \
  83. [FLEXRuntimeUtility \
  84. tryAddPropertyWithName:#name \
  85. attributes:attrs \
  86. toClass:[cls class] \
  87. ]; \
  88. } \
  89. })
  90. /// Takes: min iOS version, property name, target class, property type, and a list of attributes
  91. #define FLEXRuntimeUtilityTryAddNonatomicProperty(iOS_atLeast, name, cls, type, ...) \
  92. FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, @encode(type), PropertyKey(NonAtomic), __VA_ARGS__);
  93. /// Takes: min iOS version, property name, target class, property type (class name), and a list of attributes
  94. #define FLEXRuntimeUtilityTryAddObjectProperty(iOS_atLeast, name, cls, type, ...) \
  95. FLEXRuntimeUtilityTryAddProperty(iOS_atLeast, name, cls, FLEXEncodeClass(type), PropertyKey(NonAtomic), __VA_ARGS__);
  96. + (void)load {
  97. // A quirk of UIView and some other classes: a lot of the `@property`s are
  98. // not actually properties from the perspective of the runtime.
  99. //
  100. // We add these properties to the class at runtime if they haven't been added yet.
  101. // This way, we can use our property editor to access and change them.
  102. // The property attributes match the declared attributes in their headers.
  103. // UIView, public
  104. FLEXRuntimeUtilityTryAddNonatomicProperty(2, frame, UIView, CGRect);
  105. FLEXRuntimeUtilityTryAddNonatomicProperty(2, alpha, UIView, CGFloat);
  106. FLEXRuntimeUtilityTryAddNonatomicProperty(2, clipsToBounds, UIView, BOOL);
  107. FLEXRuntimeUtilityTryAddNonatomicProperty(2, opaque, UIView, BOOL, PropertyKeyGetter(isOpaque));
  108. FLEXRuntimeUtilityTryAddNonatomicProperty(2, hidden, UIView, BOOL, PropertyKeyGetter(isHidden));
  109. FLEXRuntimeUtilityTryAddObjectProperty(2, backgroundColor, UIView, UIColor, PropertyKey(Copy));
  110. FLEXRuntimeUtilityTryAddObjectProperty(6, constraints, UIView, NSArray, PropertyKey(ReadOnly));
  111. FLEXRuntimeUtilityTryAddObjectProperty(2, subviews, UIView, NSArray, PropertyKey(ReadOnly));
  112. FLEXRuntimeUtilityTryAddObjectProperty(2, superview, UIView, UIView, PropertyKey(ReadOnly));
  113. // UIButton, private
  114. FLEXRuntimeUtilityTryAddObjectProperty(2, font, UIButton, UIFont, PropertyKey(ReadOnly));
  115. // UIViewController, public
  116. FLEXRuntimeUtilityTryAddObjectProperty(3, toolbarItems, UIViewController, NSArray);
  117. }
  118. @end