1 /*
2 * Copyright (c) 2024 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/cj_frontend/interfaces/cj_ffi/cj_flex_ffi.h"
17
18 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_view_abstract_ffi.h"
19 #include "core/components_ng/base/view_abstract_model_ng.h"
20 #include "core/components_ng/pattern/flex/flex_model_ng.h"
21
22 using namespace OHOS::Ace;
23 using namespace OHOS::Ace::Framework;
24
25 namespace {
26 const std::vector<FlexAlign> FLEX_ALIGNS = {
27 FlexAlign::AUTO,
28 FlexAlign::FLEX_START,
29 FlexAlign::CENTER,
30 FlexAlign::FLEX_END,
31 FlexAlign::SPACE_BETWEEN,
32 FlexAlign::SPACE_AROUND,
33 FlexAlign::SPACE_EVENLY
34 };
35 } // namespace
36
37 extern "C" {
FfiOHOSAceFrameworkFlexCreate()38 void FfiOHOSAceFrameworkFlexCreate()
39 {
40 FlexModel::GetInstance()->CreateFlexRow();
41 }
42
FfiOHOSAceFrameworkFlexCreateWithParams(CJFlexParams params)43 void FfiOHOSAceFrameworkFlexCreateWithParams(CJFlexParams params)
44 {
45 if (params.wrap == 0) {
46 FlexCreateFlexComponent(params);
47 } else {
48 FlexCreateWrapComponent(params);
49 }
50 }
51
FfiOHOSAceFrameworkFlexSetFlexWidth(double width,int32_t unit)52 void FfiOHOSAceFrameworkFlexSetFlexWidth(double width, int32_t unit)
53 {
54 Dimension value(width, static_cast<DimensionUnit>(unit));
55 FfiOHOSAceFrameworkViewAbstractSetWidth(width, unit);
56 FlexModel::GetInstance()->SetFlexWidth();
57 }
58
FfiOHOSAceFrameworkFlexSetFlexHeight(double height,int32_t unit)59 void FfiOHOSAceFrameworkFlexSetFlexHeight(double height, int32_t unit)
60 {
61 Dimension value(height, static_cast<DimensionUnit>(unit));
62 FfiOHOSAceFrameworkViewAbstractSetHeight(height, unit);
63 FlexModel::GetInstance()->SetFlexHeight();
64 }
65
FfiOHOSAceFrameworkFlexSetFlexSize(double width,int32_t widthUnit,double height,int32_t heightUnit)66 void FfiOHOSAceFrameworkFlexSetFlexSize(double width, int32_t widthUnit, double height, int32_t heightUnit)
67 {
68 Dimension widthDime(width, static_cast<DimensionUnit>(widthUnit));
69 Dimension heightDime(height, static_cast<DimensionUnit>(heightUnit));
70 FfiOHOSAceFrameworkViewAbstractSetWidth(width, widthUnit);
71 FlexModel::GetInstance()->SetFlexWidth();
72 FfiOHOSAceFrameworkViewAbstractSetHeight(height, heightUnit);
73 FlexModel::GetInstance()->SetFlexHeight();
74 }
75 }
76
77 namespace OHOS::Ace {
FlexCreateFlexComponent(CJFlexParams params)78 void FlexCreateFlexComponent(CJFlexParams params)
79 {
80 FlexModel::GetInstance()->CreateFlexRow();
81 if (params.direction >= 0 && params.direction <= DIRECTION_MAX_VALUE) {
82 FlexModel::GetInstance()->SetDirection(static_cast<FlexDirection>(params.direction));
83 }
84 auto justifyVal = static_cast<int32_t>(FLEX_ALIGNS[params.justifyContent]);
85 if (justifyVal >= 0 && justifyVal <= MAIN_ALIGN_MAX_VALUE) {
86 FlexModel::GetInstance()->SetMainAxisAlign(static_cast<FlexAlign>(justifyVal));
87 }
88 if (params.alignItems >= 0 && params.alignItems <= CROSS_ALIGN_MAX_VALUE) {
89 FlexModel::GetInstance()->SetCrossAxisAlign(static_cast<FlexAlign>(params.alignItems));
90 }
91 }
FlexCreateWrapComponent(CJFlexParams params)92 void FlexCreateWrapComponent(CJFlexParams params)
93 {
94 FlexModel::GetInstance()->CreateWrap();
95 if (params.direction >= 0 && params.direction <= DIRECTION_MAX_VALUE) {
96 FlexModel::GetInstance()->SetDirection(static_cast<FlexDirection>(params.direction));
97 // WrapReverse means wrapVal = 2. Wrap means wrapVal = 1.
98 constexpr int reverseDirection = 2;
99 if (params.direction <= 1) {
100 params.direction += reverseDirection * (params.wrap - 1);
101 } else {
102 params.direction -= reverseDirection * (params.wrap - 1);
103 }
104 FlexModel::GetInstance()->SetWrapDirection(static_cast<WrapDirection>(params.direction));
105 }
106 auto justifyVal = static_cast<int32_t>(FLEX_ALIGNS[params.justifyContent]);
107 if (justifyVal >= 0 && justifyVal <= MAIN_ALIGN_MAX_VALUE) {
108 FlexModel::GetInstance()->SetWrapMainAlignment(WRAP_TABLE[justifyVal]);
109 }
110 if (params.alignItems >= 0 && params.alignItems <= CROSS_ALIGN_MAX_VALUE) {
111 FlexModel::GetInstance()->SetWrapCrossAlignment(WRAP_TABLE[params.alignItems]);
112 }
113 auto alignContentVal = static_cast<int32_t>(FLEX_ALIGNS[params.alignContent]);
114 if (alignContentVal >= 0 && alignContentVal <= MAIN_ALIGN_MAX_VALUE) {
115 FlexModel::GetInstance()->SetWrapAlignment(WRAP_TABLE[alignContentVal]);
116 }
117 }
118 } // namespace OHOS::Ace
119