1 /*
2 * Copyright (c) 2021-2022 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 "frameworks/bridge/declarative_frontend/jsview/js_texttimer.h"
17
18 #include <regex>
19
20 #include "base/log/ace_scoring_log.h"
21 #include "bridge/declarative_frontend/engine/js_types.h"
22 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "bridge/declarative_frontend/jsview/models/text_timer_model_impl.h"
24 #include "core/components/common/layout/constants.h"
25 #include "core/components/declaration/texttimer/texttimer_declaration.h"
26 #include "core/components/text/text_theme.h"
27 #include "core/components_ng/pattern/texttimer/text_timer_model.h"
28 #include "core/components_ng/pattern/texttimer/text_timer_model_ng.h"
29
30 namespace OHOS::Ace {
31
32 std::unique_ptr<TextTimerModel> TextTimerModel::instance_ = nullptr;
33 std::mutex TextTimerModel::mutex_;
34
GetInstance()35 TextTimerModel* TextTimerModel::GetInstance()
36 {
37 if (!instance_) {
38 std::lock_guard<std::mutex> lock(mutex_);
39 if (!instance_) {
40 #ifdef NG_BUILD
41 instance_.reset(new NG::TextTimerModelNG());
42 #else
43 if (Container::IsCurrentUseNewPipeline()) {
44 instance_.reset(new NG::TextTimerModelNG());
45 } else {
46 instance_.reset(new Framework::TextTimerModelImpl());
47 }
48 #endif
49 }
50 }
51 return instance_.get();
52 }
53 } // namespace OHOS::Ace
54
55 namespace OHOS::Ace::Framework {
56 namespace {
57 const std::vector<FontStyle> FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC };
58 const std::string DEFAULT_FORMAT = "HH:mm:ss.SS";
59 constexpr double MAX_COUNT_DOWN = 86400000.0;
60 } // namespace
61
Create(const JSCallbackInfo & info)62 void JSTextTimer::Create(const JSCallbackInfo& info)
63 {
64 auto controller = TextTimerModel::GetInstance()->Create();
65 if (info.Length() < 1 || !info[0]->IsObject()) {
66 SetFontDefault();
67 LOGI("TextTimer create error, info is non-valid");
68 return;
69 }
70 auto paramObject = JSRef<JSObject>::Cast(info[0]);
71 auto tempIsCountDown = paramObject->GetProperty("isCountDown");
72 if (tempIsCountDown->IsBoolean()) {
73 bool isCountDown = tempIsCountDown->ToBoolean();
74 TextTimerModel::GetInstance()->SetIsCountDown(isCountDown);
75 if (isCountDown) {
76 auto count = paramObject->GetProperty("count");
77 if (count->IsNumber()) {
78 auto inputCount = count->ToNumber<double>();
79 if (inputCount > 0 && inputCount < MAX_COUNT_DOWN) {
80 TextTimerModel::GetInstance()->SetInputCount(inputCount);
81 } else {
82 TextTimerModel::GetInstance()->SetInputCount(TIME_DEFAULT_COUNT);
83 LOGE("Parameter out of range, use default value.");
84 }
85 }
86 if (count->IsUndefined() || count->IsNull()) {
87 TextTimerModel::GetInstance()->SetInputCount(TIME_DEFAULT_COUNT);
88 LOGE("Parameter is undefined or null, use default value.");
89 }
90 }
91 }
92
93 auto controllerObj = paramObject->GetProperty("controller");
94 if (controllerObj->IsObject()) {
95 auto* jsController = JSRef<JSObject>::Cast(controllerObj)->Unwrap<JSTextTimerController>();
96 if (jsController) {
97 jsController->SetController(controller);
98 }
99 }
100 }
101
JSBind(BindingTarget globalObj)102 void JSTextTimer::JSBind(BindingTarget globalObj)
103 {
104 JSClass<JSTextTimer>::Declare("TextTimer");
105 MethodOptions opt = MethodOptions::NONE;
106 JSClass<JSTextTimer>::StaticMethod("create", &JSTextTimer::Create, opt);
107 JSClass<JSTextTimer>::StaticMethod("format", &JSTextTimer::SetFormat);
108 JSClass<JSTextTimer>::StaticMethod("fontColor", &JSTextTimer::SetTextColor);
109 JSClass<JSTextTimer>::StaticMethod("fontSize", &JSTextTimer::SetFontSize);
110 JSClass<JSTextTimer>::StaticMethod("fontWeight", &JSTextTimer::SetFontWeight, opt);
111 JSClass<JSTextTimer>::StaticMethod("fontStyle", &JSTextTimer::SetFontStyle, opt);
112 JSClass<JSTextTimer>::StaticMethod("fontFamily", &JSTextTimer::SetFontFamily, opt);
113 JSClass<JSTextTimer>::StaticMethod("onTimer", &JSTextTimer::OnTimer);
114 JSClass<JSTextTimer>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
115 JSClass<JSTextTimer>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
116 JSClass<JSTextTimer>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
117 JSClass<JSTextTimer>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
118 JSClass<JSTextTimer>::InheritAndBind<JSViewAbstract>(globalObj);
119 }
120
SetFontDefault()121 void JSTextTimer::SetFontDefault()
122 {
123 RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
124 TextTimerModel::GetInstance()->SetFontSize(textTheme->GetTextStyle().GetFontSize());
125 TextTimerModel::GetInstance()->SetTextColor(textTheme->GetTextStyle().GetTextColor());
126 TextTimerModel::GetInstance()->SetFontFamily(textTheme->GetTextStyle().GetFontFamilies());
127 TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
128 TextTimerModel::GetInstance()->SetItalicFontStyle(textTheme->GetTextStyle().GetFontStyle());
129 }
130
SetFormat(const JSCallbackInfo & info)131 void JSTextTimer::SetFormat(const JSCallbackInfo& info)
132 {
133 if (info.Length() < 1) {
134 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
135 return;
136 }
137
138 if (!info[0]->IsString()) {
139 LOGE("The arg is not string, it is supposed to be a string");
140 TextTimerModel::GetInstance()->SetFormat(DEFAULT_FORMAT);
141 return;
142 }
143
144 auto format = info[0]->ToString();
145 std::smatch result;
146 std::regex pattern("(([YyMdD]+))");
147 if (std::regex_search(format, result, pattern)) {
148 if (!result.empty()) {
149 format = DEFAULT_FORMAT;
150 }
151 }
152
153 std::string target = "HmsS:.";
154 for (auto ch : format) {
155 if (target.find(ch) == std::string::npos) {
156 format = DEFAULT_FORMAT;
157 }
158 }
159
160 auto pos = format.find("hh");
161 if (pos != std::string::npos) {
162 format.replace(pos, sizeof("hh") - 1, "HH");
163 }
164
165 TextTimerModel::GetInstance()->SetFormat(format);
166 }
167
SetFontSize(const JSCallbackInfo & info)168 void JSTextTimer::SetFontSize(const JSCallbackInfo& info)
169 {
170 if (info.Length() < 1) {
171 LOGE("JSTextInput::SetFontSize The argv is wrong, it is supposed to have at least 1 argument");
172 return;
173 }
174 auto pipelineContext = PipelineContext::GetCurrentContext();
175 CHECK_NULL_VOID_NOLOG(pipelineContext);
176 auto theme = pipelineContext->GetTheme<TextTheme>();
177 CHECK_NULL_VOID_NOLOG(theme);
178
179 CalcDimension fontSize;
180 if (!ParseJsDimensionFp(info[0], fontSize)) {
181 fontSize = theme->GetTextStyle().GetFontSize();
182 }
183
184 if (fontSize.IsNegative() || fontSize.Unit() == DimensionUnit::PERCENT) {
185 auto pipelineContext = PipelineContext::GetCurrentContext();
186 CHECK_NULL_VOID_NOLOG(pipelineContext);
187 auto theme = pipelineContext->GetTheme<TextTheme>();
188 CHECK_NULL_VOID_NOLOG(theme);
189 fontSize = theme->GetTextStyle().GetFontSize();
190 }
191
192 TextTimerModel::GetInstance()->SetFontSize(fontSize);
193 }
194
SetTextColor(const JSCallbackInfo & info)195 void JSTextTimer::SetTextColor(const JSCallbackInfo& info)
196 {
197 if (info.Length() < 1) {
198 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
199 return;
200 }
201 Color textColor;
202 if (!ParseJsColor(info[0], textColor)) {
203 auto pipelineContext = PipelineContext::GetCurrentContext();
204 CHECK_NULL_VOID_NOLOG(pipelineContext);
205 auto theme = pipelineContext->GetTheme<TextTheme>();
206 textColor = theme->GetTextStyle().GetTextColor();
207 }
208
209 TextTimerModel::GetInstance()->SetTextColor(textColor);
210 }
211
SetFontWeight(const JSCallbackInfo & info)212 void JSTextTimer::SetFontWeight(const JSCallbackInfo& info)
213 {
214 if (info.Length() < 1) {
215 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
216 return;
217 }
218 RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
219 CHECK_NULL_VOID(textTheme);
220 auto fontWeight = info[0];
221 if (fontWeight->IsUndefined()) {
222 TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
223 return;
224 }
225
226 if (!fontWeight->IsNull()) {
227 std::string weight;
228 if (fontWeight->IsNumber()) {
229 weight = std::to_string(fontWeight->ToNumber<int32_t>());
230 } else {
231 ParseJsString(fontWeight, weight);
232 }
233 TextTimerModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight));
234 } else {
235 TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
236 }
237 }
238
SetFontStyle(int32_t value)239 void JSTextTimer::SetFontStyle(int32_t value)
240 {
241 if (value < 0 || value >= static_cast<int32_t>(FONT_STYLES.size())) {
242 LOGE("TextTimer fontStyle(%{public}d) illegal value", value);
243 return;
244 }
245 TextTimerModel::GetInstance()->SetItalicFontStyle(FONT_STYLES[value]);
246 }
247
SetFontFamily(const JSCallbackInfo & info)248 void JSTextTimer::SetFontFamily(const JSCallbackInfo& info)
249 {
250 if (info.Length() < 1) {
251 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
252 return;
253 }
254 std::vector<std::string> fontFamilies;
255 if (!ParseJsFontFamilies(info[0], fontFamilies)) {
256 LOGE("Parse FontFamilies failed");
257 return;
258 }
259 TextTimerModel::GetInstance()->SetFontFamily(fontFamilies);
260 }
261
OnTimer(const JSCallbackInfo & info)262 void JSTextTimer::OnTimer(const JSCallbackInfo& info)
263 {
264 CHECK_NULL_VOID(info[0]->IsFunction());
265 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
266 auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
267 const std::string& utc, const std::string& elapsedTime) {
268 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
269 ACE_SCORING_EVENT("TextTimer.onTimer");
270 JSRef<JSVal> newJSVal[2];
271 newJSVal[0] = JSRef<JSVal>::Make(ToJSValue(utc));
272 newJSVal[1] = JSRef<JSVal>::Make(ToJSValue(elapsedTime));
273 func->ExecuteJS(2, newJSVal);
274 };
275 TextTimerModel::GetInstance()->SetOnTimer(std::move(onChange));
276 }
277
JSBind(BindingTarget globalObj)278 void JSTextTimerController::JSBind(BindingTarget globalObj)
279 {
280 JSClass<JSTextTimerController>::Declare("TextTimerController");
281 JSClass<JSTextTimerController>::CustomMethod("start", &JSTextTimerController::Start);
282 JSClass<JSTextTimerController>::CustomMethod("pause", &JSTextTimerController::Pause);
283 JSClass<JSTextTimerController>::CustomMethod("reset", &JSTextTimerController::Reset);
284 JSClass<JSTextTimerController>::Bind(
285 globalObj, JSTextTimerController::Constructor, JSTextTimerController::Destructor);
286 }
287
Constructor(const JSCallbackInfo & info)288 void JSTextTimerController::Constructor(const JSCallbackInfo& info)
289 {
290 auto timerController = Referenced::MakeRefPtr<JSTextTimerController>();
291 timerController->IncRefCount();
292 info.SetReturnValue(Referenced::RawPtr(timerController));
293 }
294
Destructor(JSTextTimerController * timerController)295 void JSTextTimerController::Destructor(JSTextTimerController* timerController)
296 {
297 if (timerController != nullptr) {
298 timerController->DecRefCount();
299 }
300 }
301
Start(const JSCallbackInfo & info)302 void JSTextTimerController::Start(const JSCallbackInfo& info)
303 {
304 if (controller_) {
305 controller_->Start();
306 }
307 }
308
Pause(const JSCallbackInfo & info)309 void JSTextTimerController::Pause(const JSCallbackInfo& info)
310 {
311 if (controller_) {
312 controller_->Pause();
313 }
314 }
315
Reset(const JSCallbackInfo & info)316 void JSTextTimerController::Reset(const JSCallbackInfo& info)
317 {
318 if (controller_) {
319 controller_->Reset();
320 }
321 }
322 } // namespace OHOS::Ace::Framework
323