LKS_HierarchyDisplayItemsMaker.m 4.5 KB

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