1 /*
2 * Copyright (c) 2025 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 #ifndef DIALOG_C_CREATEDIALOGEXAMPLE_H
17 #define DIALOG_C_CREATEDIALOGEXAMPLE_H
18
19 #include "ArkUIBaseNode.h"
20 #include "ArkUIButtonNode.h"
21 #include "ArkUITextNode.h"
22 #include "ArkUIColumnNode.h"
23 #include <arkui/native_dialog.h>
24 #include <hilog/log.h>
25
26 namespace NativeModule {
27 ArkUI_NativeDialogHandle dialogController;
28
ShowDialog()29 void ShowDialog() {
30 ArkUI_NativeDialogAPI_1 *dialogAPI = reinterpret_cast<ArkUI_NativeDialogAPI_1 *>(
31 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_DIALOG, "ArkUI_NativeDialogAPI_1"));
32 if (!dialogController) {
33 dialogController = dialogAPI->create();
34 }
35 auto textNode = std::make_shared<ArkUITextNode>();
36 textNode->SetTextContent("这是一个弹窗");
37 textNode->SetWidth(300);
38 textNode->SetHeight(300);
39 dialogAPI->setContent(dialogController, textNode->GetHandle());
40 dialogAPI->setContentAlignment(dialogController, static_cast<int32_t>(ARKUI_ALIGNMENT_CENTER), 0, 0);
41 dialogAPI->setBackgroundColor(dialogController, 0xffffffff);
42 dialogAPI->setCornerRadius(dialogController, 6, 6, 6, 6);
43 dialogAPI->setModalMode(dialogController, true);
44 dialogAPI->setAutoCancel(dialogController, true);
45 dialogAPI->show(dialogController, false);
46 }
47
CreateDialogExample()48 std::shared_ptr<ArkUIBaseNode> CreateDialogExample() {
49 // 创建组件并挂载
50 auto column = std::make_shared<ArkUIColumnNode>();
51 column->SetWidth(1, true);
52 column->SetHeight(1, true);
53 column->SetBackgroundColor(0x33ff0000);
54 column->SetPadding(20, false);
55 auto button = std::make_shared<ArkUIButtonNode>();
56 button->SetButtonLabel("打开弹窗");
57 button->RegisterOnClick([]() {
58 ShowDialog();
59 });
60 column->AddChild(button);
61
62 return column;
63 }
64 } // namespace NativeModule
65 #endif //DIALOG_C_CREATEDIALOGEXAMPLE_H
66