• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 "div_component.h"
17 #include "ace_log.h"
18 #include "key_parser.h"
19 #include "keys.h"
20 
21 namespace OHOS {
22 namespace ACELite {
DivComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)23 DivComponent::DivComponent(jerry_value_t options,
24                            jerry_value_t children,
25                            AppStyleManager* styleManager)
26     : Component(options, children, styleManager),
27       isSecondaryAxisAlignSet_(false), isVerticalLayout_(false)
28 {
29     SetComponentName(K_DIV);
30     nativeView_.SetStyle(STYLE_BACKGROUND_OPA, 0);
31 }
32 
33 
GetComponentRootView() const34 inline UIView *DivComponent::GetComponentRootView() const
35 {
36     return const_cast<FlexLayout *>(&nativeView_);
37 }
38 
ApplyPrivateStyle(const AppStyleItem * style)39 bool DivComponent::ApplyPrivateStyle(const AppStyleItem* style)
40 {
41     // Set default value
42     if (!isSecondaryAxisAlignSet_) {
43         nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_START);
44     }
45     uint16_t stylePropNameId = GetStylePropNameId(style);
46     if (!KeyParser::IsKeyValid(stylePropNameId)) {
47         return false;
48     }
49     const char * const strValue = GetStyleStrValue(style);
50     if (strValue == nullptr) {
51         return false;
52     }
53 
54     bool applyResult = true;
55     NativeViewSetDirection(style, stylePropNameId, strValue, applyResult);
56     return applyResult;
57 }
58 
NativeViewSetDirection(const AppStyleItem * style,uint16_t stylePropNameId,const char * const strValue,bool & applyResult)59 void DivComponent::NativeViewSetDirection(const AppStyleItem* style, uint16_t stylePropNameId,
60                                           const char * const strValue, bool& applyResult)
61 {
62     uint16_t valueId = KeyParser::ParseKeyId(strValue, GetStyleStrValueLen(style));
63     switch (stylePropNameId) {
64         case K_FLEX_DIRECTION: {
65             NativeViewSetLayoutDirection(valueId, applyResult);
66             break;
67         }
68         case K_JUSTIFY_CONTENT: {
69             NativeViewSetMajorAxisAlign(valueId, applyResult);
70             break;
71         }
72         case K_ALIGN_ITEMS: {
73             switch (valueId) {
74                 case K_FLEX_START:
75                     nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_START);
76                     break;
77                 case K_FLEX_END:
78                     nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_END);
79                     break;
80                 case K_CENTER:
81                     nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_CENTER);
82                     break;
83                 default:
84                     applyResult = false;
85                     break;
86             }
87             if (applyResult) {
88                 isSecondaryAxisAlignSet_ = true;
89             }
90             break;
91         }
92         case K_FLEX_WRAP: {
93             if (valueId == K_WRAP) {
94                 nativeView_.SetFlexWrap(FlexLayout::WRAP);
95             } else {
96                 nativeView_.SetFlexWrap(FlexLayout::NOWRAP);
97             }
98             break;
99         }
100         default:
101             applyResult = false;
102             break;
103     }
104 }
105 
NativeViewSetLayoutDirection(uint16_t valueId,bool & applyResult)106 void DivComponent::NativeViewSetLayoutDirection(uint16_t valueId, bool& applyResult)
107 {
108     switch (valueId) {
109         case K_COLUMN:
110             nativeView_.SetLayoutDirection(LAYOUT_VER);
111             isVerticalLayout_ = true;
112             break;
113         case K_ROW:
114             nativeView_.SetLayoutDirection(LAYOUT_HOR);
115             break;
116         case K_ROW_REVERSE:
117             nativeView_.SetLayoutDirection(LAYOUT_HOR_R);
118             break;
119         case K_COLUMN_REVERSE:
120             nativeView_.SetLayoutDirection(LAYOUT_VER_R);
121             break;
122         default:
123             applyResult = false;
124             break;
125     }
126 }
127 
NativeViewSetMajorAxisAlign(uint16_t valueId,bool & applyResult)128 void DivComponent::NativeViewSetMajorAxisAlign(uint16_t valueId, bool& applyResult)
129 {
130     switch (valueId) {
131         case K_FLEX_START:
132             nativeView_.SetMajorAxisAlign(OHOS::ALIGN_START);
133             break;
134         case K_FLEX_END:
135             nativeView_.SetMajorAxisAlign(OHOS::ALIGN_END);
136             break;
137         case K_CENTER:
138             nativeView_.SetMajorAxisAlign(OHOS::ALIGN_CENTER);
139             break;
140         case K_SPACE_BETWEEN:
141             nativeView_.SetMajorAxisAlign(OHOS::ALIGN_BETWEEN);
142             break;
143         case K_SPACE_AROUND:
144             nativeView_.SetMajorAxisAlign(OHOS::ALIGN_AROUND);
145             break;
146         case K_SPACE_EVENLY:
147             nativeView_.SetMajorAxisAlign(OHOS::ALIGN_EVENLY);
148             break;
149         default:
150             applyResult = false;
151             break;
152     }
153 }
154 
ProcessChildren()155 bool DivComponent::ProcessChildren()
156 {
157     // add all children to this container
158     AppendChildren(this);
159 
160     return true;
161 }
162 
PostUpdate(uint16_t attrKeyId)163 void DivComponent::PostUpdate(uint16_t attrKeyId)
164 {
165     nativeView_.LayoutChildren();
166 }
167 
AttachView(const Component * child)168 void DivComponent::AttachView(const Component *child)
169 {
170     if (child == nullptr) {
171         return;
172     }
173     if (!isSecondaryAxisAlignSet_) {
174         ConstrainedParameter param;
175         child->GetConstrainedParam(param);
176         DimensionType type;
177         Component *component = const_cast<Component *>(child);
178         if (isVerticalLayout_) {
179             type = child->GetDimension(K_WIDTH).type;
180             if (type == DimensionType::TYPE_UNKNOWN) {
181                 const int16_t width = this->GetWidth();
182                 component->SetWidth(width);
183                 param.maxWidth = width;
184                 component->AlignDimensions(param);
185                 child->AdaptBoxSizing();
186             }
187         } else {
188             type = child->GetDimension(K_HEIGHT).type;
189             if (type == DimensionType::TYPE_UNKNOWN) {
190                 const int16_t height = this->GetHeight();
191                 component->SetHeight(height);
192                 param.maxHeight = height;
193                 component->AlignDimensions(param);
194                 child->AdaptBoxSizing();
195             }
196         }
197     }
198     nativeView_.Add(child->GetComponentRootView());
199 }
200 
LayoutChildren()201 void DivComponent::LayoutChildren()
202 {
203     nativeView_.LayoutChildren();
204 }
205 } // namespace ACELite
206 } // namespace OHOS
207