LKS_LocalInspectManager.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // LKS_LocalInspectManager.m
  3. // LookinServer
  4. //
  5. // Created by Li Kai on 2019/5/8.
  6. // https://lookin.work
  7. //
  8. #import "LKS_LocalInspectManager.h"
  9. #import "LKS_ConnectionManager.h"
  10. #import "LKS_TraceManager.h"
  11. #import "LKS_LocalInspectViewController.h"
  12. @implementation LKS_LocalInspectContainerWindow
  13. @end
  14. @interface LKS_LocalInspectManager ()
  15. @property(nonatomic, weak) UIWindow *previousKeyWindow;
  16. @property(nonatomic, strong) LKS_LocalInspectContainerWindow *inspectorWindow;
  17. @property(nonatomic, strong) LKS_LocalInspectViewController *viewController;
  18. @property(nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
  19. @property(nonatomic, assign) BOOL isInspecting;
  20. @property(nonatomic, copy) NSArray<UIWindow *> *includedWindows;
  21. @end
  22. @implementation LKS_LocalInspectManager
  23. + (instancetype)sharedInstance {
  24. static dispatch_once_t onceToken;
  25. static LKS_LocalInspectManager *instance = nil;
  26. dispatch_once(&onceToken,^{
  27. instance = [[super allocWithZone:NULL] init];
  28. });
  29. return instance;
  30. }
  31. + (id)allocWithZone:(struct _NSZone *)zone{
  32. return [self sharedInstance];
  33. }
  34. - (void)startLocalInspectWithIncludedWindows:(NSArray<UIWindow *> *)includedWindows excludedWindows:(NSArray<UIWindow *> *)excludedWindows {
  35. NSLog(@"LookinServer - Will start inspecting in 2D");
  36. [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_WillEnter2D" object:nil];
  37. self.isInspecting = YES;
  38. [[LKS_TraceManager sharedInstance] reload];
  39. [self _setupWindowIfNeeded];
  40. self.viewController.showTitleButton = YES;
  41. self.inspectorWindow.userInteractionEnabled = YES;
  42. [self.viewController clearContents];
  43. self.viewController.prevKeyWindow = self.previousKeyWindow;
  44. self.viewController.includedWindows = includedWindows;
  45. self.viewController.excludedWindows = excludedWindows;
  46. [self.viewController startTitleButtonAnimIfNeeded];
  47. }
  48. - (void)_endLocalInspect {
  49. self.isInspecting = NO;
  50. self.viewController.showTitleButton = NO;
  51. self.viewController.includedWindows = nil;
  52. self.viewController.excludedWindows = nil;
  53. [self _removeWindowIfNeeded];
  54. self.inspectorWindow.userInteractionEnabled = NO;
  55. [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_DidExit2D" object:nil];
  56. NSLog(@"LookinServer - Did end inspecting in 2D");
  57. }
  58. - (void)_setupWindowIfNeeded {
  59. if (!self.inspectorWindow) {
  60. self.inspectorWindow = [LKS_LocalInspectContainerWindow new];
  61. self.inspectorWindow.windowLevel = UIWindowLevelAlert - 1;
  62. self.inspectorWindow.backgroundColor = [UIColor clearColor];
  63. }
  64. if (!self.viewController) {
  65. self.viewController = [LKS_LocalInspectViewController new];
  66. __weak __typeof(self)weakSelf = self;
  67. self.viewController.didSelectExit = ^{
  68. [weakSelf _endLocalInspect];
  69. };
  70. self.inspectorWindow.rootViewController = self.viewController;
  71. }
  72. if (!self.inspectorWindow.hidden) {
  73. return;
  74. }
  75. self.previousKeyWindow = [UIApplication sharedApplication].keyWindow;
  76. self.viewController.prevKeyWindow = self.previousKeyWindow;
  77. [self.inspectorWindow makeKeyAndVisible];
  78. }
  79. - (void)_removeWindowIfNeeded {
  80. if (!self.inspectorWindow || self.inspectorWindow.hidden) {
  81. return;
  82. }
  83. if ([[UIApplication sharedApplication] keyWindow] == self.inspectorWindow) {
  84. if (self.previousKeyWindow.hidden) {
  85. ///TODO 到底该用 keyWindow 还是 delegate.window
  86. [[UIApplication sharedApplication].delegate.window makeKeyWindow];
  87. } else {
  88. [self.previousKeyWindow makeKeyWindow];
  89. }
  90. }
  91. self.inspectorWindow.hidden = YES;
  92. self.previousKeyWindow = nil;
  93. self.viewController.prevKeyWindow = nil;
  94. }
  95. @end