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