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 {
52 constexpr int32_t PLATFORM_VERSION_TEN = 10;
53 } // namespace
54
JSBind(BindingTarget globalObj)55 void JSLoadingProgress::JSBind(BindingTarget globalObj)
56 {
57 JSClass<JSLoadingProgress>::Declare("LoadingProgress");
58 MethodOptions opt = MethodOptions::NONE;
59
60 JSClass<JSLoadingProgress>::StaticMethod("create", &JSLoadingProgress::Create, opt);
61 JSClass<JSLoadingProgress>::StaticMethod("color", &JSLoadingProgress::SetColor, opt);
62 JSClass<JSLoadingProgress>::StaticMethod("enableLoading", &JSLoadingProgress::SetEnableLoading, opt);
63 JSClass<JSLoadingProgress>::StaticMethod("foregroundColor", &JSLoadingProgress::SetForegroundColor, opt);
64
65 JSClass<JSLoadingProgress>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
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 auto pipeline = PipelineBase::GetCurrentContext();
81 CHECK_NULL_VOID(pipeline);
82 if (pipeline->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN) {
83 RefPtr<ProgressTheme> progressTheme = GetTheme<ProgressTheme>();
84 CHECK_NULL_VOID(progressTheme);
85 progressColor = progressTheme->GetLoadingColor();
86 } else {
87 return;
88 }
89 }
90
91 LoadingProgressModel::GetInstance()->SetColor(progressColor);
92 }
93
SetForegroundColor(const JSCallbackInfo & info)94 void JSLoadingProgress::SetForegroundColor(const JSCallbackInfo& info)
95 {
96 ForegroundColorStrategy strategy;
97 if (ParseJsColorStrategy(info[0], strategy)) {
98 ViewAbstractModel::GetInstance()->SetForegroundColorStrategy(strategy);
99 return;
100 }
101 Color progressColor;
102 if (!ParseJsColor(info[0], progressColor)) {
103 return;
104 }
105 LoadingProgressModel::GetInstance()->SetColor(progressColor);
106 }
107
SetEnableLoading(const JSCallbackInfo & info)108 void JSLoadingProgress::SetEnableLoading(const JSCallbackInfo& info)
109 {
110 bool enable = true;
111 if (info[0]->IsBoolean()) {
112 enable = info[0]->ToBoolean();
113 }
114 LoadingProgressModel::GetInstance()->SetEnableLoading(enable);
115 }
116 }; // namespace OHOS::Ace::Framework
117