FLEXManager.m 14 KB

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