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 "bridge/declarative_frontend/ark_theme/theme_apply/js_loading_progress_theme.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/components_ng/base/view_abstract_model.h"
23 #include "core/components_ng/pattern/loading_progress/loading_progress_model.h"
24 #include "core/components_ng/pattern/loading_progress/loading_progress_model_ng.h"
25 
26 namespace OHOS::Ace {
27 std::unique_ptr<LoadingProgressModel> LoadingProgressModel::instance_ = nullptr;
28 std::mutex LoadingProgressModel::mutex_;
29 
GetInstance()30 LoadingProgressModel* LoadingProgressModel::GetInstance()
31 {
32     if (!instance_) {
33         std::lock_guard<std::mutex> lock(mutex_);
34         if (!instance_) {
35 #ifdef NG_BUILD
36             instance_.reset(new NG::LoadingProgressModelNG());
37 #else
38             if (Container::IsCurrentUseNewPipeline()) {
39                 instance_.reset(new NG::LoadingProgressModelNG());
40             } else {
41                 instance_.reset(new Framework::LoadingProgressModelImpl());
42             }
43 #endif
44         }
45     }
46     return instance_.get();
47 }
48 
49 } // namespace OHOS::Ace
50 
51 namespace OHOS::Ace::Framework {
52 namespace {} // namespace
53 
JSBind(BindingTarget globalObj)54 void JSLoadingProgress::JSBind(BindingTarget globalObj)
55 {
56     JSClass<JSLoadingProgress>::Declare("LoadingProgress");
57     MethodOptions opt = MethodOptions::NONE;
58 
59     JSClass<JSLoadingProgress>::StaticMethod("create", &JSLoadingProgress::Create, opt);
60     JSClass<JSLoadingProgress>::StaticMethod("color", &JSLoadingProgress::SetColor, opt);
61     JSClass<JSLoadingProgress>::StaticMethod("enableLoading", &JSLoadingProgress::SetEnableLoading, opt);
62     JSClass<JSLoadingProgress>::StaticMethod("foregroundColor", &JSLoadingProgress::SetForegroundColor, opt);
63 
64     JSClass<JSLoadingProgress>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
65     JSClass<JSLoadingProgress>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
66     JSClass<JSLoadingProgress>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
67     JSClass<JSLoadingProgress>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
68     JSClass<JSLoadingProgress>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
69     JSClass<JSLoadingProgress>::InheritAndBind<JSViewAbstract>(globalObj);
70 }
71 
Create()72 void JSLoadingProgress::Create()
73 {
74     LoadingProgressModel::GetInstance()->Create();
75     JSLoadingProgressTheme::ApplyTheme();
76 }
77 
SetColor(const JSCallbackInfo & info)78 void JSLoadingProgress::SetColor(const JSCallbackInfo& info)
79 {
80     Color progressColor;
81     if (!ParseJsColor(info[0], progressColor)) {
82         if (Container::GreatOrEqualAPIVersion(PlatformVersion::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