LKS_HierarchyDisplayItemsMaker.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LKS_HierarchyDisplayItemsMaker.m
  4. // LookinServer
  5. //
  6. // Created by Li Kai on 2019/2/19.
  7. // https://lookin.work
  8. //
  9. #import "LKS_HierarchyDisplayItemsMaker.h"
  10. #import "LookinDisplayItem.h"
  11. #import "LKS_TraceManager.h"
  12. #import "LKS_AttrGroupsMaker.h"
  13. #import "LKS_EventHandlerMaker.h"
  14. #import "LookinServerDefines.h"
  15. #import "UIColor+LookinServer.h"
  16. #import "LKSConfigManager.h"
  17. @implementation LKS_HierarchyDisplayItemsMaker
  18. + (NSArray<LookinDisplayItem *> *)itemsWithScreenshots:(BOOL)hasScreenshots attrList:(BOOL)hasAttrList lowImageQuality:(BOOL)lowQuality includedWindows:(NSArray<UIWindow *> *)includedWindows excludedWindows:(NSArray<UIWindow *> *)excludedWindows {
  19. [[LKS_TraceManager sharedInstance] reload];
  20. NSArray<UIWindow *> *windows = [[UIApplication sharedApplication].windows copy];
  21. NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:windows.count];
  22. [windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull window, NSUInteger idx, BOOL * _Nonnull stop) {
  23. if (includedWindows.count) {
  24. if (![includedWindows containsObject:window]) {
  25. return;
  26. }
  27. } else if ([excludedWindows containsObject:window]) {
  28. return;
  29. }
  30. LookinDisplayItem *item = [self _displayItemWithLayer:window.layer screenshots:hasScreenshots attrList:hasAttrList lowImageQuality:lowQuality];
  31. item.representedAsKeyWindow = window.isKeyWindow;
  32. if (item) {
  33. [resultArray addObject:item];
  34. }
  35. }];
  36. return [resultArray copy];
  37. }
  38. + (LookinDisplayItem *)_displayItemWithLayer:(CALayer *)layer screenshots:(BOOL)hasScreenshots attrList:(BOOL)hasAttrList lowImageQuality:(BOOL)lowQuality {
  39. if (!layer || layer.lks_avoidCapturing) {
  40. return nil;
  41. }
  42. LookinDisplayItem *item = [LookinDisplayItem new];
  43. if ([self validateFrame:layer.frame]) {
  44. item.frame = layer.frame;
  45. } else {
  46. NSLog(@"LookinServer - 该 layer 的 frame(%@) 不太寻常,可能导致 Lookin 客户端中图像渲染错误,因此这里暂时将其视为 CGRectZero", NSStringFromCGRect(layer.frame));
  47. item.frame = CGRectZero;
  48. }
  49. item.bounds = layer.bounds;
  50. if (hasScreenshots) {
  51. item.soloScreenshot = [layer lks_soloScreenshotWithLowQuality:lowQuality];
  52. item.groupScreenshot = [layer lks_groupScreenshotWithLowQuality:lowQuality];
  53. item.screenshotEncodeType = LookinDisplayItemImageEncodeTypeNSData;
  54. }
  55. if (hasAttrList) {
  56. item.attributesGroupList = [LKS_AttrGroupsMaker attrGroupsForLayer:layer];
  57. }
  58. item.isHidden = layer.isHidden;
  59. item.alpha = layer.opacity;
  60. item.layerObject = [LookinObject instanceWithObject:layer];
  61. item.shouldCaptureImage = [LKSConfigManager shouldCaptureScreenshotOfLayer:layer];
  62. if (layer.lks_hostView) {
  63. UIView *view = layer.lks_hostView;
  64. item.viewObject = [LookinObject instanceWithObject:view];
  65. item.eventHandlers = [LKS_EventHandlerMaker makeForView:view];
  66. item.backgroundColor = view.backgroundColor;
  67. if (view.lks_hostViewController) {
  68. item.hostViewControllerObject = [LookinObject instanceWithObject:view.lks_hostViewController];
  69. }
  70. } else {
  71. item.backgroundColor = [UIColor lks_colorWithCGColor:layer.backgroundColor];
  72. }
  73. if (layer.sublayers.count) {
  74. NSArray<CALayer *> *sublayers = [layer.sublayers copy];
  75. NSMutableArray<LookinDisplayItem *> *array = [NSMutableArray arrayWithCapacity:sublayers.count];
  76. [sublayers enumerateObjectsUsingBlock:^(__kindof CALayer * _Nonnull sublayer, NSUInteger idx, BOOL * _Nonnull stop) {
  77. LookinDisplayItem *sublayer_item = [self _displayItemWithLayer:sublayer screenshots:hasScreenshots attrList:hasAttrList lowImageQuality:lowQuality];
  78. if (sublayer_item) {
  79. [array addObject:sublayer_item];
  80. }
  81. }];
  82. item.subitems = [array copy];
  83. }
  84. return item;
  85. }
  86. + (BOOL)validateFrame:(CGRect)frame {
  87. return !CGRectIsNull(frame) && !CGRectIsInfinite(frame) && ![self cgRectIsNaN:frame] && ![self cgRectIsInf:frame] && ![self cgRectIsUnreasonable:frame];
  88. }
  89. + (BOOL)cgRectIsNaN:(CGRect)rect {
  90. return isnan(rect.origin.x) || isnan(rect.origin.y) || isnan(rect.size.width) || isnan(rect.size.height);
  91. }
  92. + (BOOL)cgRectIsInf:(CGRect)rect {
  93. return isinf(rect.origin.x) || isinf(rect.origin.y) || isinf(rect.size.width) || isinf(rect.size.height);
  94. }
  95. + (BOOL)cgRectIsUnreasonable:(CGRect)rect {
  96. return ABS(rect.origin.x) > 100000 || ABS(rect.origin.y) > 100000 || rect.size.width < 0 || rect.size.height < 0 || rect.size.width > 100000 || rect.size.height > 100000;
  97. }
  98. @end
  99. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */