LKS_HierarchyDisplayItemsMaker.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #import "LKS_CustomAttrGroupsMaker.h"
  18. #import "LKS_CustomDisplayItemsMaker.h"
  19. #import "LKS_CustomAttrSetterManager.h"
  20. #import "LKS_MultiplatformAdapter.h"
  21. @implementation LKS_HierarchyDisplayItemsMaker
  22. + (NSArray<LookinDisplayItem *> *)itemsWithScreenshots:(BOOL)hasScreenshots attrList:(BOOL)hasAttrList lowImageQuality:(BOOL)lowQuality readCustomInfo:(BOOL)readCustomInfo saveCustomSetter:(BOOL)saveCustomSetter {
  23. [[LKS_TraceManager sharedInstance] reload];
  24. NSArray<UIWindow *> *windows = [LKS_MultiplatformAdapter allWindows];
  25. NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:windows.count];
  26. [windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull window, NSUInteger idx, BOOL * _Nonnull stop) {
  27. LookinDisplayItem *item = [self _displayItemWithLayer:window.layer screenshots:hasScreenshots attrList:hasAttrList lowImageQuality:lowQuality readCustomInfo:readCustomInfo saveCustomSetter:saveCustomSetter];
  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 readCustomInfo:(BOOL)readCustomInfo saveCustomSetter:(BOOL)saveCustomSetter {
  36. if (!layer) {
  37. return nil;
  38. }
  39. LookinDisplayItem *item = [LookinDisplayItem new];
  40. CGRect layerFrame = layer.frame;
  41. UIView *hostView = layer.lks_hostView;
  42. if (hostView && hostView.superview) {
  43. layerFrame = [hostView.superview convertRect:layerFrame toView:nil];
  44. }
  45. if ([self validateFrame:layerFrame]) {
  46. item.frame = layer.frame;
  47. } else {
  48. NSLog(@"LookinServer - The layer frame(%@) seems really weird. Lookin will ignore it to avoid potential render error in Lookin.", NSStringFromCGRect(layer.frame));
  49. item.frame = CGRectZero;
  50. }
  51. item.bounds = layer.bounds;
  52. if (hasScreenshots) {
  53. item.soloScreenshot = [layer lks_soloScreenshotWithLowQuality:lowQuality];
  54. item.groupScreenshot = [layer lks_groupScreenshotWithLowQuality:lowQuality];
  55. item.screenshotEncodeType = LookinDisplayItemImageEncodeTypeNSData;
  56. }
  57. if (hasAttrList) {
  58. item.attributesGroupList = [LKS_AttrGroupsMaker attrGroupsForLayer:layer];
  59. LKS_CustomAttrGroupsMaker *maker = [[LKS_CustomAttrGroupsMaker alloc] initWithLayer:layer];
  60. [maker execute];
  61. item.customAttrGroupList = [maker getGroups];
  62. item.customDisplayTitle = [maker getCustomDisplayTitle];
  63. item.danceuiSource = [maker getDanceUISource];
  64. }
  65. item.isHidden = layer.isHidden;
  66. item.alpha = layer.opacity;
  67. item.layerObject = [LookinObject instanceWithObject:layer];
  68. item.shouldCaptureImage = [LKSConfigManager shouldCaptureScreenshotOfLayer:layer];
  69. if (layer.lks_hostView) {
  70. UIView *view = layer.lks_hostView;
  71. item.viewObject = [LookinObject instanceWithObject:view];
  72. item.eventHandlers = [LKS_EventHandlerMaker makeForView:view];
  73. item.backgroundColor = view.backgroundColor;
  74. UIViewController* vc = [view lks_findHostViewController];
  75. if (vc) {
  76. item.hostViewControllerObject = [LookinObject instanceWithObject:vc];
  77. }
  78. } else {
  79. item.backgroundColor = [UIColor lks_colorWithCGColor:layer.backgroundColor];
  80. }
  81. if (layer.sublayers.count) {
  82. NSArray<CALayer *> *sublayers = [layer.sublayers copy];
  83. NSMutableArray<LookinDisplayItem *> *allSubitems = [NSMutableArray arrayWithCapacity:sublayers.count];
  84. [sublayers enumerateObjectsUsingBlock:^(__kindof CALayer * _Nonnull sublayer, NSUInteger idx, BOOL * _Nonnull stop) {
  85. LookinDisplayItem *sublayer_item = [self _displayItemWithLayer:sublayer screenshots:hasScreenshots attrList:hasAttrList lowImageQuality:lowQuality readCustomInfo:readCustomInfo saveCustomSetter:saveCustomSetter];
  86. if (sublayer_item) {
  87. [allSubitems addObject:sublayer_item];
  88. }
  89. }];
  90. item.subitems = [allSubitems copy];
  91. }
  92. if (readCustomInfo) {
  93. NSArray<LookinDisplayItem *> *customSubitems = [[[LKS_CustomDisplayItemsMaker alloc] initWithLayer:layer saveAttrSetter:saveCustomSetter] make];
  94. if (customSubitems.count > 0) {
  95. if (item.subitems) {
  96. item.subitems = [item.subitems arrayByAddingObjectsFromArray:customSubitems];
  97. } else {
  98. item.subitems = customSubitems;
  99. }
  100. }
  101. }
  102. return item;
  103. }
  104. + (NSArray<LookinDisplayItem *> *)subitemsOfLayer:(CALayer *)layer {
  105. if (!layer || layer.sublayers.count == 0) {
  106. return @[];
  107. }
  108. [[LKS_TraceManager sharedInstance] reload];
  109. NSMutableArray<LookinDisplayItem *> *resultSubitems = [NSMutableArray array];
  110. NSArray<CALayer *> *sublayers = [layer.sublayers copy];
  111. [sublayers enumerateObjectsUsingBlock:^(__kindof CALayer * _Nonnull sublayer, NSUInteger idx, BOOL * _Nonnull stop) {
  112. LookinDisplayItem *sublayer_item = [self _displayItemWithLayer:sublayer screenshots:NO attrList:NO lowImageQuality:NO readCustomInfo:YES saveCustomSetter:YES];
  113. if (sublayer_item) {
  114. [resultSubitems addObject:sublayer_item];
  115. }
  116. }];
  117. NSArray<LookinDisplayItem *> *customSubitems = [[[LKS_CustomDisplayItemsMaker alloc] initWithLayer:layer saveAttrSetter:YES] make];
  118. if (customSubitems.count > 0) {
  119. [resultSubitems addObjectsFromArray:customSubitems];
  120. }
  121. return resultSubitems;
  122. }
  123. + (BOOL)validateFrame:(CGRect)frame {
  124. return !CGRectIsNull(frame) && !CGRectIsInfinite(frame) && ![self cgRectIsNaN:frame] && ![self cgRectIsInf:frame] && ![self cgRectIsUnreasonable:frame];
  125. }
  126. + (BOOL)cgRectIsNaN:(CGRect)rect {
  127. return isnan(rect.origin.x) || isnan(rect.origin.y) || isnan(rect.size.width) || isnan(rect.size.height);
  128. }
  129. + (BOOL)cgRectIsInf:(CGRect)rect {
  130. return isinf(rect.origin.x) || isinf(rect.origin.y) || isinf(rect.size.width) || isinf(rect.size.height);
  131. }
  132. + (BOOL)cgRectIsUnreasonable:(CGRect)rect {
  133. 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;
  134. }
  135. @end
  136. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */