You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

937 lines
36 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // FLEXExplorerViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXExplorerViewController.h"
  9. #import "FLEXExplorerToolbar.h"
  10. #import "FLEXToolbarItem.h"
  11. #import "FLEXUtility.h"
  12. #import "FLEXHierarchyTableViewController.h"
  13. #import "FLEXGlobalsTableViewController.h"
  14. #import "FLEXObjectExplorerViewController.h"
  15. #import "FLEXObjectExplorerFactory.h"
  16. #import "FLEXNetworkHistoryTableViewController.h"
  17. static NSString *const kFLEXToolbarTopMarginDefaultsKey = @"com.flex.FLEXToolbar.topMargin";
  18. typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
  19. FLEXExplorerModeDefault,
  20. FLEXExplorerModeSelect,
  21. FLEXExplorerModeMove
  22. };
  23. @interface FLEXExplorerViewController () <FLEXHierarchyTableViewControllerDelegate, FLEXGlobalsTableViewControllerDelegate>
  24. @property (nonatomic) FLEXExplorerToolbar *explorerToolbar;
  25. /// Tracks the currently active tool/mode
  26. @property (nonatomic) FLEXExplorerMode currentMode;
  27. /// Gesture recognizer for dragging a view in move mode
  28. @property (nonatomic) UIPanGestureRecognizer *movePanGR;
  29. /// Gesture recognizer for showing additional details on the selected view
  30. @property (nonatomic) UITapGestureRecognizer *detailsTapGR;
  31. /// Only valid while a move pan gesture is in progress.
  32. @property (nonatomic) CGRect selectedViewFrameBeforeDragging;
  33. /// Only valid while a toolbar drag pan gesture is in progress.
  34. @property (nonatomic) CGRect toolbarFrameBeforeDragging;
  35. /// Borders of all the visible views in the hierarchy at the selection point.
  36. /// The keys are NSValues with the corresponding view (nonretained).
  37. @property (nonatomic) NSDictionary<NSValue *, UIView *> *outlineViewsForVisibleViews;
  38. /// The actual views at the selection point with the deepest view last.
  39. @property (nonatomic) NSArray<UIView *> *viewsAtTapPoint;
  40. /// The view that we're currently highlighting with an overlay and displaying details for.
  41. @property (nonatomic) UIView *selectedView;
  42. /// A colored transparent overlay to indicate that the view is selected.
  43. @property (nonatomic) UIView *selectedViewOverlay;
  44. /// Tracked so we can restore the key window after dismissing a modal.
  45. /// We need to become key after modal presentation so we can correctly capture input.
  46. /// If we're just showing the toolbar, we want the main app's window to remain key so that we don't interfere with input, status bar, etc.
  47. @property (nonatomic) UIWindow *previousKeyWindow;
  48. /// All views that we're KVOing. Used to help us clean up properly.
  49. @property (nonatomic) NSMutableSet<UIView *> *observedViews;
  50. @end
  51. @implementation FLEXExplorerViewController
  52. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  53. {
  54. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  55. if (self) {
  56. self.observedViews = [NSMutableSet set];
  57. }
  58. return self;
  59. }
  60. -(void)dealloc
  61. {
  62. for (UIView *view in _observedViews) {
  63. [self stopObservingView:view];
  64. }
  65. }
  66. - (void)viewDidLoad
  67. {
  68. [super viewDidLoad];
  69. // Toolbar
  70. self.explorerToolbar = [FLEXExplorerToolbar new];
  71. // Start the toolbar off below any bars that may be at the top of the view.
  72. id toolbarOriginYDefault = [[NSUserDefaults standardUserDefaults] objectForKey:kFLEXToolbarTopMarginDefaultsKey];
  73. CGFloat toolbarOriginY = toolbarOriginYDefault ? [toolbarOriginYDefault doubleValue] : 100;
  74. CGRect safeArea = [self viewSafeArea];
  75. CGSize toolbarSize = [self.explorerToolbar sizeThatFits:CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(safeArea))];
  76. [self updateToolbarPositionWithUnconstrainedFrame:CGRectMake(CGRectGetMinX(safeArea), toolbarOriginY, toolbarSize.width, toolbarSize.height)];
  77. self.explorerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
  78. [self.view addSubview:self.explorerToolbar];
  79. [self setupToolbarActions];
  80. [self setupToolbarGestures];
  81. // View selection
  82. UITapGestureRecognizer *selectionTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSelectionTap:)];
  83. [self.view addGestureRecognizer:selectionTapGR];
  84. // View moving
  85. self.movePanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleMovePan:)];
  86. self.movePanGR.enabled = self.currentMode == FLEXExplorerModeMove;
  87. [self.view addGestureRecognizer:self.movePanGR];
  88. }
  89. - (void)viewWillAppear:(BOOL)animated
  90. {
  91. [super viewWillAppear:animated];
  92. [self updateButtonStates];
  93. }
  94. #pragma mark - Rotation
  95. - (UIViewController *)viewControllerForRotationAndOrientation
  96. {
  97. UIWindow *window = self.previousKeyWindow ?: [UIApplication.sharedApplication keyWindow];
  98. UIViewController *viewController = window.rootViewController;
  99. // Obfuscating selector _viewControllerForSupportedInterfaceOrientations
  100. NSString *viewControllerSelectorString = [@[@"_vie", @"wContro", @"llerFor", @"Supported", @"Interface", @"Orientations"] componentsJoinedByString:@""];
  101. SEL viewControllerSelector = NSSelectorFromString(viewControllerSelectorString);
  102. if ([viewController respondsToSelector:viewControllerSelector]) {
  103. viewController = [viewController valueForKey:viewControllerSelectorString];
  104. }
  105. return viewController;
  106. }
  107. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  108. {
  109. UIViewController *viewControllerToAsk = [self viewControllerForRotationAndOrientation];
  110. UIInterfaceOrientationMask supportedOrientations = [FLEXUtility infoPlistSupportedInterfaceOrientationsMask];
  111. if (viewControllerToAsk && viewControllerToAsk != self) {
  112. supportedOrientations = [viewControllerToAsk supportedInterfaceOrientations];
  113. }
  114. // The UIViewController docs state that this method must not return zero.
  115. // If we weren't able to get a valid value for the supported interface
  116. // orientations, default to all supported.
  117. if (supportedOrientations == 0) {
  118. supportedOrientations = UIInterfaceOrientationMaskAll;
  119. }
  120. return supportedOrientations;
  121. }
  122. - (BOOL)shouldAutorotate
  123. {
  124. UIViewController *viewControllerToAsk = [self viewControllerForRotationAndOrientation];
  125. BOOL shouldAutorotate = YES;
  126. if (viewControllerToAsk && viewControllerToAsk != self) {
  127. shouldAutorotate = [viewControllerToAsk shouldAutorotate];
  128. }
  129. return shouldAutorotate;
  130. }
  131. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  132. {
  133. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  134. for (UIView *outlineView in self.outlineViewsForVisibleViews.allValues) {
  135. outlineView.hidden = YES;
  136. }
  137. self.selectedViewOverlay.hidden = YES;
  138. } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  139. for (UIView *view in self.viewsAtTapPoint) {
  140. NSValue *key = [NSValue valueWithNonretainedObject:view];
  141. UIView *outlineView = self.outlineViewsForVisibleViews[key];
  142. outlineView.frame = [self frameInLocalCoordinatesForView:view];
  143. if (self.currentMode == FLEXExplorerModeSelect) {
  144. outlineView.hidden = NO;
  145. }
  146. }
  147. if (self.selectedView) {
  148. self.selectedViewOverlay.frame = [self frameInLocalCoordinatesForView:self.selectedView];
  149. self.selectedViewOverlay.hidden = NO;
  150. }
  151. }];
  152. }
  153. #pragma mark - Setter Overrides
  154. - (void)setSelectedView:(UIView *)selectedView
  155. {
  156. if (![_selectedView isEqual:selectedView]) {
  157. if (![self.viewsAtTapPoint containsObject:_selectedView]) {
  158. [self stopObservingView:_selectedView];
  159. }
  160. _selectedView = selectedView;
  161. [self beginObservingView:selectedView];
  162. // Update the toolbar and selected overlay
  163. self.explorerToolbar.selectedViewDescription = [FLEXUtility descriptionForView:selectedView includingFrame:YES];
  164. self.explorerToolbar.selectedViewOverlayColor = [FLEXUtility consistentRandomColorForObject:selectedView];
  165. if (selectedView) {
  166. if (!self.selectedViewOverlay) {
  167. self.selectedViewOverlay = [UIView new];
  168. [self.view addSubview:self.selectedViewOverlay];
  169. self.selectedViewOverlay.layer.borderWidth = 1.0;
  170. }
  171. UIColor *outlineColor = [FLEXUtility consistentRandomColorForObject:selectedView];
  172. self.selectedViewOverlay.backgroundColor = [outlineColor colorWithAlphaComponent:0.2];
  173. self.selectedViewOverlay.layer.borderColor = outlineColor.CGColor;
  174. self.selectedViewOverlay.frame = [self.view convertRect:selectedView.bounds fromView:selectedView];
  175. // Make sure the selected overlay is in front of all the other subviews except the toolbar, which should always stay on top.
  176. [self.view bringSubviewToFront:self.selectedViewOverlay];
  177. [self.view bringSubviewToFront:self.explorerToolbar];
  178. } else {
  179. [self.selectedViewOverlay removeFromSuperview];
  180. self.selectedViewOverlay = nil;
  181. }
  182. // Some of the button states depend on whether we have a selected view.
  183. [self updateButtonStates];
  184. }
  185. }
  186. - (void)setViewsAtTapPoint:(NSArray<UIView *> *)viewsAtTapPoint
  187. {
  188. if (![_viewsAtTapPoint isEqual:viewsAtTapPoint]) {
  189. for (UIView *view in _viewsAtTapPoint) {
  190. if (view != self.selectedView) {
  191. [self stopObservingView:view];
  192. }
  193. }
  194. _viewsAtTapPoint = viewsAtTapPoint;
  195. for (UIView *view in viewsAtTapPoint) {
  196. [self beginObservingView:view];
  197. }
  198. }
  199. }
  200. - (void)setCurrentMode:(FLEXExplorerMode)currentMode
  201. {
  202. if (_currentMode != currentMode) {
  203. _currentMode = currentMode;
  204. switch (currentMode) {
  205. case FLEXExplorerModeDefault:
  206. [self removeAndClearOutlineViews];
  207. self.viewsAtTapPoint = nil;
  208. self.selectedView = nil;
  209. break;
  210. case FLEXExplorerModeSelect:
  211. // Make sure the outline views are unhidden in case we came from the move mode.
  212. for (NSValue *key in self.outlineViewsForVisibleViews) {
  213. UIView *outlineView = self.outlineViewsForVisibleViews[key];
  214. outlineView.hidden = NO;
  215. }
  216. break;
  217. case FLEXExplorerModeMove:
  218. // Hide all the outline views to focus on the selected view, which is the only one that will move.
  219. for (NSValue *key in self.outlineViewsForVisibleViews) {
  220. UIView *outlineView = self.outlineViewsForVisibleViews[key];
  221. outlineView.hidden = YES;
  222. }
  223. break;
  224. }
  225. self.movePanGR.enabled = currentMode == FLEXExplorerModeMove;
  226. [self updateButtonStates];
  227. }
  228. }
  229. #pragma mark - View Tracking
  230. - (void)beginObservingView:(UIView *)view
  231. {
  232. // Bail if we're already observing this view or if there's nothing to observe.
  233. if (!view || [self.observedViews containsObject:view]) {
  234. return;
  235. }
  236. for (NSString *keyPath in [[self class] viewKeyPathsToTrack]) {
  237. [view addObserver:self forKeyPath:keyPath options:0 context:NULL];
  238. }
  239. [self.observedViews addObject:view];
  240. }
  241. - (void)stopObservingView:(UIView *)view
  242. {
  243. if (!view) {
  244. return;
  245. }
  246. for (NSString *keyPath in [[self class] viewKeyPathsToTrack]) {
  247. [view removeObserver:self forKeyPath:keyPath];
  248. }
  249. [self.observedViews removeObject:view];
  250. }
  251. + (NSArray<NSString *> *)viewKeyPathsToTrack
  252. {
  253. static NSArray<NSString *> *trackedViewKeyPaths = nil;
  254. static dispatch_once_t onceToken;
  255. dispatch_once(&onceToken, ^{
  256. NSString *frameKeyPath = NSStringFromSelector(@selector(frame));
  257. trackedViewKeyPaths = @[frameKeyPath];
  258. });
  259. return trackedViewKeyPaths;
  260. }
  261. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context
  262. {
  263. [self updateOverlayAndDescriptionForObjectIfNeeded:object];
  264. }
  265. - (void)updateOverlayAndDescriptionForObjectIfNeeded:(id)object
  266. {
  267. NSUInteger indexOfView = [self.viewsAtTapPoint indexOfObject:object];
  268. if (indexOfView != NSNotFound) {
  269. UIView *view = self.viewsAtTapPoint[indexOfView];
  270. NSValue *key = [NSValue valueWithNonretainedObject:view];
  271. UIView *outline = self.outlineViewsForVisibleViews[key];
  272. if (outline) {
  273. outline.frame = [self frameInLocalCoordinatesForView:view];
  274. }
  275. }
  276. if (object == self.selectedView) {
  277. // Update the selected view description since we show the frame value there.
  278. self.explorerToolbar.selectedViewDescription = [FLEXUtility descriptionForView:self.selectedView includingFrame:YES];
  279. CGRect selectedViewOutlineFrame = [self frameInLocalCoordinatesForView:self.selectedView];
  280. self.selectedViewOverlay.frame = selectedViewOutlineFrame;
  281. }
  282. }
  283. - (CGRect)frameInLocalCoordinatesForView:(UIView *)view
  284. {
  285. // First convert to window coordinates since the view may be in a different window than our view.
  286. CGRect frameInWindow = [view convertRect:view.bounds toView:nil];
  287. // Then convert from the window to our view's coordinate space.
  288. return [self.view convertRect:frameInWindow fromView:nil];
  289. }
  290. #pragma mark - Toolbar Buttons
  291. - (void)setupToolbarActions
  292. {
  293. [self.explorerToolbar.selectItem addTarget:self action:@selector(selectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  294. [self.explorerToolbar.hierarchyItem addTarget:self action:@selector(hierarchyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  295. [self.explorerToolbar.moveItem addTarget:self action:@selector(moveButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  296. [self.explorerToolbar.globalsItem addTarget:self action:@selector(globalsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  297. [self.explorerToolbar.closeItem addTarget:self action:@selector(closeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  298. }
  299. - (void)selectButtonTapped:(FLEXToolbarItem *)sender
  300. {
  301. [self toggleSelectTool];
  302. }
  303. - (void)hierarchyButtonTapped:(FLEXToolbarItem *)sender
  304. {
  305. [self toggleViewsTool];
  306. }
  307. - (NSArray<UIView *> *)allViewsInHierarchy
  308. {
  309. NSMutableArray<UIView *> *allViews = [NSMutableArray array];
  310. NSArray<UIWindow *> *windows = [FLEXUtility allWindows];
  311. for (UIWindow *window in windows) {
  312. if (window != self.view.window) {
  313. [allViews addObject:window];
  314. [allViews addObjectsFromArray:[self allRecursiveSubviewsInView:window]];
  315. }
  316. }
  317. return allViews;
  318. }
  319. - (UIWindow *)statusWindow
  320. {
  321. NSString *statusBarString = [NSString stringWithFormat:@"%@arWindow", @"_statusB"];
  322. return [UIApplication.sharedApplication valueForKey:statusBarString];
  323. }
  324. - (void)moveButtonTapped:(FLEXToolbarItem *)sender
  325. {
  326. [self toggleMoveTool];
  327. }
  328. - (void)globalsButtonTapped:(FLEXToolbarItem *)sender
  329. {
  330. [self toggleMenuTool];
  331. }
  332. - (void)closeButtonTapped:(FLEXToolbarItem *)sender
  333. {
  334. self.currentMode = FLEXExplorerModeDefault;
  335. [self.delegate explorerViewControllerDidFinish:self];
  336. }
  337. - (void)updateButtonStates
  338. {
  339. // Move and details only active when an object is selected.
  340. BOOL hasSelectedObject = self.selectedView != nil;
  341. self.explorerToolbar.moveItem.enabled = hasSelectedObject;
  342. self.explorerToolbar.selectItem.selected = self.currentMode == FLEXExplorerModeSelect;
  343. self.explorerToolbar.moveItem.selected = self.currentMode == FLEXExplorerModeMove;
  344. }
  345. #pragma mark - Toolbar Dragging
  346. - (void)setupToolbarGestures
  347. {
  348. // Pan gesture for dragging.
  349. UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleToolbarPanGesture:)];
  350. [self.explorerToolbar.dragHandle addGestureRecognizer:panGR];
  351. // Tap gesture for hinting.
  352. UITapGestureRecognizer *hintTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToolbarHintTapGesture:)];
  353. [self.explorerToolbar.dragHandle addGestureRecognizer:hintTapGR];
  354. // Tap gesture for showing additional details
  355. self.detailsTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToolbarDetailsTapGesture:)];
  356. [self.explorerToolbar.selectedViewDescriptionContainer addGestureRecognizer:self.detailsTapGR];
  357. }
  358. - (void)handleToolbarPanGesture:(UIPanGestureRecognizer *)panGR
  359. {
  360. switch (panGR.state) {
  361. case UIGestureRecognizerStateBegan:
  362. self.toolbarFrameBeforeDragging = self.explorerToolbar.frame;
  363. [self updateToolbarPositionWithDragGesture:panGR];
  364. break;
  365. case UIGestureRecognizerStateChanged:
  366. case UIGestureRecognizerStateEnded:
  367. [self updateToolbarPositionWithDragGesture:panGR];
  368. break;
  369. default:
  370. break;
  371. }
  372. }
  373. - (void)updateToolbarPositionWithDragGesture:(UIPanGestureRecognizer *)panGR
  374. {
  375. CGPoint translation = [panGR translationInView:self.view];
  376. CGRect newToolbarFrame = self.toolbarFrameBeforeDragging;
  377. newToolbarFrame.origin.y += translation.y;
  378. [self updateToolbarPositionWithUnconstrainedFrame:newToolbarFrame];
  379. }
  380. - (void)updateToolbarPositionWithUnconstrainedFrame:(CGRect)unconstrainedFrame
  381. {
  382. CGRect safeArea = [self viewSafeArea];
  383. // We only constrain the Y-axis because We want the toolbar to handle the X-axis safeArea layout by itself
  384. CGFloat minY = CGRectGetMinY(safeArea);
  385. CGFloat maxY = CGRectGetMaxY(safeArea) - unconstrainedFrame.size.height;
  386. if (unconstrainedFrame.origin.y < minY) {
  387. unconstrainedFrame.origin.y = minY;
  388. } else if (unconstrainedFrame.origin.y > maxY) {
  389. unconstrainedFrame.origin.y = maxY;
  390. }
  391. self.explorerToolbar.frame = unconstrainedFrame;
  392. [[NSUserDefaults standardUserDefaults] setDouble:unconstrainedFrame.origin.y forKey:kFLEXToolbarTopMarginDefaultsKey];
  393. }
  394. - (void)handleToolbarHintTapGesture:(UITapGestureRecognizer *)tapGR
  395. {
  396. // Bounce the toolbar to indicate that it is draggable.
  397. // TODO: make it bouncier.
  398. if (tapGR.state == UIGestureRecognizerStateRecognized) {
  399. CGRect originalToolbarFrame = self.explorerToolbar.frame;
  400. const NSTimeInterval kHalfwayDuration = 0.2;
  401. const CGFloat kVerticalOffset = 30.0;
  402. [UIView animateWithDuration:kHalfwayDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  403. CGRect newToolbarFrame = self.explorerToolbar.frame;
  404. newToolbarFrame.origin.y += kVerticalOffset;
  405. self.explorerToolbar.frame = newToolbarFrame;
  406. } completion:^(BOOL finished) {
  407. [UIView animateWithDuration:kHalfwayDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  408. self.explorerToolbar.frame = originalToolbarFrame;
  409. } completion:nil];
  410. }];
  411. }
  412. }
  413. - (void)handleToolbarDetailsTapGesture:(UITapGestureRecognizer *)tapGR
  414. {
  415. if (tapGR.state == UIGestureRecognizerStateRecognized && self.selectedView) {
  416. FLEXObjectExplorerViewController *selectedViewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:self.selectedView];
  417. selectedViewExplorer.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(selectedViewExplorerFinished:)];
  418. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:selectedViewExplorer];
  419. [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
  420. }
  421. }
  422. #pragma mark - View Selection
  423. - (void)handleSelectionTap:(UITapGestureRecognizer *)tapGR
  424. {
  425. // Only if we're in selection mode
  426. if (self.currentMode == FLEXExplorerModeSelect && tapGR.state == UIGestureRecognizerStateRecognized) {
  427. // Note that [tapGR locationInView:nil] is broken in iOS 8, so we have to do a two step conversion to window coordinates.
  428. // Thanks to @lascorbe for finding this: https://github.com/Flipboard/FLEX/pull/31
  429. CGPoint tapPointInView = [tapGR locationInView:self.view];
  430. CGPoint tapPointInWindow = [self.view convertPoint:tapPointInView toView:nil];
  431. [self updateOutlineViewsForSelectionPoint:tapPointInWindow];
  432. }
  433. }
  434. - (void)updateOutlineViewsForSelectionPoint:(CGPoint)selectionPointInWindow
  435. {
  436. [self removeAndClearOutlineViews];
  437. // Include hidden views in the "viewsAtTapPoint" array so we can show them in the hierarchy list.
  438. self.viewsAtTapPoint = [self viewsAtPoint:selectionPointInWindow skipHiddenViews:NO];
  439. // For outlined views and the selected view, only use visible views.
  440. // Outlining hidden views adds clutter and makes the selection behavior confusing.
  441. NSArray<UIView *> *visibleViewsAtTapPoint = [self viewsAtPoint:selectionPointInWindow skipHiddenViews:YES];
  442. NSMutableDictionary<NSValue *, UIView *> *newOutlineViewsForVisibleViews = [NSMutableDictionary dictionary];
  443. for (UIView *view in visibleViewsAtTapPoint) {
  444. UIView *outlineView = [self outlineViewForView:view];
  445. [self.view addSubview:outlineView];
  446. NSValue *key = [NSValue valueWithNonretainedObject:view];
  447. [newOutlineViewsForVisibleViews setObject:outlineView forKey:key];
  448. }
  449. self.outlineViewsForVisibleViews = newOutlineViewsForVisibleViews;
  450. self.selectedView = [self viewForSelectionAtPoint:selectionPointInWindow];
  451. // Make sure the explorer toolbar doesn't end up behind the newly added outline views.
  452. [self.view bringSubviewToFront:self.explorerToolbar];
  453. [self updateButtonStates];
  454. }
  455. - (UIView *)outlineViewForView:(UIView *)view
  456. {
  457. CGRect outlineFrame = [self frameInLocalCoordinatesForView:view];
  458. UIView *outlineView = [[UIView alloc] initWithFrame:outlineFrame];
  459. outlineView.backgroundColor = UIColor.clearColor;
  460. outlineView.layer.borderColor = [FLEXUtility consistentRandomColorForObject:view].CGColor;
  461. outlineView.layer.borderWidth = 1.0;
  462. return outlineView;
  463. }
  464. - (void)removeAndClearOutlineViews
  465. {
  466. for (NSValue *key in self.outlineViewsForVisibleViews) {
  467. UIView *outlineView = self.outlineViewsForVisibleViews[key];
  468. [outlineView removeFromSuperview];
  469. }
  470. self.outlineViewsForVisibleViews = nil;
  471. }
  472. - (NSArray<UIView *> *)viewsAtPoint:(CGPoint)tapPointInWindow skipHiddenViews:(BOOL)skipHidden
  473. {
  474. NSMutableArray<UIView *> *views = [NSMutableArray array];
  475. for (UIWindow *window in [FLEXUtility allWindows]) {
  476. // Don't include the explorer's own window or subviews.
  477. if (window != self.view.window && [window pointInside:tapPointInWindow withEvent:nil]) {
  478. [views addObject:window];
  479. [views addObjectsFromArray:[self recursiveSubviewsAtPoint:tapPointInWindow inView:window skipHiddenViews:skipHidden]];
  480. }
  481. }
  482. return views;
  483. }
  484. - (UIView *)viewForSelectionAtPoint:(CGPoint)tapPointInWindow
  485. {
  486. // Select in the window that would handle the touch, but don't just use the result of hitTest:withEvent: so we can still select views with interaction disabled.
  487. // Default to the the application's key window if none of the windows want the touch.
  488. UIWindow *windowForSelection = [UIApplication.sharedApplication keyWindow];
  489. for (UIWindow *window in [FLEXUtility allWindows].reverseObjectEnumerator) {
  490. // Ignore the explorer's own window.
  491. if (window != self.view.window) {
  492. if ([window hitTest:tapPointInWindow withEvent:nil]) {
  493. windowForSelection = window;
  494. break;
  495. }
  496. }
  497. }
  498. // Select the deepest visible view at the tap point. This generally corresponds to what the user wants to select.
  499. return [self recursiveSubviewsAtPoint:tapPointInWindow inView:windowForSelection skipHiddenViews:YES].lastObject;
  500. }
  501. - (NSArray<UIView *> *)recursiveSubviewsAtPoint:(CGPoint)pointInView inView:(UIView *)view skipHiddenViews:(BOOL)skipHidden
  502. {
  503. NSMutableArray<UIView *> *subviewsAtPoint = [NSMutableArray array];
  504. for (UIView *subview in view.subviews) {
  505. BOOL isHidden = subview.hidden || subview.alpha < 0.01;
  506. if (skipHidden && isHidden) {
  507. continue;
  508. }
  509. BOOL subviewContainsPoint = CGRectContainsPoint(subview.frame, pointInView);
  510. if (subviewContainsPoint) {
  511. [subviewsAtPoint addObject:subview];
  512. }
  513. // If this view doesn't clip to its bounds, we need to check its subviews even if it doesn't contain the selection point.
  514. // They may be visible and contain the selection point.
  515. if (subviewContainsPoint || !subview.clipsToBounds) {
  516. CGPoint pointInSubview = [view convertPoint:pointInView toView:subview];
  517. [subviewsAtPoint addObjectsFromArray:[self recursiveSubviewsAtPoint:pointInSubview inView:subview skipHiddenViews:skipHidden]];
  518. }
  519. }
  520. return subviewsAtPoint;
  521. }
  522. - (NSArray<UIView *> *)allRecursiveSubviewsInView:(UIView *)view
  523. {
  524. NSMutableArray<UIView *> *subviews = [NSMutableArray array];
  525. for (UIView *subview in view.subviews) {
  526. [subviews addObject:subview];
  527. [subviews addObjectsFromArray:[self allRecursiveSubviewsInView:subview]];
  528. }
  529. return subviews;
  530. }
  531. - (NSDictionary<NSValue *, NSNumber *> *)hierarchyDepthsForViews:(NSArray<UIView *> *)views
  532. {
  533. NSMutableDictionary<NSValue *, NSNumber *> *hierarchyDepths = [NSMutableDictionary dictionary];
  534. for (UIView *view in views) {
  535. NSInteger depth = 0;
  536. UIView *tryView = view;
  537. while (tryView.superview) {
  538. tryView = tryView.superview;
  539. depth++;
  540. }
  541. [hierarchyDepths setObject:@(depth) forKey:[NSValue valueWithNonretainedObject:view]];
  542. }
  543. return hierarchyDepths;
  544. }
  545. #pragma mark - Selected View Moving
  546. - (void)handleMovePan:(UIPanGestureRecognizer *)movePanGR
  547. {
  548. switch (movePanGR.state) {
  549. case UIGestureRecognizerStateBegan:
  550. self.selectedViewFrameBeforeDragging = self.selectedView.frame;
  551. [self updateSelectedViewPositionWithDragGesture:movePanGR];
  552. break;
  553. case UIGestureRecognizerStateChanged:
  554. case UIGestureRecognizerStateEnded:
  555. [self updateSelectedViewPositionWithDragGesture:movePanGR];
  556. break;
  557. default:
  558. break;
  559. }
  560. }
  561. - (void)updateSelectedViewPositionWithDragGesture:(UIPanGestureRecognizer *)movePanGR
  562. {
  563. CGPoint translation = [movePanGR translationInView:self.selectedView.superview];
  564. CGRect newSelectedViewFrame = self.selectedViewFrameBeforeDragging;
  565. newSelectedViewFrame.origin.x = FLEXFloor(newSelectedViewFrame.origin.x + translation.x);
  566. newSelectedViewFrame.origin.y = FLEXFloor(newSelectedViewFrame.origin.y + translation.y);
  567. self.selectedView.frame = newSelectedViewFrame;
  568. }
  569. #pragma mark - Safe Area Handling
  570. - (CGRect)viewSafeArea
  571. {
  572. CGRect safeArea = self.view.bounds;
  573. if (@available(iOS 11.0, *)) {
  574. safeArea = UIEdgeInsetsInsetRect(self.view.bounds, self.view.safeAreaInsets);
  575. }
  576. return safeArea;
  577. }
  578. - (void)viewSafeAreaInsetsDidChange
  579. {
  580. if (@available(iOS 11.0, *)) {
  581. [super viewSafeAreaInsetsDidChange];
  582. CGRect safeArea = [self viewSafeArea];
  583. CGSize toolbarSize = [self.explorerToolbar sizeThatFits:CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(safeArea))];
  584. [self updateToolbarPositionWithUnconstrainedFrame:CGRectMake(CGRectGetMinX(self.explorerToolbar.frame), CGRectGetMinY(self.explorerToolbar.frame), toolbarSize.width, toolbarSize.height)];
  585. }
  586. }
  587. #pragma mark - Touch Handling
  588. - (BOOL)shouldReceiveTouchAtWindowPoint:(CGPoint)pointInWindowCoordinates
  589. {
  590. BOOL shouldReceiveTouch = NO;
  591. CGPoint pointInLocalCoordinates = [self.view convertPoint:pointInWindowCoordinates fromView:nil];
  592. // Always if it's on the toolbar
  593. if (CGRectContainsPoint(self.explorerToolbar.frame, pointInLocalCoordinates)) {
  594. shouldReceiveTouch = YES;
  595. }
  596. // Always if we're in selection mode
  597. if (!shouldReceiveTouch && self.currentMode == FLEXExplorerModeSelect) {
  598. shouldReceiveTouch = YES;
  599. }
  600. // Always in move mode too
  601. if (!shouldReceiveTouch && self.currentMode == FLEXExplorerModeMove) {
  602. shouldReceiveTouch = YES;
  603. }
  604. // Always if we have a modal presented
  605. if (!shouldReceiveTouch && self.presentedViewController) {
  606. shouldReceiveTouch = YES;
  607. }
  608. return shouldReceiveTouch;
  609. }
  610. #pragma mark - FLEXHierarchyTableViewControllerDelegate
  611. - (void)hierarchyViewController:(FLEXHierarchyTableViewController *)hierarchyViewController didFinishWithSelectedView:(UIView *)selectedView
  612. {
  613. // Note that we need to wait until the view controller is dismissed to calculated the frame of the outline view.
  614. // Otherwise the coordinate conversion doesn't give the correct result.
  615. [self toggleViewsToolWithCompletion:^{
  616. // If the selected view is outside of the tap point array (selected from "Full Hierarchy"),
  617. // then clear out the tap point array and remove all the outline views.
  618. if (![self.viewsAtTapPoint containsObject:selectedView]) {
  619. self.viewsAtTapPoint = nil;
  620. [self removeAndClearOutlineViews];
  621. }
  622. // If we now have a selected view and we didn't have one previously, go to "select" mode.
  623. if (self.currentMode == FLEXExplorerModeDefault && selectedView) {
  624. self.currentMode = FLEXExplorerModeSelect;
  625. }
  626. // The selected view setter will also update the selected view overlay appropriately.
  627. self.selectedView = selectedView;
  628. }];
  629. }
  630. #pragma mark - FLEXGlobalsViewControllerDelegate
  631. - (void)globalsViewControllerDidFinish:(FLEXGlobalsTableViewController *)globalsViewController
  632. {
  633. [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
  634. }
  635. #pragma mark - FLEXObjectExplorerViewController Done Action
  636. - (void)selectedViewExplorerFinished:(id)sender
  637. {
  638. [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
  639. }
  640. #pragma mark - Modal Presentation and Window Management
  641. - (void)makeKeyAndPresentViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion
  642. {
  643. // Save the current key window so we can restore it following dismissal.
  644. self.previousKeyWindow = UIApplication.sharedApplication.keyWindow;
  645. // Make our window key to correctly handle input.
  646. [self.view.window makeKeyWindow];
  647. // Fix for iOS 13, regarding custom UIMenu callouts not appearing because
  648. // the UITextEffectsWindow has a lower level than the FLEX window by default
  649. // until a text field is activated, bringing it above the FLEX window.
  650. if (@available(iOS 13, *)) {
  651. for (UIWindow *window in UIApplication.sharedApplication.windows) {
  652. if ([window isKindOfClass:NSClassFromString(@"UITextEffectsWindow")]) {
  653. if (window.windowLevel <= self.view.window.windowLevel) {
  654. window.windowLevel = self.view.window.windowLevel + 1;
  655. break;
  656. }
  657. }
  658. }
  659. }
  660. // Move the status bar on top of FLEX so we can get scroll to top behavior for taps.
  661. [[self statusWindow] setWindowLevel:self.view.window.windowLevel + 1.0];
  662. // Show the view controller.
  663. [self presentViewController:viewController animated:animated completion:completion];
  664. }
  665. - (void)resignKeyAndDismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion
  666. {
  667. UIWindow *previousKeyWindow = self.previousKeyWindow;
  668. self.previousKeyWindow = nil;
  669. [previousKeyWindow makeKeyWindow];
  670. [[previousKeyWindow rootViewController] setNeedsStatusBarAppearanceUpdate];
  671. // Restore the status bar window's normal window level.
  672. // We want it above FLEX while a modal is presented for scroll to top, but below FLEX otherwise for exploration.
  673. [[self statusWindow] setWindowLevel:UIWindowLevelStatusBar];
  674. [self dismissViewControllerAnimated:animated completion:completion];
  675. }
  676. - (BOOL)wantsWindowToBecomeKey
  677. {
  678. return self.previousKeyWindow != nil;
  679. }
  680. - (void)toggleToolWithViewControllerProvider:(UIViewController *(^)(void))future completion:(void(^)(void))completion
  681. {
  682. if (self.presentedViewController) {
  683. [self resignKeyAndDismissViewControllerAnimated:YES completion:completion];
  684. } else if (future) {
  685. [self makeKeyAndPresentViewController:future() animated:YES completion:completion];
  686. }
  687. }
  688. #pragma mark - Keyboard Shortcut Helpers
  689. - (void)toggleSelectTool
  690. {
  691. if (self.currentMode == FLEXExplorerModeSelect) {
  692. self.currentMode = FLEXExplorerModeDefault;
  693. } else {
  694. self.currentMode = FLEXExplorerModeSelect;
  695. }
  696. }
  697. - (void)toggleMoveTool
  698. {
  699. if (self.currentMode == FLEXExplorerModeMove) {
  700. self.currentMode = FLEXExplorerModeDefault;
  701. } else {
  702. self.currentMode = FLEXExplorerModeMove;
  703. }
  704. }
  705. - (void)toggleViewsTool
  706. {
  707. [self toggleViewsToolWithCompletion:nil];
  708. }
  709. - (void)toggleViewsToolWithCompletion:(void(^)(void))completion
  710. {
  711. [self toggleToolWithViewControllerProvider:^UIViewController *{
  712. NSArray<UIView *> *allViews = [self allViewsInHierarchy];
  713. NSDictionary *depthsForViews = [self hierarchyDepthsForViews:allViews];
  714. FLEXHierarchyTableViewController *hierarchyTVC = [[FLEXHierarchyTableViewController alloc] initWithViews:allViews viewsAtTap:self.viewsAtTapPoint selectedView:self.selectedView depths:depthsForViews];
  715. hierarchyTVC.delegate = self;
  716. return [[UINavigationController alloc] initWithRootViewController:hierarchyTVC];
  717. } completion:^{
  718. if (completion) {
  719. completion();
  720. }
  721. }];
  722. }
  723. - (void)toggleMenuTool
  724. {
  725. [self toggleToolWithViewControllerProvider:^UIViewController *{
  726. FLEXGlobalsTableViewController *globalsViewController = [FLEXGlobalsTableViewController new];
  727. globalsViewController.delegate = self;
  728. [FLEXGlobalsTableViewController setApplicationWindow:[UIApplication.sharedApplication keyWindow]];
  729. return [[UINavigationController alloc] initWithRootViewController:globalsViewController];
  730. } completion:nil];
  731. }
  732. - (void)handleDownArrowKeyPressed
  733. {
  734. if (self.currentMode == FLEXExplorerModeMove) {
  735. CGRect frame = self.selectedView.frame;
  736. frame.origin.y += 1.0 / UIScreen.mainScreen.scale;
  737. self.selectedView.frame = frame;
  738. } else if (self.currentMode == FLEXExplorerModeSelect && self.viewsAtTapPoint.count > 0) {
  739. NSInteger selectedViewIndex = [self.viewsAtTapPoint indexOfObject:self.selectedView];
  740. if (selectedViewIndex > 0) {
  741. self.selectedView = [self.viewsAtTapPoint objectAtIndex:selectedViewIndex - 1];
  742. }
  743. }
  744. }
  745. - (void)handleUpArrowKeyPressed
  746. {
  747. if (self.currentMode == FLEXExplorerModeMove) {
  748. CGRect frame = self.selectedView.frame;
  749. frame.origin.y -= 1.0 / UIScreen.mainScreen.scale;
  750. self.selectedView.frame = frame;
  751. } else if (self.currentMode == FLEXExplorerModeSelect && self.viewsAtTapPoint.count > 0) {
  752. NSInteger selectedViewIndex = [self.viewsAtTapPoint indexOfObject:self.selectedView];
  753. if (selectedViewIndex < self.viewsAtTapPoint.count - 1) {
  754. self.selectedView = [self.viewsAtTapPoint objectAtIndex:selectedViewIndex + 1];
  755. }
  756. }
  757. }
  758. - (void)handleRightArrowKeyPressed
  759. {
  760. if (self.currentMode == FLEXExplorerModeMove) {
  761. CGRect frame = self.selectedView.frame;
  762. frame.origin.x += 1.0 / UIScreen.mainScreen.scale;
  763. self.selectedView.frame = frame;
  764. }
  765. }
  766. - (void)handleLeftArrowKeyPressed
  767. {
  768. if (self.currentMode == FLEXExplorerModeMove) {
  769. CGRect frame = self.selectedView.frame;
  770. frame.origin.x -= 1.0 / UIScreen.mainScreen.scale;
  771. self.selectedView.frame = frame;
  772. }
  773. }
  774. @end