1 /*
2 * Copyright (c) 2023 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/menu_item_modifier.h"
16
17 #include "core/components/common/layout/constants.h"
18 #include "core/components/common/properties/text_style.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/base/view_abstract.h"
21 #include "core/components_ng/pattern/menu/menu_item/menu_item_model_ng.h"
22 #include "core/pipeline/base/element_register.h"
23 #include "frameworks/bridge/common/utils/utils.h"
24
25 namespace OHOS::Ace::NG {
26 const char DELIMITER = '|';
27 constexpr int32_t SIZE_OF_FONT_INFO = 3;
28 static const char* ERR_CODE = "-1";
29 const std::string DEFAULT_FONT_WEIGHT = "normal";
30 const std::string DEFAULT_FONT_FAMILY = "HarmonyOS Sans";
31 const Ace::FontStyle DEFAULT_FONT_STYLE = Ace::FontStyle::NORMAL;
32 const std::vector<OHOS::Ace::FontStyle> FONT_STYLES = { OHOS::Ace::FontStyle::NORMAL, OHOS::Ace::FontStyle::ITALIC };
SetMenuItemSelected(ArkUINodeHandle node,ArkUI_Bool value)33 void SetMenuItemSelected(ArkUINodeHandle node, ArkUI_Bool value)
34 {
35 auto* frameNode = reinterpret_cast<FrameNode*>(node);
36 CHECK_NULL_VOID(frameNode);
37 MenuItemModelNG::SetSelected(frameNode, value);
38 }
39
ResetMenuItemSelected(ArkUINodeHandle node)40 void ResetMenuItemSelected(ArkUINodeHandle node)
41 {
42 auto* frameNode = reinterpret_cast<FrameNode*>(node);
43 CHECK_NULL_VOID(frameNode);
44 MenuItemModelNG::SetSelected(frameNode, false);
45 }
46
SetLabelFontColor(ArkUINodeHandle node,const uint32_t color)47 void SetLabelFontColor(ArkUINodeHandle node, const uint32_t color)
48 {
49 auto* frameNode = reinterpret_cast<FrameNode*>(node);
50 CHECK_NULL_VOID(frameNode);
51 MenuItemModelNG::SetLabelFontColor(frameNode, Color(color));
52 }
53
ResetLabelFontColor(ArkUINodeHandle node)54 void ResetLabelFontColor(ArkUINodeHandle node)
55 {
56 auto* frameNode = reinterpret_cast<FrameNode*>(node);
57 CHECK_NULL_VOID(frameNode);
58 std::optional<Color> color = std::nullopt;
59 MenuItemModelNG::SetLabelFontColor(frameNode, color);
60 }
61
SetContentFontColor(ArkUINodeHandle node,const uint32_t color)62 void SetContentFontColor(ArkUINodeHandle node, const uint32_t color)
63 {
64 auto* frameNode = reinterpret_cast<FrameNode*>(node);
65 CHECK_NULL_VOID(frameNode);
66 MenuItemModelNG::SetFontColor(frameNode, Color(color));
67 }
68
ResetContentFontColor(ArkUINodeHandle node)69 void ResetContentFontColor(ArkUINodeHandle node)
70 {
71 auto* frameNode = reinterpret_cast<FrameNode*>(node);
72 CHECK_NULL_VOID(frameNode);
73 std::optional<Color> color = std::nullopt;
74 MenuItemModelNG::SetFontColor(frameNode, color);
75 }
76
SetLabelFont(ArkUINodeHandle node,const char * fontInfo,int32_t styleVal)77 void SetLabelFont(ArkUINodeHandle node, const char* fontInfo, int32_t styleVal)
78 {
79 auto* frameNode = reinterpret_cast<FrameNode*>(node);
80 CHECK_NULL_VOID(frameNode);
81
82 std::vector<std::string> res;
83 std::string fontValues = std::string(fontInfo);
84 StringUtils::StringSplitter(fontValues, DELIMITER, res);
85 if (res.empty() || res.size() != SIZE_OF_FONT_INFO) {
86 return;
87 }
88 CalcDimension fontSize;
89 if (res[0] != ERR_CODE) { // 0: position of font size data
90 fontSize = StringUtils::StringToCalcDimension(res[0], false, DimensionUnit::FP);
91 }
92 MenuItemModelNG::SetLabelFontSize(frameNode, fontSize);
93
94 if (res[1] != ERR_CODE) { // 1: position of font weight data
95 MenuItemModelNG::SetLabelFontWeight(frameNode, Framework::ConvertStrToFontWeight(res[1]));
96 } else {
97 MenuItemModelNG::SetLabelFontWeight(frameNode, FontWeight::NORMAL);
98 }
99
100 if (styleVal >= 0 && styleVal < static_cast<int32_t>(FONT_STYLES.size())) {
101 MenuItemModelNG::SetLabelFontStyle(frameNode, FONT_STYLES[styleVal]);
102 } else {
103 MenuItemModelNG::SetLabelFontStyle(frameNode, DEFAULT_FONT_STYLE);
104 }
105
106 if (res[2] != ERR_CODE) { // 2: position of font family data
107 MenuItemModelNG::SetLabelFontFamily(frameNode, Framework::ConvertStrToFontFamilies(res[2]));
108 } else {
109 MenuItemModelNG::SetLabelFontFamily(frameNode, Framework::ConvertStrToFontFamilies(DEFAULT_FONT_FAMILY));
110 }
111 }
112
ResetLabelFont(ArkUINodeHandle node)113 void ResetLabelFont(ArkUINodeHandle node)
114 {
115 auto* frameNode = reinterpret_cast<FrameNode*>(node);
116 CHECK_NULL_VOID(frameNode);
117 CalcDimension fontSize;
118 FontWeight fontWeight = StringUtils::StringToFontWeight(DEFAULT_FONT_WEIGHT);
119 MenuItemModelNG::SetLabelFontSize(frameNode, fontSize);
120 MenuItemModelNG::SetLabelFontWeight(frameNode, fontWeight);
121 MenuItemModelNG::SetLabelFontFamily(frameNode, Framework::ConvertStrToFontFamilies(DEFAULT_FONT_FAMILY));
122 MenuItemModelNG::SetLabelFontStyle(frameNode, DEFAULT_FONT_STYLE);
123 }
124
SetContentFont(ArkUINodeHandle node,const char * fontInfo,int32_t styleVal)125 void SetContentFont(ArkUINodeHandle node, const char* fontInfo, int32_t styleVal)
126 {
127 auto* frameNode = reinterpret_cast<FrameNode*>(node);
128 CHECK_NULL_VOID(frameNode);
129
130 std::vector<std::string> res;
131 std::string fontValues = std::string(fontInfo);
132 StringUtils::StringSplitter(fontValues, DELIMITER, res);
133 if (res.empty() || res.size() != SIZE_OF_FONT_INFO) {
134 return;
135 }
136
137 CalcDimension fontSize;
138 if (res[0] != ERR_CODE) { // 0: position of font size data
139 fontSize = StringUtils::StringToCalcDimension(res[0], false, DimensionUnit::FP);
140 }
141 MenuItemModelNG::SetFontSize(frameNode, fontSize);
142
143 if (res[1] != ERR_CODE) { // 1: position of font weight data
144 MenuItemModelNG::SetFontWeight(frameNode, Framework::ConvertStrToFontWeight(res[1]));
145 } else {
146 MenuItemModelNG::SetFontWeight(frameNode, FontWeight::NORMAL);
147 }
148
149 if (styleVal >= 0 && styleVal < static_cast<int32_t>(FONT_STYLES.size())) {
150 MenuItemModelNG::SetFontStyle(frameNode, FONT_STYLES[styleVal]);
151 } else {
152 MenuItemModelNG::SetFontStyle(frameNode, DEFAULT_FONT_STYLE);
153 }
154
155 if (res[2] != ERR_CODE) { // 2: position of font family data
156 MenuItemModelNG::SetFontFamily(frameNode, Framework::ConvertStrToFontFamilies(res[2]));
157 } else {
158 MenuItemModelNG::SetFontFamily(frameNode, Framework::ConvertStrToFontFamilies(DEFAULT_FONT_FAMILY));
159 }
160 }
161
ResetContentFont(ArkUINodeHandle node)162 void ResetContentFont(ArkUINodeHandle node)
163 {
164 auto* frameNode = reinterpret_cast<FrameNode*>(node);
165 CHECK_NULL_VOID(frameNode);
166 CalcDimension fontSize;
167 FontWeight fontWeight = StringUtils::StringToFontWeight(DEFAULT_FONT_WEIGHT);
168 MenuItemModelNG::SetFontSize(frameNode, fontSize);
169 MenuItemModelNG::SetFontWeight(frameNode, fontWeight);
170 MenuItemModelNG::SetFontFamily(frameNode, Framework::ConvertStrToFontFamilies(DEFAULT_FONT_FAMILY));
171 MenuItemModelNG::SetFontStyle(frameNode, DEFAULT_FONT_STYLE);
172 }
173
SetSelectIcon(ArkUINodeHandle node,ArkUI_Bool showIcon)174 void SetSelectIcon(ArkUINodeHandle node, ArkUI_Bool showIcon)
175 {
176 auto* frameNode = reinterpret_cast<FrameNode*>(node);
177 CHECK_NULL_VOID(frameNode);
178 MenuItemModelNG::SetSelectIcon(frameNode, showIcon);
179 }
180
ResetSelectIcon(ArkUINodeHandle node)181 void ResetSelectIcon(ArkUINodeHandle node)
182 {
183 auto* frameNode = reinterpret_cast<FrameNode*>(node);
184 CHECK_NULL_VOID(frameNode);
185 MenuItemModelNG::SetSelectIcon(frameNode, false);
186 }
187
SetSelectIconSrc(ArkUINodeHandle node,const char * iconSrc)188 void SetSelectIconSrc(ArkUINodeHandle node, const char* iconSrc)
189 {
190 auto* frameNode = reinterpret_cast<FrameNode*>(node);
191 CHECK_NULL_VOID(frameNode);
192 std::string iconPathStr;
193 if (iconSrc != nullptr) {
194 iconPathStr = iconSrc;
195 }
196 MenuItemModelNG::SetSelectIconSrc(frameNode, iconPathStr);
197 }
198
ResetSelectIconSrc(ArkUINodeHandle node)199 void ResetSelectIconSrc(ArkUINodeHandle node)
200 {
201 auto* frameNode = reinterpret_cast<FrameNode*>(node);
202 CHECK_NULL_VOID(frameNode);
203 std::string iconPathStr;
204 MenuItemModelNG::SetSelectIconSrc(frameNode, iconPathStr);
205 }
206
SetSelectIconSymbol(ArkUINodeHandle node,void * symbolFunction)207 void SetSelectIconSymbol(ArkUINodeHandle node, void* symbolFunction)
208 {
209 auto* frameNode = reinterpret_cast<FrameNode*>(node);
210 CHECK_NULL_VOID(frameNode);
211 if (symbolFunction) {
212 auto symbolCallback = reinterpret_cast<std::function<void(WeakPtr<NG::FrameNode>)>*>(symbolFunction);
213 MenuItemModelNG::SetSelectIconSymbol(frameNode, std::move(*symbolCallback));
214 } else {
215 MenuItemModelNG::SetSelectIconSymbol(frameNode, nullptr);
216 }
217 }
218
ResetSelectIconSymbol(ArkUINodeHandle node)219 void ResetSelectIconSymbol(ArkUINodeHandle node)
220 {
221 auto* frameNode = reinterpret_cast<FrameNode*>(node);
222 CHECK_NULL_VOID(frameNode);
223 std::string iconPathStr;
224 MenuItemModelNG::SetSelectIconSymbol(frameNode, nullptr);
225 }
226
227 namespace NodeModifier {
GetMenuItemModifier()228 const ArkUIMenuItemModifier* GetMenuItemModifier()
229 {
230 static const ArkUIMenuItemModifier modifier = { SetMenuItemSelected, ResetMenuItemSelected, SetLabelFontColor,
231 ResetLabelFontColor, SetContentFontColor, ResetContentFontColor, SetLabelFont, ResetLabelFont, SetContentFont,
232 ResetContentFont, SetSelectIcon, ResetSelectIcon, SetSelectIconSrc, ResetSelectIconSrc, SetSelectIconSymbol,
233 ResetSelectIconSymbol };
234
235 return &modifier;
236 }
237
GetCJUIMenuItemModifier()238 const CJUIMenuItemModifier* GetCJUIMenuItemModifier()
239 {
240 static const CJUIMenuItemModifier modifier = { SetMenuItemSelected, ResetMenuItemSelected, SetLabelFontColor,
241 ResetLabelFontColor, SetContentFontColor, ResetContentFontColor, SetLabelFont, ResetLabelFont, SetContentFont,
242 ResetContentFont, SetSelectIcon, ResetSelectIcon, SetSelectIconSrc, ResetSelectIconSrc, SetSelectIconSymbol,
243 ResetSelectIconSymbol };
244
245 return &modifier;
246 }
247 }
248 } // namespace OHOS::Ace::NG