• 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/popup/popup_element_v2.h"
17 #include "core/event/ace_event_helper.h"
18 
19 namespace OHOS::Ace {
20 
PerformBuild()21 void PopupElementV2::PerformBuild()
22 {
23     PopupElement::PerformBuild();
24     auto popupComponentV2 = AceType::DynamicCast<PopupComponentV2>(component_);
25     if (!popupComponentV2) {
26         return;
27     }
28 
29     auto themeManager = GetThemeManager();
30     if (!themeManager) {
31         LOGE("themeManager is null.");
32         return;
33     }
34 
35     popupComponentV2->Initialization(themeManager, context_);
36     if (!popupComponentV2->GetOnStateChange().IsEmpty()) {
37         onStateChange_ =
38             AceAsyncEvent<void(const std::string&)>::Create(popupComponentV2->GetOnStateChange(), context_);
39     }
40 
41     if (!popupComponentV2->GetChangeEvent().IsEmpty()) {
42         changeEvent_ =
43             AceAsyncEvent<void(const std::string&)>::Create(popupComponentV2->GetChangeEvent(), context_);
44     }
45 
46     if (IsDeclarative()) {
47         HandleDeclarativePerformBuild();
48     }
49 }
50 
HandleDeclarativePerformBuild()51 void PopupElementV2::HandleDeclarativePerformBuild()
52 {
53     if (!popup_) {
54         LOGE("popup is null.");
55         return;
56     }
57     if (popup_->GetPopupParam()->IsShowInSubWindow()) {
58         auto context = context_.Upgrade();
59         if (context) {
60             auto id = popup_->GetPopupParam()->GetTargetId();
61             auto targetElement = context->GetComposedElementById(id);
62             if (!targetElement) {
63                 popup_->GetPopupParam()->SetIsShow(false);
64                 return;
65             }
66             auto targetRender = targetElement->GetRenderNode();
67             if (!targetRender) {
68                 return;
69             }
70             auto targetSize_ = targetRender->GetLayoutSize();
71             auto targetOffset_ = targetRender->GetGlobalOffset();
72             popup_->GetPopupParam()->SetTargetSize(targetSize_);
73             popup_->GetPopupParam()->SetTargetOffset(
74                 targetOffset_ + context->GetDisplayWindowRectInfo().GetOffset());
75         }
76     }
77     if (popup_->GetPopupParam()->IsShowInSubWindow()) {
78         if (popup_->GetPopupParam()->IsShow()) {
79             if (!hasShown_ && ShowPopupInSubWindow()) {
80                 showId_ = GetId();
81                 OnStateChange(true);
82             }
83         } else {
84             if (hasShown_ && CancelPopupInSubWindow(showId_)) {
85                 showId_.clear();
86                 OnStateChange(false);
87             }
88         }
89         return;
90     }
91     if (popup_->GetPopupParam()->IsShow()) {
92         if (!hasShown_ && ShowPopup()) {
93             showId_ = GetId();
94             OnStateChange(true);
95         }
96     } else {
97         if (hasShown_ && CancelPopup(showId_)) {
98             showId_.clear();
99             OnStateChange(false);
100         }
101     }
102 }
103 
IsDeclarative()104 bool PopupElementV2::IsDeclarative()
105 {
106     auto context = context_.Upgrade();
107     if (!context) {
108         return false;
109     }
110 
111     return context->GetIsDeclarative();
112 }
113 
OnStateChange(bool isVisible)114 void PopupElementV2::OnStateChange(bool isVisible)
115 {
116     if (hasShown_ == isVisible) {
117         return;
118     }
119     hasShown_ = isVisible;
120 
121     if (changeEvent_ && !isVisible) {
122         changeEvent_("false");
123         return;
124     }
125 
126     if (!onStateChange_) {
127         return;
128     }
129     auto json = JsonUtil::Create(true);
130     json->Put("isVisible", isVisible);
131     onStateChange_(json->ToString());
132 }
133 
134 } // namespace OHOS::Ace