1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2022 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_tree_manager.h"
17a3e0fd82Sopenharmony_ci
18a3e0fd82Sopenharmony_cinamespace OHOS {
19a3e0fd82Sopenharmony_ciUITreeManager& UITreeManager::GetInstance()
20a3e0fd82Sopenharmony_ci{
21a3e0fd82Sopenharmony_ci    static UITreeManager manager;
22a3e0fd82Sopenharmony_ci    return manager;
23a3e0fd82Sopenharmony_ci}
24a3e0fd82Sopenharmony_ci
25a3e0fd82Sopenharmony_civoid UITreeManager::RegistViewLifeEvent(ViewFunc* func)
26a3e0fd82Sopenharmony_ci{
27a3e0fd82Sopenharmony_ci    auto p = lifeEvents_.Begin();
28a3e0fd82Sopenharmony_ci    if (func == nullptr) {
29a3e0fd82Sopenharmony_ci        return;
30a3e0fd82Sopenharmony_ci    }
31a3e0fd82Sopenharmony_ci    while (p != lifeEvents_.End()) {
32a3e0fd82Sopenharmony_ci        if (p->data_ == func) {
33a3e0fd82Sopenharmony_ci            return;
34a3e0fd82Sopenharmony_ci        }
35a3e0fd82Sopenharmony_ci        p = p->next_;
36a3e0fd82Sopenharmony_ci    }
37a3e0fd82Sopenharmony_ci    lifeEvents_.PushBack(func);
38a3e0fd82Sopenharmony_ci}
39a3e0fd82Sopenharmony_ci
40a3e0fd82Sopenharmony_civoid UITreeManager::UnregistViewLifeEvent(ViewFunc* func)
41a3e0fd82Sopenharmony_ci{
42a3e0fd82Sopenharmony_ci    auto p = lifeEvents_.Begin();
43a3e0fd82Sopenharmony_ci    while (p != lifeEvents_.End()) {
44a3e0fd82Sopenharmony_ci        if (p->data_ == func) {
45a3e0fd82Sopenharmony_ci            lifeEvents_.Remove(p);
46a3e0fd82Sopenharmony_ci            return;
47a3e0fd82Sopenharmony_ci        }
48a3e0fd82Sopenharmony_ci        p = p->next_;
49a3e0fd82Sopenharmony_ci    }
50a3e0fd82Sopenharmony_ci}
51a3e0fd82Sopenharmony_ci
52a3e0fd82Sopenharmony_civoid UITreeManager::GetLastEvent(UIView*& view, ViewLifeEvent& event)
53a3e0fd82Sopenharmony_ci{
54a3e0fd82Sopenharmony_ci    view = lastEventView_;
55a3e0fd82Sopenharmony_ci    event = lastEvent_;
56a3e0fd82Sopenharmony_ci}
57a3e0fd82Sopenharmony_ci
58a3e0fd82Sopenharmony_civoid UITreeManager::OnLifeEvent(UIView* view, ViewLifeEvent event)
59a3e0fd82Sopenharmony_ci{
60a3e0fd82Sopenharmony_ci    lastEventView_ = view;
61a3e0fd82Sopenharmony_ci    lastEvent_ = event;
62a3e0fd82Sopenharmony_ci    auto p = lifeEvents_.Begin();
63a3e0fd82Sopenharmony_ci    while (p != lifeEvents_.End()) {
64a3e0fd82Sopenharmony_ci        p->data_();
65a3e0fd82Sopenharmony_ci        p = p->next_;
66a3e0fd82Sopenharmony_ci    }
67a3e0fd82Sopenharmony_ci}
68a3e0fd82Sopenharmony_ci
69a3e0fd82Sopenharmony_ci} // namespace OHOS
70