FLEXManager.m 12 KB

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