• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "frameworks/bridge/declarative_frontend/jsview/js_effect_component.h"
17 
18 #include "bridge/declarative_frontend/jsview/models/effect_component_model_impl.h"
19 #include "core/components_ng/pattern/effect_component/effect_component_model_ng.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_utils.h"
22 
23 namespace OHOS::Ace {
24 std::unique_ptr<EffectComponentModel> EffectComponentModel::instance_ = nullptr;
25 std::mutex EffectComponentModel::mutex_;
26 
GetInstance()27 EffectComponentModel* EffectComponentModel::GetInstance()
28 {
29     if (!instance_) {
30         std::lock_guard<std::mutex> lock(mutex_);
31         if (!instance_) {
32 #ifdef NG_BUILD
33             instance_.reset(new NG::EffectComponentModelNG());
34 #else
35             if (Container::IsCurrentUseNewPipeline()) {
36                 instance_.reset(new NG::EffectComponentModelNG());
37             } else {
38                 // empty implementation
39                 instance_.reset(new Framework::EffectComponentModelImpl());
40             }
41 #endif
42         }
43     }
44     return instance_.get();
45 }
46 } // namespace OHOS::Ace
47 
48 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)49 void JSEffectComponent::Create(const JSCallbackInfo& info)
50 {
51     if (info.Length() <= 0 || !info[0]->IsObject()) {
52         EffectComponentModel::GetInstance()->Create();
53         return;
54     }
55     auto obj = JSRef<JSObject>::Cast(info[0]);
56     auto independentLayerVal = obj->GetProperty("effectLayer");
57     NG::EffectLayer independentLayer = NG::EffectLayer::NONE;
58     if (independentLayerVal->IsNumber()) {
59         int32_t tmpLayer = independentLayerVal->ToNumber<int32_t>();
60         if (tmpLayer >= static_cast<int32_t>(NG::EffectLayer::NONE) &&
61             tmpLayer <= static_cast<int32_t>(NG::EffectLayer::TEXT)) {
62             independentLayer = static_cast<NG::EffectLayer>(tmpLayer);
63             EffectComponentModel::GetInstance()->Create(independentLayer);
64             return;
65         }
66     }
67     EffectComponentModel::GetInstance()->Create();
68 }
69 
AlwaysSnapshot(const JSCallbackInfo & info)70 void JSEffectComponent::AlwaysSnapshot(const JSCallbackInfo& info)
71 {
72     if (info[0]->IsBoolean()) {
73         EffectComponentModel::GetInstance()->AlwaysSnapshot(info[0]->ToBoolean());
74         return;
75     }
76     EffectComponentModel::GetInstance()->AlwaysSnapshot(false);
77 }
78 
JSBind(BindingTarget globalObj)79 void JSEffectComponent::JSBind(BindingTarget globalObj)
80 {
81     JSClass<JSEffectComponent>::Declare("EffectComponent");
82     MethodOptions opt = MethodOptions::NONE;
83     JSClass<JSEffectComponent>::StaticMethod("create", &JSEffectComponent::Create, opt);
84     JSClass<JSEffectComponent>::StaticMethod("alwaysSnapshot", &JSEffectComponent::AlwaysSnapshot, opt);
85     JSClass<JSEffectComponent>::StaticMethod("pop", &JSContainerBase::Pop, opt);
86 
87     JSClass<JSEffectComponent>::InheritAndBind<JSViewAbstract>(globalObj);
88 }
89 } // namespace OHOS::Ace::Framework
90