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 "requirement_pangesture_test.h"
17
18 #include <arkui/native_gesture.h>
19 #include <cstdint>
20 #include <string>
21
22 #include "../manager/plugin_manager.h"
23 #include "swiper_component.h"
24
25 namespace ArkUICapiTest {
26
27 ArkUI_NativeNodeAPI_1* RequirementPanGestureTest::nodeAPI = nullptr;
28
OnEventGesture(ArkUI_GestureEvent * event,void * extraParams)29 static void OnEventGesture(ArkUI_GestureEvent* event, void* extraParams)
30 {
31 auto swipe = OH_ArkUI_GestureEvent_GetNode(event);
32 ArkUI_NumberValue ColorValue[] = { { .u32 = COLOR_BLACK } };
33 ArkUI_AttributeItem ColorItem = { ColorValue, sizeof(ColorValue) / sizeof(ArkUI_NumberValue) };
34 RequirementPanGestureTest::nodeAPI->setAttribute(swipe, NODE_BACKGROUND_COLOR, &ColorItem);
35 }
36
CreateTextNodeToRequirement(uint32_t color)37 static std::shared_ptr<TextComponent> CreateTextNodeToRequirement(uint32_t color)
38 {
39 auto text = std::make_shared<TextComponent>();
40 text->SetWidth(SIZE_100);
41 text->SetHeight(SIZE_150);
42 text->SetBackgroundColor(color);
43 text->SetTextAlign(ARKUI_TEXT_ALIGNMENT_CENTER);
44 return text;
45 }
46
CreateSwiperNodeToFirstRequirement(ArkUI_NativeGestureAPI_1 * gestureAPI,const std::string & id)47 static auto CreateSwiperNodeToFirstRequirement(ArkUI_NativeGestureAPI_1* gestureAPI, const std::string& id)
48 {
49 auto swiper = std::make_shared<SwiperComponent>();
50 swiper->SetHeight(SIZE_100);
51 swiper->SetWidth(SIZE_150);
52 swiper->SetBorderWidth(PARAM_1);
53 swiper->SetId(id);
54 swiper->SetMargin(DEFAULT_MARGIN);
55 auto panGesture = gestureAPI->createPanGesture(1, GESTURE_DIRECTION_ALL, 5);
56 gestureAPI->setGestureEventTarget(panGesture, GESTURE_EVENT_ACTION_ACCEPT, nullptr, &OnEventGesture);
57 gestureAPI->addGestureToNode(swiper->GetComponent(), panGesture, NORMAL, NORMAL_GESTURE_MASK);
58 return swiper;
59 }
60
CreateSwiperNodeToSecondRequirement(ArkUI_NativeGestureAPI_1 * gestureAPI,const std::string & id)61 static auto CreateSwiperNodeToSecondRequirement(ArkUI_NativeGestureAPI_1* gestureAPI, const std::string& id)
62 {
63 auto swiper = std::make_shared<SwiperComponent>();
64 auto text1 = CreateTextNodeToRequirement(COLOR_GREEN);
65 auto text2 = CreateTextNodeToRequirement(COLOR_BLUE);
66 auto text3 = CreateTextNodeToRequirement(COLOR_RED);
67 std::vector<std::shared_ptr<TextComponent>> texts = { text1, text2, text3 };
68 swiper->SetHeight(SIZE_100);
69 swiper->SetWidth(SIZE_150);
70 swiper->SetBorderWidth(PARAM_1);
71 swiper->SetId(id);
72 swiper->SetMargin(DEFAULT_MARGIN);
73 for (auto text : texts) {
74 swiper->AddChild(text);
75 }
76 auto panGesture = gestureAPI->createPanGesture(1, GESTURE_DIRECTION_ALL, 5);
77 gestureAPI->setGestureEventTarget(panGesture, GESTURE_EVENT_ACTION_ACCEPT, nullptr, &OnEventGesture);
78 gestureAPI->addGestureToNode(swiper->GetComponent(), panGesture, NORMAL, NORMAL_GESTURE_MASK);
79 return swiper;
80 }
createGesture(ArkUI_NativeGestureAPI_1 * gestureAPI,ArkUI_NodeHandle columnP1,ArkUI_NodeHandle columnP2)81 static void createGesture(ArkUI_NativeGestureAPI_1* gestureAPI, ArkUI_NodeHandle columnP1, ArkUI_NodeHandle columnP2)
82 {
83 RequirementPanGestureTest::nodeAPI->addChild(
84 columnP1, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper1")->GetComponent());
85 RequirementPanGestureTest::nodeAPI->addChild(
86 columnP1, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper2")->GetComponent());
87 RequirementPanGestureTest::nodeAPI->addChild(
88 columnP1, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper3")->GetComponent());
89 RequirementPanGestureTest::nodeAPI->addChild(
90 columnP1, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper4")->GetComponent());
91 RequirementPanGestureTest::nodeAPI->addChild(
92 columnP1, CreateSwiperNodeToSecondRequirement(gestureAPI, "swiper5")->GetComponent());
93 RequirementPanGestureTest::nodeAPI->addChild(
94 columnP2, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper6")->GetComponent());
95 RequirementPanGestureTest::nodeAPI->addChild(
96 columnP2, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper7")->GetComponent());
97 RequirementPanGestureTest::nodeAPI->addChild(
98 columnP2, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper8")->GetComponent());
99 RequirementPanGestureTest::nodeAPI->addChild(
100 columnP2, CreateSwiperNodeToFirstRequirement(gestureAPI, "swiper9")->GetComponent());
101 RequirementPanGestureTest::nodeAPI->addChild(
102 columnP2, CreateSwiperNodeToSecondRequirement(gestureAPI, "swiper10")->GetComponent());
103 }
CreateNativeNode(napi_env env,napi_callback_info info)104 napi_value RequirementPanGestureTest::CreateNativeNode(napi_env env, napi_callback_info info)
105 {
106 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "RequirementPanGestureTest", "CreateNativeNode");
107
108 size_t argc = 1;
109 napi_value args[1] = { nullptr };
110 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
111 size_t length = 64;
112 size_t strLength = 0;
113 char xComponentID[64] = { 0 };
114 napi_get_value_string_utf8(env, args[0], xComponentID, length, &strLength);
115
116 if ((env == nullptr) || (info == nullptr)) {
117 OH_LOG_Print(
118 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "RequirementPanGestureTest", "GetContext env or info is null");
119 return nullptr;
120 }
121
122 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, RequirementPanGestureTest::nodeAPI);
123 ArkUI_NativeGestureAPI_1* gestureAPI = nullptr;
124 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_GESTURE, ArkUI_NativeGestureAPI_1, gestureAPI);
125
126 auto columnP1 = RequirementPanGestureTest::nodeAPI->createNode(ARKUI_NODE_COLUMN);
127 auto columnP2 = RequirementPanGestureTest::nodeAPI->createNode(ARKUI_NODE_COLUMN);
128 createGesture(gestureAPI, columnP1, columnP2);
129 auto row = RequirementPanGestureTest::nodeAPI->createNode(ARKUI_NODE_ROW);
130 RequirementPanGestureTest::nodeAPI->addChild(row, columnP1);
131 RequirementPanGestureTest::nodeAPI->addChild(row, columnP2);
132
133 std::string id(xComponentID);
134 if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), row) ==
135 INVALID_PARAM) {
136 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "RequirementPanGestureTest",
137 "OH_NativeXComponent_AttachNativeRootNode failed");
138 }
139
140 napi_value exports;
141 if (napi_create_object(env, &exports) != napi_ok) {
142 napi_throw_type_error(env, NULL, "napi_create_object failed");
143 return nullptr;
144 }
145
146 return exports;
147 }
148 } // namespace ArkUICapiTest