FLEXExplorerViewController.m 38 KB

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