1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/components/root/root_element.h"
17 
18 #include "core/components/container_modal/container_modal_element.h"
19 #include "core/components/dialog_modal/dialog_modal_element.h"
20 #include "core/components/root/render_root.h"
21 #include "core/components/root/root_component.h"
22 #include "core/components/semi_modal/semi_modal_element.h"
23 
24 namespace OHOS::Ace {
25 
PerformBuild()26 void RootElement::PerformBuild()
27 {
28     const RefPtr<RootComponent> root = AceType::DynamicCast<RootComponent>(component_);
29     if (root) {
30         active_ = true;
31         auto first = children_.begin();
32         const auto& stageElement = (first == children_.end()) ? nullptr : *first;
33         UpdateChild(stageElement, root->GetChild());
34     }
35 }
36 
GetOverlayElement(WindowModal windowModal) const37 RefPtr<Element> RootElement::GetOverlayElement(WindowModal windowModal) const
38 {
39     if (windowModal == WindowModal::SEMI_MODAL || windowModal == WindowModal::SEMI_MODAL_FULL_SCREEN) {
40         auto semiModal = AceType::DynamicCast<SemiModalElement>(GetFirstChild());
41         if (!semiModal) {
42             LOGE("Get overlay element failed. SemiModal element is null!");
43             return RefPtr<OverlayElement>();
44         }
45         return semiModal->GetOverlayElement();
46     } else if (windowModal == WindowModal::DIALOG_MODAL) {
47         auto dialogModal = AceType::DynamicCast<DialogModalElement>(GetFirstChild());
48         if (!dialogModal) {
49             LOGE("Get overlay element failed. DialogModal element is null!");
50             return RefPtr<OverlayElement>();
51         }
52         return dialogModal->GetOverlayElement();
53     } else if (windowModal == WindowModal::CONTAINER_MODAL) {
54         auto containerModal = AceType::DynamicCast<ContainerModalElement>(GetFirstChild());
55         if (!containerModal) {
56             LOGE("Get overlay element failed. containerModal element is null!");
57             return RefPtr<OverlayElement>();
58         }
59         return containerModal->GetOverlayElement();
60     } else {
61         auto stack = GetFirstChild();
62         if (!stack) {
63             return RefPtr<OverlayElement>();
64         }
65         auto child = stack->GetChildren();
66         if (child.size() > 1) {
67             auto it = child.begin();
68             it++;
69             return AceType::DynamicCast<OverlayElement>(*it);
70         }
71         return RefPtr<OverlayElement>();
72     }
73 }
74 
HandleSpecifiedKey(const KeyEvent & keyEvent)75 void RootElement::HandleSpecifiedKey(const KeyEvent& keyEvent)
76 {
77     const auto& context = context_.Upgrade();
78     if (!context) {
79         LOGE("Handle specified key failed. context is null!");
80         return;
81     }
82     WindowModal windowModal = context->GetWindowModal();
83     if ((windowModal == WindowModal::DIALOG_MODAL) || (windowModal == WindowModal::SEMI_MODAL)) {
84         bool isFullWindow = false;
85         if (windowModal == WindowModal::SEMI_MODAL) {
86             auto semiModal = AceType::DynamicCast<SemiModalElement>(GetFirstChild());
87             if (!semiModal) {
88                 LOGE("Handle specified key failed. SemiModal element is null!");
89                 return;
90             }
91             isFullWindow = semiModal->IsFullWindow();
92         }
93         if (keyEvent.code == KeyCode::KEY_HOME && keyEvent.action == KeyAction::UP && !isFullWindow) {
94             const auto& render = AceType::DynamicCast<RenderRoot>(renderNode_);
95             if (render) {
96                 render->SetBgColor(Color::TRANSPARENT);
97             }
98         }
99     }
100 }
101 
102 } // namespace OHOS::Ace
103