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
57 if (customNode) {
58 customNode->Build(nullptr);
59 }
60
61 // update layout and render props
62 auto dialogLayoutProp = AceType::DynamicCast<DialogLayoutProperty>(dialog->GetLayoutProperty());
63 CHECK_NULL_RETURN(dialogLayoutProp, dialog);
64 dialogLayoutProp->UpdateDialogAlignment(param.alignment);
65 dialogLayoutProp->UpdateDialogOffset(param.offset);
66 dialogLayoutProp->UpdateGridCount(param.gridCount);
67 dialogLayoutProp->UpdateUseCustomStyle(param.customStyle);
68 dialogLayoutProp->UpdateAutoCancel(param.autoCancel);
69 dialogLayoutProp->UpdateShowInSubWindow(param.isShowInSubWindow);
70 dialogLayoutProp->UpdateDialogButtonDirection(param.buttonDirection);
71 dialogLayoutProp->UpdateIsModal(param.isModal);
72 dialogLayoutProp->UpdateIsScenceBoardDialog(param.isScenceBoardDialog);
73 // create gray background
74 auto dialogContext = dialog->GetRenderContext();
75 CHECK_NULL_RETURN(dialogContext, dialog);
76 if ((dialogLayoutProp->GetShowInSubWindowValue(false) && dialogLayoutProp->GetIsModal().value_or(true)) ||
77 !dialogLayoutProp->GetIsModal().value_or(true)) {
78 dialogContext->UpdateBackgroundColor(param.maskColor.value_or(Color(0x00000000)));
79 } else {
80 dialogContext->UpdateBackgroundColor(param.maskColor.value_or(dialogTheme->GetMaskColorEnd()));
81 }
82 if (dialogLayoutProp->GetIsScenceBoardDialog().value_or(false)) {
83 dialogContext->UpdateBackgroundColor(param.maskColor.value_or(dialogTheme->GetMaskColorEnd()));
84 }
85 // set onCancel callback
86 auto hub = dialog->GetEventHub<DialogEventHub>();
87 CHECK_NULL_RETURN(hub, nullptr);
88 hub->SetOnCancel(param.onCancel);
89 hub->SetOnSuccess(param.onSuccess);
90
91 auto pattern = dialog->GetPattern<DialogPattern>();
92 CHECK_NULL_RETURN(pattern, nullptr);
93 pattern->BuildChild(param);
94
95 // set open and close animation
96 pattern->SetOpenAnimation(param.openAnimation);
97 pattern->SetCloseAnimation(param.closeAnimation);
98
99 pattern->SetDialogProperties(param);
100
101 dialog->MarkModifyDone();
102 return dialog;
103 }
104
105 } // namespace OHOS::Ace::NG
106