FLEXWindowManagerController.m 11 KB

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