• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()->SetTextColorByUser(false);
130     TextTimerModel::GetInstance()->SetFontFamily(textTheme->GetTextStyle().GetFontFamilies());
131     TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
132     TextTimerModel::GetInstance()->SetItalicFontStyle(textTheme->GetTextStyle().GetFontStyle());
133 }
134 
SetFormat(const JSCallbackInfo & info)135 void JSTextTimer::SetFormat(const JSCallbackInfo& info)
136 {
137     if (info.Length() < 1) {
138         return;
139     }
140 
141     if (!info[0]->IsString()) {
142         TextTimerModel::GetInstance()->SetFormat(DEFAULT_FORMAT);
143         return;
144     }
145 
146     auto format = info[0]->ToString();
147     std::smatch result;
148     std::regex pattern("(([YyMdD]+))");
149     if (std::regex_search(format, result, pattern)) {
150         if (!result.empty()) {
151             format = DEFAULT_FORMAT;
152         }
153     }
154 
155     std::string target = "HmsS:.";
156     for (auto ch : format) {
157         if (target.find(ch) == std::string::npos) {
158             format = DEFAULT_FORMAT;
159         }
160     }
161 
162     auto pos = format.find("hh");
163     if (pos != std::string::npos) {
164         format.replace(pos, sizeof("hh") - 1, "HH");
165     }
166 
167     TextTimerModel::GetInstance()->SetFormat(format);
168 }
169 
SetFontSize(const JSCallbackInfo & info)170 void JSTextTimer::SetFontSize(const JSCallbackInfo& info)
171 {
172     if (info.Length() < 1) {
173         return;
174     }
175     auto pipelineContext = PipelineContext::GetCurrentContext();
176     CHECK_NULL_VOID(pipelineContext);
177     auto theme = pipelineContext->GetTheme<TextTheme>();
178     CHECK_NULL_VOID(theme);
179 
180     CalcDimension fontSize;
181     RefPtr<ResourceObject> resObj;
182     if (SystemProperties::ConfigChangePerform()) {
183         bool state = ParseJsDimensionFp(info[0], fontSize, resObj);
184         TextTimerModel::GetInstance()->CreateWithResourceObj(JsTextTimerResourceType::FONTSIZE, resObj);
185         if (state && !fontSize.IsNegative() && fontSize.Unit() != DimensionUnit::PERCENT) {
186             TextTimerModel::GetInstance()->SetFontSize(fontSize);
187             TextTimerModel::GetInstance()->SetFontSizeByUser(true);
188             return;
189         } else {
190             auto pipelineContext = PipelineContext::GetCurrentContext();
191             CHECK_NULL_VOID(pipelineContext);
192             auto theme = pipelineContext->GetTheme<TextTheme>();
193             CHECK_NULL_VOID(theme);
194             fontSize = theme->GetTextStyle().GetFontSize();
195             TextTimerModel::GetInstance()->SetFontSize(fontSize);
196             TextTimerModel::GetInstance()->SetFontSizeByUser(false);
197         }
198     } else {
199         if (!ParseJsDimensionFp(info[0], fontSize)) {
200             fontSize = theme->GetTextStyle().GetFontSize();
201         }
202         if (fontSize.IsNegative() || fontSize.Unit() == DimensionUnit::PERCENT) {
203             auto pipelineContext = PipelineContext::GetCurrentContext();
204             CHECK_NULL_VOID(pipelineContext);
205             auto theme = pipelineContext->GetTheme<TextTheme>();
206             CHECK_NULL_VOID(theme);
207             fontSize = theme->GetTextStyle().GetFontSize();
208         }
209         TextTimerModel::GetInstance()->SetFontSize(fontSize);
210     }
211 }
212 
SetTextColor(const JSCallbackInfo & info)213 void JSTextTimer::SetTextColor(const JSCallbackInfo& info)
214 {
215     if (info.Length() < 1) {
216         return;
217     }
218     Color textColor;
219     RefPtr<ResourceObject> resObj;
220     if (SystemProperties::ConfigChangePerform()) {
221         bool state = ParseJsColor(info[0], textColor, resObj);
222         TextTimerModel::GetInstance()->CreateWithResourceObj(JsTextTimerResourceType::TEXTCOLOR, resObj);
223         if (state) {
224             TextTimerModel::GetInstance()->SetTextColor(textColor);
225         } else {
226             auto pipelineContext = PipelineContext::GetCurrentContext();
227             CHECK_NULL_VOID(pipelineContext);
228             auto theme = pipelineContext->GetTheme<TextTheme>();
229             textColor = theme->GetTextStyle().GetTextColor();
230             TextTimerModel::GetInstance()->SetTextColor(textColor);
231             TextTimerModel::GetInstance()->SetTextColorByUser(false);
232         }
233     } else {
234         if (!ParseJsColor(info[0], textColor)) {
235             auto pipelineContext = PipelineContext::GetCurrentContext();
236             CHECK_NULL_VOID(pipelineContext);
237             auto theme = pipelineContext->GetTheme<TextTheme>();
238             textColor = theme->GetTextStyle().GetTextColor();
239         }
240         TextTimerModel::GetInstance()->SetTextColor(textColor);
241     }
242 }
243 
SetTextShadow(const JSCallbackInfo & info)244 void JSTextTimer::SetTextShadow(const JSCallbackInfo& info)
245 {
246     if (info.Length() < 1) {
247         return;
248     }
249     std::vector<Shadow> shadows;
250     ParseTextShadowFromShadowObject(info[0], shadows);
251     if (!shadows.empty() || SystemProperties::ConfigChangePerform()) {
252         TextTimerModel::GetInstance()->SetTextShadow(shadows);
253     }
254 }
255 
SetFontWeight(const JSCallbackInfo & info)256 void JSTextTimer::SetFontWeight(const JSCallbackInfo& info)
257 {
258     if (info.Length() < 1) {
259         return;
260     }
261     RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
262     CHECK_NULL_VOID(textTheme);
263     auto fontWeight = info[0];
264     if (fontWeight->IsUndefined()) {
265         TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
266         if (SystemProperties::ConfigChangePerform()) {
267             TextTimerModel::GetInstance()->CreateWithResourceObj(JsTextTimerResourceType::FONTWEIGHT, nullptr);
268         }
269         TextTimerModel::GetInstance()->SetFontWeightByUser(false);
270         return;
271     }
272 
273     if (fontWeight->IsNull()) {
274         TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
275         if (SystemProperties::ConfigChangePerform()) {
276             TextTimerModel::GetInstance()->CreateWithResourceObj(JsTextTimerResourceType::FONTWEIGHT, nullptr);
277         }
278         TextTimerModel::GetInstance()->SetFontWeightByUser(false);
279         return;
280     }
281     std::string weight;
282     if (fontWeight->IsNumber()) {
283         weight = std::to_string(fontWeight->ToNumber<int32_t>());
284     } else {
285         if (SystemProperties::ConfigChangePerform()) {
286             RefPtr<ResourceObject> resObj;
287             ParseJsString(fontWeight, weight, resObj);
288             TextTimerModel::GetInstance()->CreateWithResourceObj(JsTextTimerResourceType::FONTWEIGHT, resObj);
289         } else {
290             ParseJsString(fontWeight, weight);
291         }
292     }
293     TextTimerModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight));
294     TextTimerModel::GetInstance()->SetFontWeightByUser(true);
295 }
296 
SetFontStyle(int32_t value)297 void JSTextTimer::SetFontStyle(int32_t value)
298 {
299     if (value < 0 || value >= static_cast<int32_t>(FONT_STYLES.size())) {
300         return;
301     }
302     TextTimerModel::GetInstance()->SetItalicFontStyle(FONT_STYLES[value]);
303 }
304 
SetFontFamily(const JSCallbackInfo & info)305 void JSTextTimer::SetFontFamily(const JSCallbackInfo& info)
306 {
307     if (info.Length() < 1) {
308         return;
309     }
310     std::vector<std::string> fontFamilies;
311     RefPtr<ResourceObject> resObj;
312     if (SystemProperties::ConfigChangePerform()) {
313         bool state = ParseJsFontFamilies(info[0], fontFamilies, resObj);
314         TextTimerModel::GetInstance()->CreateWithResourceObj(JsTextTimerResourceType::FONTFAMILY, resObj);
315         if (state) {
316             TextTimerModel::GetInstance()->SetFontFamily(fontFamilies);
317             TextTimerModel::GetInstance()->SetFontFamilyByUser(true);
318         }
319     } else {
320         if (!ParseJsFontFamilies(info[0], fontFamilies)) {
321             return;
322         }
323         TextTimerModel::GetInstance()->SetFontFamily(fontFamilies);
324     }
325 }
326 
OnTimer(const JSCallbackInfo & info)327 void JSTextTimer::OnTimer(const JSCallbackInfo& info)
328 {
329     CHECK_NULL_VOID(info[0]->IsFunction());
330     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
331     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
332     auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
333                         int64_t utc, int64_t elapsedTime) {
334         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
335         ACE_SCORING_EVENT("TextTimer.onTimer");
336         PipelineContext::SetCallBackNode(node);
337         JSRef<JSVal> newJSVal[2];
338         newJSVal[0] = JSRef<JSVal>::Make(ToJSValue(utc));
339         newJSVal[1] = JSRef<JSVal>::Make(ToJSValue(elapsedTime));
340         func->ExecuteJS(2, newJSVal);
341     };
342     TextTimerModel::GetInstance()->SetOnTimer(std::move(onChange));
343 }
344 
JSBind(BindingTarget globalObj)345 void JSTextTimerController::JSBind(BindingTarget globalObj)
346 {
347     JSClass<JSTextTimerController>::Declare("TextTimerController");
348     JSClass<JSTextTimerController>::CustomMethod("start", &JSTextTimerController::Start);
349     JSClass<JSTextTimerController>::CustomMethod("pause", &JSTextTimerController::Pause);
350     JSClass<JSTextTimerController>::CustomMethod("reset", &JSTextTimerController::Reset);
351     JSClass<JSTextTimerController>::Bind(
352         globalObj, JSTextTimerController::Constructor, JSTextTimerController::Destructor);
353 }
354 
Constructor(const JSCallbackInfo & info)355 void JSTextTimerController::Constructor(const JSCallbackInfo& info)
356 {
357     auto timerController = Referenced::MakeRefPtr<JSTextTimerController>();
358     timerController->IncRefCount();
359     info.SetReturnValue(Referenced::RawPtr(timerController));
360 }
361 
Destructor(JSTextTimerController * timerController)362 void JSTextTimerController::Destructor(JSTextTimerController* timerController)
363 {
364     if (timerController != nullptr) {
365         timerController->DecRefCount();
366     }
367 }
368 
Start(const JSCallbackInfo & info)369 void JSTextTimerController::Start(const JSCallbackInfo& info)
370 {
371     ContainerScope scope(instanceId_);
372     if (controller_) {
373         controller_->Start();
374     }
375 }
376 
Pause(const JSCallbackInfo & info)377 void JSTextTimerController::Pause(const JSCallbackInfo& info)
378 {
379     ContainerScope scope(instanceId_);
380     if (controller_) {
381         controller_->Pause();
382     }
383 }
384 
Reset(const JSCallbackInfo & info)385 void JSTextTimerController::Reset(const JSCallbackInfo& info)
386 {
387     ContainerScope scope(instanceId_);
388     if (controller_) {
389         controller_->Reset();
390     }
391 }
392 } // namespace OHOS::Ace::Framework
393