1 /*
2  * Copyright (c) 2023 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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 #include "interfaces/napi/kits/utils/napi_utils.h"
24 #include "js_native_api.h"
25 #include "js_native_api_types.h"
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28 
29 #include "core/common/ace_engine.h"
30 #include "core/common/container.h"
31 #include "core/components_ng/pattern/app_bar/app_bar_view.h"
32 
33 namespace OHOS::Ace::Napi {
ObtainAppBar()34 static RefPtr<NG::AppBarView> ObtainAppBar()
35 {
36     auto container = Container::Current();
37     CHECK_NULL_RETURN(container, nullptr);
38     return container->GetAppBar();
39 }
40 
SetBarInUIThread(TaskExecutor::Task && task,const std::string & name)41 static void SetBarInUIThread(TaskExecutor::Task&& task, const std::string& name)
42 {
43     auto taskExecutor = Container::CurrentTaskExecutor();
44     CHECK_NULL_VOID(taskExecutor);
45     taskExecutor->PostTask(std::move(task), TaskExecutor::TaskType::UI, name, PriorityType::VIP);
46 }
47 
JSSetVisible(napi_env env,napi_callback_info info)48 static napi_value JSSetVisible(napi_env env, napi_callback_info info)
49 {
50     napi_value argv[1] = { 0 };
51     napi_valuetype valueType = napi_undefined;
52     if (!GetSingleParam(env, info, argv, valueType) || (valueType != napi_boolean)) {
53         LOGW("invalid boolean value for visible");
54         return nullptr;
55     }
56     bool visible = true;
57     napi_get_value_bool(env, argv[0], &visible);
58     auto appBar = ObtainAppBar();
59     CHECK_NULL_RETURN(appBar, nullptr);
60     SetBarInUIThread([visible, appBar]() { appBar->SetVisible(visible); }, "ArkUIAppBarSetVisible");
61     return nullptr;
62 }
63 
JSSetBackgroundColor(napi_env env,napi_callback_info info)64 static napi_value JSSetBackgroundColor(napi_env env, napi_callback_info info)
65 {
66     napi_value argv[1] = { 0 };
67     napi_valuetype valueType = napi_undefined;
68     if (!GetSingleParam(env, info, argv, valueType)) {
69         return nullptr;
70     }
71     std::optional<Color> color = GetOptionalColor(env, argv[0], valueType);
72     if (!color && valueType != napi_undefined) {
73         return nullptr;
74     }
75     auto appBar = ObtainAppBar();
76     CHECK_NULL_RETURN(appBar, nullptr);
77     SetBarInUIThread([color, appBar]() { appBar->SetRowColor(color); }, "ArkUIAppBarSetRowColor");
78     return nullptr;
79 }
80 
JSSetTitleContent(napi_env env,napi_callback_info info)81 static napi_value JSSetTitleContent(napi_env env, napi_callback_info info)
82 {
83     napi_value argv[1] = { 0 };
84     napi_valuetype valueType;
85     std::string str;
86     if (!GetSingleParam(env, info, argv, valueType) || (valueType != napi_string)) {
87         LOGW("invalid string value for content");
88         return nullptr;
89     }
90     bool result = GetNapiString(env, argv[0], str, valueType);
91     if (!result) {
92         return nullptr;
93     }
94     auto appBar = ObtainAppBar();
95     CHECK_NULL_RETURN(appBar, nullptr);
96     SetBarInUIThread([str, appBar]() { appBar->SetContent(str); }, "ArkUIAppBarSetContent");
97     return nullptr;
98 }
99 
JSSetTitleFontStyle(napi_env env,napi_callback_info info)100 static napi_value JSSetTitleFontStyle(napi_env env, napi_callback_info info)
101 {
102     napi_value argv[1] = { 0 };
103     napi_valuetype valueType = napi_undefined;
104     if (!GetSingleParam(env, info, argv, valueType) || (valueType != napi_number)) {
105         LOGW("invalid number value for fontStyle");
106         return nullptr;
107     }
108     uint32_t num;
109     napi_get_value_uint32(env, argv[0], &num);
110     auto appBar = ObtainAppBar();
111     CHECK_NULL_RETURN(appBar, nullptr);
112     SetBarInUIThread(
113         [num, appBar]() { appBar->SetFontStyle(num == 0 ? Ace::FontStyle::NORMAL : Ace::FontStyle::ITALIC); },
114         "ArkUIAppBarSetFontStyle");
115     return nullptr;
116 }
117 
JSSetIconColor(napi_env env,napi_callback_info info)118 static napi_value JSSetIconColor(napi_env env, napi_callback_info info)
119 {
120     napi_value argv[1] = { 0 };
121     napi_valuetype valueType = napi_undefined;
122     if (!GetSingleParam(env, info, argv, valueType)) {
123         return nullptr;
124     }
125     std::optional<Color> color = GetOptionalColor(env, argv[0], valueType);
126     if (!color && valueType != napi_undefined) {
127         return nullptr;
128     }
129     auto appBar = ObtainAppBar();
130     CHECK_NULL_RETURN(appBar, nullptr);
131     SetBarInUIThread([color, appBar]() { appBar->SetIconColor(color); }, "ArkUIAppBarSetIconColor");
132     return nullptr;
133 }
134 
Export(napi_env env,napi_value exports)135 static napi_value Export(napi_env env, napi_value exports)
136 {
137     napi_property_descriptor properties[] = {
138         DECLARE_NAPI_FUNCTION("setVisible", JSSetVisible),
139         DECLARE_NAPI_FUNCTION("setBackgroundColor", JSSetBackgroundColor),
140         DECLARE_NAPI_FUNCTION("setTitleContent", JSSetTitleContent),
141         DECLARE_NAPI_FUNCTION("setTitleFontStyle", JSSetTitleFontStyle),
142         DECLARE_NAPI_FUNCTION("setIconColor", JSSetIconColor),
143     };
144     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties));
145     return exports;
146 }
147 
148 static napi_module atomic_service_bar_module = {
149     .nm_version = 1,
150     .nm_flags = 0,
151     .nm_filename = nullptr,
152     .nm_register_func = Export,
153     .nm_modname = "atomicservicebar",
154     .nm_priv = ((void*)0),
155     .reserved = { 0 },
156 };
157 
RegisterAtomicServiceBar()158 extern "C" __attribute__((constructor)) void RegisterAtomicServiceBar()
159 {
160     napi_module_register(&atomic_service_bar_module);
161 }
162 } // namespace OHOS::Ace::Napi
163