1 /*
2 * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_UTILS_H
18
19 #include <string>
20 #include <vector>
21
22 #include "core/components/common/layout/constants.h"
23 #include "core/components/common/layout/align_declaration.h"
24
25 namespace OHOS::Ace::V2 {
26
27 namespace {
28
29 const char* AXIS_TYPE[] = {
30 "Axis.VERTICAL",
31 "Axis.HORIZONTAL",
32 "Axis.FREE",
33 "Axis.NONE",
34 };
35
36 }
37
ConvertStackFitToString(StackFit stackFit)38 inline std::string ConvertStackFitToString(StackFit stackFit)
39 {
40 std::string result = "";
41 switch (stackFit) {
42 case StackFit::STRETCH:
43 result = "StackFit.Stretch";
44 break;
45 case StackFit::INHERIT:
46 result = "StackFit.Inherit";
47 break;
48 case StackFit::FIRST_CHILD:
49 result = "StackFit.FirstChild";
50 break;
51 case StackFit::KEEP:
52 default:
53 result = "StackFit.Keep";
54 break;
55 }
56 return result;
57 }
58
ConvertOverflowToString(Overflow overflow)59 inline std::string ConvertOverflowToString(Overflow overflow)
60 {
61 std::string result = "";
62 switch (overflow) {
63 case Overflow::CLIP:
64 result = "Overflow.Clip";
65 break;
66 case Overflow::SCROLL:
67 result = "Overflow.Scroll";
68 break;
69 case Overflow::FORCE_CLIP:
70 result = "Overflow.ForceClip";
71 break;
72 case Overflow::OBSERVABLE:
73 default:
74 result = "Overflow.Observable";
75 break;
76 }
77 return result;
78 }
79
ConvertBoolToString(bool flag)80 inline std::string ConvertBoolToString(bool flag)
81 {
82 return flag ? "true" : "false";
83 }
84
ConvertTabBarModeToString(TabBarMode barMode)85 inline std::string ConvertTabBarModeToString(TabBarMode barMode)
86 {
87 std::string result = "";
88 switch (barMode) {
89 case TabBarMode::SCROLLABEL:
90 result = "BarMode.Scrollable";
91 break;
92 case TabBarMode::FIXED_START:
93 result = "BarMode.FixedStart";
94 break;
95 case TabBarMode::FIXED:
96 default:
97 result = "BarMode.Fixed";
98 break;
99 }
100 return result;
101 }
102
ConvertBarPositionToString(BarPosition barPosition)103 inline std::string ConvertBarPositionToString(BarPosition barPosition)
104 {
105 std::string result = "";
106 switch (barPosition) {
107 case BarPosition::END:
108 result = "BarPosition.End";
109 break;
110 case BarPosition::START:
111 default:
112 result = "BarPosition.Start";
113 break;
114 }
115 return result;
116 }
117
ConvertFlexDirectionToStirng(FlexDirection direction)118 inline std::string ConvertFlexDirectionToStirng(FlexDirection direction)
119 {
120 if (direction == FlexDirection::ROW) {
121 return "FlexDirection.Row";
122 } else if (direction == FlexDirection::ROW_REVERSE) {
123 return "FlexDirection.RowReverse";
124 } else if (direction == FlexDirection::COLUMN) {
125 return "FlexDirection.Column";
126 } else {
127 return "FlexDirection.ColumnReverse";
128 }
129 }
130
ConvertWrapDirectionToStirng(WrapDirection direction)131 inline std::string ConvertWrapDirectionToStirng(WrapDirection direction)
132 {
133 if (direction == WrapDirection::HORIZONTAL) {
134 return "FlexDirection.Row";
135 } else if (direction == WrapDirection::HORIZONTAL_REVERSE) {
136 return "FlexDirection.RowReverse";
137 } else if (direction == WrapDirection::VERTICAL) {
138 return "FlexDirection.Column";
139 } else {
140 return "FlexDirection.ColumnReverse";
141 }
142 }
143
ConvertFlexAlignToStirng(FlexAlign align)144 inline std::string ConvertFlexAlignToStirng(FlexAlign align)
145 {
146 if (align == FlexAlign::FLEX_START) {
147 return "FlexAlign.Start";
148 } else if (align == FlexAlign::CENTER) {
149 return "FlexAlign.Center";
150 } else if (align == FlexAlign::FLEX_END) {
151 return "FlexAlign.End";
152 } else if (align == FlexAlign::SPACE_BETWEEN) {
153 return "FlexAlign.SpaceBetween";
154 } else if (align == FlexAlign::SPACE_AROUND) {
155 return "FlexAlign.SpaceAround";
156 } else if (align == FlexAlign::BASELINE) {
157 return "FlexAlign.Baseline";
158 } else if (align == FlexAlign::STRETCH) {
159 return "FlexAlign.Stretch";
160 } else {
161 return "FlexAlign.SpaceEvenly";
162 }
163 }
164
ConvertWrapAlignmentToStirng(WrapAlignment align)165 inline std::string ConvertWrapAlignmentToStirng(WrapAlignment align)
166 {
167 if (align == WrapAlignment::START) {
168 return "FlexAlign.Start";
169 } else if (align == WrapAlignment::CENTER) {
170 return "FlexAlign.Center";
171 } else if (align == WrapAlignment::END) {
172 return "FlexAlign.End";
173 } else if (align == WrapAlignment::SPACE_BETWEEN) {
174 return "FlexAlign.SpaceBetween";
175 } else if (align == WrapAlignment::SPACE_AROUND) {
176 return "FlexAlign.SpaceAround";
177 } else if (align == WrapAlignment::STRETCH) {
178 return "FlexAlign.Stretch";
179 } else if (align == WrapAlignment::BASELINE) {
180 return "FlexAlign.Baseline";
181 } else {
182 return "FlexAlign.SpaceEvenly";
183 }
184 }
185
ConvertWrapTextDecorationToStirng(TextDecoration decoration)186 inline std::string ConvertWrapTextDecorationToStirng(TextDecoration decoration)
187 {
188 static const LinearEnumMapNode<TextDecoration, std::string> decorationTable[] = {
189 { TextDecoration::NONE, "TextDecorationType.None" },
190 { TextDecoration::UNDERLINE, "TextDecorationType.Underline" },
191 { TextDecoration::OVERLINE, "TextDecorationType.Overline" },
192 { TextDecoration::LINE_THROUGH, "TextDecorationType.LineThrough" },
193 { TextDecoration::INHERIT, "TextDecorationType.Inherit" },
194 };
195
196 auto index = BinarySearchFindIndex(decorationTable, ArraySize(decorationTable), decoration);
197 return index < 0 ? "TextDecorationType.None" : decorationTable[index].value;
198 }
199
ConvertWrapTextCaseToStirng(TextCase textCase)200 inline std::string ConvertWrapTextCaseToStirng(TextCase textCase)
201 {
202 static const LinearEnumMapNode<TextCase, std::string> textCaseTable[] = {
203 { TextCase::NORMAL, "TextCase.Normal" },
204 { TextCase::LOWERCASE, "TextCase.LowerCase" },
205 { TextCase::UPPERCASE, "TextCase.UpperCase" },
206 };
207
208 auto index = BinarySearchFindIndex(textCaseTable, ArraySize(textCaseTable), textCase);
209 return index < 0 ? "TextCase.Normal" : textCaseTable[index].value;
210 }
211
ConvertWrapImageFitToString(ImageFit imageFit)212 inline std::string ConvertWrapImageFitToString(ImageFit imageFit)
213 {
214 static const LinearEnumMapNode<ImageFit, std::string> imageFitTable[] = {
215 { ImageFit::COVER, "ImageFit.Cover" },
216 { ImageFit::FILL, "ImageFit.Fill" },
217 { ImageFit::CONTAIN, "ImageFit.Contain" },
218 { ImageFit::FITWIDTH, "ImageFit.FitWidth" },
219 { ImageFit::FITHEIGHT, "ImageFit.FitHeight" },
220 { ImageFit::NONE, "ImageFit.None" },
221 { ImageFit::SCALEDOWN, "ImageFit.ScaleDown" },
222 };
223
224 auto index = BinarySearchFindIndex(imageFitTable, ArraySize(imageFitTable), imageFit);
225 return index < 0 ? "ImageFit.Cover" : imageFitTable[index].value;
226 }
227
ConvertWrapImageRepeatToString(ImageRepeat repeat)228 inline std::string ConvertWrapImageRepeatToString(ImageRepeat repeat)
229 {
230 static const LinearEnumMapNode<ImageRepeat, std::string> imageRepeatTable[] = {
231 { ImageRepeat::NOREPEAT, "ImageRepeat.NoRepeat" },
232 { ImageRepeat::REPEAT, "ImageRepeat.XY" },
233 { ImageRepeat::REPEATX, "ImageRepeat.X" },
234 { ImageRepeat::REPEATY, "ImageRepeat.Y" },
235 };
236
237 auto index = BinarySearchFindIndex(imageRepeatTable, ArraySize(imageRepeatTable), repeat);
238 return index < 0 ? "ImageRepeat.NoRepeat" : imageRepeatTable[index].value;
239 }
240
ConvertWrapImageInterpolationToString(ImageInterpolation imageInterpolation)241 inline std::string ConvertWrapImageInterpolationToString(ImageInterpolation imageInterpolation)
242 {
243 static const LinearEnumMapNode<ImageInterpolation, std::string> imageInterpolationTable[] = {
244 { ImageInterpolation::NONE, "ImageInterpolation.None" },
245 { ImageInterpolation::LOW, "ImageInterpolation.Low" },
246 { ImageInterpolation::MEDIUM, "ImageInterpolation.Medium" },
247 { ImageInterpolation::HIGH, "ImageInterpolation.High" },
248 };
249
250 auto index =
251 BinarySearchFindIndex(imageInterpolationTable, ArraySize(imageInterpolationTable), imageInterpolation);
252 return index < 0 ? "ImageInterpolation.None" : imageInterpolationTable[index].value;
253 }
254
ConvertWrapImageRenderModeToString(ImageRenderMode imageRenderMode)255 inline std::string ConvertWrapImageRenderModeToString(ImageRenderMode imageRenderMode)
256 {
257 static const LinearEnumMapNode<ImageRenderMode, std::string> imageRenderModeTable[] = {
258 { ImageRenderMode::ORIGINAL, "ImageRenderMode.Original" },
259 { ImageRenderMode::TEMPLATE, "ImageRenderMode.Template" },
260 };
261
262 auto index = BinarySearchFindIndex(imageRenderModeTable, ArraySize(imageRenderModeTable), imageRenderMode);
263 return index < 0 ? "ImageRenderMode.Original" : imageRenderModeTable[index].value;
264 }
265
ConvertWrapTextAlignToString(TextAlign textAlign)266 inline std::string ConvertWrapTextAlignToString(TextAlign textAlign)
267 {
268 static const LinearEnumMapNode<TextAlign, std::string> textAlignTable[] = {
269 { TextAlign::LEFT, "TextAlign.Left" },
270 { TextAlign::RIGHT, "TextAlign.Right" },
271 { TextAlign::CENTER, "TextAlign.Center" },
272 { TextAlign::JUSTIFY, "TextAlign.Justify" },
273 { TextAlign::START, "TextAlign.Start" },
274 { TextAlign::END, "TextAlign.End" },
275 };
276
277 auto index = BinarySearchFindIndex(textAlignTable, ArraySize(textAlignTable), textAlign);
278 return index < 0 ? "TextAlign.Start" : textAlignTable[index].value;
279 }
280
ConvertWrapTextOverflowToString(TextOverflow textOverflow)281 inline std::string ConvertWrapTextOverflowToString(TextOverflow textOverflow)
282 {
283 static const LinearEnumMapNode<TextOverflow, std::string> textOverflowTable[] = {
284 { TextOverflow::CLIP, "TextOverflow.Clip" },
285 { TextOverflow::ELLIPSIS, "TextOverflow.Ellipsis" },
286 { TextOverflow::NONE, "TextOverflow.None" },
287 };
288
289 auto index = BinarySearchFindIndex(textOverflowTable, ArraySize(textOverflowTable), textOverflow);
290 return index < 0 ? "TextAlign.Start" : textOverflowTable[index].value;
291 }
292
ConvertWrapFontStyleToStirng(FontStyle fontStyle)293 inline std::string ConvertWrapFontStyleToStirng(FontStyle fontStyle)
294 {
295 static const LinearEnumMapNode<FontStyle, std::string> fontStyleTable[] = {
296 { FontStyle::NORMAL, "FontStyle.Normal" },
297 { FontStyle::ITALIC, "FontStyle.Italic" },
298 };
299
300 auto index = BinarySearchFindIndex(fontStyleTable, ArraySize(fontStyleTable), fontStyle);
301 return index < 0 ? "FontStyle.Normal" : fontStyleTable[index].value;
302 }
303
ConvertWrapFontWeightToStirng(FontWeight fontWeight)304 inline std::string ConvertWrapFontWeightToStirng(FontWeight fontWeight)
305 {
306 static const LinearEnumMapNode<FontWeight, std::string> fontWeightTable[] = {
307 { FontWeight::W100, "FontWeight.100" },
308 { FontWeight::W200, "FontWeight.200" },
309 { FontWeight::W300, "FontWeight.300" },
310 { FontWeight::W400, "FontWeight.400" },
311 { FontWeight::W500, "FontWeight.500" },
312 { FontWeight::W600, "FontWeight.600" },
313 { FontWeight::W700, "FontWeight.700" },
314 { FontWeight::W800, "FontWeight.800" },
315 { FontWeight::W900, "FontWeight.900" },
316 { FontWeight::BOLD, "FontWeight.Bold" },
317 { FontWeight::NORMAL, "FontWeight.Normal" },
318 { FontWeight::BOLDER, "FontWeight.Bolder" },
319 { FontWeight::LIGHTER, "FontWeight.Lighter" },
320 { FontWeight::MEDIUM, "FontWeight.Medium" },
321 { FontWeight::REGULAR, "FontWeight.Regular" },
322 };
323
324 auto index = BinarySearchFindIndex(fontWeightTable, ArraySize(fontWeightTable), fontWeight);
325 return index < 0 ? "FontWeight.Normal" : fontWeightTable[index].value;
326 }
327
ConvertColorToString(Color color)328 inline std::string ConvertColorToString(Color color)
329 {
330 return color.ColorToString();
331 }
332
ConvertAxisToString(Axis axis)333 inline std::string ConvertAxisToString(Axis axis)
334 {
335 return AXIS_TYPE[static_cast<int32_t>(axis)];
336 }
337
ConvertSideToString(AlignDeclaration::Edge edge)338 inline std::string ConvertSideToString(AlignDeclaration::Edge edge)
339 {
340 if (edge == AlignDeclaration::Edge::TOP) {
341 return "Edge::Top";
342 } else if (edge == AlignDeclaration::Edge::CENTER) {
343 return "Edge::Center";
344 } else if (edge == AlignDeclaration::Edge::BOTTOM) {
345 return "Edge::Bottom";
346 } else if (edge == AlignDeclaration::Edge::BASELINE) {
347 return "Edge::Baseline";
348 } else if (edge == AlignDeclaration::Edge::START) {
349 return "Edge::Start";
350 } else if (edge == AlignDeclaration::Edge::MIDDLE) {
351 return "Edge::Middle";
352 } else if (edge == AlignDeclaration::Edge::END) {
353 return "Edge::End";
354 } else {
355 return "Edge::Center";
356 }
357 }
358 } // namespace OHOS::Ace::V2
359
360 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_UTILS_H