• 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("onAppear", &JSInteractableView::JsOnAppear);
117     JSClass<JSTextTimer>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
118     JSClass<JSTextTimer>::StaticMethod("textShadow", &JSTextTimer::SetTextShadow, opt);
119     JSClass<JSTextTimer>::InheritAndBind<JSViewAbstract>(globalObj);
120 }
121 
SetFontDefault()122 void JSTextTimer::SetFontDefault()
123 {
124     RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
125     TextTimerModel::GetInstance()->SetFontSize(textTheme->GetTextStyle().GetFontSize());
126     TextTimerModel::GetInstance()->SetTextColor(textTheme->GetTextStyle().GetTextColor());
127     TextTimerModel::GetInstance()->SetFontFamily(textTheme->GetTextStyle().GetFontFamilies());
128     TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
129     TextTimerModel::GetInstance()->SetItalicFontStyle(textTheme->GetTextStyle().GetFontStyle());
130 }
131 
SetFormat(const JSCallbackInfo & info)132 void JSTextTimer::SetFormat(const JSCallbackInfo& info)
133 {
134     if (info.Length() < 1) {
135         return;
136     }
137 
138     if (!info[0]->IsString()) {
139         TextTimerModel::GetInstance()->SetFormat(DEFAULT_FORMAT);
140         return;
141     }
142 
143     auto format = info[0]->ToString();
144     std::smatch result;
145     std::regex pattern("(([YyMdD]+))");
146     if (std::regex_search(format, result, pattern)) {
147         if (!result.empty()) {
148             format = DEFAULT_FORMAT;
149         }
150     }
151 
152     std::string target = "HmsS:.";
153     for (auto ch : format) {
154         if (target.find(ch) == std::string::npos) {
155             format = DEFAULT_FORMAT;
156         }
157     }
158 
159     auto pos = format.find("hh");
160     if (pos != std::string::npos) {
161         format.replace(pos, sizeof("hh") - 1, "HH");
162     }
163 
164     TextTimerModel::GetInstance()->SetFormat(format);
165 }
166 
SetFontSize(const JSCallbackInfo & info)167 void JSTextTimer::SetFontSize(const JSCallbackInfo& info)
168 {
169     if (info.Length() < 1) {
170         return;
171     }
172     auto pipelineContext = PipelineContext::GetCurrentContext();
173     CHECK_NULL_VOID(pipelineContext);
174     auto theme = pipelineContext->GetTheme<TextTheme>();
175     CHECK_NULL_VOID(theme);
176 
177     CalcDimension fontSize;
178     if (!ParseJsDimensionFp(info[0], fontSize)) {
179         fontSize = theme->GetTextStyle().GetFontSize();
180     }
181 
182     if (fontSize.IsNegative() || fontSize.Unit() == DimensionUnit::PERCENT) {
183         auto pipelineContext = PipelineContext::GetCurrentContext();
184         CHECK_NULL_VOID(pipelineContext);
185         auto theme = pipelineContext->GetTheme<TextTheme>();
186         CHECK_NULL_VOID(theme);
187         fontSize = theme->GetTextStyle().GetFontSize();
188     }
189 
190     TextTimerModel::GetInstance()->SetFontSize(fontSize);
191 }
192 
SetTextColor(const JSCallbackInfo & info)193 void JSTextTimer::SetTextColor(const JSCallbackInfo& info)
194 {
195     if (info.Length() < 1) {
196         return;
197     }
198     Color textColor;
199     if (!ParseJsColor(info[0], textColor)) {
200         auto pipelineContext = PipelineContext::GetCurrentContext();
201         CHECK_NULL_VOID(pipelineContext);
202         auto theme = pipelineContext->GetTheme<TextTheme>();
203         textColor = theme->GetTextStyle().GetTextColor();
204     }
205 
206     TextTimerModel::GetInstance()->SetTextColor(textColor);
207 }
208 
SetTextShadow(const JSCallbackInfo & info)209 void JSTextTimer::SetTextShadow(const JSCallbackInfo& info)
210 {
211     if (info.Length() < 1) {
212         return;
213     }
214     std::vector<Shadow> shadows;
215     ParseTextShadowFromShadowObject(info[0], shadows);
216     if (!shadows.empty()) {
217         TextTimerModel::GetInstance()->SetTextShadow(shadows);
218     }
219 }
220 
SetFontWeight(const JSCallbackInfo & info)221 void JSTextTimer::SetFontWeight(const JSCallbackInfo& info)
222 {
223     if (info.Length() < 1) {
224         return;
225     }
226     RefPtr<TextTheme> textTheme = GetTheme<TextTheme>();
227     CHECK_NULL_VOID(textTheme);
228     auto fontWeight = info[0];
229     if (fontWeight->IsUndefined()) {
230         TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
231         return;
232     }
233 
234     if (!fontWeight->IsNull()) {
235         std::string weight;
236         if (fontWeight->IsNumber()) {
237             weight = std::to_string(fontWeight->ToNumber<int32_t>());
238         } else {
239             ParseJsString(fontWeight, weight);
240         }
241         TextTimerModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight));
242     } else {
243         TextTimerModel::GetInstance()->SetFontWeight(textTheme->GetTextStyle().GetFontWeight());
244     }
245 }
246 
SetFontStyle(int32_t value)247 void JSTextTimer::SetFontStyle(int32_t value)
248 {
249     if (value < 0 || value >= static_cast<int32_t>(FONT_STYLES.size())) {
250         return;
251     }
252     TextTimerModel::GetInstance()->SetItalicFontStyle(FONT_STYLES[value]);
253 }
254 
SetFontFamily(const JSCallbackInfo & info)255 void JSTextTimer::SetFontFamily(const JSCallbackInfo& info)
256 {
257     if (info.Length() < 1) {
258         return;
259     }
260     std::vector<std::string> fontFamilies;
261     if (!ParseJsFontFamilies(info[0], fontFamilies)) {
262         return;
263     }
264     TextTimerModel::GetInstance()->SetFontFamily(fontFamilies);
265 }
266 
OnTimer(const JSCallbackInfo & info)267 void JSTextTimer::OnTimer(const JSCallbackInfo& info)
268 {
269     CHECK_NULL_VOID(info[0]->IsFunction());
270     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
271     WeakPtr<NG::FrameNode> targetNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
272     auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
273                         int64_t utc, int64_t elapsedTime) {
274         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
275         ACE_SCORING_EVENT("TextTimer.onTimer");
276         PipelineContext::SetCallBackNode(node);
277         JSRef<JSVal> newJSVal[2];
278         newJSVal[0] = JSRef<JSVal>::Make(ToJSValue(utc));
279         newJSVal[1] = JSRef<JSVal>::Make(ToJSValue(elapsedTime));
280         func->ExecuteJS(2, newJSVal);
281     };
282     TextTimerModel::GetInstance()->SetOnTimer(std::move(onChange));
283 }
284 
JSBind(BindingTarget globalObj)285 void JSTextTimerController::JSBind(BindingTarget globalObj)
286 {
287     JSClass<JSTextTimerController>::Declare("TextTimerController");
288     JSClass<JSTextTimerController>::CustomMethod("start", &JSTextTimerController::Start);
289     JSClass<JSTextTimerController>::CustomMethod("pause", &JSTextTimerController::Pause);
290     JSClass<JSTextTimerController>::CustomMethod("reset", &JSTextTimerController::Reset);
291     JSClass<JSTextTimerController>::Bind(
292         globalObj, JSTextTimerController::Constructor, JSTextTimerController::Destructor);
293 }
294 
Constructor(const JSCallbackInfo & info)295 void JSTextTimerController::Constructor(const JSCallbackInfo& info)
296 {
297     auto timerController = Referenced::MakeRefPtr<JSTextTimerController>();
298     timerController->IncRefCount();
299     info.SetReturnValue(Referenced::RawPtr(timerController));
300 }
301 
Destructor(JSTextTimerController * timerController)302 void JSTextTimerController::Destructor(JSTextTimerController* timerController)
303 {
304     if (timerController != nullptr) {
305         timerController->DecRefCount();
306     }
307 }
308 
Start(const JSCallbackInfo & info)309 void JSTextTimerController::Start(const JSCallbackInfo& info)
310 {
311     ContainerScope scope(instanceId_);
312     if (controller_) {
313         controller_->Start();
314     }
315 }
316 
Pause(const JSCallbackInfo & info)317 void JSTextTimerController::Pause(const JSCallbackInfo& info)
318 {
319     ContainerScope scope(instanceId_);
320     if (controller_) {
321         controller_->Pause();
322     }
323 }
324 
Reset(const JSCallbackInfo & info)325 void JSTextTimerController::Reset(const JSCallbackInfo& info)
326 {
327     ContainerScope scope(instanceId_);
328     if (controller_) {
329         controller_->Reset();
330     }
331 }
332 } // namespace OHOS::Ace::Framework
333