1 /*
2 * Copyright (c) 2021-2023 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 "bridge/declarative_frontend/jsview/js_loading_progress.h"
17
18 #include "base/utils/utils.h"
19 #include "bridge/declarative_frontend/jsview/models/loading_progress_model_impl.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components_ng/base/view_abstract_model.h"
22 #include "core/components_ng/pattern/loading_progress/loading_progress_model.h"
23 #include "core/components_ng/pattern/loading_progress/loading_progress_model_ng.h"
24
25 namespace OHOS::Ace {
26 std::unique_ptr<LoadingProgressModel> LoadingProgressModel::instance_ = nullptr;
27 std::mutex LoadingProgressModel::mutex_;
28
GetInstance()29 LoadingProgressModel* LoadingProgressModel::GetInstance()
30 {
31 if (!instance_) {
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (!instance_) {
34 #ifdef NG_BUILD
35 instance_.reset(new NG::LoadingProgressModelNG());
36 #else
37 if (Container::IsCurrentUseNewPipeline()) {
38 instance_.reset(new NG::LoadingProgressModelNG());
39 } else {
40 instance_.reset(new Framework::LoadingProgressModelImpl());
41 }
42 #endif
43 }
44 }
45 return instance_.get();
46 }
47
48 } // namespace OHOS::Ace
49
50 namespace OHOS::Ace::Framework {
51 namespace {} // namespace
52
JSBind(BindingTarget globalObj)53 void JSLoadingProgress::JSBind(BindingTarget globalObj)
54 {
55 JSClass<JSLoadingProgress>::Declare("LoadingProgress");
56 MethodOptions opt = MethodOptions::NONE;
57
58 JSClass<JSLoadingProgress>::StaticMethod("create", &JSLoadingProgress::Create, opt);
59 JSClass<JSLoadingProgress>::StaticMethod("color", &JSLoadingProgress::SetColor, opt);
60 JSClass<JSLoadingProgress>::StaticMethod("enableLoading", &JSLoadingProgress::SetEnableLoading, opt);
61 JSClass<JSLoadingProgress>::StaticMethod("foregroundColor", &JSLoadingProgress::SetForegroundColor, opt);
62
63 JSClass<JSLoadingProgress>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
64 JSClass<JSLoadingProgress>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
65 JSClass<JSLoadingProgress>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
66 JSClass<JSLoadingProgress>::InheritAndBind<JSViewAbstract>(globalObj);
67 }
68
Create()69 void JSLoadingProgress::Create()
70 {
71 LoadingProgressModel::GetInstance()->Create();
72 }
73
SetColor(const JSCallbackInfo & info)74 void JSLoadingProgress::SetColor(const JSCallbackInfo& info)
75 {
76 Color progressColor;
77 if (!ParseJsColor(info[0], progressColor)) {
78 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
79 RefPtr<ProgressTheme> progressTheme = GetTheme<ProgressTheme>();
80 CHECK_NULL_VOID(progressTheme);
81 progressColor = progressTheme->GetLoadingColor();
82 } else {
83 return;
84 }
85 }
86
87 LoadingProgressModel::GetInstance()->SetColor(progressColor);
88 }
89
SetForegroundColor(const JSCallbackInfo & info)90 void JSLoadingProgress::SetForegroundColor(const JSCallbackInfo& info)
91 {
92 ForegroundColorStrategy strategy;
93 if (ParseJsColorStrategy(info[0], strategy)) {
94 ViewAbstractModel::GetInstance()->SetForegroundColorStrategy(strategy);
95 return;
96 }
97 Color progressColor;
98 if (!ParseJsColor(info[0], progressColor)) {
99 return;
100 }
101 LoadingProgressModel::GetInstance()->SetColor(progressColor);
102 }
103
SetEnableLoading(const JSCallbackInfo & info)104 void JSLoadingProgress::SetEnableLoading(const JSCallbackInfo& info)
105 {
106 bool enable = true;
107 if (info[0]->IsBoolean()) {
108 enable = info[0]->ToBoolean();
109 }
110 LoadingProgressModel::GetInstance()->SetEnableLoading(enable);
111 }
112 }; // namespace OHOS::Ace::Framework
113