LKS_LocalInspectManager.m 3.8 KB

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