FLEXManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //
  2. // FLEXManager.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/4/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXManager.h"
  9. #import "FLEXExplorerViewController.h"
  10. #import "FLEXWindow.h"
  11. #import "FLEXGlobalsTableViewControllerEntry.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. #import "FLEXObjectExplorerViewController.h"
  14. #import "FLEXNetworkObserver.h"
  15. #import "FLEXNetworkRecorder.h"
  16. #import "FLEXKeyboardShortcutManager.h"
  17. #import "FLEXFileBrowserTableViewController.h"
  18. #import "FLEXNetworkHistoryTableViewController.h"
  19. @interface FLEXManager () <FLEXWindowEventDelegate, FLEXExplorerViewControllerDelegate>
  20. @property (nonatomic, strong) FLEXWindow *explorerWindow;
  21. @property (nonatomic, strong) FLEXExplorerViewController *explorerViewController;
  22. @property (nonatomic, readonly, strong) NSMutableArray *userGlobalEntries;
  23. @end
  24. @implementation FLEXManager
  25. + (instancetype)sharedManager
  26. {
  27. static FLEXManager *sharedManager = nil;
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. sharedManager = [[[self class] alloc] init];
  31. });
  32. return sharedManager;
  33. }
  34. - (instancetype)init
  35. {
  36. self = [super init];
  37. if (self) {
  38. _userGlobalEntries = [[NSMutableArray alloc] init];
  39. }
  40. return self;
  41. }
  42. - (FLEXWindow *)explorerWindow
  43. {
  44. NSAssert([NSThread isMainThread], @"You must use %@ from the main thread only.", NSStringFromClass([self class]));
  45. if (!_explorerWindow) {
  46. _explorerWindow = [[FLEXWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  47. _explorerWindow.eventDelegate = self;
  48. _explorerWindow.rootViewController = self.explorerViewController;
  49. }
  50. return _explorerWindow;
  51. }
  52. - (FLEXExplorerViewController *)explorerViewController
  53. {
  54. if (!_explorerViewController) {
  55. _explorerViewController = [[FLEXExplorerViewController alloc] init];
  56. _explorerViewController.delegate = self;
  57. }
  58. return _explorerViewController;
  59. }
  60. - (void)showExplorer
  61. {
  62. self.explorerWindow.hidden = NO;
  63. }
  64. - (void)hideExplorer
  65. {
  66. self.explorerWindow.hidden = YES;
  67. }
  68. - (BOOL)isHidden
  69. {
  70. return self.explorerWindow.isHidden;
  71. }
  72. - (BOOL)isNetworkDebuggingEnabled
  73. {
  74. return [FLEXNetworkObserver isEnabled];
  75. }
  76. - (void)setNetworkDebuggingEnabled:(BOOL)networkDebuggingEnabled
  77. {
  78. [FLEXNetworkObserver setEnabled:networkDebuggingEnabled];
  79. }
  80. - (NSUInteger)networkResponseCacheByteLimit
  81. {
  82. return [[FLEXNetworkRecorder defaultRecorder] responseCacheByteLimit];
  83. }
  84. - (void)setNetworkResponseCacheByteLimit:(NSUInteger)networkResponseCacheByteLimit
  85. {
  86. [[FLEXNetworkRecorder defaultRecorder] setResponseCacheByteLimit:networkResponseCacheByteLimit];
  87. }
  88. #pragma mark - FLEXWindowEventDelegate
  89. - (BOOL)shouldHandleTouchAtPoint:(CGPoint)pointInWindow
  90. {
  91. // Ask the explorer view controller
  92. return [self.explorerViewController shouldReceiveTouchAtWindowPoint:pointInWindow];
  93. }
  94. - (BOOL)canBecomeKeyWindow
  95. {
  96. // Only when the explorer view controller wants it because it needs to accept key input & affect the status bar.
  97. return [self.explorerViewController wantsWindowToBecomeKey];
  98. }
  99. #pragma mark - FLEXExplorerViewControllerDelegate
  100. - (void)explorerViewControllerDidFinish:(FLEXExplorerViewController *)explorerViewController
  101. {
  102. [self hideExplorer];
  103. }
  104. #pragma mark - Simulator Shortcuts
  105. - (void)registerSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(dispatch_block_t)action description:(NSString *)description
  106. {
  107. # if TARGET_OS_SIMULATOR
  108. [[FLEXKeyboardShortcutManager sharedManager] registerSimulatorShortcutWithKey:key modifiers:modifiers action:action description:description];
  109. #endif
  110. }
  111. - (void)setSimulatorShortcutsEnabled:(BOOL)simulatorShortcutsEnabled
  112. {
  113. # if TARGET_OS_SIMULATOR
  114. [[FLEXKeyboardShortcutManager sharedManager] setEnabled:simulatorShortcutsEnabled];
  115. #endif
  116. }
  117. - (BOOL)simulatorShortcutsEnabled
  118. {
  119. # if TARGET_OS_SIMULATOR
  120. return [[FLEXKeyboardShortcutManager sharedManager] isEnabled];
  121. #else
  122. return NO;
  123. #endif
  124. }
  125. - (void)registerDefaultSimulatorShortcuts
  126. {
  127. [self registerSimulatorShortcutWithKey:@"f" modifiers:0 action:^{
  128. if ([self isHidden]) {
  129. [self showExplorer];
  130. } else {
  131. [self hideExplorer];
  132. }
  133. } description:@"Toggle FLEX toolbar"];
  134. [self registerSimulatorShortcutWithKey:@"g" modifiers:0 action:^{
  135. [self showExplorerIfNeeded];
  136. [self.explorerViewController toggleMenuTool];
  137. } description:@"Toggle FLEX globlas menu"];
  138. [self registerSimulatorShortcutWithKey:@"v" modifiers:0 action:^{
  139. [self showExplorerIfNeeded];
  140. [self.explorerViewController toggleViewsTool];
  141. } description:@"Toggle view hierarchy menu"];
  142. [self registerSimulatorShortcutWithKey:@"s" modifiers:0 action:^{
  143. [self showExplorerIfNeeded];
  144. [self.explorerViewController toggleSelectTool];
  145. } description:@"Toggle select tool"];
  146. [self registerSimulatorShortcutWithKey:@"m" modifiers:0 action:^{
  147. [self showExplorerIfNeeded];
  148. [self.explorerViewController toggleMoveTool];
  149. } description:@"Toggle move tool"];
  150. [self registerSimulatorShortcutWithKey:@"n" modifiers:0 action:^{
  151. [self toggleTopViewControllerOfClass:[FLEXNetworkHistoryTableViewController class]];
  152. } description:@"Toggle network history view"];
  153. [self registerSimulatorShortcutWithKey:UIKeyInputDownArrow modifiers:0 action:^{
  154. if ([self isHidden]) {
  155. [self tryScrollDown];
  156. } else {
  157. [self.explorerViewController handleDownArrowKeyPressed];
  158. }
  159. } description:@"Cycle view selection\n\t\tMove view down\n\t\tScroll down"];
  160. [self registerSimulatorShortcutWithKey:UIKeyInputUpArrow modifiers:0 action:^{
  161. if ([self isHidden]) {
  162. [self tryScrollUp];
  163. } else {
  164. [self.explorerViewController handleUpArrowKeyPressed];
  165. }
  166. } description:@"Cycle view selection\n\t\tMove view up\n\t\tScroll up"];
  167. [self registerSimulatorShortcutWithKey:UIKeyInputRightArrow modifiers:0 action:^{
  168. if (![self isHidden]) {
  169. [self.explorerViewController handleRightArrowKeyPressed];
  170. }
  171. } description:@"Move selected view right"];
  172. [self registerSimulatorShortcutWithKey:UIKeyInputLeftArrow modifiers:0 action:^{
  173. if ([self isHidden]) {
  174. [self tryGoBack];
  175. } else {
  176. [self.explorerViewController handleLeftArrowKeyPressed];
  177. }
  178. } description:@"Move selected view left"];
  179. [self registerSimulatorShortcutWithKey:UIKeyInputEscape modifiers:0 action:^{
  180. [[[self topViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
  181. } description:@"End editing text\n\t\tDismiss top view controller"];
  182. [self registerSimulatorShortcutWithKey:@"o" modifiers:UIKeyModifierCommand|UIKeyModifierShift action:^{
  183. [self toggleTopViewControllerOfClass:[FLEXFileBrowserTableViewController class]];
  184. } description:@"Toggle file browser menu"];
  185. }
  186. + (void)load
  187. {
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. [[[self class] sharedManager] registerDefaultSimulatorShortcuts];
  190. });
  191. }
  192. #pragma mark - Extensions
  193. - (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock
  194. {
  195. NSParameterAssert(entryName);
  196. NSParameterAssert(objectFutureBlock);
  197. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  198. entryName = entryName.copy;
  199. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  200. return entryName;
  201. } viewControllerFuture:^UIViewController *{
  202. return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
  203. }];
  204. [self.userGlobalEntries addObject:entry];
  205. }
  206. - (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock
  207. {
  208. NSParameterAssert(entryName);
  209. NSParameterAssert(viewControllerFutureBlock);
  210. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  211. entryName = entryName.copy;
  212. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  213. return entryName;
  214. } viewControllerFuture:^UIViewController *{
  215. UIViewController *viewController = viewControllerFutureBlock();
  216. NSCAssert(viewController, @"'%@' entry returned nil viewController. viewControllerFutureBlock should never return nil.", entryName);
  217. return viewController;
  218. }];
  219. [self.userGlobalEntries addObject:entry];
  220. }
  221. - (void)tryScrollDown
  222. {
  223. UIScrollView *firstScrollView = [self firstScrollView];
  224. CGPoint contentOffset = [firstScrollView contentOffset];
  225. CGFloat distance = floor(firstScrollView.frame.size.height / 2.0);
  226. CGFloat maxContentOffsetY = firstScrollView.contentSize.height + firstScrollView.contentInset.bottom - firstScrollView.frame.size.height;
  227. distance = MIN(maxContentOffsetY - firstScrollView.contentOffset.y, distance);
  228. contentOffset.y += distance;
  229. [firstScrollView setContentOffset:contentOffset animated:YES];
  230. }
  231. - (void)tryScrollUp
  232. {
  233. UIScrollView *firstScrollView = [self firstScrollView];
  234. CGPoint contentOffset = [firstScrollView contentOffset];
  235. CGFloat distance = floor(firstScrollView.frame.size.height / 2.0);
  236. CGFloat minContentOffsetY = -firstScrollView.contentInset.top;
  237. distance = MIN(firstScrollView.contentOffset.y - minContentOffsetY, distance);
  238. contentOffset.y -= distance;
  239. [firstScrollView setContentOffset:contentOffset animated:YES];
  240. }
  241. - (UIScrollView *)firstScrollView
  242. {
  243. NSMutableArray *views = [[[[UIApplication sharedApplication] keyWindow] subviews] mutableCopy];
  244. UIScrollView *scrollView = nil;
  245. while ([views count] > 0) {
  246. UIView *view = [views firstObject];
  247. [views removeObjectAtIndex:0];
  248. if ([view isKindOfClass:[UIScrollView class]]) {
  249. scrollView = (UIScrollView *)view;
  250. break;
  251. } else {
  252. [views addObjectsFromArray:[view subviews]];
  253. }
  254. }
  255. return scrollView;
  256. }
  257. - (void)tryGoBack
  258. {
  259. UINavigationController *navigationController = nil;
  260. UIViewController *topViewController = [self topViewController];
  261. if ([topViewController isKindOfClass:[UINavigationController class]]) {
  262. navigationController = (UINavigationController *)topViewController;
  263. } else {
  264. navigationController = topViewController.navigationController;
  265. }
  266. [navigationController popViewControllerAnimated:YES];
  267. }
  268. - (UIViewController *)topViewController
  269. {
  270. UIViewController *topViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  271. while ([topViewController presentedViewController]) {
  272. topViewController = [topViewController presentedViewController];
  273. }
  274. return topViewController;
  275. }
  276. - (void)toggleTopViewControllerOfClass:(Class)class
  277. {
  278. UIViewController *topViewController = [self topViewController];
  279. if ([topViewController isKindOfClass:[UINavigationController class]] && [[[(UINavigationController *)topViewController viewControllers] firstObject] isKindOfClass:[class class]]) {
  280. [[topViewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
  281. } else {
  282. id viewController = [[class alloc] init];
  283. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  284. [topViewController presentViewController:navigationController animated:YES completion:nil];
  285. }
  286. }
  287. - (void)showExplorerIfNeeded
  288. {
  289. if ([self isHidden]) {
  290. [self showExplorer];
  291. }
  292. }
  293. @end