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 #include "datepicker_test.h"
17 #include "../manager/plugin_manager.h"
18 #include <arkui/native_interface.h>
19 #include <arkui/native_node.h>
20 #include <arkui/native_type.h>
21 #include <string>
22
23 namespace ArkUIDatePickerCApiDemo {
24
25 #define ON_CLICK_EVENT_ID_01 6101
26 #define ON_CLICK_EVENT_ID_02 6102
27 #define ON_CLICK_EVENT_ID_03 6103
28
29 #define BUTTON_LUNAR 1
30 #define BUTTON_CAN_LOOP 2
31 #define BUTTON_MODE 3
32
33 std::vector<int> g_loopValues = {1, 0, -1};
34 int g_loopIndex = 0;
35 int g_modeCount = 3;
36 bool g_isLunar = false;
37 int g_mode = 0;
38
SetAttributeInt32(ArkUI_NativeNodeAPI_1 * nodeAPI,ArkUI_NodeHandle node,ArkUI_NodeAttributeType attr,int32_t value)39 void SetAttributeInt32(ArkUI_NativeNodeAPI_1* nodeAPI, ArkUI_NodeHandle node,
40 ArkUI_NodeAttributeType attr, int32_t value)
41 {
42 ArkUI_NumberValue val[] = {{.i32 = value}};
43 ArkUI_AttributeItem item = {val, sizeof(val) / sizeof(ArkUI_NumberValue)};
44 nodeAPI->setAttribute(node, attr, &item);
45 }
46
UpdateButtonLabel(ArkUI_NativeNodeAPI_1 * nodeAPI,ArkUI_NodeHandle parent,int32_t childIndex,const char * text)47 void UpdateButtonLabel(ArkUI_NativeNodeAPI_1* nodeAPI, ArkUI_NodeHandle parent,
48 int32_t childIndex, const char* text)
49 {
50 auto button = nodeAPI->getChildAt(parent, childIndex);
51 ArkUI_AttributeItem text_item = {nullptr, 0};
52 text_item.string = text;
53 nodeAPI->setAttribute(button, NODE_BUTTON_LABEL, &text_item);
54 }
55
OnEventReceive(ArkUI_NodeEvent * event)56 static void OnEventReceive(ArkUI_NodeEvent *event)
57 {
58 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "ButtonFontScaleTest", "OnEventReceive");
59 if (!event) {
60 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ButtonFontScaleTest", "OnEventReceive: event is null");
61 return;
62 }
63
64 ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
65 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
66 auto nodeHandler = OH_ArkUI_NodeEvent_GetNodeHandle(event);
67 auto column = nodeAPI->getParent(nodeHandler);
68 auto test_datePicker = nodeAPI->getChildAt(column, 0);
69
70 switch (OH_ArkUI_NodeEvent_GetTargetId(event)) {
71 case ON_CLICK_EVENT_ID_01: {
72 g_isLunar = !g_isLunar;
73 SetAttributeInt32(nodeAPI, test_datePicker, NODE_DATE_PICKER_LUNAR, g_isLunar);
74 UpdateButtonLabel(nodeAPI, column, BUTTON_LUNAR, g_isLunar ? "切换为公历" : "切换为农历");
75 break;
76 }
77 case ON_CLICK_EVENT_ID_02: {
78 g_loopIndex = (g_loopIndex + 1) % g_loopValues.size();
79 SetAttributeInt32(nodeAPI, test_datePicker, NODE_DATE_PICKER_CAN_LOOP, g_loopValues[g_loopIndex]);
80 UpdateButtonLabel(nodeAPI, column, BUTTON_CAN_LOOP,
81 g_loopIndex == 0 ? "切换循环:true"
82 : g_loopIndex == 1 ? "切换循环:false"
83 : "切换循环:异常");
84 break;
85 }
86 case ON_CLICK_EVENT_ID_03: {
87 g_mode = (g_mode + 1) % g_modeCount;
88 SetAttributeInt32(nodeAPI, test_datePicker, NODE_DATE_PICKER_MODE, g_mode);
89 UpdateButtonLabel(nodeAPI, column, BUTTON_MODE,
90 g_mode == 0 ? "切换为年月模式"
91 : g_mode == 1 ? "切换为月日模式"
92 : "切换为年月日模式");
93 break;
94 }
95 default:
96 return;
97 }
98 }
99
createButton(ArkUI_NativeNodeAPI_1 * nodeAPI,const char * text)100 ArkUI_NodeHandle createButton(ArkUI_NativeNodeAPI_1 *nodeAPI, const char *text)
101 {
102 auto btn = nodeAPI->createNode(ARKUI_NODE_BUTTON);
103 ArkUI_NumberValue height_value1[] = {{.f32 = 50}};
104 ArkUI_AttributeItem height_item1 = {height_value1, sizeof(height_value1) / sizeof(ArkUI_NumberValue)};
105 ArkUI_NumberValue width_value1[] = {{.f32 = 150}};
106 ArkUI_AttributeItem width_item1 = {width_value1, sizeof(width_value1) / sizeof(ArkUI_NumberValue)};
107
108 nodeAPI->setAttribute(btn, NODE_WIDTH, &width_item1);
109 nodeAPI->setAttribute(btn, NODE_HEIGHT, &height_item1);
110
111 ArkUI_AttributeItem textItem = {.string = text};
112 nodeAPI->setAttribute(btn, NODE_BUTTON_LABEL, &textItem);
113
114 ArkUI_NumberValue marginVal[] = {{.f32 = DEFAULT_MARGIN}};
115 ArkUI_AttributeItem marginItem = {marginVal, 1};
116 nodeAPI->setAttribute(btn, NODE_MARGIN, &marginItem);
117 return btn;
118 };
119
CreateNativeNode(napi_env env,napi_callback_info info)120 napi_value DatePickerTest::CreateNativeNode(napi_env env, napi_callback_info info)
121 {
122 // 参数校验和初始化
123 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "DatePickerTest", "CreateNativeNode");
124 if (!env || !info) {
125 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DatePickerTest", "Invalid env or info");
126 return nullptr;
127 }
128
129 // 获取XComponentID
130 char xComponentID[PARAM_64] = {0};
131 napi_value args[PARAM_1] = {nullptr};
132 size_t argc = PARAM_1;
133 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
134 napi_get_value_string_utf8(env, args[0], xComponentID, PARAM_64, nullptr);
135
136 // 创建节点API实例
137 ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
138 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
139
140 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
141 auto datePicker = nodeAPI->createNode(ARKUI_NODE_DATE_PICKER);
142 auto buttons = {createButton(nodeAPI, "切换为农历"), createButton(nodeAPI, "切换循环:true"),
143 createButton(nodeAPI, "切换为年月模式")};
144
145 // 配置日期选择器
146 ArkUI_NumberValue modeVal[] = {{.i32 = ARKUI_DATEPICKER_MODE_DATE}};
147 ArkUI_AttributeItem modeItem = {modeVal, 1};
148 nodeAPI->setAttribute(datePicker, NODE_DATE_PICKER_MODE, &modeItem);
149
150 ArkUI_AttributeItem selected = {.string = "2024-01-01"};
151 ArkUI_AttributeItem start = {.string = "2024-01-01"};
152 ArkUI_AttributeItem end = {.string = "2025-12-31"};
153 nodeAPI->setAttribute(datePicker, NODE_DATE_PICKER_SELECTED, &selected);
154 nodeAPI->setAttribute(datePicker, NODE_DATE_PICKER_START, &start);
155 nodeAPI->setAttribute(datePicker, NODE_DATE_PICKER_END, &end);
156
157 // 组装节点树
158 nodeAPI->addChild(column, datePicker);
159 for (auto btn : buttons) {
160 nodeAPI->addChild(column, btn);
161 }
162
163 // 注册事件
164 nodeAPI->registerNodeEventReceiver(&OnEventReceive);
165 int eventId = ON_CLICK_EVENT_ID_01;
166 for (auto btn : buttons) {
167 nodeAPI->registerNodeEvent(btn, NODE_ON_CLICK, eventId++, nullptr);
168 }
169
170 // 挂载到XComponent
171 std::string id(xComponentID);
172 OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), column);
173 return nullptr;
174 }
175 } // namespace ArkUIDatePickerCApiDemo
176