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_utils.h"
23 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
24 #include "bridge/declarative_frontend/jsview/models/text_timer_model_impl.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components/declaration/texttimer/texttimer_declaration.h"
27 #include "core/components/text/text_theme.h"
28 #include "core/components_ng/base/view_stack_processor.h"
29 #include "core/components_ng/pattern/texttimer/text_timer_model.h"
30 #include "core/components_ng/pattern/texttimer/text_timer_model_ng.h"
31
32 namespace OHOS::Ace {
33
34 std::unique_ptr<TextTimerModel> TextTimerModel::instance_ = nullptr;
35 std::mutex TextTimerModel::mutex_;
36
GetInstance()37 TextTimerModel* TextTimerModel::GetInstance()
38 {
39 if (!instance_) {
40 std::lock_guard<std::mutex> lock(mutex_);
41 if (!instance_) {
42 #ifdef NG_BUILD
43 instance_.reset(new NG::TextTimerModelNG());
44 #else
45 if (Container::IsCurrentUseNewPipeline()) {
46 instance_.reset(new NG::TextTimerModelNG());
47 } else {
48 instance_.reset(new Framework::TextTimerModelImpl());
49 }
50 #endif
51 }
52 }
53 return instance_.get();
54 }
55 } // namespace OHOS::Ace
56
57 namespace OHOS::Ace::Framework {
58 namespace {
59 const std::vector<FontStyle> FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC };
60 const std::string DEFAULT_FORMAT = "HH:mm:ss.SS";
61 constexpr double MAX_COUNT_DOWN = 86400000.0;
62 } // namespace
63
Create(const JSCallbackInfo & info)64 void JSTextTimer::Create(const JSCallbackInfo& info)
65 {
66 auto controller = TextTimerModel::GetInstance()->Create();
67 if (info.Length() < 1 || !info[0]->IsObject()) {
68 SetFontDefault();
69 return;
70 }
71 auto paramObject = JSRef<JSObject>::Cast(info[0]);
72 auto tempIsCountDown = paramObject->GetProperty("isCountDown");
73 if (tempIsCountDown->IsBoolean()) {
74 bool isCountDown = tempIsCountDown->ToBoolean();
75 TextTimerModel::GetInstance()->SetIsCountDown(isCountDown);
76 if (isCountDown) {
77 auto count = paramObject->GetProperty("count");
78 if (count->IsNumber()) {
79 auto inputCount = count->ToNumber<double>();
80 if (inputCount > 0 && inputCount < MAX_COUNT_DOWN) {
81 TextTimerModel::GetInstance()->SetInputCount(inputCount);
82 } else {
83 TextTimerModel::GetInstance()->SetInputCount(TIME_DEFAULT_COUNT);
84 }
85 }
86 if (count->IsUndefined() || count->IsNull()) {
87 TextTimerModel::GetInstance()->SetInputCount(TIME_DEFAULT_COUNT);
88 }
89 }
90 }
91
92 auto controllerObj = paramObject->GetProperty("controller");
93 if (controllerObj->IsObject()) {
94 auto* jsController = JSRef<JSObject>::Cast(controllerObj)->Unwrap<JSTextTimerController>();
95 if (jsController) {
96 jsController->SetInstanceId(Container::CurrentId());
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("onAttach", &JSInteractableView::JsOnAttach);
117 JSClass<JSTextTimer>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
118 JSClass<JSTextTimer>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
119 JSClass<JSTextTimer>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
120 JSClass<JSTextTimer>::StaticMethod("textShadow", &JSTextTimer::SetTextShadow, opt);
121 JSClass<JSTextTimer>::InheritAndBind<JSViewAbstract>(globalObj);
122 }
123
SetFontDefault()124 void JSTextTimer::SetFontDefault()
125 {
126 RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
127 TextTimerModel::GetInstance()->SetFontSize(textTheme->GetTextStyle().GetFontSize());
128 TextTimerModel::GetInstance()->SetTextColor(textTheme->GetTextStyle().GetTextColor());
129 TextTimerModel::GetInstance()->SetFontFamily(textTheme->GetTextStyle().GetFontFamilies());
130 TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
131 TextTimerModel::GetInstance()->SetItalicFontStyle(textTheme->GetTextStyle().GetFontStyle());
132 }
133
SetFormat(const JSCallbackInfo & info)134 void JSTextTimer::SetFormat(const JSCallbackInfo& info)
135 {
136 if (info.Length() < 1) {
137 return;
138 }
139
140 if (!info[0]->IsString()) {
141 TextTimerModel::GetInstance()->SetFormat(DEFAULT_FORMAT);
142 return;
143 }
144
145 auto format = info[0]->ToString();
146 std::smatch result;
147 std::regex pattern("(([YyMdD]+))");
148 if (std::regex_search(format, result, pattern)) {
149 if (!result.empty()) {
150 format = DEFAULT_FORMAT;
151 }
152 }
153
154 std::string target = "HmsS:.";
155 for (auto ch : format) {
156 if (target.find(ch) == std::string::npos) {
157 format = DEFAULT_FORMAT;
158 }
159 }
160
161 auto pos = format.find("hh");
162 if (pos != std::string::npos) {
163 format.replace(pos, sizeof("hh") - 1, "HH");
164 }
165
166 TextTimerModel::GetInstance()->SetFormat(format);
167 }
168
SetFontSize(const JSCallbackInfo & info)169 void JSTextTimer::SetFontSize(const JSCallbackInfo& info)
170 {
171 if (info.Length() < 1) {
172 return;
173 }
174 auto pipelineContext = PipelineContext::GetCurrentContext();
175 CHECK_NULL_VOID(pipelineContext);
176 auto theme = pipelineContext->GetTheme<TextTheme>();
177 CHECK_NULL_VOID(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(pipelineContext);
187 auto theme = pipelineContext->GetTheme<TextTheme>();
188 CHECK_NULL_VOID(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 return;
199 }
200 Color textColor;
201 if (!ParseJsColor(info[0], textColor)) {
202 auto pipelineContext = PipelineContext::GetCurrentContext();
203 CHECK_NULL_VOID(pipelineContext);
204 auto theme = pipelineContext->GetTheme<TextTheme>();
205 textColor = theme->GetTextStyle().GetTextColor();
206 }
207
208 TextTimerModel::GetInstance()->SetTextColor(textColor);
209 }
210
SetTextShadow(const JSCallbackInfo & info)211 void JSTextTimer::SetTextShadow(const JSCallbackInfo& info)
212 {
213 if (info.Length() < 1) {
214 return;
215 }
216 std::vector<Shadow> shadows;
217 ParseTextShadowFromShadowObject(info[0], shadows);
218 if (!shadows.empty()) {
219 TextTimerModel::GetInstance()->SetTextShadow(shadows);
220 }
221 }
222
SetFontWeight(const JSCallbackInfo & info)223 void JSTextTimer::SetFontWeight(const JSCallbackInfo& info)
224 {
225 if (info.Length() < 1) {
226 return;
227 }
228 RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
229 CHECK_NULL_VOID(textTheme);
230 auto fontWeight = info[0];
231 if (fontWeight->IsUndefined()) {
232 TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
233 return;
234 }
235
236 if (!fontWeight->IsNull()) {
237 std::string weight;
238 if (fontWeight->IsNumber()) {
239 weight = std::to_string(fontWeight->ToNumber<int32_t>());
240 } else {
241 ParseJsString(fontWeight, weight);
242 }
243 TextTimerModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight));
244 } else {
245 TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
246 }
247 }
248
SetFontStyle(int32_t value)249 void JSTextTimer::SetFontStyle(int32_t value)
250 {
251 if (value < 0 || value >= static_cast<int32_t>(FONT_STYLES.size())) {
252 return;
253 }
254 TextTimerModel::GetInstance()->SetItalicFontStyle(FONT_STYLES[value]);
255 }
256
SetFontFamily(const JSCallbackInfo & info)257 void JSTextTimer::SetFontFamily(const JSCallbackInfo& info)
258 {
259 if (info.Length() < 1) {
260 return;
261 }
262 std::vector<std::string> fontFamilies;
263 if (!ParseJsFontFamilies(info[0], fontFamilies)) {
264 return;
265 }
266 TextTimerModel::GetInstance()->SetFontFamily(fontFamilies);
267 }
268
OnTimer(const JSCallbackInfo & info)269 void JSTextTimer::OnTimer(const JSCallbackInfo& info)
270 {
271 CHECK_NULL_VOID(info[0]->IsFunction());
272 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
273 WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
274 auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
275 int64_t utc, int64_t elapsedTime) {
276 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
277 ACE_SCORING_EVENT("TextTimer.onTimer");
278 PipelineContext::SetCallBackNode(node);
279 JSRef<JSVal> newJSVal[2];
280 newJSVal[0] = JSRef<JSVal>::Make(ToJSValue(utc));
281 newJSVal[1] = JSRef<JSVal>::Make(ToJSValue(elapsedTime));
282 func->ExecuteJS(2, newJSVal);
283 };
284 TextTimerModel::GetInstance()->SetOnTimer(std::move(onChange));
285 }
286
JSBind(BindingTarget globalObj)287 void JSTextTimerController::JSBind(BindingTarget globalObj)
288 {
289 JSClass<JSTextTimerController>::Declare("TextTimerController");
290 JSClass<JSTextTimerController>::CustomMethod("start", &JSTextTimerController::Start);
291 JSClass<JSTextTimerController>::CustomMethod("pause", &JSTextTimerController::Pause);
292 JSClass<JSTextTimerController>::CustomMethod("reset", &JSTextTimerController::Reset);
293 JSClass<JSTextTimerController>::Bind(
294 globalObj, JSTextTimerController::Constructor, JSTextTimerController::Destructor);
295 }
296
Constructor(const JSCallbackInfo & info)297 void JSTextTimerController::Constructor(const JSCallbackInfo& info)
298 {
299 auto timerController = Referenced::MakeRefPtr<JSTextTimerController>();
300 timerController->IncRefCount();
301 info.SetReturnValue(Referenced::RawPtr(timerController));
302 }
303
Destructor(JSTextTimerController * timerController)304 void JSTextTimerController::Destructor(JSTextTimerController* timerController)
305 {
306 if (timerController != nullptr) {
307 timerController->DecRefCount();
308 }
309 }
310
Start(const JSCallbackInfo & info)311 void JSTextTimerController::Start(const JSCallbackInfo& info)
312 {
313 ContainerScope scope(instanceId_);
314 if (controller_) {
315 controller_->Start();
316 }
317 }
318
Pause(const JSCallbackInfo & info)319 void JSTextTimerController::Pause(const JSCallbackInfo& info)
320 {
321 ContainerScope scope(instanceId_);
322 if (controller_) {
323 controller_->Pause();
324 }
325 }
326
Reset(const JSCallbackInfo & info)327 void JSTextTimerController::Reset(const JSCallbackInfo& info)
328 {
329 ContainerScope scope(instanceId_);
330 if (controller_) {
331 controller_->Reset();
332 }
333 }
334 } // namespace OHOS::Ace::Framework
335