1 /*
2 * Copyright (c) 2023-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 #include "core/interfaces/native/node/node_stack_modifier.h"
16
17 #include "core/components_ng/pattern/stack/stack_model_ng.h"
18
19 namespace OHOS::Ace::NG {
20 namespace {
21 Alignment DEFAULT_STACK_ALIGNMENT = Alignment::CENTER;
22 constexpr int NUM_0 = 0;
23 constexpr int NUM_1 = 1;
24 constexpr int NUM_2 = 2;
25 constexpr int NUM_3 = 3;
26 constexpr int NUM_4 = 4;
27 constexpr int NUM_5 = 5;
28 constexpr int NUM_6 = 6;
29 constexpr int NUM_7 = 7;
30 constexpr int NUM_8 = 8;
31 constexpr int32_t DEFAULT_ALIGNMENT_INDEX = 4;
SetAlignContent(ArkUINodeHandle node,ArkUI_Int32 align)32 void SetAlignContent(ArkUINodeHandle node, ArkUI_Int32 align)
33 {
34 auto* frameNode = reinterpret_cast<FrameNode*>(node);
35 CHECK_NULL_VOID(frameNode);
36 Alignment alignment = Alignment::CENTER;
37 switch (align) {
38 case NUM_0:
39 alignment = Alignment::TOP_LEFT;
40 break;
41 case NUM_1:
42 alignment = Alignment::TOP_CENTER;
43 break;
44 case NUM_2:
45 alignment = Alignment::TOP_RIGHT;
46 break;
47 case NUM_3:
48 alignment = Alignment::CENTER_LEFT;
49 break;
50 case NUM_4:
51 alignment = Alignment::CENTER;
52 break;
53 case NUM_5:
54 alignment = Alignment::CENTER_RIGHT;
55 break;
56 case NUM_6:
57 alignment = Alignment::BOTTOM_LEFT;
58 break;
59 case NUM_7:
60 alignment = Alignment::BOTTOM_CENTER;
61 break;
62 case NUM_8:
63 alignment = Alignment::BOTTOM_RIGHT;
64 break;
65 default:
66 alignment = Alignment::CENTER;
67 }
68 StackModelNG::SetAlignment(frameNode, alignment);
69 }
70
ResetAlignContent(ArkUINodeHandle node)71 void ResetAlignContent(ArkUINodeHandle node)
72 {
73 auto* frameNode = reinterpret_cast<FrameNode*>(node);
74 CHECK_NULL_VOID(frameNode);
75 StackModelNG::SetAlignment(frameNode, DEFAULT_STACK_ALIGNMENT);
76 }
77
findAlignmentIndex(Alignment & alignment)78 int32_t findAlignmentIndex(Alignment& alignment)
79 {
80 std::vector<Alignment> alignMents = {
81 Alignment::TOP_LEFT,
82 Alignment::TOP_CENTER,
83 Alignment::TOP_RIGHT,
84 Alignment::CENTER_LEFT,
85 Alignment::CENTER,
86 Alignment::CENTER_RIGHT,
87 Alignment::BOTTOM_LEFT,
88 Alignment::BOTTOM_CENTER,
89 Alignment::BOTTOM_RIGHT,
90 };
91 auto iterator = std::find(alignMents.begin(), alignMents.end(), alignment);
92 if (iterator == alignMents.end()) {
93 return DEFAULT_ALIGNMENT_INDEX;
94 }
95 return iterator - alignMents.begin();
96 }
97
GetAlignContent(ArkUINodeHandle node)98 int32_t GetAlignContent(ArkUINodeHandle node)
99 {
100 auto* frameNode = reinterpret_cast<FrameNode*>(node);
101 CHECK_NULL_RETURN(frameNode, DEFAULT_ALIGNMENT_INDEX);
102 auto currentAlignment = StackModelNG::GetAlignment(frameNode);
103 return findAlignmentIndex(currentAlignment);
104 }
105 } // namespace
106 namespace NodeModifier {
GetStackModifier()107 const ArkUIStackModifier* GetStackModifier()
108 {
109 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
110 static const ArkUIStackModifier modifier = {
111 .setAlignContent = SetAlignContent,
112 .resetAlignContent = ResetAlignContent,
113 .getAlignContent = GetAlignContent,
114 };
115 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
116
117 return &modifier;
118 }
119
GetCJUIStackModifier()120 const CJUIStackModifier* GetCJUIStackModifier()
121 {
122 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
123 static const CJUIStackModifier modifier = {
124 .setAlignContent = SetAlignContent,
125 .resetAlignContent = ResetAlignContent,
126 .getAlignContent = GetAlignContent,
127 };
128 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
129
130 return &modifier;
131 }
132 }
133 }
134