1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "components/ui_view_group.h"
17a3e0fd82Sopenharmony_ci
18a3e0fd82Sopenharmony_ci#include <cstring>
19a3e0fd82Sopenharmony_ci
20a3e0fd82Sopenharmony_ci#include "components/root_view.h"
21a3e0fd82Sopenharmony_ci#include "components/ui_tree_manager.h"
22a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_log.h"
23a3e0fd82Sopenharmony_ci
24a3e0fd82Sopenharmony_cinamespace OHOS {
25a3e0fd82Sopenharmony_ciUIViewGroup::UIViewGroup()
26a3e0fd82Sopenharmony_ci    : childrenHead_(nullptr),
27a3e0fd82Sopenharmony_ci      childrenRenderHead_(nullptr),
28a3e0fd82Sopenharmony_ci      childrenTail_(nullptr),
29a3e0fd82Sopenharmony_ci      childrenNum_(0),
30a3e0fd82Sopenharmony_ci      isDragging_(false),
31a3e0fd82Sopenharmony_ci      disallowIntercept_(false),
32a3e0fd82Sopenharmony_ci      isAutoSize_(false)
33a3e0fd82Sopenharmony_ci{
34a3e0fd82Sopenharmony_ci    isViewGroup_ = true;
35a3e0fd82Sopenharmony_ci#if ENABLE_FOCUS_MANAGER
36a3e0fd82Sopenharmony_ci    isInterceptFocus_ = false;
37a3e0fd82Sopenharmony_ci#endif
38a3e0fd82Sopenharmony_ci}
39a3e0fd82Sopenharmony_ci
40a3e0fd82Sopenharmony_ciUIViewGroup::~UIViewGroup() {}
41a3e0fd82Sopenharmony_ci
42a3e0fd82Sopenharmony_civoid UIViewGroup::Add(UIView* view)
43a3e0fd82Sopenharmony_ci{
44a3e0fd82Sopenharmony_ci    if ((view == this) || (view == nullptr)) {
45a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("view can not be nullptr and added to self");
46a3e0fd82Sopenharmony_ci        return;
47a3e0fd82Sopenharmony_ci    }
48a3e0fd82Sopenharmony_ci    if (view->GetParent() != nullptr) {
49a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("can not add view multi times");
50a3e0fd82Sopenharmony_ci        return;
51a3e0fd82Sopenharmony_ci    }
52a3e0fd82Sopenharmony_ci
53a3e0fd82Sopenharmony_ci    if (childrenHead_ == nullptr) {
54a3e0fd82Sopenharmony_ci        childrenHead_ = view;
55a3e0fd82Sopenharmony_ci    } else {
56a3e0fd82Sopenharmony_ci        if (childrenTail_ == nullptr) {
57a3e0fd82Sopenharmony_ci            return;
58a3e0fd82Sopenharmony_ci        }
59a3e0fd82Sopenharmony_ci        childrenTail_->SetNextSibling(view);
60a3e0fd82Sopenharmony_ci    }
61a3e0fd82Sopenharmony_ci    view->SetParent(this);
62a3e0fd82Sopenharmony_ci    view->SetNextSibling(nullptr);
63a3e0fd82Sopenharmony_ci    childrenTail_ = view;
64a3e0fd82Sopenharmony_ci    childrenNum_++;
65a3e0fd82Sopenharmony_ci    if (isAutoSize_) {
66a3e0fd82Sopenharmony_ci        AutoResize();
67a3e0fd82Sopenharmony_ci    }
68a3e0fd82Sopenharmony_ci    UpdateRenderView(view);
69a3e0fd82Sopenharmony_ci    OnChildChanged();
70a3e0fd82Sopenharmony_ci}
71a3e0fd82Sopenharmony_ci
72a3e0fd82Sopenharmony_civoid UIViewGroup::Insert(UIView* prevView, UIView* insertView)
73a3e0fd82Sopenharmony_ci{
74a3e0fd82Sopenharmony_ci    if ((insertView == nullptr) || (insertView == this)) {
75a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("insertView can not be nullptr and insert to self");
76a3e0fd82Sopenharmony_ci        return;
77a3e0fd82Sopenharmony_ci    }
78a3e0fd82Sopenharmony_ci
79a3e0fd82Sopenharmony_ci    if (insertView->GetParent() != nullptr) {
80a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("can not insert view multi times");
81a3e0fd82Sopenharmony_ci        return;
82a3e0fd82Sopenharmony_ci    }
83a3e0fd82Sopenharmony_ci
84a3e0fd82Sopenharmony_ci    if (childrenHead_ == nullptr) {
85a3e0fd82Sopenharmony_ci        Add(insertView);
86a3e0fd82Sopenharmony_ci        UpdateRenderView(insertView);
87a3e0fd82Sopenharmony_ci        return;
88a3e0fd82Sopenharmony_ci    }
89a3e0fd82Sopenharmony_ci
90a3e0fd82Sopenharmony_ci    if (prevView == nullptr) {
91a3e0fd82Sopenharmony_ci        insertView->SetNextSibling(childrenHead_);
92a3e0fd82Sopenharmony_ci        insertView->SetParent(this);
93a3e0fd82Sopenharmony_ci        childrenHead_ = insertView;
94a3e0fd82Sopenharmony_ci    } else {
95a3e0fd82Sopenharmony_ci        UIView* nextView = prevView->GetNextSibling();
96a3e0fd82Sopenharmony_ci        prevView->SetNextSibling(insertView);
97a3e0fd82Sopenharmony_ci        insertView->SetNextSibling(nextView);
98a3e0fd82Sopenharmony_ci        insertView->SetParent(this);
99a3e0fd82Sopenharmony_ci    }
100a3e0fd82Sopenharmony_ci    if (childrenTail_ == prevView) {
101a3e0fd82Sopenharmony_ci        childrenTail_ = insertView;
102a3e0fd82Sopenharmony_ci    }
103a3e0fd82Sopenharmony_ci    childrenNum_++;
104a3e0fd82Sopenharmony_ci    if (isAutoSize_) {
105a3e0fd82Sopenharmony_ci        AutoResize();
106a3e0fd82Sopenharmony_ci    }
107a3e0fd82Sopenharmony_ci    UpdateRenderView(insertView);
108a3e0fd82Sopenharmony_ci    OnChildChanged();
109a3e0fd82Sopenharmony_ci}
110a3e0fd82Sopenharmony_ci
111a3e0fd82Sopenharmony_civoid UIViewGroup::Remove(UIView* view)
112a3e0fd82Sopenharmony_ci{
113a3e0fd82Sopenharmony_ci    if ((childrenHead_ == nullptr) || (view == nullptr)) {
114a3e0fd82Sopenharmony_ci        return;
115a3e0fd82Sopenharmony_ci    }
116a3e0fd82Sopenharmony_ci    UITreeManager::GetInstance().OnLifeEvent(view, UITreeManager::REMOVE);
117a3e0fd82Sopenharmony_ci
118a3e0fd82Sopenharmony_ci#if LOCAL_RENDER
119a3e0fd82Sopenharmony_ci    RootView::GetInstance()->RemoveViewFromInvalidMap(view);
120a3e0fd82Sopenharmony_ci    InvalidateRect(view->GetRect());
121a3e0fd82Sopenharmony_ci#endif
122a3e0fd82Sopenharmony_ci    if (childrenHead_ == view) {
123a3e0fd82Sopenharmony_ci        RemoveRenderView(view);
124a3e0fd82Sopenharmony_ci        childrenHead_ = childrenHead_->GetNextSibling();
125a3e0fd82Sopenharmony_ci        view->SetParent(nullptr);
126a3e0fd82Sopenharmony_ci        view->SetNextSibling(nullptr);
127a3e0fd82Sopenharmony_ci        if (childrenTail_ == view) {
128a3e0fd82Sopenharmony_ci            childrenTail_ = nullptr;
129a3e0fd82Sopenharmony_ci        }
130a3e0fd82Sopenharmony_ci        childrenNum_--;
131a3e0fd82Sopenharmony_ci        OnChildChanged();
132a3e0fd82Sopenharmony_ci        return;
133a3e0fd82Sopenharmony_ci    }
134a3e0fd82Sopenharmony_ci    UIView* node = childrenHead_;
135a3e0fd82Sopenharmony_ci    while (node->GetNextSibling() != nullptr) {
136a3e0fd82Sopenharmony_ci        if (node->GetNextSibling() == view) {
137a3e0fd82Sopenharmony_ci            RemoveRenderView(view);
138a3e0fd82Sopenharmony_ci            node->SetNextSibling(view->GetNextSibling());
139a3e0fd82Sopenharmony_ci            view->SetParent(nullptr);
140a3e0fd82Sopenharmony_ci            view->SetNextSibling(nullptr);
141a3e0fd82Sopenharmony_ci            if (childrenTail_ == view) {
142a3e0fd82Sopenharmony_ci                childrenTail_ = node;
143a3e0fd82Sopenharmony_ci            }
144a3e0fd82Sopenharmony_ci            childrenNum_--;
145a3e0fd82Sopenharmony_ci            OnChildChanged();
146a3e0fd82Sopenharmony_ci            return;
147a3e0fd82Sopenharmony_ci        }
148a3e0fd82Sopenharmony_ci        node = node->GetNextSibling();
149a3e0fd82Sopenharmony_ci    }
150a3e0fd82Sopenharmony_ci}
151a3e0fd82Sopenharmony_ci
152a3e0fd82Sopenharmony_civoid UIViewGroup::RemoveAll()
153a3e0fd82Sopenharmony_ci{
154a3e0fd82Sopenharmony_ci    UIView* node = childrenHead_;
155a3e0fd82Sopenharmony_ci    childrenHead_ = nullptr;
156a3e0fd82Sopenharmony_ci    childrenTail_ = nullptr;
157a3e0fd82Sopenharmony_ci    childrenRenderHead_ = nullptr;
158a3e0fd82Sopenharmony_ci    childrenNum_ = 0;
159a3e0fd82Sopenharmony_ci    UIView* tmp = nullptr;
160a3e0fd82Sopenharmony_ci    while (node != nullptr) {
161a3e0fd82Sopenharmony_ci        tmp = node;
162a3e0fd82Sopenharmony_ci        UITreeManager::GetInstance().OnLifeEvent(node, UITreeManager::REMOVE);
163a3e0fd82Sopenharmony_ci        node = node->GetNextSibling();
164a3e0fd82Sopenharmony_ci        tmp->SetParent(nullptr);
165a3e0fd82Sopenharmony_ci        tmp->SetNextSibling(nullptr);
166a3e0fd82Sopenharmony_ci        tmp->SetNextRenderSibling(nullptr);
167a3e0fd82Sopenharmony_ci    }
168a3e0fd82Sopenharmony_ci    OnChildChanged();
169a3e0fd82Sopenharmony_ci}
170a3e0fd82Sopenharmony_ci
171a3e0fd82Sopenharmony_civoid UIViewGroup::GetTargetView(const Point& point, UIView** last)
172a3e0fd82Sopenharmony_ci{
173a3e0fd82Sopenharmony_ci    if (last == nullptr) {
174a3e0fd82Sopenharmony_ci        return;
175a3e0fd82Sopenharmony_ci    }
176a3e0fd82Sopenharmony_ci
177a3e0fd82Sopenharmony_ci    Rect rect = GetRect();
178a3e0fd82Sopenharmony_ci    if (disallowIntercept_) {
179a3e0fd82Sopenharmony_ci        *last = nullptr;
180a3e0fd82Sopenharmony_ci        return;
181a3e0fd82Sopenharmony_ci    }
182a3e0fd82Sopenharmony_ci    if (!rect.IsContains(point)) {
183a3e0fd82Sopenharmony_ci        return;
184a3e0fd82Sopenharmony_ci    }
185a3e0fd82Sopenharmony_ci    if (!visible_) {
186a3e0fd82Sopenharmony_ci        return;
187a3e0fd82Sopenharmony_ci    }
188a3e0fd82Sopenharmony_ci    if (touchable_) {
189a3e0fd82Sopenharmony_ci        *last = this;
190a3e0fd82Sopenharmony_ci    }
191a3e0fd82Sopenharmony_ci    if (isDragging_) {
192a3e0fd82Sopenharmony_ci        return;
193a3e0fd82Sopenharmony_ci    }
194a3e0fd82Sopenharmony_ci    UIView* view = GetChildrenRenderHead();
195a3e0fd82Sopenharmony_ci    while (view != nullptr) {
196a3e0fd82Sopenharmony_ci        if (!view->IsViewGroup()) {
197a3e0fd82Sopenharmony_ci            rect = view->GetRect();
198a3e0fd82Sopenharmony_ci            if (rect.IsContains(point)) {
199a3e0fd82Sopenharmony_ci                view->GetTargetView(point, last);
200a3e0fd82Sopenharmony_ci            }
201a3e0fd82Sopenharmony_ci        } else {
202a3e0fd82Sopenharmony_ci            UIViewGroup* viewGroup = static_cast<UIViewGroup*>(view);
203a3e0fd82Sopenharmony_ci            viewGroup->GetTargetView(point, last);
204a3e0fd82Sopenharmony_ci        }
205a3e0fd82Sopenharmony_ci        view = view->GetNextRenderSibling();
206a3e0fd82Sopenharmony_ci    }
207a3e0fd82Sopenharmony_ci}
208a3e0fd82Sopenharmony_ci
209a3e0fd82Sopenharmony_civoid UIViewGroup::GetTargetView(const Point& point, UIView** current, UIView** target)
210a3e0fd82Sopenharmony_ci{
211a3e0fd82Sopenharmony_ci    if ((current == nullptr) || (target == nullptr)) {
212a3e0fd82Sopenharmony_ci        return;
213a3e0fd82Sopenharmony_ci    }
214a3e0fd82Sopenharmony_ci
215a3e0fd82Sopenharmony_ci    Rect rect = GetRect();
216a3e0fd82Sopenharmony_ci    if (disallowIntercept_) {
217a3e0fd82Sopenharmony_ci        *current = nullptr;
218a3e0fd82Sopenharmony_ci        *target = nullptr;
219a3e0fd82Sopenharmony_ci        return;
220a3e0fd82Sopenharmony_ci    }
221a3e0fd82Sopenharmony_ci    if (!rect.IsContains(point)) {
222a3e0fd82Sopenharmony_ci        return;
223a3e0fd82Sopenharmony_ci    }
224a3e0fd82Sopenharmony_ci    if (!visible_) {
225a3e0fd82Sopenharmony_ci        return;
226a3e0fd82Sopenharmony_ci    }
227a3e0fd82Sopenharmony_ci    *target = this;
228a3e0fd82Sopenharmony_ci    if (touchable_) {
229a3e0fd82Sopenharmony_ci        *current = this;
230a3e0fd82Sopenharmony_ci    }
231a3e0fd82Sopenharmony_ci    if (isDragging_) {
232a3e0fd82Sopenharmony_ci        return;
233a3e0fd82Sopenharmony_ci    }
234a3e0fd82Sopenharmony_ci    Point pointTran = point;
235a3e0fd82Sopenharmony_ci    if (transMap_ != nullptr && !GetTransformMap().IsInvalid()) {
236a3e0fd82Sopenharmony_ci        Rect relativeRect = GetOrigRect();
237a3e0fd82Sopenharmony_ci        pointTran = GetTransformMap().GetOrigPoint(point, relativeRect);
238a3e0fd82Sopenharmony_ci    }
239a3e0fd82Sopenharmony_ci    UIView* view = GetChildrenRenderHead();
240a3e0fd82Sopenharmony_ci    while (view != nullptr) {
241a3e0fd82Sopenharmony_ci        if (!view->IsViewGroup()) {
242a3e0fd82Sopenharmony_ci            rect = view->GetRect();
243a3e0fd82Sopenharmony_ci            if (rect.IsContains(pointTran)) {
244a3e0fd82Sopenharmony_ci                view->GetTargetView(pointTran, current, target);
245a3e0fd82Sopenharmony_ci            }
246a3e0fd82Sopenharmony_ci        } else {
247a3e0fd82Sopenharmony_ci            UIViewGroup* viewGroup = static_cast<UIViewGroup*>(view);
248a3e0fd82Sopenharmony_ci            viewGroup->GetTargetView(pointTran, current, target);
249a3e0fd82Sopenharmony_ci        }
250a3e0fd82Sopenharmony_ci        view = view->GetNextRenderSibling();
251a3e0fd82Sopenharmony_ci    }
252a3e0fd82Sopenharmony_ci}
253a3e0fd82Sopenharmony_ci
254a3e0fd82Sopenharmony_ciRect UIViewGroup::GetAllChildRelativeRect() const
255a3e0fd82Sopenharmony_ci{
256a3e0fd82Sopenharmony_ci    Rect rect = {0, 0, 0, 0};
257a3e0fd82Sopenharmony_ci    UIView* view = childrenHead_;
258a3e0fd82Sopenharmony_ci    bool isRectValid = false;
259a3e0fd82Sopenharmony_ci    while (view != nullptr) {
260a3e0fd82Sopenharmony_ci        if (!view->IsVisible()) {
261a3e0fd82Sopenharmony_ci            view = view->GetNextSibling();
262a3e0fd82Sopenharmony_ci            continue;
263a3e0fd82Sopenharmony_ci        }
264a3e0fd82Sopenharmony_ci        Rect rectChild = view->GetRelativeRect();
265a3e0fd82Sopenharmony_ci        if (!isRectValid) {
266a3e0fd82Sopenharmony_ci            rect = rectChild;
267a3e0fd82Sopenharmony_ci            isRectValid = true;
268a3e0fd82Sopenharmony_ci        } else {
269a3e0fd82Sopenharmony_ci            rect.Join(rect, rectChild);
270a3e0fd82Sopenharmony_ci        }
271a3e0fd82Sopenharmony_ci        view = view->GetNextSibling();
272a3e0fd82Sopenharmony_ci    }
273a3e0fd82Sopenharmony_ci    return rect;
274a3e0fd82Sopenharmony_ci}
275a3e0fd82Sopenharmony_ci
276a3e0fd82Sopenharmony_ciUIView* UIViewGroup::GetChildrenRenderHead() const
277a3e0fd82Sopenharmony_ci{
278a3e0fd82Sopenharmony_ci    return childrenRenderHead_;
279a3e0fd82Sopenharmony_ci}
280a3e0fd82Sopenharmony_ci
281a3e0fd82Sopenharmony_civoid UIViewGroup::SetChildrenRenderHead(UIView* renderHead)
282a3e0fd82Sopenharmony_ci{
283a3e0fd82Sopenharmony_ci    if ((renderHead != nullptr) && (renderHead->GetParent() != this)) {
284a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("can not set as render head if it is not a child view");
285a3e0fd82Sopenharmony_ci        return;
286a3e0fd82Sopenharmony_ci    }
287a3e0fd82Sopenharmony_ci    childrenRenderHead_ = renderHead;
288a3e0fd82Sopenharmony_ci}
289a3e0fd82Sopenharmony_ci
290a3e0fd82Sopenharmony_ciUIView* UIViewGroup::GetChildById(const char* id) const
291a3e0fd82Sopenharmony_ci{
292a3e0fd82Sopenharmony_ci    if (id == nullptr || childrenHead_ == nullptr) {
293a3e0fd82Sopenharmony_ci        return nullptr;
294a3e0fd82Sopenharmony_ci    }
295a3e0fd82Sopenharmony_ci    UIView* child = childrenHead_;
296a3e0fd82Sopenharmony_ci    while (child != nullptr) {
297a3e0fd82Sopenharmony_ci        if ((child->GetViewId() != nullptr) && !strcmp(child->GetViewId(), id)) {
298a3e0fd82Sopenharmony_ci            return child;
299a3e0fd82Sopenharmony_ci        } else if (child->IsViewGroup() && static_cast<UIViewGroup*>(child)->GetChildrenHead() != nullptr) {
300a3e0fd82Sopenharmony_ci            child = static_cast<UIViewGroup*>(child)->GetChildrenHead();
301a3e0fd82Sopenharmony_ci            continue;
302a3e0fd82Sopenharmony_ci        } else if (child->GetNextSibling() != nullptr) {
303a3e0fd82Sopenharmony_ci            child = child->GetNextSibling();
304a3e0fd82Sopenharmony_ci            continue;
305a3e0fd82Sopenharmony_ci        }
306a3e0fd82Sopenharmony_ci        while (child->GetParent() != this && child->GetParent()->GetNextSibling() == nullptr) {
307a3e0fd82Sopenharmony_ci            child = child->GetParent();
308a3e0fd82Sopenharmony_ci        }
309a3e0fd82Sopenharmony_ci        if (child->GetParent() != this) {
310a3e0fd82Sopenharmony_ci            child = child->GetParent()->GetNextSibling();
311a3e0fd82Sopenharmony_ci            continue;
312a3e0fd82Sopenharmony_ci        }
313a3e0fd82Sopenharmony_ci        break;
314a3e0fd82Sopenharmony_ci    }
315a3e0fd82Sopenharmony_ci    return nullptr;
316a3e0fd82Sopenharmony_ci}
317a3e0fd82Sopenharmony_ci
318a3e0fd82Sopenharmony_civoid UIViewGroup::MoveChildByOffset(int16_t xOffset, int16_t yOffset)
319a3e0fd82Sopenharmony_ci{
320a3e0fd82Sopenharmony_ci    UIView* view = childrenHead_;
321a3e0fd82Sopenharmony_ci    while (view != nullptr) {
322a3e0fd82Sopenharmony_ci        int16_t x = view->GetX() + xOffset;
323a3e0fd82Sopenharmony_ci        int16_t y = view->GetY() + yOffset;
324a3e0fd82Sopenharmony_ci        view->SetPosition(x, y);
325a3e0fd82Sopenharmony_ci        view = view->GetNextSibling();
326a3e0fd82Sopenharmony_ci    }
327a3e0fd82Sopenharmony_ci}
328a3e0fd82Sopenharmony_ci
329a3e0fd82Sopenharmony_civoid UIViewGroup::AutoResize()
330a3e0fd82Sopenharmony_ci{
331a3e0fd82Sopenharmony_ci    Rect rect = GetAllChildRelativeRect();
332a3e0fd82Sopenharmony_ci    SetWidth(rect.GetWidth() + rect.GetLeft());
333a3e0fd82Sopenharmony_ci    SetHeight(rect.GetHeight() + rect.GetTop());
334a3e0fd82Sopenharmony_ci}
335a3e0fd82Sopenharmony_ci
336a3e0fd82Sopenharmony_civoid UIViewGroup::RemoveRenderView(UIView* targetView)
337a3e0fd82Sopenharmony_ci{
338a3e0fd82Sopenharmony_ci    if (targetView == nullptr) {
339a3e0fd82Sopenharmony_ci        return;
340a3e0fd82Sopenharmony_ci    }
341a3e0fd82Sopenharmony_ci
342a3e0fd82Sopenharmony_ci    if (targetView->GetParent() == nullptr) {
343a3e0fd82Sopenharmony_ci        return;
344a3e0fd82Sopenharmony_ci    }
345a3e0fd82Sopenharmony_ci
346a3e0fd82Sopenharmony_ci    UIViewGroup* viewGroup = reinterpret_cast<UIViewGroup*>(targetView->GetParent());
347a3e0fd82Sopenharmony_ci    UIView* node = viewGroup->GetChildrenRenderHead();
348a3e0fd82Sopenharmony_ci    if (node == nullptr) {
349a3e0fd82Sopenharmony_ci        return;
350a3e0fd82Sopenharmony_ci    }
351a3e0fd82Sopenharmony_ci    if (node == targetView) {
352a3e0fd82Sopenharmony_ci        viewGroup->SetChildrenRenderHead(node->GetNextRenderSibling());
353a3e0fd82Sopenharmony_ci        targetView->SetNextRenderSibling(nullptr);
354a3e0fd82Sopenharmony_ci    } else {
355a3e0fd82Sopenharmony_ci        while (node->GetNextRenderSibling() != nullptr) {
356a3e0fd82Sopenharmony_ci            if (node->GetNextRenderSibling() == targetView) {
357a3e0fd82Sopenharmony_ci                node->SetNextRenderSibling(targetView->GetNextRenderSibling());
358a3e0fd82Sopenharmony_ci                targetView->SetNextRenderSibling(nullptr);
359a3e0fd82Sopenharmony_ci                break;
360a3e0fd82Sopenharmony_ci            }
361a3e0fd82Sopenharmony_ci            node = node->GetNextRenderSibling();
362a3e0fd82Sopenharmony_ci        }
363a3e0fd82Sopenharmony_ci    }
364a3e0fd82Sopenharmony_ci}
365a3e0fd82Sopenharmony_ci
366a3e0fd82Sopenharmony_civoid UIViewGroup::UpdateRenderView(UIView* targetView)
367a3e0fd82Sopenharmony_ci{
368a3e0fd82Sopenharmony_ci    if (targetView == nullptr) {
369a3e0fd82Sopenharmony_ci        return;
370a3e0fd82Sopenharmony_ci    }
371a3e0fd82Sopenharmony_ci
372a3e0fd82Sopenharmony_ci    if (targetView->GetParent() == nullptr) {
373a3e0fd82Sopenharmony_ci        return;
374a3e0fd82Sopenharmony_ci    }
375a3e0fd82Sopenharmony_ci    RemoveRenderView(targetView);
376a3e0fd82Sopenharmony_ci
377a3e0fd82Sopenharmony_ci    UIViewGroup* viewGroup = reinterpret_cast<UIViewGroup*>(targetView->GetParent());
378a3e0fd82Sopenharmony_ci    UIView* curView = viewGroup->GetChildrenRenderHead();
379a3e0fd82Sopenharmony_ci    if (curView == nullptr) {
380a3e0fd82Sopenharmony_ci        viewGroup->SetChildrenRenderHead(targetView);
381a3e0fd82Sopenharmony_ci        targetView->SetNextRenderSibling(nullptr);
382a3e0fd82Sopenharmony_ci        return;
383a3e0fd82Sopenharmony_ci    }
384a3e0fd82Sopenharmony_ci
385a3e0fd82Sopenharmony_ci    int16_t curZIndex = curView->GetZIndex();
386a3e0fd82Sopenharmony_ci    int16_t targetZIndex = targetView->GetZIndex();
387a3e0fd82Sopenharmony_ci    UIView* nextView = curView->GetNextRenderSibling();
388a3e0fd82Sopenharmony_ci    UIView* preView = nullptr;
389a3e0fd82Sopenharmony_ci
390a3e0fd82Sopenharmony_ci    if (curZIndex > targetZIndex) {
391a3e0fd82Sopenharmony_ci        targetView->SetNextRenderSibling(curView);
392a3e0fd82Sopenharmony_ci        viewGroup->SetChildrenRenderHead(targetView);
393a3e0fd82Sopenharmony_ci        return;
394a3e0fd82Sopenharmony_ci    }
395a3e0fd82Sopenharmony_ci
396a3e0fd82Sopenharmony_ci    while (nextView != nullptr) {
397a3e0fd82Sopenharmony_ci        int16_t nextZIndex = nextView->GetZIndex();
398a3e0fd82Sopenharmony_ci        if (curZIndex == targetZIndex) {
399a3e0fd82Sopenharmony_ci            InsertRenderView(curView, preView, targetView);
400a3e0fd82Sopenharmony_ci            return;
401a3e0fd82Sopenharmony_ci        }
402a3e0fd82Sopenharmony_ci        if ((curZIndex < targetZIndex) && (targetZIndex < nextZIndex)) {
403a3e0fd82Sopenharmony_ci            curView->SetNextRenderSibling(targetView);
404a3e0fd82Sopenharmony_ci            targetView->SetNextRenderSibling(nextView);
405a3e0fd82Sopenharmony_ci            return;
406a3e0fd82Sopenharmony_ci        }
407a3e0fd82Sopenharmony_ci        preView = curView;
408a3e0fd82Sopenharmony_ci        curView = nextView;
409a3e0fd82Sopenharmony_ci        nextView = nextView->GetNextRenderSibling();
410a3e0fd82Sopenharmony_ci        curZIndex = curView->GetZIndex();
411a3e0fd82Sopenharmony_ci    }
412a3e0fd82Sopenharmony_ci
413a3e0fd82Sopenharmony_ci    if (curZIndex == targetZIndex) {
414a3e0fd82Sopenharmony_ci        InsertRenderView(curView, preView, targetView);
415a3e0fd82Sopenharmony_ci    } else {
416a3e0fd82Sopenharmony_ci        curView->SetNextRenderSibling(targetView);
417a3e0fd82Sopenharmony_ci        targetView->SetNextRenderSibling(nullptr);
418a3e0fd82Sopenharmony_ci    }
419a3e0fd82Sopenharmony_ci}
420a3e0fd82Sopenharmony_ci
421a3e0fd82Sopenharmony_cinamespace {
422a3e0fd82Sopenharmony_ci    bool AheadOfTargetView(UIView* currentView, UIView* targetView)
423a3e0fd82Sopenharmony_ci    {
424a3e0fd82Sopenharmony_ci        if ((targetView == nullptr) || (currentView == nullptr)) {
425a3e0fd82Sopenharmony_ci            return  false;
426a3e0fd82Sopenharmony_ci        }
427a3e0fd82Sopenharmony_ci        while (currentView != nullptr) {
428a3e0fd82Sopenharmony_ci            UIView* nextView = currentView->GetNextSibling();
429a3e0fd82Sopenharmony_ci            if (nextView == targetView) {
430a3e0fd82Sopenharmony_ci                return  true;
431a3e0fd82Sopenharmony_ci            }
432a3e0fd82Sopenharmony_ci            currentView = nextView;
433a3e0fd82Sopenharmony_ci        }
434a3e0fd82Sopenharmony_ci        return false;
435a3e0fd82Sopenharmony_ci    }
436a3e0fd82Sopenharmony_ci}
437a3e0fd82Sopenharmony_ci
438a3e0fd82Sopenharmony_civoid UIViewGroup::InsertRenderView(UIView* anchorView, UIView* anchorPreView, UIView* targetView)
439a3e0fd82Sopenharmony_ci{
440a3e0fd82Sopenharmony_ci    if ((targetView == nullptr) || (anchorView == nullptr)) {
441a3e0fd82Sopenharmony_ci        return;
442a3e0fd82Sopenharmony_ci    }
443a3e0fd82Sopenharmony_ci
444a3e0fd82Sopenharmony_ci    if (anchorView->GetParent() == nullptr) {
445a3e0fd82Sopenharmony_ci        return;
446a3e0fd82Sopenharmony_ci    }
447a3e0fd82Sopenharmony_ci    int16_t targetZIndex = targetView->GetZIndex();
448a3e0fd82Sopenharmony_ci    int16_t curZIndex;
449a3e0fd82Sopenharmony_ci    UIView* node = anchorView;
450a3e0fd82Sopenharmony_ci    UIView* lastView = anchorPreView;
451a3e0fd82Sopenharmony_ci    while (node != nullptr) {
452a3e0fd82Sopenharmony_ci        curZIndex = node->GetZIndex();
453a3e0fd82Sopenharmony_ci        if (curZIndex == targetZIndex) {
454a3e0fd82Sopenharmony_ci            if (AheadOfTargetView(node, targetView)) {
455a3e0fd82Sopenharmony_ci                lastView = node;
456a3e0fd82Sopenharmony_ci            } else if (lastView == nullptr) {
457a3e0fd82Sopenharmony_ci                UIViewGroup* viewGroup = reinterpret_cast<UIViewGroup*>(anchorView->GetParent());
458a3e0fd82Sopenharmony_ci                targetView->SetNextRenderSibling(viewGroup->GetChildrenRenderHead());
459a3e0fd82Sopenharmony_ci                viewGroup->SetChildrenRenderHead(targetView);
460a3e0fd82Sopenharmony_ci                return;
461a3e0fd82Sopenharmony_ci            } else {
462a3e0fd82Sopenharmony_ci                lastView->SetNextRenderSibling(targetView);
463a3e0fd82Sopenharmony_ci                targetView->SetNextRenderSibling(node);
464a3e0fd82Sopenharmony_ci                return;
465a3e0fd82Sopenharmony_ci            }
466a3e0fd82Sopenharmony_ci        } else if (curZIndex > targetZIndex) {
467a3e0fd82Sopenharmony_ci            lastView->SetNextRenderSibling(targetView);
468a3e0fd82Sopenharmony_ci            targetView->SetNextRenderSibling(node);
469a3e0fd82Sopenharmony_ci            return;
470a3e0fd82Sopenharmony_ci        }
471a3e0fd82Sopenharmony_ci        node = node->GetNextRenderSibling();
472a3e0fd82Sopenharmony_ci    }
473a3e0fd82Sopenharmony_ci    lastView->SetNextRenderSibling(targetView);
474a3e0fd82Sopenharmony_ci    targetView->SetNextRenderSibling(nullptr);
475a3e0fd82Sopenharmony_ci}
476a3e0fd82Sopenharmony_ci} // namespace OHOS
477