• 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     uint16_t valueId = KeyParser::ParseKeyId(strValue, GetStyleStrValueLen(style));
56     switch (stylePropNameId) {
57         case K_FLEX_DIRECTION: {
58             switch (valueId) {
59                 case K_COLUMN:
60                     nativeView_.SetLayoutDirection(LAYOUT_VER);
61                     isVerticalLayout_ = true;
62                     break;
63                 case K_ROW:
64                     nativeView_.SetLayoutDirection(LAYOUT_HOR);
65                     break;
66                 case K_ROW_REVERSE:
67                     nativeView_.SetLayoutDirection(LAYOUT_HOR_R);
68                     break;
69                 case K_COLUMN_REVERSE:
70                     nativeView_.SetLayoutDirection(LAYOUT_VER_R);
71                     break;
72                 default:
73                     applyResult = false;
74                     break;
75             }
76             break;
77         }
78         case K_JUSTIFY_CONTENT: {
79             switch (valueId) {
80                 case K_FLEX_START:
81                     nativeView_.SetMajorAxisAlign(OHOS::ALIGN_START);
82                     break;
83                 case K_FLEX_END:
84                     nativeView_.SetMajorAxisAlign(OHOS::ALIGN_END);
85                     break;
86                 case K_CENTER:
87                     nativeView_.SetMajorAxisAlign(OHOS::ALIGN_CENTER);
88                     break;
89                 case K_SPACE_BETWEEN:
90                     nativeView_.SetMajorAxisAlign(OHOS::ALIGN_BETWEEN);
91                     break;
92                 case K_SPACE_AROUND:
93                     nativeView_.SetMajorAxisAlign(OHOS::ALIGN_AROUND);
94                     break;
95                 case K_SPACE_EVENLY:
96                     nativeView_.SetMajorAxisAlign(OHOS::ALIGN_EVENLY);
97                     break;
98                 default:
99                     applyResult = false;
100                     break;
101             }
102             break;
103         }
104         case K_ALIGN_ITEMS: {
105             switch (valueId) {
106                 case K_FLEX_START:
107                     nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_START);
108                     break;
109                 case K_FLEX_END:
110                     nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_END);
111                     break;
112                 case K_CENTER:
113                     nativeView_.SetSecondaryAxisAlign(OHOS::ALIGN_CENTER);
114                     break;
115                 default:
116                     applyResult = false;
117                     break;
118             }
119             if (applyResult)
120                 isSecondaryAxisAlignSet_ = true;
121             break;
122         }
123         case K_FLEX_WRAP: {
124             if (valueId == K_WRAP) {
125                 nativeView_.SetFlexWrap(FlexLayout::WRAP);
126             } else {
127                 nativeView_.SetFlexWrap(FlexLayout::NOWRAP);
128             }
129             break;
130         }
131         default:
132             applyResult = false;
133             break;
134     }
135 
136     return applyResult;
137 }
138 
ProcessChildren()139 bool DivComponent::ProcessChildren()
140 {
141     // add all children to this container
142     AppendChildren(this);
143 
144     return true;
145 }
146 
PostUpdate(uint16_t attrKeyId)147 void DivComponent::PostUpdate(uint16_t attrKeyId)
148 {
149     nativeView_.LayoutChildren();
150 }
151 
AttachView(const Component * child)152 void DivComponent::AttachView(const Component *child)
153 {
154     if (child == nullptr) {
155         return;
156     }
157     if (!isSecondaryAxisAlignSet_) {
158         ConstrainedParameter param;
159         child->GetConstrainedParam(param);
160         DimensionType type;
161         Component *component = const_cast<Component *>(child);
162         if (isVerticalLayout_) {
163             type = child->GetDimension(K_WIDTH).type;
164             if (type == DimensionType::TYPE_UNKNOWN) {
165                 const int16_t width = this->GetWidth();
166                 component->SetWidth(width);
167                 param.maxWidth = width;
168                 component->AlignDimensions(param);
169                 child->AdaptBoxSizing();
170             }
171         } else {
172             type = child->GetDimension(K_HEIGHT).type;
173             if (type == DimensionType::TYPE_UNKNOWN) {
174                 const int16_t height = this->GetHeight();
175                 component->SetHeight(height);
176                 param.maxHeight = height;
177                 component->AlignDimensions(param);
178                 child->AdaptBoxSizing();
179             }
180         }
181     }
182     nativeView_.Add(child->GetComponentRootView());
183 }
184 
LayoutChildren()185 void DivComponent::LayoutChildren()
186 {
187     nativeView_.LayoutChildren();
188 }
189 } // namespace ACELite
190 } // namespace OHOS
191