FLEXExplorerViewController.m 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 = [self 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. - (NSArray *)allWindows
  323. {
  324. BOOL includeInternalWindows = YES;
  325. BOOL onlyVisibleWindows = NO;
  326. NSArray *allWindowsComponents = @[@"al", @"lWindo", @"wsIncl", @"udingInt", @"ernalWin", @"dows:o", @"nlyVisi", @"bleWin", @"dows:"];
  327. SEL allWindowsSelector = NSSelectorFromString([allWindowsComponents componentsJoinedByString:@""]);
  328. NSMethodSignature *methodSignature = [[UIWindow class] methodSignatureForSelector:allWindowsSelector];
  329. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  330. invocation.target = [UIWindow class];
  331. invocation.selector = allWindowsSelector;
  332. [invocation setArgument:&includeInternalWindows atIndex:2];
  333. [invocation setArgument:&onlyVisibleWindows atIndex:3];
  334. [invocation invoke];
  335. __unsafe_unretained NSArray *windows = nil;
  336. [invocation getReturnValue:&windows];
  337. return windows;
  338. }
  339. - (UIWindow *)statusWindow
  340. {
  341. NSString *statusBarString = [NSString stringWithFormat:@"%@arWindow", @"_statusB"];
  342. return [[UIApplication sharedApplication] valueForKey:statusBarString];
  343. }
  344. - (void)moveButtonTapped:(FLEXToolbarItem *)sender
  345. {
  346. [self toggleMoveTool];
  347. }
  348. - (void)globalsButtonTapped:(FLEXToolbarItem *)sender
  349. {
  350. [self toggleMenuTool];
  351. }
  352. - (void)closeButtonTapped:(FLEXToolbarItem *)sender
  353. {
  354. self.currentMode = FLEXExplorerModeDefault;
  355. [self.delegate explorerViewControllerDidFinish:self];
  356. }
  357. - (void)updateButtonStates
  358. {
  359. // Move and details only active when an object is selected.
  360. BOOL hasSelectedObject = self.selectedView != nil;
  361. self.explorerToolbar.moveItem.enabled = hasSelectedObject;
  362. self.explorerToolbar.selectItem.selected = self.currentMode == FLEXExplorerModeSelect;
  363. self.explorerToolbar.moveItem.selected = self.currentMode == FLEXExplorerModeMove;
  364. }
  365. #pragma mark - Toolbar Dragging
  366. - (void)setupToolbarGestures
  367. {
  368. // Pan gesture for dragging.
  369. UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleToolbarPanGesture:)];
  370. [self.explorerToolbar.dragHandle addGestureRecognizer:panGR];
  371. // Tap gesture for hinting.
  372. UITapGestureRecognizer *hintTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToolbarHintTapGesture:)];
  373. [self.explorerToolbar.dragHandle addGestureRecognizer:hintTapGR];
  374. // Tap gesture for showing additional details
  375. self.detailsTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleToolbarDetailsTapGesture:)];
  376. [self.explorerToolbar.selectedViewDescriptionContainer addGestureRecognizer:self.detailsTapGR];
  377. }
  378. - (void)handleToolbarPanGesture:(UIPanGestureRecognizer *)panGR
  379. {
  380. switch (panGR.state) {
  381. case UIGestureRecognizerStateBegan:
  382. self.toolbarFrameBeforeDragging = self.explorerToolbar.frame;
  383. [self updateToolbarPostionWithDragGesture:panGR];
  384. break;
  385. case UIGestureRecognizerStateChanged:
  386. case UIGestureRecognizerStateEnded:
  387. [self updateToolbarPostionWithDragGesture:panGR];
  388. break;
  389. default:
  390. break;
  391. }
  392. }
  393. - (void)updateToolbarPostionWithDragGesture:(UIPanGestureRecognizer *)panGR
  394. {
  395. CGPoint translation = [panGR translationInView:self.view];
  396. CGRect newToolbarFrame = self.toolbarFrameBeforeDragging;
  397. newToolbarFrame.origin.y += translation.y;
  398. CGFloat maxY = CGRectGetMaxY(self.view.bounds) - newToolbarFrame.size.height;
  399. if (newToolbarFrame.origin.y < 0.0) {
  400. newToolbarFrame.origin.y = 0.0;
  401. } else if (newToolbarFrame.origin.y > maxY) {
  402. newToolbarFrame.origin.y = maxY;
  403. }
  404. self.explorerToolbar.frame = newToolbarFrame;
  405. }
  406. - (void)handleToolbarHintTapGesture:(UITapGestureRecognizer *)tapGR
  407. {
  408. // Bounce the toolbar to indicate that it is draggable.
  409. // TODO: make it bouncier.
  410. if (tapGR.state == UIGestureRecognizerStateRecognized) {
  411. CGRect originalToolbarFrame = self.explorerToolbar.frame;
  412. const NSTimeInterval kHalfwayDuration = 0.2;
  413. const CGFloat kVerticalOffset = 30.0;
  414. [UIView animateWithDuration:kHalfwayDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  415. CGRect newToolbarFrame = self.explorerToolbar.frame;
  416. newToolbarFrame.origin.y += kVerticalOffset;
  417. self.explorerToolbar.frame = newToolbarFrame;
  418. } completion:^(BOOL finished) {
  419. [UIView animateWithDuration:kHalfwayDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  420. self.explorerToolbar.frame = originalToolbarFrame;
  421. } completion:nil];
  422. }];
  423. }
  424. }
  425. - (void)handleToolbarDetailsTapGesture:(UITapGestureRecognizer *)tapGR
  426. {
  427. if (tapGR.state == UIGestureRecognizerStateRecognized && self.selectedView) {
  428. FLEXObjectExplorerViewController *selectedViewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:self.selectedView];
  429. selectedViewExplorer.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(selectedViewExplorerFinished:)];
  430. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:selectedViewExplorer];
  431. [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
  432. }
  433. }
  434. #pragma mark - View Selection
  435. - (void)handleSelectionTap:(UITapGestureRecognizer *)tapGR
  436. {
  437. // Only if we're in selection mode
  438. if (self.currentMode == FLEXExplorerModeSelect && tapGR.state == UIGestureRecognizerStateRecognized) {
  439. // Note that [tapGR locationInView:nil] is broken in iOS 8, so we have to do a two step conversion to window coordinates.
  440. // Thanks to @lascorbe for finding this: https://github.com/Flipboard/FLEX/pull/31
  441. CGPoint tapPointInView = [tapGR locationInView:self.view];
  442. CGPoint tapPointInWindow = [self.view convertPoint:tapPointInView toView:nil];
  443. [self updateOutlineViewsForSelectionPoint:tapPointInWindow];
  444. }
  445. }
  446. - (void)updateOutlineViewsForSelectionPoint:(CGPoint)selectionPointInWindow
  447. {
  448. [self removeAndClearOutlineViews];
  449. // Include hidden views in the "viewsAtTapPoint" array so we can show them in the hierarchy list.
  450. self.viewsAtTapPoint = [self viewsAtPoint:selectionPointInWindow skipHiddenViews:NO];
  451. // For outlined views and the selected view, only use visible views.
  452. // Outlining hidden views adds clutter and makes the selection behavior confusing.
  453. NSArray *visibleViewsAtTapPoint = [self viewsAtPoint:selectionPointInWindow skipHiddenViews:YES];
  454. NSMutableDictionary *newOutlineViewsForVisibleViews = [NSMutableDictionary dictionary];
  455. for (UIView *view in visibleViewsAtTapPoint) {
  456. UIView *outlineView = [self outlineViewForView:view];
  457. [self.view addSubview:outlineView];
  458. NSValue *key = [NSValue valueWithNonretainedObject:view];
  459. [newOutlineViewsForVisibleViews setObject:outlineView forKey:key];
  460. }
  461. self.outlineViewsForVisibleViews = newOutlineViewsForVisibleViews;
  462. self.selectedView = [self viewForSelectionAtPoint:selectionPointInWindow];
  463. // Make sure the explorer toolbar doesn't end up behind the newly added outline views.
  464. [self.view bringSubviewToFront:self.explorerToolbar];
  465. [self updateButtonStates];
  466. }
  467. - (UIView *)outlineViewForView:(UIView *)view
  468. {
  469. CGRect outlineFrame = [self frameInLocalCoordinatesForView:view];
  470. UIView *outlineView = [[UIView alloc] initWithFrame:outlineFrame];
  471. outlineView.backgroundColor = [UIColor clearColor];
  472. outlineView.layer.borderColor = [[FLEXUtility consistentRandomColorForObject:view] CGColor];
  473. outlineView.layer.borderWidth = 1.0;
  474. return outlineView;
  475. }
  476. - (void)removeAndClearOutlineViews
  477. {
  478. for (id key in self.outlineViewsForVisibleViews) {
  479. UIView *outlineView = self.outlineViewsForVisibleViews[key];
  480. [outlineView removeFromSuperview];
  481. }
  482. self.outlineViewsForVisibleViews = nil;
  483. }
  484. - (NSArray *)viewsAtPoint:(CGPoint)tapPointInWindow skipHiddenViews:(BOOL)skipHidden
  485. {
  486. NSMutableArray *views = [NSMutableArray array];
  487. for (UIWindow *window in [self allWindows]) {
  488. // Don't include the explorer's own window or subviews.
  489. if (window != self.view.window && [window pointInside:tapPointInWindow withEvent:nil]) {
  490. [views addObject:window];
  491. [views addObjectsFromArray:[self recursiveSubviewsAtPoint:tapPointInWindow inView:window skipHiddenViews:skipHidden]];
  492. }
  493. }
  494. return views;
  495. }
  496. - (UIView *)viewForSelectionAtPoint:(CGPoint)tapPointInWindow
  497. {
  498. // 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.
  499. // Default to the the application's key window if none of the windows want the touch.
  500. UIWindow *windowForSelection = [[UIApplication sharedApplication] keyWindow];
  501. for (UIWindow *window in [[self allWindows] reverseObjectEnumerator]) {
  502. // Ignore the explorer's own window.
  503. if (window != self.view.window) {
  504. if ([window hitTest:tapPointInWindow withEvent:nil]) {
  505. windowForSelection = window;
  506. break;
  507. }
  508. }
  509. }
  510. // Select the deepest visible view at the tap point. This generally corresponds to what the user wants to select.
  511. return [[self recursiveSubviewsAtPoint:tapPointInWindow inView:windowForSelection skipHiddenViews:YES] lastObject];
  512. }
  513. - (NSArray *)recursiveSubviewsAtPoint:(CGPoint)pointInView inView:(UIView *)view skipHiddenViews:(BOOL)skipHidden
  514. {
  515. NSMutableArray *subviewsAtPoint = [NSMutableArray array];
  516. for (UIView *subview in view.subviews) {
  517. BOOL isHidden = subview.hidden || subview.alpha < 0.01;
  518. if (skipHidden && isHidden) {
  519. continue;
  520. }
  521. BOOL subviewContainsPoint = CGRectContainsPoint(subview.frame, pointInView);
  522. if (subviewContainsPoint) {
  523. [subviewsAtPoint addObject:subview];
  524. }
  525. // If this view doesn't clip to its bounds, we need to check its subviews even if it doesn't contain the selection point.
  526. // They may be visible and contain the selection point.
  527. if (subviewContainsPoint || !subview.clipsToBounds) {
  528. CGPoint pointInSubview = [view convertPoint:pointInView toView:subview];
  529. [subviewsAtPoint addObjectsFromArray:[self recursiveSubviewsAtPoint:pointInSubview inView:subview skipHiddenViews:skipHidden]];
  530. }
  531. }
  532. return subviewsAtPoint;
  533. }
  534. - (NSArray *)allRecursiveSubviewsInView:(UIView *)view
  535. {
  536. NSMutableArray *subviews = [NSMutableArray array];
  537. for (UIView *subview in view.subviews) {
  538. [subviews addObject:subview];
  539. [subviews addObjectsFromArray:[self allRecursiveSubviewsInView:subview]];
  540. }
  541. return subviews;
  542. }
  543. - (NSDictionary *)hierarchyDepthsForViews:(NSArray *)views
  544. {
  545. NSMutableDictionary *hierarchyDepths = [NSMutableDictionary dictionary];
  546. for (UIView *view in views) {
  547. NSInteger depth = 0;
  548. UIView *tryView = view;
  549. while (tryView.superview) {
  550. tryView = tryView.superview;
  551. depth++;
  552. }
  553. [hierarchyDepths setObject:@(depth) forKey:[NSValue valueWithNonretainedObject:view]];
  554. }
  555. return hierarchyDepths;
  556. }
  557. #pragma mark - Selected View Moving
  558. - (void)handleMovePan:(UIPanGestureRecognizer *)movePanGR
  559. {
  560. switch (movePanGR.state) {
  561. case UIGestureRecognizerStateBegan:
  562. self.selectedViewFrameBeforeDragging = self.selectedView.frame;
  563. [self updateSelectedViewPositionWithDragGesture:movePanGR];
  564. break;
  565. case UIGestureRecognizerStateChanged:
  566. case UIGestureRecognizerStateEnded:
  567. [self updateSelectedViewPositionWithDragGesture:movePanGR];
  568. break;
  569. default:
  570. break;
  571. }
  572. }
  573. - (void)updateSelectedViewPositionWithDragGesture:(UIPanGestureRecognizer *)movePanGR
  574. {
  575. CGPoint translation = [movePanGR translationInView:self.selectedView.superview];
  576. CGRect newSelectedViewFrame = self.selectedViewFrameBeforeDragging;
  577. newSelectedViewFrame.origin.x = FLEXFloor(newSelectedViewFrame.origin.x + translation.x);
  578. newSelectedViewFrame.origin.y = FLEXFloor(newSelectedViewFrame.origin.y + translation.y);
  579. self.selectedView.frame = newSelectedViewFrame;
  580. }
  581. #pragma mark - Touch Handling
  582. - (BOOL)shouldReceiveTouchAtWindowPoint:(CGPoint)pointInWindowCoordinates
  583. {
  584. BOOL shouldReceiveTouch = NO;
  585. CGPoint pointInLocalCoordinates = [self.view convertPoint:pointInWindowCoordinates fromView:nil];
  586. // Always if it's on the toolbar
  587. if (CGRectContainsPoint(self.explorerToolbar.frame, pointInLocalCoordinates)) {
  588. shouldReceiveTouch = YES;
  589. }
  590. // Always if we're in selection mode
  591. if (!shouldReceiveTouch && self.currentMode == FLEXExplorerModeSelect) {
  592. shouldReceiveTouch = YES;
  593. }
  594. // Always in move mode too
  595. if (!shouldReceiveTouch && self.currentMode == FLEXExplorerModeMove) {
  596. shouldReceiveTouch = YES;
  597. }
  598. // Always if we have a modal presented
  599. if (!shouldReceiveTouch && self.presentedViewController) {
  600. shouldReceiveTouch = YES;
  601. }
  602. return shouldReceiveTouch;
  603. }
  604. #pragma mark - FLEXHierarchyTableViewControllerDelegate
  605. - (void)hierarchyViewController:(FLEXHierarchyTableViewController *)hierarchyViewController didFinishWithSelectedView:(UIView *)selectedView
  606. {
  607. // Note that we need to wait until the view controller is dismissed to calculated the frame of the outline view.
  608. // Otherwise the coordinate conversion doesn't give the correct result.
  609. [self resignKeyAndDismissViewControllerAnimated:YES completion:^{
  610. // If the selected view is outside of the tap point array (selected from "Full Hierarchy"),
  611. // then clear out the tap point array and remove all the outline views.
  612. if (![self.viewsAtTapPoint containsObject:selectedView]) {
  613. self.viewsAtTapPoint = nil;
  614. [self removeAndClearOutlineViews];
  615. }
  616. // If we now have a selected view and we didn't have one previously, go to "select" mode.
  617. if (self.currentMode == FLEXExplorerModeDefault && selectedView) {
  618. self.currentMode = FLEXExplorerModeSelect;
  619. }
  620. // The selected view setter will also update the selected view overlay appropriately.
  621. self.selectedView = selectedView;
  622. }];
  623. }
  624. #pragma mark - FLEXGlobalsViewControllerDelegate
  625. - (void)globalsViewControllerDidFinish:(FLEXGlobalsTableViewController *)globalsViewController
  626. {
  627. [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
  628. }
  629. #pragma mark - FLEXObjectExplorerViewController Done Action
  630. - (void)selectedViewExplorerFinished:(id)sender
  631. {
  632. [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
  633. }
  634. #pragma mark - Modal Presentation and Window Management
  635. - (void)makeKeyAndPresentViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion
  636. {
  637. // Save the current key window so we can restore it following dismissal.
  638. self.previousKeyWindow = [[UIApplication sharedApplication] keyWindow];
  639. // Make our window key to correctly handle input.
  640. [self.view.window makeKeyWindow];
  641. // Move the status bar on top of FLEX so we can get scroll to top behavior for taps.
  642. [[self statusWindow] setWindowLevel:self.view.window.windowLevel + 1.0];
  643. // If this app doesn't use view controller based status bar management and we're on iOS 7+,
  644. // make sure the status bar style is UIStatusBarStyleDefault. We don't actully have to check
  645. // for view controller based management because the global methods no-op if that is turned on.
  646. self.previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
  647. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  648. // Show the view controller.
  649. [self presentViewController:viewController animated:animated completion:completion];
  650. }
  651. - (void)resignKeyAndDismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion
  652. {
  653. UIWindow *previousKeyWindow = self.previousKeyWindow;
  654. self.previousKeyWindow = nil;
  655. [previousKeyWindow makeKeyWindow];
  656. [[previousKeyWindow rootViewController] setNeedsStatusBarAppearanceUpdate];
  657. // Restore the status bar window's normal window level.
  658. // We want it above FLEX while a modal is presented for scroll to top, but below FLEX otherwise for exploration.
  659. [[self statusWindow] setWindowLevel:UIWindowLevelStatusBar];
  660. // Restore the stauts bar style if the app is using global status bar management.
  661. [[UIApplication sharedApplication] setStatusBarStyle:self.previousStatusBarStyle];
  662. [self dismissViewControllerAnimated:animated completion:completion];
  663. }
  664. - (BOOL)wantsWindowToBecomeKey
  665. {
  666. return self.previousKeyWindow != nil;
  667. }
  668. #pragma mark - Keyboard Shortcut Helpers
  669. - (void)toggleSelectTool
  670. {
  671. if (self.currentMode == FLEXExplorerModeSelect) {
  672. self.currentMode = FLEXExplorerModeDefault;
  673. } else {
  674. self.currentMode = FLEXExplorerModeSelect;
  675. }
  676. }
  677. - (void)toggleMoveTool
  678. {
  679. if (self.currentMode == FLEXExplorerModeMove) {
  680. self.currentMode = FLEXExplorerModeDefault;
  681. } else {
  682. self.currentMode = FLEXExplorerModeMove;
  683. }
  684. }
  685. - (void)toggleViewsTool
  686. {
  687. BOOL viewsModalShown = [[self presentedViewController] isKindOfClass:[UINavigationController class]];
  688. viewsModalShown = viewsModalShown && [[[(UINavigationController *)[self presentedViewController] viewControllers] firstObject] isKindOfClass:[FLEXHierarchyTableViewController class]];
  689. if (viewsModalShown) {
  690. [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
  691. } else {
  692. void (^presentBlock)() = ^{
  693. NSArray *allViews = [self allViewsInHierarchy];
  694. NSDictionary *depthsForViews = [self hierarchyDepthsForViews:allViews];
  695. FLEXHierarchyTableViewController *hierarchyTVC = [[FLEXHierarchyTableViewController alloc] initWithViews:allViews viewsAtTap:self.viewsAtTapPoint selectedView:self.selectedView depths:depthsForViews];
  696. hierarchyTVC.delegate = self;
  697. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:hierarchyTVC];
  698. [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
  699. };
  700. if (self.presentedViewController) {
  701. [self resignKeyAndDismissViewControllerAnimated:NO completion:presentBlock];
  702. } else {
  703. presentBlock();
  704. }
  705. }
  706. }
  707. - (void)toggleMenuTool
  708. {
  709. BOOL menuModalShown = [[self presentedViewController] isKindOfClass:[UINavigationController class]];
  710. menuModalShown = menuModalShown && [[[(UINavigationController *)[self presentedViewController] viewControllers] firstObject] isKindOfClass:[FLEXGlobalsTableViewController class]];
  711. if (menuModalShown) {
  712. [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
  713. } else {
  714. void (^presentBlock)() = ^{
  715. FLEXGlobalsTableViewController *globalsViewController = [[FLEXGlobalsTableViewController alloc] init];
  716. globalsViewController.delegate = self;
  717. [FLEXGlobalsTableViewController setApplicationWindow:[[UIApplication sharedApplication] keyWindow]];
  718. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:globalsViewController];
  719. [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
  720. };
  721. if (self.presentedViewController) {
  722. [self resignKeyAndDismissViewControllerAnimated:NO completion:presentBlock];
  723. } else {
  724. presentBlock();
  725. }
  726. }
  727. }
  728. - (void)handleDownArrowKeyPressed
  729. {
  730. if (self.currentMode == FLEXExplorerModeMove) {
  731. CGRect frame = self.selectedView.frame;
  732. frame.origin.y += 1.0 / [[UIScreen mainScreen] scale];
  733. self.selectedView.frame = frame;
  734. } else if (self.currentMode == FLEXExplorerModeSelect && [self.viewsAtTapPoint count] > 0) {
  735. NSInteger selectedViewIndex = [self.viewsAtTapPoint indexOfObject:self.selectedView];
  736. if (selectedViewIndex > 0) {
  737. self.selectedView = [self.viewsAtTapPoint objectAtIndex:selectedViewIndex - 1];
  738. }
  739. }
  740. }
  741. - (void)handleUpArrowKeyPressed
  742. {
  743. if (self.currentMode == FLEXExplorerModeMove) {
  744. CGRect frame = self.selectedView.frame;
  745. frame.origin.y -= 1.0 / [[UIScreen mainScreen] scale];
  746. self.selectedView.frame = frame;
  747. } else if (self.currentMode == FLEXExplorerModeSelect && [self.viewsAtTapPoint count] > 0) {
  748. NSInteger selectedViewIndex = [self.viewsAtTapPoint indexOfObject:self.selectedView];
  749. if (selectedViewIndex < [self.viewsAtTapPoint count] - 1) {
  750. self.selectedView = [self.viewsAtTapPoint objectAtIndex:selectedViewIndex + 1];
  751. }
  752. }
  753. }
  754. - (void)handleRightArrowKeyPressed
  755. {
  756. if (self.currentMode == FLEXExplorerModeMove) {
  757. CGRect frame = self.selectedView.frame;
  758. frame.origin.x += 1.0 / [[UIScreen mainScreen] scale];
  759. self.selectedView.frame = frame;
  760. }
  761. }
  762. - (void)handleLeftArrowKeyPressed
  763. {
  764. if (self.currentMode == FLEXExplorerModeMove) {
  765. CGRect frame = self.selectedView.frame;
  766. frame.origin.x -= 1.0 / [[UIScreen mainScreen] scale];
  767. self.selectedView.frame = frame;
  768. }
  769. }
  770. @end