1 /*
2 * Copyright (c) 2024 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_linear_indicator_controller.h"
17
18 #include "base/utils/utils.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20 #include "core/components_ng/pattern/linear_indicator/linear_indicator_theme.h"
21
22 namespace OHOS::Ace::Framework {
23
JSBind(BindingTarget globalObj)24 void JSLinearIndicatorController::JSBind(BindingTarget globalObj)
25 {
26 JSClass<JSLinearIndicatorController>::Declare("LinearIndicatorController");
27 JSClass<JSLinearIndicatorController>::CustomMethod("setProgress", &JSLinearIndicatorController::SetProgress);
28 JSClass<JSLinearIndicatorController>::CustomMethod("start", &JSLinearIndicatorController::Start);
29 JSClass<JSLinearIndicatorController>::CustomMethod("pause", &JSLinearIndicatorController::Pause);
30 JSClass<JSLinearIndicatorController>::CustomMethod("stop", &JSLinearIndicatorController::Stop);
31 JSClass<JSLinearIndicatorController>::Bind(
32 globalObj, JSLinearIndicatorController::Constructor, JSLinearIndicatorController::Destructor);
33 }
34
Constructor(const JSCallbackInfo & args)35 void JSLinearIndicatorController::Constructor(const JSCallbackInfo& args)
36 {
37 auto linearIndicatorController = Referenced::MakeRefPtr<JSLinearIndicatorController>();
38 linearIndicatorController->IncRefCount();
39 args.SetReturnValue(Referenced::RawPtr(linearIndicatorController));
40 }
41
Destructor(JSLinearIndicatorController * linearIndicatorController)42 void JSLinearIndicatorController::Destructor(JSLinearIndicatorController* linearIndicatorController)
43 {
44 if (linearIndicatorController) {
45 linearIndicatorController->DecRefCount();
46 }
47 }
48
SetProgress(const JSCallbackInfo & args)49 void JSLinearIndicatorController::SetProgress(const JSCallbackInfo& args)
50 {
51 if (args.Length() < 2) {
52 return;
53 }
54 auto arg0 = args[0];
55 auto arg1 = args[1];
56 int32_t index = 0;
57 float value = .0f;
58 if (arg0->IsUndefined() || arg0->IsNull() || (!ConvertFromJSValue(arg0, index))) {
59 index = 0;
60 }
61 if (arg1->IsUndefined() || arg1->IsNull() ||(!ConvertFromJSValue(arg1, value))) {
62 value = .0f;
63 }
64 if (linearIndicatorController_) {
65 linearIndicatorController_->SetProgress(index, value);
66 }
67 }
68
Start(const JSCallbackInfo & args)69 void JSLinearIndicatorController::Start(const JSCallbackInfo& args)
70 {
71 auto pipeline = PipelineContext::GetCurrentContextSafelyWithCheck();
72 CHECK_NULL_VOID(pipeline);
73 auto theme = pipeline->GetThemeManager()->GetTheme<NG::LinearIndicatorTheme>();
74 CHECK_NULL_VOID(theme);
75 int32_t defaultDuration = theme->GetDefaultDurationTime();
76 int32_t defaultInterval = theme->GetDefaultIntervalTime();
77 int32_t duration = defaultDuration;
78 int32_t interval = defaultInterval;
79 if (args.Length() > 0) {
80 auto arg0 = args[0];
81 if (arg0->IsObject()) {
82 JSRef<JSObject> obj = JSRef<JSObject>::Cast(arg0);
83 if (!ConvertFromJSValue(obj->GetProperty("duration"), duration)) {
84 duration = defaultDuration;
85 }
86 if (!ConvertFromJSValue(obj->GetProperty("interval"), interval)) {
87 interval = defaultInterval;
88 }
89 }
90 }
91 if (linearIndicatorController_) {
92 linearIndicatorController_->Start(duration, interval);
93 }
94 }
95
Pause(const JSCallbackInfo & args)96 void JSLinearIndicatorController::Pause(const JSCallbackInfo& args)
97 {
98 if (linearIndicatorController_) {
99 linearIndicatorController_->Pause();
100 }
101 }
102
Stop(const JSCallbackInfo & args)103 void JSLinearIndicatorController::Stop(const JSCallbackInfo& args)
104 {
105 if (linearIndicatorController_) {
106 linearIndicatorController_->Stop();
107 }
108 }
109
110 } // namespace OHOS::Ace::Framework
111