1 /*
2  * Copyright (c) 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 #include "core/components_ng/pattern/dialog/dialog_view.h"
16 
17 #include <string>
18 
19 #include "base/memory/ace_type.h"
20 #include "base/memory/referenced.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/ui_node.h"
24 #include "core/components_ng/pattern/dialog/dialog_pattern.h"
25 #include "core/components_v2/inspector/inspector_constants.h"
26 #include "core/pipeline/base/element_register.h"
27 #include "core/pipeline_ng/pipeline_context.h"
28 
29 namespace OHOS::Ace::NG {
CreateDialogNode(const DialogProperties & param,const RefPtr<UINode> & customNode=nullptr)30 RefPtr<FrameNode> DialogView::CreateDialogNode(
31     const DialogProperties& param, const RefPtr<UINode>& customNode = nullptr)
32 {
33     auto pipeline = PipelineBase::GetCurrentContext();
34     CHECK_NULL_RETURN(pipeline, nullptr);
35     auto dialogTheme = pipeline->GetTheme<DialogTheme>();
36     CHECK_NULL_RETURN(dialogTheme, nullptr);
37 
38     std::string tag;
39     switch (param.type) {
40         case DialogType::ALERT_DIALOG: {
41             tag = V2::ALERT_DIALOG_ETS_TAG;
42             break;
43         }
44         case DialogType::ACTION_SHEET: {
45             tag = V2::ACTION_SHEET_DIALOG_ETS_TAG;
46             break;
47         }
48         default:
49             tag = V2::DIALOG_ETS_TAG;
50             break;
51     }
52     auto nodeId = ElementRegister::GetInstance()->MakeUniqueId();
53     ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", tag.c_str(), nodeId);
54     RefPtr<FrameNode> dialog = FrameNode::CreateFrameNode(tag, nodeId,
55         AceType::MakeRefPtr<DialogPattern>(dialogTheme, customNode));
56     if (customNode) {
57         customNode->Build(nullptr);
58     }
59     // update layout and render props
60     auto dialogLayoutProp = AceType::DynamicCast<DialogLayoutProperty>(dialog->GetLayoutProperty());
61     CHECK_NULL_RETURN(dialogLayoutProp, dialog);
62     dialogLayoutProp->UpdateDialogAlignment(param.alignment);
63     dialogLayoutProp->UpdateDialogOffset(param.offset);
64     dialogLayoutProp->UpdateUseCustomStyle(param.customStyle);
65     dialogLayoutProp->UpdateAutoCancel(param.autoCancel);
66     dialogLayoutProp->UpdateShowInSubWindow(param.isShowInSubWindow);
67     dialogLayoutProp->UpdateDialogButtonDirection(param.buttonDirection);
68     dialogLayoutProp->UpdateIsModal(param.isModal);
69     dialogLayoutProp->UpdateIsScenceBoardDialog(param.isScenceBoardDialog);
70     dialogLayoutProp->UpdateEnableHoverMode(param.enableHoverMode);
71     if (param.width.has_value() && NonNegative(param.width.value().Value())) {
72         dialogLayoutProp->UpdateWidth(param.width.value());
73     } else {
74         dialogLayoutProp->UpdateGridCount(param.gridCount);
75     }
76     if (param.height.has_value() && NonNegative(param.height.value().Value())) {
77         dialogLayoutProp->UpdateHeight(param.height.value());
78     }
79     if (param.hoverModeArea.has_value()) {
80         dialogLayoutProp->UpdateHoverModeArea(param.hoverModeArea.value());
81     }
82     // create gray background
83     auto dialogContext = dialog->GetRenderContext();
84     CHECK_NULL_RETURN(dialogContext, dialog);
85     auto pattern = dialog->GetPattern<DialogPattern>();
86     CHECK_NULL_RETURN(pattern, dialog);
87     pattern->SetDialogProperties(param);
88 
89     auto isSubWindow = dialogLayoutProp->GetShowInSubWindowValue(false) && !pattern->IsUIExtensionSubWindow();
90     if ((isSubWindow && dialogLayoutProp->GetIsModal().value_or(true)) ||
91         !dialogLayoutProp->GetIsModal().value_or(true)) {
92         dialogContext->UpdateBackgroundColor(Color(0x00000000));
93     } else {
94         dialogContext->UpdateBackgroundColor(param.maskColor.value_or(dialogTheme->GetMaskColorEnd()));
95     }
96     if (dialogLayoutProp->GetIsScenceBoardDialog().value_or(false)) {
97         dialogContext->UpdateBackgroundColor(param.maskColor.value_or(dialogTheme->GetMaskColorEnd()));
98     }
99     // set onCancel callback
100     auto hub = dialog->GetEventHub<DialogEventHub>();
101     CHECK_NULL_RETURN(hub, dialog);
102     hub->SetOnCancel(param.onCancel);
103     hub->SetOnSuccess(param.onSuccess);
104 
105     pattern->BuildChild(param);
106     pattern->SetOnWillDismiss(param.onWillDismiss);
107     pattern->SetOnWillDismissByNDK(param.onWillDismissCallByNDK);
108 
109     if (param.transitionEffect != nullptr) {
110         dialogContext->UpdateChainedTransition(param.transitionEffect);
111     } else {
112         // set open and close animation
113         pattern->SetOpenAnimation(param.openAnimation);
114         pattern->SetCloseAnimation(param.closeAnimation);
115     }
116 
117     dialog->MarkModifyDone();
118     return dialog;
119 }
120 
121 } // namespace OHOS::Ace::NG
122