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 "bridge/declarative_frontend/jsview/js_linear_gradient.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
19 
20 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)21 void JSLinearGradient::JSBind(BindingTarget globalObj)
22 {
23     JSClass<JSLinearGradient>::Declare("LinearGradient");
24     JSClass<JSLinearGradient>::Bind(globalObj, JSLinearGradient::Constructor, JSLinearGradient::Destructor);
25 }
26 
Constructor(const JSCallbackInfo & args)27 void JSLinearGradient::Constructor(const JSCallbackInfo& args)
28 {
29     auto jsLinearGradientPtr = Referenced::MakeRefPtr<JSLinearGradient>();
30     jsLinearGradientPtr->IncRefCount();
31     args.SetReturnValue(Referenced::RawPtr(jsLinearGradientPtr));
32 
33     if (args.Length() < 1) {
34         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
35         return;
36     }
37 
38     if (!args[0]->IsArray()) {
39         return;
40     }
41 
42     auto paramArray = JSRef<JSArray>::Cast(args[0]);
43     size_t length = paramArray->Length();
44     for (size_t i = 0; i < length; i++) {
45         auto item = paramArray->GetValueAt(i);
46         if (!item->IsObject()) {
47             return;
48         }
49         auto itemObject = JSRef<JSObject>::Cast(item);
50         JSRef<JSVal> jsColor = itemObject->GetProperty("color");
51         Color color;
52         if (!JSViewAbstract::ParseJsColor(jsColor, color)) {
53             return;
54         }
55         JSRef<JSVal> jsOffset = itemObject->GetProperty("offset");
56         CalcDimension offset;
57         if (!JSViewAbstract::ParseJsDimensionVp(jsOffset, offset)) {
58             return;
59         }
60 
61         if (Negative(offset.ConvertToVp())) {
62             offset = Dimension(0.0, DimensionUnit::VP);
63         }
64 
65         if (GreatNotEqual(offset.ConvertToVp(), 1.0)) {
66             offset = Dimension(1.0, DimensionUnit::VP);
67         }
68         jsLinearGradientPtr->gradient_.push_back(std::make_pair(color, offset));
69     }
70 }
71 
Destructor(JSLinearGradient * jsLinearGradientPtr)72 void JSLinearGradient::Destructor(JSLinearGradient* jsLinearGradientPtr)
73 {
74     if (jsLinearGradientPtr != nullptr) {
75         jsLinearGradientPtr->DecRefCount();
76     }
77 }
78 } // namespace OHOS::Ace::Framework
79