FLEXWindowManagerController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // FLEXWindowManagerController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 2/6/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXWindowManagerController.h"
  9. #import "FLEXManager+Private.h"
  10. #import "FLEXUtility.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. @interface FLEXWindowManagerController ()
  13. @property (nonatomic) UIWindow *keyWindow;
  14. @property (nonatomic, copy) NSString *keyWindowSubtitle;
  15. @property (nonatomic, copy) NSArray<UIWindow *> *windows;
  16. @property (nonatomic, copy) NSArray<NSString *> *windowSubtitles;
  17. @property (nonatomic, copy) NSArray<UIScene *> *scenes API_AVAILABLE(ios(13));
  18. @property (nonatomic, copy) NSArray<NSString *> *sceneSubtitles;
  19. @property (nonatomic, copy) NSArray<NSArray *> *sections;
  20. @end
  21. @implementation FLEXWindowManagerController
  22. #pragma mark - Initialization
  23. - (id)init {
  24. return [self initWithStyle:UITableViewStylePlain];
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.title = @"Windows";
  29. if (@available(iOS 13, *)) {
  30. self.title = @"Windows and Scenes";
  31. }
  32. [self disableToolbar];
  33. [self reloadData];
  34. }
  35. #pragma mark - Private
  36. - (void)reloadData {
  37. self.keyWindow = UIApplication.sharedApplication.keyWindow;
  38. self.windows = UIApplication.sharedApplication.windows;
  39. self.keyWindowSubtitle = self.windowSubtitles[[self.windows indexOfObject:self.keyWindow]];
  40. self.windowSubtitles = [self.windows flex_mapped:^id(UIWindow *window, NSUInteger idx) {
  41. return [NSString stringWithFormat:@"Level: %@ — Root: %@",
  42. @(window.windowLevel), window.rootViewController
  43. ];
  44. }];
  45. if (@available(iOS 13, *)) {
  46. self.scenes = UIApplication.sharedApplication.connectedScenes.allObjects;
  47. self.sceneSubtitles = [self.scenes flex_mapped:^id(UIScene *scene, NSUInteger idx) {
  48. return [self sceneDescription:scene];
  49. }];
  50. self.sections = @[@[self.keyWindow], self.windows, self.scenes];
  51. } else {
  52. self.sections = @[@[self.keyWindow], self.windows];
  53. }
  54. }
  55. - (void)dismissAnimated {
  56. [self dismissViewControllerAnimated:YES completion:nil];
  57. }
  58. - (void)showRevertOrDismissAlert:(void(^)())revertBlock {
  59. [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
  60. [self reloadData];
  61. [self.tableView reloadData];
  62. UIWindow *highestWindow = UIApplication.sharedApplication.keyWindow;
  63. UIWindowLevel maxLevel = 0;
  64. for (UIWindow *window in UIApplication.sharedApplication.windows) {
  65. if (window.windowLevel > maxLevel) {
  66. maxLevel = window.windowLevel;
  67. highestWindow = window;
  68. }
  69. }
  70. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  71. make.title(@"Keep Changes?");
  72. make.message(@"If you do not wish to keep these settings, choose 'Revert Changes' below.");
  73. make.button(@"Keep Changes").destructiveStyle();
  74. make.button(@"Keep Changes and Dismiss").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  75. [self dismissAnimated];
  76. });
  77. make.button(@"Revert Changes").cancelStyle().handler(^(NSArray<NSString *> *strings) {
  78. revertBlock();
  79. [self reloadData];
  80. [self.tableView reloadData];
  81. });
  82. } showFrom:[FLEXUtility topViewControllerInWindow:highestWindow]];
  83. }
  84. - (NSString *)sceneDescription:(UIScene *)scene API_AVAILABLE(ios(13)) {
  85. NSString *state = [self stringFromSceneState:scene.activationState];
  86. NSString *title = scene.title.length ? scene.title : nil;
  87. NSString *suffix = nil;
  88. if ([scene isKindOfClass:[UIWindowScene class]]) {
  89. UIWindowScene *windowScene = (id)scene;
  90. suffix = FLEXPluralString(windowScene.windows.count, @"windows", @"window");
  91. }
  92. NSMutableString *description = state.mutableCopy;
  93. if (title) {
  94. [description appendFormat:@" — %@", title];
  95. }
  96. if (suffix) {
  97. [description appendFormat:@" — %@", suffix];
  98. }
  99. return description.copy;
  100. }
  101. - (NSString *)stringFromSceneState:(UISceneActivationState)state API_AVAILABLE(ios(13)) {
  102. switch (state) {
  103. case UISceneActivationStateUnattached:
  104. return @"Unattached";
  105. case UISceneActivationStateForegroundActive:
  106. return @"Active";
  107. case UISceneActivationStateForegroundInactive:
  108. return @"Inactive";
  109. case UISceneActivationStateBackground:
  110. return @"Backgrounded";
  111. }
  112. return [NSString stringWithFormat:@"Unknown state: %@", @(state)];
  113. }
  114. #pragma mark - Table View Data Source
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  116. return self.sections.count;
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  119. return self.sections[section].count;
  120. }
  121. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  122. switch (section) {
  123. case 0: return @"Key Window";
  124. case 1: return @"Windows";
  125. case 2: return @"Connected Scenes";
  126. }
  127. return nil;
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  130. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXDetailCell forIndexPath:indexPath];
  131. cell.accessoryType = UITableViewCellAccessoryDetailButton;
  132. cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  133. UIWindow *window = nil;
  134. NSString *subtitle = nil;
  135. switch (indexPath.section) {
  136. case 0:
  137. window = self.keyWindow;
  138. subtitle = self.keyWindowSubtitle;
  139. break;
  140. case 1:
  141. window = self.windows[indexPath.row];
  142. subtitle = self.windowSubtitles[indexPath.row];
  143. break;
  144. case 2:
  145. if (@available(iOS 13, *)) {
  146. UIScene *scene = self.scenes[indexPath.row];
  147. cell.textLabel.text = scene.description;
  148. cell.detailTextLabel.text = self.sceneSubtitles[indexPath.row];
  149. return cell;
  150. }
  151. }
  152. cell.textLabel.text = window.description;
  153. cell.detailTextLabel.text = [NSString
  154. stringWithFormat:@"Level: %@ — Root: %@",
  155. @((NSInteger)window.windowLevel), window.rootViewController.class
  156. ];
  157. return cell;
  158. }
  159. #pragma mark - Table View Delegate
  160. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  161. UIWindow *window = nil;
  162. NSString *subtitle = nil;
  163. FLEXWindow *flex = FLEXManager.sharedManager.explorerWindow;
  164. id cancelHandler = ^{
  165. [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
  166. };
  167. switch (indexPath.section) {
  168. case 0:
  169. window = self.keyWindow;
  170. subtitle = self.keyWindowSubtitle;
  171. break;
  172. case 1:
  173. window = self.windows[indexPath.row];
  174. subtitle = self.windowSubtitles[indexPath.row];
  175. break;
  176. case 2:
  177. if (@available(iOS 13, *)) {
  178. UIScene *scene = self.scenes[indexPath.row];
  179. UIWindowScene *oldScene = flex.windowScene;
  180. BOOL isWindowScene = [scene isKindOfClass:[UIWindowScene class]];
  181. BOOL isFLEXScene = isWindowScene ? flex.windowScene == scene : NO;
  182. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  183. make.title(NSStringFromClass(scene.class));
  184. if (isWindowScene) {
  185. if (isFLEXScene) {
  186. make.message(@"Already the FLEX window scene");
  187. }
  188. make.button(@"Set as FLEX Window Scene")
  189. .handler(^(NSArray<NSString *> *strings) {
  190. flex.windowScene = (id)scene;
  191. [self showRevertOrDismissAlert:^{
  192. flex.windowScene = oldScene;
  193. }];
  194. }).enabled(!isFLEXScene);
  195. make.button(@"Cancel").cancelStyle();
  196. } else {
  197. make.message(@"Not a UIWindowScene");
  198. make.button(@"Dismiss").cancelStyle().handler(cancelHandler);
  199. }
  200. } showFrom:self];
  201. }
  202. }
  203. __block UIWindow *targetWindow = nil, *oldKeyWindow = nil;
  204. __block UIWindowLevel oldLevel;
  205. __block BOOL wasVisible;
  206. subtitle = [subtitle stringByAppendingString:
  207. @"\n\n1) Adjust the FLEX window level relative to this window,\n"
  208. "2) adjust this window's level relative to the FLEX window,\n"
  209. "3) set this window's level to a specific value, or\n"
  210. "4) make this window the key window if it isn't already."
  211. ];
  212. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  213. make.title(NSStringFromClass(window.class)).message(subtitle);
  214. make.button(@"Adjust FLEX Window Level").handler(^(NSArray<NSString *> *strings) {
  215. targetWindow = flex; oldLevel = flex.windowLevel;
  216. flex.windowLevel = window.windowLevel + strings.firstObject.integerValue;
  217. [self showRevertOrDismissAlert:^{ targetWindow.windowLevel = oldLevel; }];
  218. });
  219. make.button(@"Adjust This Window's Level").handler(^(NSArray<NSString *> *strings) {
  220. targetWindow = window; oldLevel = window.windowLevel;
  221. window.windowLevel = flex.windowLevel + strings.firstObject.integerValue;
  222. [self showRevertOrDismissAlert:^{ targetWindow.windowLevel = oldLevel; }];
  223. });
  224. make.button(@"Set This Window's Level").handler(^(NSArray<NSString *> *strings) {
  225. targetWindow = window; oldLevel = window.windowLevel;
  226. window.windowLevel = strings.firstObject.integerValue;
  227. [self showRevertOrDismissAlert:^{ targetWindow.windowLevel = oldLevel; }];
  228. });
  229. make.button(@"Make Key And Visible").handler(^(NSArray<NSString *> *strings) {
  230. oldKeyWindow = UIApplication.sharedApplication.keyWindow;
  231. wasVisible = window.hidden;
  232. [window makeKeyAndVisible];
  233. [self showRevertOrDismissAlert:^{
  234. window.hidden = wasVisible;
  235. [oldKeyWindow makeKeyWindow];
  236. }];
  237. }).enabled(!window.isKeyWindow && !window.hidden);
  238. make.button(@"Cancel").cancelStyle().handler(cancelHandler);
  239. make.textField(@"+/- window level, i.e. 5 or -10");
  240. } showFrom:self];
  241. }
  242. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)ip {
  243. [self.navigationController pushViewController:
  244. [FLEXObjectExplorerFactory explorerViewControllerForObject:self.sections[ip.section][ip.row]]
  245. animated:YES];
  246. }
  247. @end