• 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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_ANIMATABLE_ARITHMETIC_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_ANIMATABLE_ARITHMETIC_H
18 
19 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
20 #include "bridge/declarative_frontend/engine/js_types.h"
21 #include "core/components_ng/animation/animatable_arithmetic.h"
22 
23 namespace OHOS::Ace::Framework {
24 using OHOS::Ace::NG::CustomAnimatableArithmetic;
25 
26 class JSAnimatableArithmetic : public CustomAnimatableArithmetic {
27     DECLARE_ACE_TYPE(JSAnimatableArithmetic, CustomAnimatableArithmetic)
28 public:
29     JSAnimatableArithmetic() = default;
30     ~JSAnimatableArithmetic() override = default;
31 
JSAnimatableArithmetic(const JSRef<JSObject> & jsObject,JSExecutionContext ctx)32     explicit JSAnimatableArithmetic(const JSRef<JSObject>& jsObject, JSExecutionContext ctx)
33         : jsObject_(jsObject), ctx_(ctx)
34     {
35         auto addVal = jsObject_->GetProperty("plus");
36         if (addVal->IsFunction()) {
37             addFunc_ = JSRef<JSFunc>::Cast(addVal);
38         } else {
39             LOGD("'plus' function does not exist on AnimatableArithmetic object");
40         }
41         auto minusVal = jsObject_->GetProperty("subtract");
42         if (minusVal->IsFunction()) {
43             minusFunc_ = JSRef<JSFunc>::Cast(minusVal);
44         } else {
45             LOGD("'minus' function does not exist on AnimatableArithmetic object");
46         }
47         auto multiplyVal = jsObject_->GetProperty("multiply");
48         if (multiplyVal->IsFunction()) {
49             multiplyFunc_ = JSRef<JSFunc>::Cast(multiplyVal);
50         } else {
51             LOGD("'multiply' function does not exist on AnimatableArithmetic object");
52         }
53         auto equalsVal = jsObject_->GetProperty("equals");
54         if (equalsVal->IsFunction()) {
55             equalsFunc_ = JSRef<JSFunc>::Cast(equalsVal);
56         } else {
57             LOGD("'equals' function does not exist on AnimatableArithmetic object");
58         }
59     }
60 
Add(const RefPtr<CustomAnimatableArithmetic> & value)61     RefPtr<CustomAnimatableArithmetic> Add(const RefPtr<CustomAnimatableArithmetic>& value) const override
62     {
63         RefPtr<JSAnimatableArithmetic> rhs = AceType::DynamicCast<JSAnimatableArithmetic>(value);
64         if (!rhs) {
65             return {};
66         }
67         JAVASCRIPT_EXECUTION_SCOPE(ctx_);
68         JSRef<JSVal> argv[1] = { rhs->jsObject_ };
69         auto retVal = addFunc_->Call(jsObject_, 1, argv);
70         if (!retVal->IsObject()) {
71             LOGE("add: result is not an object!");
72             return {};
73         }
74 
75         return MakeRefPtr<JSAnimatableArithmetic>(JSRef<JSObject>::Cast(retVal), ctx_);
76     }
77 
Minus(const RefPtr<CustomAnimatableArithmetic> & value)78     RefPtr<CustomAnimatableArithmetic> Minus(const RefPtr<CustomAnimatableArithmetic>& value) const override
79     {
80         RefPtr<JSAnimatableArithmetic> rhs = AceType::DynamicCast<JSAnimatableArithmetic>(value);
81         if (!rhs) {
82             return {};
83         }
84         JAVASCRIPT_EXECUTION_SCOPE(ctx_);
85         JSRef<JSVal> argv[1] = { rhs->jsObject_ };
86         auto retVal = minusFunc_->Call(jsObject_, 1, argv);
87         if (!retVal->IsObject()) {
88             LOGE("minus: result is not an object!");
89             return {};
90         }
91 
92         return MakeRefPtr<JSAnimatableArithmetic>(JSRef<JSObject>::Cast(retVal), ctx_);
93     }
94 
Multiply(const float scale)95     RefPtr<CustomAnimatableArithmetic> Multiply(const float scale) const override
96     {
97         JAVASCRIPT_EXECUTION_SCOPE(ctx_);
98         JSRef<JSVal> argv[1] = { JSRef<JSVal>::Make(ToJSValue(scale)) };
99         auto retVal = multiplyFunc_->Call(jsObject_, 1, argv);
100         if (!retVal->IsObject()) {
101             LOGE("multiply: result is not an object!");
102             return {};
103         }
104 
105         return MakeRefPtr<JSAnimatableArithmetic>(JSRef<JSObject>::Cast(retVal), ctx_);
106     }
107 
IsEqual(const RefPtr<CustomAnimatableArithmetic> & value)108     bool IsEqual(const RefPtr<CustomAnimatableArithmetic>& value) const override
109     {
110         RefPtr<JSAnimatableArithmetic> rhs = AceType::DynamicCast<JSAnimatableArithmetic>(value);
111         if (!rhs) {
112             return false;
113         }
114 
115         JAVASCRIPT_EXECUTION_SCOPE(ctx_);
116         JSRef<JSVal> argv[1] = { rhs->jsObject_ };
117         auto retVal = equalsFunc_->Call(jsObject_, 1, argv);
118         if (!retVal->IsBoolean()) {
119             LOGE("equals: result is not an boolean!");
120             return false;
121         }
122 
123         return retVal->ToBoolean();
124     }
125 
GetObject()126     JSRef<JSObject> GetObject() const
127     {
128         return jsObject_;
129     }
130 
131 private:
132     JSRef<JSObject> jsObject_;
133     JSExecutionContext ctx_;
134     JSRef<JSFunc> addFunc_;
135     JSRef<JSFunc> minusFunc_;
136     JSRef<JSFunc> multiplyFunc_;
137     JSRef<JSFunc> equalsFunc_;
138 };
139 } // namespace OHOS::Ace::Framework
140 
141 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_ANIMATABLE_ARITHMETIC_H
142