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