FHSViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // FHSViewController.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 1/6/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FHSViewController.h"
  9. #import "FHSSnapshotView.h"
  10. #import "FLEXHierarchyViewController.h"
  11. #import "FLEXColor.h"
  12. #import "FLEXAlert.h"
  13. #import "FLEXWindow.h"
  14. #import "FLEXResources.h"
  15. #import "NSArray+Functional.h"
  16. #import "UIBarButtonItem+FLEX.h"
  17. BOOL const kFHSViewControllerExcludeFLEXWindows = YES;
  18. @interface FHSViewController () <FHSSnapshotViewDelegate>
  19. /// An array of only the target views whose hierarchies
  20. /// we wish to snapshot, not every view in the snapshot.
  21. @property (nonatomic, readonly) NSArray<UIView *> *targetViews;
  22. @property (nonatomic, readonly) NSArray<FHSView *> *views;
  23. @property (nonatomic ) NSArray<FHSViewSnapshot *> *snapshots;
  24. @property (nonatomic, ) FHSSnapshotView *snapshotView;
  25. @property (nonatomic, readonly) UIView *containerView;
  26. @property (nonatomic, readonly) NSArray<UIView *> *viewsAtTap;
  27. @property (nonatomic, readonly) NSMutableSet<Class> *forceHideHeaders;
  28. @end
  29. @implementation FHSViewController
  30. @synthesize views = _views;
  31. @synthesize snapshotView = _snapshotView;
  32. #pragma mark - Initialization
  33. + (instancetype)snapshotWindows:(NSArray<UIWindow *> *)windows {
  34. return [[self alloc] initWithViews:windows viewsAtTap:nil selectedView:nil];
  35. }
  36. + (instancetype)snapshotView:(UIView *)view {
  37. return [[self alloc] initWithViews:@[view] viewsAtTap:nil selectedView:nil];
  38. }
  39. + (instancetype)snapshotViewsAtTap:(NSArray<UIView *> *)viewsAtTap selectedView:(UIView *)view {
  40. NSParameterAssert(viewsAtTap.count);
  41. NSParameterAssert(view.window);
  42. return [[self alloc] initWithViews:@[view.window] viewsAtTap:viewsAtTap selectedView:view];
  43. }
  44. - (id)initWithViews:(NSArray<UIView *> *)views
  45. viewsAtTap:(NSArray<UIView *> *)viewsAtTap
  46. selectedView:(UIView *)view {
  47. NSParameterAssert(views.count);
  48. self = [super init];
  49. if (self) {
  50. _forceHideHeaders = [NSMutableSet setWithObject:NSClassFromString(@"_UITableViewCellSeparatorView")];
  51. _selectedView = view;
  52. _viewsAtTap = viewsAtTap;
  53. if (!viewsAtTap && kFHSViewControllerExcludeFLEXWindows) {
  54. Class flexwindow = [FLEXWindow class];
  55. views = [views flex_filtered:^BOOL(UIView *view, NSUInteger idx) {
  56. return [view class] != flexwindow;
  57. }];
  58. }
  59. _targetViews = views;
  60. _views = [views flex_mapped:^id(UIView *view, NSUInteger idx) {
  61. BOOL isScrollView = [view.superview isKindOfClass:[UIScrollView class]];
  62. return [FHSView forView:view isInScrollView:isScrollView];
  63. }];
  64. }
  65. return self;
  66. }
  67. - (void)refreshSnapshotView {
  68. // Alert view to block interaction while we load everything
  69. UIAlertController *loading = [FLEXAlert makeAlert:^(FLEXAlert *make) {
  70. make.title(@"Please Wait").message(@"Generating snapshot…");
  71. }];
  72. [self presentViewController:loading animated:YES completion:^{
  73. self.snapshots = [self.views flex_mapped:^id(FHSView *view, NSUInteger idx) {
  74. return [FHSViewSnapshot snapshotWithView:view];
  75. }];
  76. FHSSnapshotView *newSnapshotView = [FHSSnapshotView delegate:self];
  77. // This work is highly intensive so we do it on a background thread first
  78. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  79. // Setting the snapshots computes lots of SCNNodes, takes several seconds
  80. newSnapshotView.snapshots = self.snapshots;
  81. // After we finish generating all the model objects and scene nodes, display the view
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. // Dismiss alert
  84. [loading dismissViewControllerAnimated:YES completion:nil];
  85. self.snapshotView = newSnapshotView;
  86. });
  87. });
  88. }];
  89. }
  90. #pragma mark - View Controller Lifecycle
  91. - (void)loadView {
  92. [super loadView];
  93. self.view.backgroundColor = FLEXColor.primaryBackgroundColor;
  94. }
  95. - (void)viewDidLoad {
  96. [super viewDidLoad];
  97. // Initialize back bar button item for 3D view to look like a button
  98. self.navigationItem.hidesBackButton = YES;
  99. self.navigationItem.leftBarButtonItem = [UIBarButtonItem
  100. itemWithImage:FLEXResources.toggle2DIcon
  101. target:self.navigationController
  102. action:@selector(toggleHierarchyMode)
  103. ];
  104. }
  105. - (void)viewDidAppear:(BOOL)animated {
  106. [super viewDidAppear:animated];
  107. if (!_snapshotView) {
  108. [self refreshSnapshotView];
  109. }
  110. }
  111. #pragma mark - Public
  112. - (void)setSelectedView:(UIView *)view {
  113. _selectedView = view;
  114. self.snapshotView.selectedView = view ? [self snapshotForView:view] : nil;
  115. }
  116. #pragma mark - Private
  117. #pragma mark Properties
  118. - (FHSSnapshotView *)snapshotView {
  119. return self.isViewLoaded ? _snapshotView : nil;
  120. }
  121. - (void)setSnapshotView:(FHSSnapshotView *)snapshotView {
  122. NSParameterAssert(snapshotView);
  123. _snapshotView = snapshotView;
  124. // Initialize our toolbar items
  125. self.toolbarItems = @[
  126. [UIBarButtonItem itemWithCustomView:snapshotView.spacingSlider],
  127. UIBarButtonItem.flex_flexibleSpace,
  128. [UIBarButtonItem
  129. itemWithImage:FLEXResources.moreIcon
  130. target:self action:@selector(didPressOptionsButton)
  131. ],
  132. UIBarButtonItem.flex_flexibleSpace,
  133. [UIBarButtonItem itemWithCustomView:snapshotView.depthSlider]
  134. ];
  135. [self resizeToolbarItems:self.view.frame.size];
  136. // If we have views-at-tap, dim the other views
  137. [snapshotView emphasizeViews:self.viewsAtTap];
  138. // Set the selected view, if any
  139. snapshotView.selectedView = [self snapshotForView:self.selectedView];
  140. snapshotView.headerExclusions = self.forceHideHeaders.allObjects;
  141. [snapshotView setNeedsLayout];
  142. // Remove old snapshot, if any, and add the new one
  143. [_snapshotView removeFromSuperview];
  144. snapshotView.frame = self.containerView.bounds;
  145. [self.containerView addSubview:snapshotView];
  146. }
  147. - (UIView *)containerView {
  148. return self.view;
  149. }
  150. #pragma mark Helper
  151. - (FHSViewSnapshot *)snapshotForView:(UIView *)view {
  152. if (!view || !self.snapshots.count) return nil;
  153. for (FHSViewSnapshot *snapshot in self.snapshots) {
  154. FHSViewSnapshot *found = [snapshot snapshotForView:view];
  155. if (found) {
  156. return found;
  157. }
  158. }
  159. // Error: we have snapshots but the view we requested is not in one
  160. @throw NSInternalInconsistencyException;
  161. return nil;
  162. }
  163. #pragma mark Events
  164. - (void)didPressOptionsButton {
  165. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  166. if (self.selectedView) {
  167. make.button(@"Hide selected view").handler(^(NSArray<NSString *> *strings) {
  168. [self.snapshotView hideView:[self snapshotForView:self.selectedView]];
  169. });
  170. make.button(@"Hide headers for views like this").handler(^(NSArray<NSString *> *strings) {
  171. Class cls = [self.selectedView class];
  172. if (![self.forceHideHeaders containsObject:cls]) {
  173. [self.forceHideHeaders addObject:[self.selectedView class]];
  174. self.snapshotView.headerExclusions = self.forceHideHeaders.allObjects;
  175. }
  176. });
  177. }
  178. make.title(@"Options");
  179. make.button(@"Toggle headers").handler(^(NSArray<NSString *> *strings) {
  180. [self.snapshotView toggleShowHeaders];
  181. });
  182. make.button(@"Toggle outlines").handler(^(NSArray<NSString *> *strings) {
  183. [self.snapshotView toggleShowBorders];
  184. });
  185. make.button(@"Cancel").cancelStyle();
  186. } showFrom:self];
  187. }
  188. - (void)resizeToolbarItems:(CGSize)viewSize {
  189. CGFloat sliderHeights = self.snapshotView.spacingSlider.bounds.size.height;
  190. CGFloat sliderWidths = viewSize.width / 3.f;
  191. CGRect frame = CGRectMake(0, 0, sliderWidths, sliderHeights);
  192. self.snapshotView.spacingSlider.frame = frame;
  193. self.snapshotView.depthSlider.frame = frame;
  194. [self.navigationController.toolbar setNeedsLayout];
  195. }
  196. - (void)viewWillTransitionToSize:(CGSize)size
  197. withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  198. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  199. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  200. [self resizeToolbarItems:self.view.frame.size];
  201. } completion:nil];
  202. }
  203. #pragma mark FHSSnapshotViewDelegate
  204. - (void)didDeselectView:(FHSViewSnapshot *)snapshot {
  205. // Our setter would also call the setter for the snapshot view,
  206. // which we don't need to do here since it is already selected
  207. _selectedView = nil;
  208. }
  209. - (void)didLongPressView:(FHSViewSnapshot *)snapshot {
  210. }
  211. - (void)didSelectView:(FHSViewSnapshot *)snapshot {
  212. // Our setter would also call the setter for the snapshot view,
  213. // which we don't need to do here since it is already selected
  214. _selectedView = snapshot.view.view;
  215. }
  216. @end