• 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 <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 }
30 
JSClearFocus(napi_env env,napi_callback_info info)31 static napi_value JSClearFocus(napi_env env, napi_callback_info info)
32 {
33     auto delegate = EngineHelper::GetCurrentDelegateSafely();
34     if (!delegate) {
35         return nullptr;
36     }
37     delegate->ResetFocus();
38     return nullptr;
39 }
40 
JSRequestFocus(napi_env env,napi_callback_info info)41 static napi_value JSRequestFocus(napi_env env, napi_callback_info info)
42 {
43     size_t argc = 1;
44     napi_value argv = nullptr;
45     napi_value thisVar = nullptr;
46     void* data = nullptr;
47     napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data);
48     NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
49     napi_valuetype type = napi_undefined;
50     napi_typeof(env, argv, &type);
51     NAPI_ASSERT(env, type == napi_string, "the type of arg is not string");
52     char outBuffer[STR_BUFFER_SIZE] = { 0 };
53     size_t outSize = 0;
54     napi_get_value_string_utf8(env, argv, outBuffer, STR_BUFFER_SIZE, &outSize);
55     std::string key = std::string(outBuffer);
56 
57     napi_value obj = nullptr;
58     auto delegate = EngineHelper::GetCurrentDelegateSafely();
59     if (!delegate) {
60         napi_get_boolean(env, false, &obj);
61         return obj;
62     }
63     auto focusCallback = [env](NG::RequestFocusResult result) {
64         switch (result) {
65             case NG::RequestFocusResult::NON_FOCUSABLE:
66                 NapiThrow(env, "This component is not focusable.", ERROR_CODE_NON_FOCUSABLE);
67                 break;
68             case NG::RequestFocusResult::NON_FOCUSABLE_ANCESTOR:
69                 NapiThrow(env, "This component has unfocusable ancestor.", ERROR_CODE_NON_FOCUSABLE_ANCESTOR);
70                 break;
71             case NG::RequestFocusResult::NON_EXIST:
72                 NapiThrow(env,
73                     "The component doesn't exist, is currently invisible, or has been disabled.",
74                     ERROR_CODE_NON_EXIST);
75                 break;
76             default:
77                 NapiThrow(env, "An internal error occurred.", ERROR_CODE_INTERNAL_ERROR);
78                 break;
79         }
80     };
81     delegate->SetRequestFocusCallback(focusCallback);
82     delegate->RequestFocus(key, true);
83     delegate->ResetRequestFocusCallback();
84     napi_get_null(env, &obj);
85     return obj;
86 }
87 
registerFunc(napi_env env,napi_value exports)88 static napi_value registerFunc(napi_env env, napi_value exports)
89 {
90     napi_property_descriptor animatorDesc[] = {
91         DECLARE_NAPI_FUNCTION("clearFocus", JSClearFocus),
92         DECLARE_NAPI_FUNCTION("requestFocus", JSRequestFocus),
93     };
94     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(animatorDesc) / sizeof(animatorDesc[0]), animatorDesc));
95     return exports;
96 }
97 
98 static napi_module focusControllerModule = {
99     .nm_version = 1,
100     .nm_flags = 0,
101     .nm_filename = nullptr,
102     .nm_register_func = registerFunc,
103     .nm_modname = "arkui.focusController",
104     .nm_priv = ((void*)0),
105     .reserved = { 0 },
106 };
107 
FocusControllerRegister()108 extern "C" __attribute__((constructor)) void FocusControllerRegister()
109 {
110     napi_module_register(&focusControllerModule);
111 }
112 } // namespace OHOS::Ace::Napi