• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/log/log.h"
19 #include "base/utils/utils.h"
20 #include "core/components/container_modal/container_modal_element.h"
21 #include "core/components/dialog_modal/dialog_modal_element.h"
22 #include "core/components/root/render_root.h"
23 #include "core/components/root/root_component.h"
24 #include "core/components/semi_modal/semi_modal_element.h"
25 
26 namespace OHOS::Ace {
27 
PerformBuild()28 void RootElement::PerformBuild()
29 {
30     const RefPtr<RootComponent> root = AceType::DynamicCast<RootComponent>(component_);
31     if (root) {
32         active_ = true;
33         auto first = children_.begin();
34         const auto& stageElement = (first == children_.end()) ? nullptr : *first;
35         UpdateChild(stageElement, root->GetChild());
36     }
37 }
38 
GetOverlayElement(WindowModal windowModal) const39 RefPtr<Element> RootElement::GetOverlayElement(WindowModal windowModal) const
40 {
41     if (windowModal == WindowModal::SEMI_MODAL || windowModal == WindowModal::SEMI_MODAL_FULL_SCREEN) {
42         auto semiModal = AceType::DynamicCast<SemiModalElement>(GetFirstChild());
43         if (!semiModal) {
44             LOGE("Get overlay element failed. SemiModal element is null!");
45             return RefPtr<OverlayElement>();
46         }
47         return semiModal->GetOverlayElement();
48     } else if (windowModal == WindowModal::DIALOG_MODAL) {
49         auto dialogModal = AceType::DynamicCast<DialogModalElement>(GetFirstChild());
50         if (!dialogModal) {
51             LOGE("Get overlay element failed. DialogModal element is null!");
52             return RefPtr<OverlayElement>();
53         }
54         return dialogModal->GetOverlayElement();
55     } else if (windowModal == WindowModal::CONTAINER_MODAL) {
56         auto containerModal = AceType::DynamicCast<ContainerModalElement>(GetFirstChild());
57         if (!containerModal) {
58             LOGE("Get overlay element failed. containerModal element is null!");
59             return RefPtr<OverlayElement>();
60         }
61         return containerModal->GetOverlayElement();
62     } else {
63         auto stack = GetFirstChild();
64         if (!stack) {
65             return RefPtr<OverlayElement>();
66         }
67         auto child = stack->GetChildren();
68         if (child.size() > 1) {
69             auto it = child.begin();
70             it++;
71             return AceType::DynamicCast<OverlayElement>(*it);
72         }
73         return RefPtr<OverlayElement>();
74     }
75 }
76 
HandleSpecifiedKey(const KeyEvent & keyEvent)77 void RootElement::HandleSpecifiedKey(const KeyEvent& keyEvent)
78 {
79     const auto& context = context_.Upgrade();
80     if (!context) {
81         LOGE("Handle specified key failed. context is null!");
82         return;
83     }
84     WindowModal windowModal = context->GetWindowModal();
85     if ((windowModal == WindowModal::DIALOG_MODAL) || (windowModal == WindowModal::SEMI_MODAL)) {
86         bool isFullWindow = false;
87         if (windowModal == WindowModal::SEMI_MODAL) {
88             auto semiModal = AceType::DynamicCast<SemiModalElement>(GetFirstChild());
89             if (!semiModal) {
90                 LOGE("Handle specified key failed. SemiModal element is null!");
91                 return;
92             }
93             isFullWindow = semiModal->IsFullWindow();
94         }
95         if (keyEvent.code == KeyCode::KEY_HOME && keyEvent.action == KeyAction::UP && !isFullWindow) {
96             const auto& render = AceType::DynamicCast<RenderRoot>(renderNode_);
97             if (render) {
98                 render->SetBgColor(Color::TRANSPARENT);
99             }
100         }
101     }
102 }
103 
104 } // namespace OHOS::Ace
105