• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_layoutable_view.h"
17 
18 namespace OHOS::Ace::Framework {
19 
20 constexpr float DEFAULT_LAYOUT_WEIGHT = 0.0f;
21 
JsPixelRound(const JSCallbackInfo & info)22 void JSLayoutableView::JsPixelRound(const JSCallbackInfo& info)
23 {
24     uint16_t value = 0;
25     JSRef<JSVal> arg = info[0];
26     if (!arg->IsObject()) {
27         return;
28     }
29     JSRef<JSObject> object = JSRef<JSObject>::Cast(arg);
30     ParsePixelRoundOfStart(object, value);
31     ParsePixelRoundOfTop(object, value);
32     ParsePixelRoundOfEnd(object, value);
33     ParsePixelRoundOfBottom(object, value);
34     ViewAbstractModel::GetInstance()->SetPixelRound(value);
35 }
36 
JsChainWeight(const JSCallbackInfo & info)37 void JSLayoutableView::JsChainWeight(const JSCallbackInfo& info)
38 {
39     NG::ChainWeightPair chainWeightPair(DEFAULT_LAYOUT_WEIGHT, DEFAULT_LAYOUT_WEIGHT);
40     auto jsVal = info[0];
41     if (jsVal->IsObject()) {
42         JSRef<JSObject> val = JSRef<JSObject>::Cast(jsVal);
43         auto weightX = val->GetProperty("horizontal");
44         auto weightY = val->GetProperty("vertical");
45         if (weightX->IsNumber()) {
46             chainWeightPair.first = weightX->ToNumber<float>();
47         }
48         if (weightY->IsNumber()) {
49             chainWeightPair.second = weightY->ToNumber<float>();
50         }
51     }
52     ViewAbstractModel::GetInstance()->SetChainWeight(chainWeightPair);
53 }
54 
ParsePixelRoundOfStart(const JSRef<JSObject> & object,uint16_t & value)55 void JSLayoutableView::ParsePixelRoundOfStart(const JSRef<JSObject>& object, uint16_t& value)
56 {
57     JSRef<JSVal> jsStartValue = object->GetProperty("start");
58     if (jsStartValue->IsNumber()) {
59         int32_t startValue = jsStartValue->ToNumber<int32_t>();
60         if (PixelRoundCalcPolicy::FORCE_CEIL == static_cast<PixelRoundCalcPolicy>(startValue)) {
61             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_CEIL_START);
62         } else if (PixelRoundCalcPolicy::FORCE_FLOOR == static_cast<PixelRoundCalcPolicy>(startValue)) {
63             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_FLOOR_START);
64         } else if (PixelRoundCalcPolicy::NO_FORCE_ROUND == static_cast<PixelRoundCalcPolicy>(startValue)) {
65             value |= static_cast<uint16_t>(PixelRoundPolicy::NO_FORCE_ROUND_START);
66         }
67     }
68 }
69 
ParsePixelRoundOfTop(const JSRef<JSObject> & object,uint16_t & value)70 void JSLayoutableView::ParsePixelRoundOfTop(const JSRef<JSObject>& object, uint16_t& value)
71 {
72     JSRef<JSVal> jsTopValue = object->GetProperty("top");
73     if (jsTopValue->IsNumber()) {
74         int32_t topValue = jsTopValue->ToNumber<int32_t>();
75         if (PixelRoundCalcPolicy::FORCE_CEIL == static_cast<PixelRoundCalcPolicy>(topValue)) {
76             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_CEIL_TOP);
77         } else if (PixelRoundCalcPolicy::FORCE_FLOOR == static_cast<PixelRoundCalcPolicy>(topValue)) {
78             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_FLOOR_TOP);
79         } else if (PixelRoundCalcPolicy::NO_FORCE_ROUND == static_cast<PixelRoundCalcPolicy>(topValue)) {
80             value |= static_cast<uint16_t>(PixelRoundPolicy::NO_FORCE_ROUND_TOP);
81         }
82     }
83 }
84 
ParsePixelRoundOfEnd(const JSRef<JSObject> & object,uint16_t & value)85 void JSLayoutableView::ParsePixelRoundOfEnd(const JSRef<JSObject>& object, uint16_t& value)
86 {
87     JSRef<JSVal> jsEndValue = object->GetProperty("end");
88     if (jsEndValue->IsNumber()) {
89         int32_t endValue = jsEndValue->ToNumber<int32_t>();
90         if (PixelRoundCalcPolicy::FORCE_CEIL == static_cast<PixelRoundCalcPolicy>(endValue)) {
91             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_CEIL_END);
92         } else if (PixelRoundCalcPolicy::FORCE_FLOOR == static_cast<PixelRoundCalcPolicy>(endValue)) {
93             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_FLOOR_END);
94         } else if (PixelRoundCalcPolicy::NO_FORCE_ROUND == static_cast<PixelRoundCalcPolicy>(endValue)) {
95             value |= static_cast<uint16_t>(PixelRoundPolicy::NO_FORCE_ROUND_END);
96         }
97     }
98 }
99 
ParsePixelRoundOfBottom(const JSRef<JSObject> & object,uint16_t & value)100 void JSLayoutableView::ParsePixelRoundOfBottom(const JSRef<JSObject>& object, uint16_t& value)
101 {
102     JSRef<JSVal> jsBottomValue = object->GetProperty("bottom");
103     if (jsBottomValue->IsNumber()) {
104         int32_t bottomValue = jsBottomValue->ToNumber<int32_t>();
105         if (PixelRoundCalcPolicy::FORCE_CEIL == static_cast<PixelRoundCalcPolicy>(bottomValue)) {
106             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_CEIL_BOTTOM);
107         } else if (PixelRoundCalcPolicy::FORCE_FLOOR == static_cast<PixelRoundCalcPolicy>(bottomValue)) {
108             value |= static_cast<uint16_t>(PixelRoundPolicy::FORCE_FLOOR_BOTTOM);
109         } else if (PixelRoundCalcPolicy::NO_FORCE_ROUND == static_cast<PixelRoundCalcPolicy>(bottomValue)) {
110             value |= static_cast<uint16_t>(PixelRoundPolicy::NO_FORCE_ROUND_BOTTOM);
111         }
112     }
113 }
114 
115 } // namespace OHOS::Ace::Framework