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 <string>
17
18 #include "interfaces/napi/kits/utils/napi_utils.h"
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "native_engine/native_value.h"
22
23 #include "frameworks/bridge/common/utils/componentInfo.h"
24 #include "frameworks/bridge/common/utils/engine_helper.h"
25
26 namespace OHOS::Ace::Napi {
27 namespace {
28 constexpr size_t STR_BUFFER_SIZE = 1024;
29 constexpr size_t ARGC_ACTIVATE_PARAMTER = 2;
30 }
31
JSClearFocus(napi_env env,napi_callback_info info)32 static napi_value JSClearFocus(napi_env env, napi_callback_info info)
33 {
34 auto delegate = EngineHelper::GetCurrentDelegateSafely();
35 if (!delegate) {
36 return nullptr;
37 }
38 delegate->ResetFocus();
39 return nullptr;
40 }
41
JSRequestFocus(napi_env env,napi_callback_info info)42 static napi_value JSRequestFocus(napi_env env, napi_callback_info info)
43 {
44 size_t argc = 1;
45 napi_value argv = nullptr;
46 napi_value thisVar = nullptr;
47 void* data = nullptr;
48 napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data);
49 NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
50 napi_valuetype type = napi_undefined;
51 napi_typeof(env, argv, &type);
52 NAPI_ASSERT(env, type == napi_string, "the type of arg is not string");
53 char outBuffer[STR_BUFFER_SIZE] = { 0 };
54 size_t outSize = 0;
55 napi_get_value_string_utf8(env, argv, outBuffer, STR_BUFFER_SIZE, &outSize);
56 std::string key = std::string(outBuffer);
57
58 napi_value obj = nullptr;
59 auto delegate = EngineHelper::GetCurrentDelegateSafely();
60 if (!delegate) {
61 napi_get_boolean(env, false, &obj);
62 return obj;
63 }
64 auto focusCallback = [env](NG::RequestFocusResult result) {
65 switch (result) {
66 case NG::RequestFocusResult::NON_FOCUSABLE:
67 NapiThrow(env, "This component is not focusable.", ERROR_CODE_NON_FOCUSABLE);
68 break;
69 case NG::RequestFocusResult::NON_FOCUSABLE_ANCESTOR:
70 NapiThrow(env, "This component has unfocusable ancestor.", ERROR_CODE_NON_FOCUSABLE_ANCESTOR);
71 break;
72 case NG::RequestFocusResult::NON_EXIST:
73 NapiThrow(env,
74 "The component doesn't exist, is currently invisible, or has been disabled.",
75 ERROR_CODE_NON_EXIST);
76 break;
77 default:
78 NapiThrow(env, "An internal error occurred.", ERROR_CODE_INTERNAL_ERROR);
79 break;
80 }
81 };
82 delegate->SetRequestFocusCallback(focusCallback);
83 delegate->RequestFocus(key, true);
84 delegate->ResetRequestFocusCallback();
85 napi_get_null(env, &obj);
86 return obj;
87 }
88
JsSetAutoFocusTransfer(napi_env env,napi_callback_info info)89 static napi_value JsSetAutoFocusTransfer(napi_env env, napi_callback_info info)
90 {
91 auto delegate = EngineHelper::GetCurrentDelegateSafely();
92 if (!delegate) {
93 return nullptr;
94 }
95 napi_value argv[1] = { 0 };
96 napi_valuetype valueType = napi_undefined;
97 if (!GetSingleParam(env, info, argv, valueType) || (valueType != napi_boolean)) {
98 return nullptr;
99 }
100 bool isAutoFocusTransfer = true;
101 napi_get_value_bool(env, argv[0], &isAutoFocusTransfer);
102 delegate->SetAutoFocusTransfer(isAutoFocusTransfer);
103 return nullptr;
104 }
105
JSActivate(napi_env env,napi_callback_info info)106 static napi_value JSActivate(napi_env env, napi_callback_info info)
107 {
108 size_t argc = ARGC_ACTIVATE_PARAMTER;
109 napi_value argv[ARGC_ACTIVATE_PARAMTER] = { nullptr };
110 napi_value thisVar = nullptr;
111 void* data = nullptr;
112 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
113 NAPI_ASSERT(env, argc >= 1, "requires at least 1 parameter");
114 napi_valuetype type = napi_undefined;
115 napi_typeof(env, argv[0], &type);
116 NAPI_ASSERT(env, type == napi_boolean, "the type of argv[0] is not bool");
117 bool isActive = false;
118 napi_get_value_bool(env, argv[0], &isActive);
119
120 bool autoInactive = true;
121 if (argc == ARGC_ACTIVATE_PARAMTER) {
122 napi_typeof(env, argv[1], &type);
123 NAPI_ASSERT(env, type == napi_boolean, "the type of argv[1] is not bool");
124 napi_get_value_bool(env, argv[1], &autoInactive);
125 }
126
127 napi_value obj = nullptr;
128 auto delegate = EngineHelper::GetCurrentDelegateSafely();
129 if (!delegate) {
130 napi_get_boolean(env, false, &obj);
131 return obj;
132 }
133 delegate->Activate(isActive, autoInactive);
134 napi_get_null(env, &obj);
135 return obj;
136 }
137
registerFunc(napi_env env,napi_value exports)138 static napi_value registerFunc(napi_env env, napi_value exports)
139 {
140 napi_property_descriptor animatorDesc[] = {
141 DECLARE_NAPI_FUNCTION("clearFocus", JSClearFocus),
142 DECLARE_NAPI_FUNCTION("requestFocus", JSRequestFocus),
143 DECLARE_NAPI_FUNCTION("setAutoFocusTransfer", JsSetAutoFocusTransfer),
144 DECLARE_NAPI_FUNCTION("activate", JSActivate),
145 };
146 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(animatorDesc) / sizeof(animatorDesc[0]), animatorDesc));
147 return exports;
148 }
149
150 static napi_module focusControllerModule = {
151 .nm_version = 1,
152 .nm_flags = 0,
153 .nm_filename = nullptr,
154 .nm_register_func = registerFunc,
155 .nm_modname = "arkui.focusController",
156 .nm_priv = ((void*)0),
157 .reserved = { 0 },
158 };
159
FocusControllerRegister()160 extern "C" __attribute__((constructor)) void FocusControllerRegister()
161 {
162 napi_module_register(&focusControllerModule);
163 }
164 } // namespace OHOS::Ace::Napi