1a69a01cdSopenharmony_ci/*
2a69a01cdSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3a69a01cdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a69a01cdSopenharmony_ci * you may not use this file except in compliance with the License.
5a69a01cdSopenharmony_ci * You may obtain a copy of the License at
6a69a01cdSopenharmony_ci *
7a69a01cdSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a69a01cdSopenharmony_ci *
9a69a01cdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a69a01cdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a69a01cdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a69a01cdSopenharmony_ci * See the License for the specific language governing permissions and
13a69a01cdSopenharmony_ci * limitations under the License.
14a69a01cdSopenharmony_ci */
15a69a01cdSopenharmony_ci
16a69a01cdSopenharmony_ci#include "component_tree.h"
17a69a01cdSopenharmony_ci
18a69a01cdSopenharmony_ci#include "tree_manager.h"
19a69a01cdSopenharmony_ci#include "wukong_define.h"
20a69a01cdSopenharmony_ci
21a69a01cdSopenharmony_cinamespace OHOS {
22a69a01cdSopenharmony_cinamespace WuKong {
23a69a01cdSopenharmony_cinamespace {
24a69a01cdSopenharmony_ciconst uint8_t COMPONENT_TYPE_POSION = 48;
25a69a01cdSopenharmony_ciconst uint8_t COMPONENT_WIDTH_POSION = 36;
26a69a01cdSopenharmony_ciconst uint8_t COMPONENT_HEIGHT_POSION = 24;
27a69a01cdSopenharmony_ciconst uint8_t COMPONENT_CONTENT_POSION = 8;
28a69a01cdSopenharmony_ciconst uint8_t COMPONENT_RESERVED_POSION = 0;
29a69a01cdSopenharmony_ci}  // namespace
30a69a01cdSopenharmony_cibool ComponentTree::RecursUpdateInfo(const std::shared_ptr<ComponentTree>& source)
31a69a01cdSopenharmony_ci{
32a69a01cdSopenharmony_ci    if (source == nullptr) {
33a69a01cdSopenharmony_ci        ERROR_LOG("the argument source is nullptr.");
34a69a01cdSopenharmony_ci        return false;
35a69a01cdSopenharmony_ci    }
36a69a01cdSopenharmony_ci    // Copy source count info to new component node.
37a69a01cdSopenharmony_ci    expectedInputCount_ = source->expectedInputCount_;
38a69a01cdSopenharmony_ci    inputCount_ = source->inputCount_;
39a69a01cdSopenharmony_ci    inputTypeCountMap_ = source->inputTypeCountMap_;
40a69a01cdSopenharmony_ci
41a69a01cdSopenharmony_ci    // Recurs component tree for copy count info.
42a69a01cdSopenharmony_ci    for (auto child : children_) {
43a69a01cdSopenharmony_ci        for (auto sourceChild : source->children_) {
44a69a01cdSopenharmony_ci            if (child->IsEqual(sourceChild)) {
45a69a01cdSopenharmony_ci                std::static_pointer_cast<ComponentTree>(child)->RecursUpdateInfo(
46a69a01cdSopenharmony_ci                    std::static_pointer_cast<ComponentTree>(sourceChild));
47a69a01cdSopenharmony_ci                break;
48a69a01cdSopenharmony_ci            }
49a69a01cdSopenharmony_ci        }
50a69a01cdSopenharmony_ci    }
51a69a01cdSopenharmony_ci    return true;
52a69a01cdSopenharmony_ci}
53a69a01cdSopenharmony_ci
54a69a01cdSopenharmony_ci/**
55a69a01cdSopenharmony_ci * @brief component Node Id specification format
56a69a01cdSopenharmony_ci * @details
57a69a01cdSopenharmony_ci * |----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
58a69a01cdSopenharmony_ci * |TYPE-16            |WIDTH-12      |HEIGHT-12     |CONTENT - 16       |RESERVED |
59a69a01cdSopenharmony_ci */
60a69a01cdSopenharmony_cibool ComponentTree::SetNodeId()
61a69a01cdSopenharmony_ci{
62a69a01cdSopenharmony_ci    const uint8_t typeCount = 2;
63a69a01cdSopenharmony_ci    const uint8_t contentCount = 2;
64a69a01cdSopenharmony_ci    nodeId_ = 0;
65a69a01cdSopenharmony_ci    auto elementInfo = TreeManager::GetInstance()->GetNewElementInfoList(index_);
66a69a01cdSopenharmony_ci    if (elementInfo == nullptr) {
67a69a01cdSopenharmony_ci        ERROR_LOG("get new element info is nullptr");
68a69a01cdSopenharmony_ci        return false;
69a69a01cdSopenharmony_ci    }
70a69a01cdSopenharmony_ci
71a69a01cdSopenharmony_ci    rect_ = elementInfo->GetRectInScreen();
72a69a01cdSopenharmony_ci    isVisible_ = elementInfo->IsVisible();
73a69a01cdSopenharmony_ci    // type is component type or component id of the ElementInfo
74a69a01cdSopenharmony_ci    type_ = elementInfo->GetComponentType();
75a69a01cdSopenharmony_ci    uint64_t type = GetSubName(type_, typeCount);
76a69a01cdSopenharmony_ci    // w is width of the ElementInfo
77a69a01cdSopenharmony_ci    uint64_t w = (uint64_t)(rect_.GetRightBottomXScreenPostion() - rect_.GetLeftTopXScreenPostion());
78a69a01cdSopenharmony_ci    // h is width of the ElementInfo
79a69a01cdSopenharmony_ci    uint64_t h = (uint64_t)(rect_.GetRightBottomYScreenPostion() - rect_.GetLeftTopYScreenPostion());
80a69a01cdSopenharmony_ci    // the ElementInfo content of 2 length
81a69a01cdSopenharmony_ci    uint64_t str = GetSubName(elementInfo->GetContent(), contentCount);
82a69a01cdSopenharmony_ci
83a69a01cdSopenharmony_ci    // the ElementInfo name of 4 length
84a69a01cdSopenharmony_ci    TRACK_LOG_STR("component Type: (%d), Width: (%d), Height: (%d)", (uint32_t)type, (int32_t)w, (int32_t)h);
85a69a01cdSopenharmony_ci    nodeId_ |= type << COMPONENT_TYPE_POSION;
86a69a01cdSopenharmony_ci    nodeId_ |= w << COMPONENT_WIDTH_POSION;
87a69a01cdSopenharmony_ci    nodeId_ |= h << COMPONENT_HEIGHT_POSION;
88a69a01cdSopenharmony_ci    nodeId_ |= str << COMPONENT_CONTENT_POSION;
89a69a01cdSopenharmony_ci    nodeId_ |= index_ << COMPONENT_RESERVED_POSION;
90a69a01cdSopenharmony_ci    inspectorKey_ =  elementInfo->GetInspectorKey();
91a69a01cdSopenharmony_ci    TRACK_LOG_STR("component Node ID: (0x%016llX), inspectorKey: (%s)", nodeId_, inspectorKey_.c_str());
92a69a01cdSopenharmony_ci    return true;
93a69a01cdSopenharmony_ci}
94a69a01cdSopenharmony_ci}  // namespace WuKong
95a69a01cdSopenharmony_ci}  // namespace OHOS
96