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_ng/pattern/texttimer/text_timer_model.h"
26 #include "core/components_ng/pattern/texttimer/text_timer_model_ng.h"
27
28 namespace OHOS::Ace {
29
30 std::unique_ptr<TextTimerModel> TextTimerModel::instance_ = nullptr;
31
GetInstance()32 TextTimerModel* TextTimerModel::GetInstance()
33 {
34 if (!instance_) {
35 #ifdef NG_BUILD
36 instance_.reset(new NG::TextTimerModelNG());
37 #else
38 if (Container::IsCurrentUseNewPipeline()) {
39 instance_.reset(new NG::TextTimerModelNG());
40 } else {
41 instance_.reset(new Framework::TextTimerModelImpl());
42 }
43 #endif
44 }
45 return instance_.get();
46 }
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
50 namespace {
51 const std::vector<FontStyle> FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC };
52 const std::string DEFAULT_FORMAT = "HH:mm:ss.SS";
53 constexpr double MAX_COUNT_DOWN = 86400000.0;
54 } // namespace
55
Create(const JSCallbackInfo & info)56 void JSTextTimer::Create(const JSCallbackInfo& info)
57 {
58 if (info.Length() < 1 || !info[0]->IsObject()) {
59 LOGE("TextTimer create error, info is non-valid");
60 return;
61 }
62 auto controller = TextTimerModel::GetInstance()->Create();
63 auto paramObject = JSRef<JSObject>::Cast(info[0]);
64 auto tempIsCountDown = paramObject->GetProperty("isCountDown");
65 if (tempIsCountDown->IsBoolean()) {
66 bool isCountDown = tempIsCountDown->ToBoolean();
67 TextTimerModel::GetInstance()->SetIsCountDown(isCountDown);
68 if (isCountDown) {
69 auto count = paramObject->GetProperty("count");
70 if (count->IsNumber()) {
71 auto inputCount = count->ToNumber<double>();
72 if (inputCount > 0 && inputCount < MAX_COUNT_DOWN) {
73 TextTimerModel::GetInstance()->SetInputCount(inputCount);
74 } else {
75 LOGE("Parameter out of range, use default value.");
76 }
77 }
78 }
79 }
80
81 auto controllerObj = paramObject->GetProperty("controller");
82 if (controllerObj->IsObject()) {
83 auto* jsController = JSRef<JSObject>::Cast(controllerObj)->Unwrap<JSTextTimerController>();
84 if (jsController) {
85 jsController->SetController(controller);
86 }
87 }
88 }
89
JSBind(BindingTarget globalObj)90 void JSTextTimer::JSBind(BindingTarget globalObj)
91 {
92 JSClass<JSTextTimer>::Declare("TextTimer");
93 MethodOptions opt = MethodOptions::NONE;
94 JSClass<JSTextTimer>::StaticMethod("create", &JSTextTimer::Create, opt);
95 JSClass<JSTextTimer>::StaticMethod("format", &JSTextTimer::SetFormat);
96 JSClass<JSTextTimer>::StaticMethod("fontColor", &JSTextTimer::SetTextColor);
97 JSClass<JSTextTimer>::StaticMethod("fontSize", &JSTextTimer::SetFontSize);
98 JSClass<JSTextTimer>::StaticMethod("fontWeight", &JSTextTimer::SetFontWeight, opt);
99 JSClass<JSTextTimer>::StaticMethod("fontStyle", &JSTextTimer::SetFontStyle, opt);
100 JSClass<JSTextTimer>::StaticMethod("fontFamily", &JSTextTimer::SetFontFamily, opt);
101 JSClass<JSTextTimer>::StaticMethod("onTimer", &JSTextTimer::OnTimer);
102 JSClass<JSTextTimer>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
103 JSClass<JSTextTimer>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
104 JSClass<JSTextTimer>::Inherit<JSViewAbstract>();
105 JSClass<JSTextTimer>::Bind<>(globalObj);
106 }
107
SetFormat(const JSCallbackInfo & info)108 void JSTextTimer::SetFormat(const JSCallbackInfo& info)
109 {
110 if (info.Length() < 1) {
111 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
112 return;
113 }
114
115 if (!info[0]->IsString()) {
116 LOGE("arg is not string.");
117 return;
118 }
119
120 auto format = info[0]->ToString();
121 std::smatch result;
122 std::regex pattern("(([YyMdD]+))");
123 if (std::regex_search(format, result, pattern)) {
124 if (!result.empty()) {
125 format = DEFAULT_FORMAT;
126 }
127 }
128
129 auto pos = format.find("hh");
130 if (pos != std::string::npos) {
131 format.replace(pos, sizeof("hh") - 1, "HH");
132 }
133
134 TextTimerModel::GetInstance()->SetFormat(format);
135 }
136
SetFontSize(const JSCallbackInfo & info)137 void JSTextTimer::SetFontSize(const JSCallbackInfo& info)
138 {
139 if (info.Length() < 1) {
140 LOGE("JSTextInput::SetFontSize The argv is wrong, it is supposed to have at least 1 argument");
141 return;
142 }
143 Dimension fontSize;
144 if (!ParseJsDimensionFp(info[0], fontSize)) {
145 return;
146 }
147
148 TextTimerModel::GetInstance()->SetFontSize(fontSize);
149 }
150
SetTextColor(const JSCallbackInfo & info)151 void JSTextTimer::SetTextColor(const JSCallbackInfo& info)
152 {
153 if (info.Length() < 1) {
154 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
155 return;
156 }
157 Color textColor;
158 if (!ParseJsColor(info[0], textColor)) {
159 return;
160 }
161
162 TextTimerModel::GetInstance()->SetTextColor(textColor);
163 }
164
SetFontWeight(const std::string & value)165 void JSTextTimer::SetFontWeight(const std::string& value)
166 {
167 TextTimerModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(value));
168 }
169
SetFontStyle(int32_t value)170 void JSTextTimer::SetFontStyle(int32_t value)
171 {
172 if (value < 0 || value >= static_cast<int32_t>(FONT_STYLES.size())) {
173 LOGE("TextTimer fontStyle(%{public}d) illegal value", value);
174 return;
175 }
176 TextTimerModel::GetInstance()->SetItalicFontStyle(FONT_STYLES[value]);
177 }
178
SetFontFamily(const JSCallbackInfo & info)179 void JSTextTimer::SetFontFamily(const JSCallbackInfo& info)
180 {
181 if (info.Length() < 1) {
182 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
183 return;
184 }
185 std::vector<std::string> fontFamilies;
186 if (!ParseJsFontFamilies(info[0], fontFamilies)) {
187 LOGE("Parse FontFamilies failed");
188 return;
189 }
190 TextTimerModel::GetInstance()->SetFontFamily(fontFamilies);
191 }
192
OnTimer(const JSCallbackInfo & info)193 void JSTextTimer::OnTimer(const JSCallbackInfo& info)
194 {
195 CHECK_NULL_VOID(info[0]->IsFunction());
196 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
197 auto onChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
198 const std::string& utc, const std::string& elapsedTime) {
199 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
200 ACE_SCORING_EVENT("TextTimer.onTimer");
201 JSRef<JSVal> newJSVal[2];
202 newJSVal[0] = JSRef<JSVal>::Make(ToJSValue(utc));
203 newJSVal[1] = JSRef<JSVal>::Make(ToJSValue(elapsedTime));
204 func->ExecuteJS(2, newJSVal);
205 };
206 TextTimerModel::GetInstance()->SetOnTimer(std::move(onChange));
207 }
208
JSBind(BindingTarget globalObj)209 void JSTextTimerController::JSBind(BindingTarget globalObj)
210 {
211 JSClass<JSTextTimerController>::Declare("TextTimerController");
212 JSClass<JSTextTimerController>::CustomMethod("start", &JSTextTimerController::Start);
213 JSClass<JSTextTimerController>::CustomMethod("pause", &JSTextTimerController::Pause);
214 JSClass<JSTextTimerController>::CustomMethod("reset", &JSTextTimerController::Reset);
215 JSClass<JSTextTimerController>::Bind(
216 globalObj, JSTextTimerController::Constructor, JSTextTimerController::Destructor);
217 }
218
Constructor(const JSCallbackInfo & info)219 void JSTextTimerController::Constructor(const JSCallbackInfo& info)
220 {
221 auto timerController = Referenced::MakeRefPtr<JSTextTimerController>();
222 timerController->IncRefCount();
223 info.SetReturnValue(Referenced::RawPtr(timerController));
224 }
225
Destructor(JSTextTimerController * timerController)226 void JSTextTimerController::Destructor(JSTextTimerController* timerController)
227 {
228 if (timerController != nullptr) {
229 timerController->DecRefCount();
230 }
231 }
232
Start(const JSCallbackInfo & info)233 void JSTextTimerController::Start(const JSCallbackInfo& info)
234 {
235 if (controller_) {
236 controller_->Start();
237 }
238 }
239
Pause(const JSCallbackInfo & info)240 void JSTextTimerController::Pause(const JSCallbackInfo& info)
241 {
242 if (controller_) {
243 controller_->Pause();
244 }
245 }
246
Reset(const JSCallbackInfo & info)247 void JSTextTimerController::Reset(const JSCallbackInfo& info)
248 {
249 if (controller_) {
250 controller_->Reset();
251 }
252 }
253 } // namespace OHOS::Ace::Framework