• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "drag_getModifierKeyStates.h"
17 #include "../manager/plugin_manager.h"
18 #include <string>
19 
20 namespace ArkUICapiTest {
21 
createChildNode(ArkUI_NativeNodeAPI_1 * nodeAPI,bool enabled)22 static auto createChildNode(ArkUI_NativeNodeAPI_1 *nodeAPI, bool enabled)
23 {
24     auto nodeHandle = nodeAPI->createNode(ARKUI_NODE_TEXT);
25     ArkUI_AttributeItem LABEL_Item = {.string = "drag_getModifierKeyStates Test"};
26     ArkUI_NumberValue fontSize[] = {20};
27     ArkUI_AttributeItem Font_Item = {fontSize, 1};
28     ArkUI_NumberValue marginValue[] = {20};
29     ArkUI_AttributeItem marginItem = {marginValue, 1};
30     nodeAPI->setAttribute(nodeHandle, NODE_TEXT_CONTENT, &LABEL_Item);
31     nodeAPI->setAttribute(nodeHandle, NODE_TEXT_FONT, &Font_Item);
32     nodeAPI->setAttribute(nodeHandle, NODE_MARGIN, &marginItem);
33     nodeAPI->registerNodeEvent(nodeHandle, NODE_ON_DRAG_START, 1, nullptr);
34     OH_ArkUI_SetNodeDraggable(nodeHandle, true);
35 
36     return nodeHandle;
37 }
38 
OnEventReceive(ArkUI_NodeEvent * event)39 static void OnEventReceive(ArkUI_NodeEvent *event)
40 {
41     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getModifierKeyStates", "OnEventReceive");
42     if (event == nullptr) {
43         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "getModifierKeyStates", "OnEventReceive: event is null");
44         return;
45     }
46 
47     auto dragEvent = OH_ArkUI_NodeEvent_GetDragEvent(event);
48     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getModifierKeyStates", "OnEventReceive : %{public}p",
49                  dragEvent);
50 
51     ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
52     OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
53     auto nodeHandler = OH_ArkUI_NodeEvent_GetNodeHandle(event);
54 
55     uint64_t pressedKeys = 0;
56     auto result = OH_ArkUI_DragEvent_GetModifierKeyStates(dragEvent, &pressedKeys);
57     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getModifierKeyStates",
58                  "Drag getModifierKeyStates result : %{public}d", result);
59     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getModifierKeyStates",
60                  "Drag getModifierKeyStates pressedKeys : %{public}llu", pressedKeys);
61     if (result == 0 && pressedKeys == 0) {
62         ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
63         ArkUI_AttributeItem background_color_item = {background_color_value,
64                                                      sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
65         nodeAPI->setAttribute(nodeHandler, NODE_BACKGROUND_COLOR, &background_color_item);
66     } else {
67         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getModifierKeyStates",
68                      "Drag getModifierKeyStates result : %{public}d", result);
69     }
70 }
71 
CreateNativeNode(napi_env env,napi_callback_info info)72 napi_value GetModifierKeyStatesTest::CreateNativeNode(napi_env env, napi_callback_info info)
73 {
74     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getModifierKeyStates", "CreateNativeNode");
75 
76     size_t argc = PARAM_1;
77     napi_value args[PARAM_1] = {nullptr};
78     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
79     size_t length = PARAM_64;
80     size_t strLength = PARAM_0;
81     char xComponentID[PARAM_64] = {PARAM_0};
82     napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
83 
84     if ((env == nullptr) || (info == nullptr)) {
85         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "getModifierKeyStates", "GetContext env or info is null");
86         return nullptr;
87     }
88 
89     ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
90     OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
91     auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
92     auto text1 = createChildNode(nodeAPI, true);
93     auto text2 = createChildNode(nodeAPI, true);
94 
95     ArkUI_AttributeItem id_item = {};
96     id_item.string = "GetModifierKeyStates";
97     nodeAPI->setAttribute(text1, NODE_ID, &id_item);
98     id_item.string = "textDragTest2";
99     nodeAPI->setAttribute(text2, NODE_ID, &id_item);
100 
101     nodeAPI->addChild(column, text1);
102     nodeAPI->addChild(column, text2);
103 
104     nodeAPI->registerNodeEventReceiver(&OnEventReceive);
105 
106     std::string id(xComponentID);
107     if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), column) ==
108         INVALID_PARAM) {
109         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "getModifierKeyStates",
110                      "OH_NativeXComponent_AttachNativeRootNode failed");
111     }
112 
113     napi_value exports;
114     if (napi_create_object(env, &exports) != napi_ok) {
115         napi_throw_type_error(env, nullptr, "napi_create_object failed");
116         return nullptr;
117     }
118 
119     return exports;
120 }
121 } // namespace ArkUICapiTest