FLEXWindowManagerController.m 11 KB

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