FLEXManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. - (void)setNetworkRequestHostBlacklist:(NSArray<NSString *> *)networkRequestHostBlacklist {
  97. [FLEXNetworkRecorder defaultRecorder].hostBlacklist = networkRequestHostBlacklist;
  98. }
  99. - (NSArray<NSString *> *)hostBlacklist {
  100. return [FLEXNetworkRecorder defaultRecorder].hostBlacklist;
  101. }
  102. #pragma mark - FLEXWindowEventDelegate
  103. - (BOOL)shouldHandleTouchAtPoint:(CGPoint)pointInWindow
  104. {
  105. // Ask the explorer view controller
  106. return [self.explorerViewController shouldReceiveTouchAtWindowPoint:pointInWindow];
  107. }
  108. - (BOOL)canBecomeKeyWindow
  109. {
  110. // Only when the explorer view controller wants it because it needs to accept key input & affect the status bar.
  111. return [self.explorerViewController wantsWindowToBecomeKey];
  112. }
  113. #pragma mark - FLEXExplorerViewControllerDelegate
  114. - (void)explorerViewControllerDidFinish:(FLEXExplorerViewController *)explorerViewController
  115. {
  116. [self hideExplorer];
  117. }
  118. #pragma mark - Simulator Shortcuts
  119. - (void)registerSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(dispatch_block_t)action description:(NSString *)description
  120. {
  121. # if TARGET_OS_SIMULATOR
  122. [[FLEXKeyboardShortcutManager sharedManager] registerSimulatorShortcutWithKey:key modifiers:modifiers action:action description:description];
  123. #endif
  124. }
  125. - (void)setSimulatorShortcutsEnabled:(BOOL)simulatorShortcutsEnabled
  126. {
  127. # if TARGET_OS_SIMULATOR
  128. [[FLEXKeyboardShortcutManager sharedManager] setEnabled:simulatorShortcutsEnabled];
  129. #endif
  130. }
  131. - (BOOL)simulatorShortcutsEnabled
  132. {
  133. # if TARGET_OS_SIMULATOR
  134. return [[FLEXKeyboardShortcutManager sharedManager] isEnabled];
  135. #else
  136. return NO;
  137. #endif
  138. }
  139. - (void)registerDefaultSimulatorShortcuts
  140. {
  141. [self registerSimulatorShortcutWithKey:@"f" modifiers:0 action:^{
  142. [self toggleExplorer];
  143. } description:@"Toggle FLEX toolbar"];
  144. [self registerSimulatorShortcutWithKey:@"g" modifiers:0 action:^{
  145. [self showExplorerIfNeeded];
  146. [self.explorerViewController toggleMenuTool];
  147. } description:@"Toggle FLEX globals menu"];
  148. [self registerSimulatorShortcutWithKey:@"v" modifiers:0 action:^{
  149. [self showExplorerIfNeeded];
  150. [self.explorerViewController toggleViewsTool];
  151. } description:@"Toggle view hierarchy menu"];
  152. [self registerSimulatorShortcutWithKey:@"s" modifiers:0 action:^{
  153. [self showExplorerIfNeeded];
  154. [self.explorerViewController toggleSelectTool];
  155. } description:@"Toggle select tool"];
  156. [self registerSimulatorShortcutWithKey:@"m" modifiers:0 action:^{
  157. [self showExplorerIfNeeded];
  158. [self.explorerViewController toggleMoveTool];
  159. } description:@"Toggle move tool"];
  160. [self registerSimulatorShortcutWithKey:@"n" modifiers:0 action:^{
  161. [self toggleTopViewControllerOfClass:[FLEXNetworkHistoryTableViewController class]];
  162. } description:@"Toggle network history view"];
  163. [self registerSimulatorShortcutWithKey:UIKeyInputDownArrow modifiers:0 action:^{
  164. if ([self isHidden]) {
  165. [self tryScrollDown];
  166. } else {
  167. [self.explorerViewController handleDownArrowKeyPressed];
  168. }
  169. } description:@"Cycle view selection\n\t\tMove view down\n\t\tScroll down"];
  170. [self registerSimulatorShortcutWithKey:UIKeyInputUpArrow modifiers:0 action:^{
  171. if ([self isHidden]) {
  172. [self tryScrollUp];
  173. } else {
  174. [self.explorerViewController handleUpArrowKeyPressed];
  175. }
  176. } description:@"Cycle view selection\n\t\tMove view up\n\t\tScroll up"];
  177. [self registerSimulatorShortcutWithKey:UIKeyInputRightArrow modifiers:0 action:^{
  178. if (![self isHidden]) {
  179. [self.explorerViewController handleRightArrowKeyPressed];
  180. }
  181. } description:@"Move selected view right"];
  182. [self registerSimulatorShortcutWithKey:UIKeyInputLeftArrow modifiers:0 action:^{
  183. if ([self isHidden]) {
  184. [self tryGoBack];
  185. } else {
  186. [self.explorerViewController handleLeftArrowKeyPressed];
  187. }
  188. } description:@"Move selected view left"];
  189. [self registerSimulatorShortcutWithKey:@"?" modifiers:0 action:^{
  190. [self toggleTopViewControllerOfClass:[FLEXKeyboardHelpViewController class]];
  191. } description:@"Toggle (this) help menu"];
  192. [self registerSimulatorShortcutWithKey:UIKeyInputEscape modifiers:0 action:^{
  193. [[[self topViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
  194. } description:@"End editing text\n\t\tDismiss top view controller"];
  195. [self registerSimulatorShortcutWithKey:@"o" modifiers:UIKeyModifierCommand|UIKeyModifierShift action:^{
  196. [self toggleTopViewControllerOfClass:[FLEXFileBrowserTableViewController class]];
  197. } description:@"Toggle file browser menu"];
  198. }
  199. + (void)load
  200. {
  201. dispatch_async(dispatch_get_main_queue(), ^{
  202. [[[self class] sharedManager] registerDefaultSimulatorShortcuts];
  203. });
  204. }
  205. #pragma mark - Extensions
  206. - (void)registerGlobalEntryWithName:(NSString *)entryName objectFutureBlock:(id (^)(void))objectFutureBlock
  207. {
  208. NSParameterAssert(entryName);
  209. NSParameterAssert(objectFutureBlock);
  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. return [FLEXObjectExplorerFactory explorerViewControllerForObject:objectFutureBlock()];
  216. }];
  217. [self.userGlobalEntries addObject:entry];
  218. }
  219. - (void)registerGlobalEntryWithName:(NSString *)entryName viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock
  220. {
  221. NSParameterAssert(entryName);
  222. NSParameterAssert(viewControllerFutureBlock);
  223. NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
  224. entryName = entryName.copy;
  225. FLEXGlobalsTableViewControllerEntry *entry = [FLEXGlobalsTableViewControllerEntry entryWithNameFuture:^NSString *{
  226. return entryName;
  227. } viewControllerFuture:^UIViewController *{
  228. UIViewController *viewController = viewControllerFutureBlock();
  229. NSCAssert(viewController, @"'%@' entry returned nil viewController. viewControllerFutureBlock should never return nil.", entryName);
  230. return viewController;
  231. }];
  232. [self.userGlobalEntries addObject:entry];
  233. }
  234. - (void)tryScrollDown
  235. {
  236. UIScrollView *firstScrollView = [self firstScrollView];
  237. CGPoint contentOffset = [firstScrollView contentOffset];
  238. CGFloat distance = floor(firstScrollView.frame.size.height / 2.0);
  239. CGFloat maxContentOffsetY = firstScrollView.contentSize.height + firstScrollView.contentInset.bottom - firstScrollView.frame.size.height;
  240. distance = MIN(maxContentOffsetY - firstScrollView.contentOffset.y, distance);
  241. contentOffset.y += distance;
  242. [firstScrollView setContentOffset:contentOffset animated:YES];
  243. }
  244. - (void)tryScrollUp
  245. {
  246. UIScrollView *firstScrollView = [self firstScrollView];
  247. CGPoint contentOffset = [firstScrollView contentOffset];
  248. CGFloat distance = floor(firstScrollView.frame.size.height / 2.0);
  249. CGFloat minContentOffsetY = -firstScrollView.contentInset.top;
  250. distance = MIN(firstScrollView.contentOffset.y - minContentOffsetY, distance);
  251. contentOffset.y -= distance;
  252. [firstScrollView setContentOffset:contentOffset animated:YES];
  253. }
  254. - (UIScrollView *)firstScrollView
  255. {
  256. NSMutableArray *views = [[[[UIApplication sharedApplication] keyWindow] subviews] mutableCopy];
  257. UIScrollView *scrollView = nil;
  258. while ([views count] > 0) {
  259. UIView *view = [views firstObject];
  260. [views removeObjectAtIndex:0];
  261. if ([view isKindOfClass:[UIScrollView class]]) {
  262. scrollView = (UIScrollView *)view;
  263. break;
  264. } else {
  265. [views addObjectsFromArray:[view subviews]];
  266. }
  267. }
  268. return scrollView;
  269. }
  270. - (void)tryGoBack
  271. {
  272. UINavigationController *navigationController = nil;
  273. UIViewController *topViewController = [self topViewController];
  274. if ([topViewController isKindOfClass:[UINavigationController class]]) {
  275. navigationController = (UINavigationController *)topViewController;
  276. } else {
  277. navigationController = topViewController.navigationController;
  278. }
  279. [navigationController popViewControllerAnimated:YES];
  280. }
  281. - (UIViewController *)topViewController
  282. {
  283. UIViewController *topViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  284. while ([topViewController presentedViewController]) {
  285. topViewController = [topViewController presentedViewController];
  286. }
  287. return topViewController;
  288. }
  289. - (void)toggleTopViewControllerOfClass:(Class)class
  290. {
  291. UIViewController *topViewController = [self topViewController];
  292. if ([topViewController isKindOfClass:[UINavigationController class]] && [[[(UINavigationController *)topViewController viewControllers] firstObject] isKindOfClass:[class class]]) {
  293. [[topViewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
  294. } else {
  295. id viewController = [[class alloc] init];
  296. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  297. [topViewController presentViewController:navigationController animated:YES completion:nil];
  298. }
  299. }
  300. - (void)showExplorerIfNeeded
  301. {
  302. if ([self isHidden]) {
  303. [self showExplorer];
  304. }
  305. }
  306. @end