FHSViewController.m 9.1 KB

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