void DumpTreeNG(const RefPtr<NG::FrameNode>& parent, int32_t depth, NodeId nodeID, int32_t pageId) { auto node = GetInspectorById(parent, nodeID); NG::RectF rect = node->GetTransformRectRelativeToWindow(); DumpLog::GetInstance().AddDesc("ID: "
时间: 2024-04-03 10:33:01 浏览: 162
+ std::to_string(node->GetAccessibilityId()));
DumpLog::GetInstance().AddDesc("compid: " + node->GetInspectorId().value_or(""));
std::vector<int32_t> children;
for (const auto& item : node->GetChildren()) {
GetFrameNodeChildren(item, children, pageId);
}
DumpLog::GetInstance().Print(depth, node->GetTag(), children.size());
for (auto nodeId : children) {
DumpTreeNG(node, depth + 1, nodeId, pageId);
}
}
这段代码的作用是遍历一个 FrameNode 的子树,并输出每个节点的信息。如果节点是不活跃的,则不输出。其中,“ID”是节点的可访问性 ID,“compid”是节点的 Inspector ID,“depth”是当前节点的深度,“tag”是节点的标签,而“children.size()”则是当前节点的子节点数目。
如果需要将父节点和子节点的信息全部存放在一个vector<ComponentInfo>容器中,可以改写如下:
void DumpTreeNG(const RefPtr<NG::FrameNode>& parent, int32_t depth, NodeId nodeID, int32_t pageId, std::vector<ComponentInfo>& com1) {
auto node = GetInspectorById(parent, nodeID);
if (!node) {
return;
}
NG::RectF rect = node->GetTransformRectRelativeToWindow();
if (!node->IsActive()) {
return;
}
ComponentInfo info;
info.ID = std::to_string(node->GetAccessibilityId());
info.text = node->GetTag();
info.children = std::vector<ComponentInfo>();
std::vector<int32_t> children;
for (const auto& item : node->GetChildren()) {
GetFrameNodeChildren(item, children, pageId);
for (auto nodeId : children) {
ComponentInfo childInfo;
DumpTreeNG(item, depth + 1, nodeId, pageId, childInfo.children);
info.children.push_back(childInfo);
}
}
com1.push_back(info);
}
其中,ComponentInfo是一个struct,成员变量包括 ID(节点的可访问性 ID)、text(节点的标签)、children(子节点的信息)。遍历时将节点信息存储在ComponentInfo中,并将其加入到com1容器中。
阅读全文