FLEXExplorerViewController.m 35 KB

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