1 /*
2 * Copyright (c) 2021-2025 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("onAttach", &JSInteractableView::JsOnAttach);
64 JSClass<JSLoadingProgress>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
65 JSClass<JSLoadingProgress>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
66 JSClass<JSLoadingProgress>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
67 JSClass<JSLoadingProgress>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
68 JSClass<JSLoadingProgress>::InheritAndBind<JSViewAbstract>(globalObj);
69 }
70
Create()71 void JSLoadingProgress::Create()
72 {
73 LoadingProgressModel::GetInstance()->Create();
74 }
75
SetColor(const JSCallbackInfo & info)76 void JSLoadingProgress::SetColor(const JSCallbackInfo& info)
77 {
78 Color progressColor;
79 if (!ParseJsColor(info[0], progressColor)) {
80 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
81 RefPtr<ProgressTheme> progressTheme = GetTheme<ProgressTheme>();
82 CHECK_NULL_VOID(progressTheme);
83 progressColor = progressTheme->GetLoadingParseFailedColor();
84 } else {
85 return;
86 }
87 }
88
89 LoadingProgressModel::GetInstance()->SetColor(progressColor);
90 }
91
SetForegroundColor(const JSCallbackInfo & info)92 void JSLoadingProgress::SetForegroundColor(const JSCallbackInfo& info)
93 {
94 ForegroundColorStrategy strategy;
95 if (ParseJsColorStrategy(info[0], strategy)) {
96 ViewAbstractModel::GetInstance()->SetForegroundColorStrategy(strategy);
97 return;
98 }
99 Color progressColor;
100 if (!ParseJsColor(info[0], progressColor)) {
101 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
102 LoadingProgressModel::GetInstance()->SetForegroundColorParseFailed(true);
103 LoadingProgressModel::GetInstance()->ResetColor();
104 }
105 return;
106 }
107 LoadingProgressModel::GetInstance()->SetForegroundColorParseFailed(false);
108 LoadingProgressModel::GetInstance()->SetColor(progressColor);
109 }
110
SetEnableLoading(const JSCallbackInfo & info)111 void JSLoadingProgress::SetEnableLoading(const JSCallbackInfo& info)
112 {
113 bool enable = true;
114 if (info[0]->IsBoolean()) {
115 enable = info[0]->ToBoolean();
116 }
117 LoadingProgressModel::GetInstance()->SetEnableLoading(enable);
118 }
119 }; // namespace OHOS::Ace::Framework
120