• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/textpicker_modifier.h"
16 
17 #include "core/components/common/properties/text_style.h"
18 #include "core/pipeline/base/element_register.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components_ng/pattern/picker/picker_type_define.h"
22 #include "bridge/common/utils/utils.h"
23 
24 namespace OHOS::Ace::NG {
25 constexpr int32_t SIZE_OF_THREE = 3;
26 constexpr int32_t POS_0 = 0;
27 constexpr int32_t POS_1 = 1;
28 constexpr int32_t POS_2 = 2;
29 const char DEFAULT_DELIMITER = '|';
30 
SetTextpickerBackgroundColor(NodeHandle node,uint32_t color)31 void SetTextpickerBackgroundColor(NodeHandle node, uint32_t color)
32 {
33     auto* frameNode = reinterpret_cast<FrameNode*>(node);
34     CHECK_NULL_VOID(frameNode);
35     TextPickerModelNG::SetBackgroundColor(frameNode, Color(color));
36 }
37 
ResetTextpickerBackgroundColor(NodeHandle node)38 void ResetTextpickerBackgroundColor(NodeHandle node)
39 {
40     auto* frameNode = reinterpret_cast<FrameNode*>(node);
41     CHECK_NULL_VOID(frameNode);
42     auto pipeline = PipelineBase::GetCurrentContext();
43     CHECK_NULL_VOID(pipeline);
44     auto theme = pipeline->GetTheme<DialogTheme>();
45     CHECK_NULL_VOID(theme);
46     TextPickerModelNG::SetBackgroundColor(frameNode, theme->GetBackgroundColor());
47 }
48 
SetTextpickerCanLoop(NodeHandle node,bool canLoop)49 void SetTextpickerCanLoop(NodeHandle node, bool canLoop)
50 {
51     auto* frameNode = reinterpret_cast<FrameNode*>(node);
52     CHECK_NULL_VOID(frameNode);
53     TextPickerModelNG::SetCanLoop(frameNode, canLoop);
54 }
55 
ResetTextpickerCanLoop(NodeHandle node)56 void ResetTextpickerCanLoop(NodeHandle node)
57 {
58     auto* frameNode = reinterpret_cast<FrameNode*>(node);
59     CHECK_NULL_VOID(frameNode);
60     TextPickerModelNG::SetCanLoop(frameNode, true);
61 }
62 
ResetTextpickerSelected(NodeHandle node)63 void ResetTextpickerSelected(NodeHandle node) {}
64 
SetTextpickerSelectedIndex(NodeHandle node,uint32_t * values,int32_t size)65 void SetTextpickerSelectedIndex(NodeHandle node, uint32_t* values, int32_t size)
66 {
67     auto* frameNode = reinterpret_cast<FrameNode*>(node);
68     CHECK_NULL_VOID(frameNode);
69 
70     if (TextPickerModel::GetInstance()->IsSingle()) {
71         SetSelectedIndexSingle(frameNode, values, size);
72     } else {
73         SetSelectedIndexMulti(frameNode, values, size);
74     }
75 }
76 
SetSelectedIndexSingle(FrameNode * frameNode,uint32_t * selectedValues,const int32_t size)77 void SetSelectedIndexSingle(FrameNode* frameNode, uint32_t* selectedValues, const int32_t size)
78 {
79     std::vector<NG::RangeContent> rangeResult;
80     TextPickerModel::GetInstance()->GetSingleRange(rangeResult);
81     if (selectedValues[0] >= rangeResult.size()) {
82         TextPickerModelNG::SetSelected(frameNode, 0);
83     } else {
84         TextPickerModelNG::SetSelected(frameNode, selectedValues[0]);
85     }
86 }
87 
SetSelectedIndexMultiInternal(FrameNode * frameNode,uint32_t count,std::vector<NG::TextCascadePickerOptions> & options,std::vector<uint32_t> & selectedValues)88 void SetSelectedIndexMultiInternal(FrameNode* frameNode,
89     uint32_t count, std::vector<NG::TextCascadePickerOptions>& options, std::vector<uint32_t>& selectedValues)
90 {
91     if (!TextPickerModel::GetInstance()->IsCascade()) {
92         SetSelectedInternal(count, options, selectedValues);
93     } else {
94         TextPickerModelNG::SetHasSelectAttr(frameNode, true);
95         ProcessCascadeSelected(options, 0, selectedValues);
96         uint32_t maxCount = TextPickerModel::GetInstance()->GetMaxCount();
97         if (selectedValues.size() < maxCount) {
98             auto differ = maxCount - selectedValues.size();
99             for (uint32_t i = 0; i < differ; i++) {
100                 selectedValues.emplace_back(0);
101             }
102         }
103     }
104 }
105 
SetSelectedIndexSingleInternal(const std::vector<NG::TextCascadePickerOptions> & options,uint32_t count,uint32_t & selectedValue,std::vector<uint32_t> & selectedValues)106 void SetSelectedIndexSingleInternal(const std::vector<NG::TextCascadePickerOptions>& options,
107     uint32_t count, uint32_t& selectedValue, std::vector<uint32_t>& selectedValues)
108 {
109     if (options.size() > 0) {
110         if (selectedValue >= options[0].rangeResult.size()) {
111             selectedValue = 0;
112         }
113         selectedValues.emplace_back(selectedValue);
114         for (uint32_t i = 1; i < count; i++) {
115             selectedValues.emplace_back(0);
116         }
117     } else {
118         for (uint32_t i = 0; i < count; i++) {
119             selectedValues.emplace_back(0);
120         }
121     }
122 }
123 
SetSelectedInternal(uint32_t count,std::vector<NG::TextCascadePickerOptions> & options,std::vector<uint32_t> & selectedValues)124 void SetSelectedInternal(
125     uint32_t count, std::vector<NG::TextCascadePickerOptions>& options, std::vector<uint32_t>& selectedValues)
126 {
127     for (uint32_t i = 0; i < count; i++) {
128         if (i > selectedValues.size() - 1) {
129             selectedValues.emplace_back(0);
130         } else {
131             if (selectedValues[i] >= options[i].rangeResult.size()) {
132                 selectedValues[i] = 0;
133             }
134         }
135     }
136 }
137 
SetSelectedIndexMulti(FrameNode * frameNode,uint32_t * inputs,const int32_t size)138 void SetSelectedIndexMulti(FrameNode* frameNode, uint32_t* inputs, const int32_t size)
139 {
140     std::vector<NG::TextCascadePickerOptions> options;
141     TextPickerModel::GetInstance()->GetMultiOptions(options);
142 
143     auto count =
144         TextPickerModel::GetInstance()->IsCascade() ? TextPickerModel::GetInstance()->GetMaxCount() : options.size();
145     std::vector<uint32_t> selectedValues;
146 
147     if (size >= 0) {
148         selectedValues.assign(inputs, inputs + size);
149         SetSelectedIndexMultiInternal(frameNode, count, options, selectedValues);
150     } else {
151         TextPickerModelNG::SetHasSelectAttr(frameNode, true);
152         SetSelectedIndexSingleInternal(options, count, inputs[0], selectedValues);
153     }
154     TextPickerModelNG::SetSelecteds(frameNode, selectedValues);
155 }
156 
ProcessCascadeSelected(const std::vector<NG::TextCascadePickerOptions> & options,uint32_t index,std::vector<uint32_t> & selectedValues)157 void ProcessCascadeSelected(
158     const std::vector<NG::TextCascadePickerOptions>& options, uint32_t index, std::vector<uint32_t>& selectedValues)
159 {
160     std::vector<std::string> rangeResultValue;
161     for (size_t i = 0; i < options.size(); i++) {
162         rangeResultValue.emplace_back(options[i].rangeResult[0]);
163     }
164 
165     if (index > selectedValues.size() - 1) {
166         selectedValues.emplace_back(0);
167     }
168     if (selectedValues[index] >= rangeResultValue.size()) {
169         selectedValues[index] = 0;
170     }
171     if (selectedValues[index] <= options.size() - 1 && options[selectedValues[index]].children.size() > 0) {
172         ProcessCascadeSelected(options[selectedValues[index]].children, index + 1, selectedValues);
173     }
174 }
175 
SetTextpickerTextStyle(NodeHandle node,uint32_t color,const char * fontInfo,int32_t styleVal)176 void SetTextpickerTextStyle(NodeHandle node, uint32_t color, const char* fontInfo, int32_t styleVal)
177 {
178     auto* frameNode = reinterpret_cast<FrameNode*>(node);
179     CHECK_NULL_VOID(frameNode);
180     NG::PickerTextStyle pickerTextStyle;
181     GetPickerTextStyle(color, fontInfo, styleVal, pickerTextStyle);
182     auto context = frameNode->GetContext();
183     CHECK_NULL_VOID(context);
184     auto themeManager = context->GetThemeManager();
185     CHECK_NULL_VOID(themeManager);
186     auto pickerTheme = themeManager->GetTheme<PickerTheme>();
187     TextPickerModelNG::SetNormalTextStyle(frameNode, pickerTheme, pickerTextStyle);
188 }
189 
ResetTextpickerTextStyle(NodeHandle node)190 void ResetTextpickerTextStyle(NodeHandle node)
191 {
192     auto* frameNode = reinterpret_cast<FrameNode*>(node);
193     CHECK_NULL_VOID(frameNode);
194     NG::PickerTextStyle pickerTextStyle;
195     auto context = frameNode->GetContext();
196     CHECK_NULL_VOID(context);
197     auto themeManager = context->GetThemeManager();
198     CHECK_NULL_VOID(themeManager);
199     auto pickerTheme = themeManager->GetTheme<PickerTheme>();
200     TextPickerModelNG::SetNormalTextStyle(frameNode, pickerTheme, pickerTextStyle);
201 }
202 
SetTextpickerSelectedTextStyle(NodeHandle node,uint32_t color,const char * fontInfo,int32_t styleVal)203 void SetTextpickerSelectedTextStyle(NodeHandle node, uint32_t color, const char* fontInfo, int32_t styleVal)
204 {
205     auto* frameNode = reinterpret_cast<FrameNode*>(node);
206     CHECK_NULL_VOID(frameNode);
207     NG::PickerTextStyle pickerTextStyle;
208     GetPickerTextStyle(color, fontInfo, styleVal, pickerTextStyle);
209     auto context = frameNode->GetContext();
210     CHECK_NULL_VOID(context);
211     auto themeManager = context->GetThemeManager();
212     CHECK_NULL_VOID(themeManager);
213     auto pickerTheme = themeManager->GetTheme<PickerTheme>();
214     TextPickerModelNG::SetSelectedTextStyle(frameNode, pickerTheme, pickerTextStyle);
215 }
216 
ResetTextpickerSelectedTextStyle(NodeHandle node)217 void ResetTextpickerSelectedTextStyle(NodeHandle node)
218 {
219     auto* frameNode = reinterpret_cast<FrameNode*>(node);
220     CHECK_NULL_VOID(frameNode);
221     NG::PickerTextStyle pickerTextStyle;
222     auto context = frameNode->GetContext();
223     CHECK_NULL_VOID(context);
224     auto themeManager = context->GetThemeManager();
225     CHECK_NULL_VOID(themeManager);
226     auto pickerTheme = themeManager->GetTheme<PickerTheme>();
227     TextPickerModelNG::SetSelectedTextStyle(frameNode, pickerTheme, pickerTextStyle);
228 }
229 
SetTextpickerDisappearTextStyle(NodeHandle node,uint32_t color,const char * fontInfo,int32_t styleVal)230 void SetTextpickerDisappearTextStyle(NodeHandle node, uint32_t color, const char* fontInfo, int32_t styleVal)
231 {
232     auto* frameNode = reinterpret_cast<FrameNode*>(node);
233     CHECK_NULL_VOID(frameNode);
234     NG::PickerTextStyle pickerTextStyle;
235     GetPickerTextStyle(color, fontInfo, styleVal, pickerTextStyle);
236     auto context = frameNode->GetContext();
237     CHECK_NULL_VOID(context);
238     auto themeManager = context->GetThemeManager();
239     CHECK_NULL_VOID(themeManager);
240     auto pickerTheme = themeManager->GetTheme<PickerTheme>();
241     TextPickerModelNG::SetDisappearTextStyle(frameNode, pickerTheme, pickerTextStyle);
242 }
243 
ResetTextpickerDisappearTextStyle(NodeHandle node)244 void ResetTextpickerDisappearTextStyle(NodeHandle node)
245 {
246     auto* frameNode = reinterpret_cast<FrameNode*>(node);
247     CHECK_NULL_VOID(frameNode);
248     NG::PickerTextStyle pickerTextStyle;
249     auto context = frameNode->GetContext();
250     CHECK_NULL_VOID(context);
251     auto themeManager = context->GetThemeManager();
252     CHECK_NULL_VOID(themeManager);
253     auto pickerTheme = themeManager->GetTheme<PickerTheme>();
254     TextPickerModelNG::SetDisappearTextStyle(frameNode, pickerTheme, pickerTextStyle);
255 }
256 
SetTextpickerDefaultPickerItemHeight(NodeHandle node,double dVal,int32_t dUnit)257 void SetTextpickerDefaultPickerItemHeight(NodeHandle node, double dVal, int32_t dUnit)
258 {
259     auto* frameNode = reinterpret_cast<FrameNode*>(node);
260     CHECK_NULL_VOID(frameNode);
261     TextPickerModelNG::SetDefaultPickerItemHeight(
262         frameNode, Dimension(dVal, static_cast<DimensionUnit>(dUnit)));
263 }
264 
ResetTextpickerDefaultPickerItemHeight(NodeHandle node)265 void ResetTextpickerDefaultPickerItemHeight(NodeHandle node)
266 {
267     auto* frameNode = reinterpret_cast<FrameNode*>(node);
268     CHECK_NULL_VOID(frameNode);
269     auto height = Dimension(0.0);
270     TextPickerModelNG::SetDefaultPickerItemHeight(frameNode, height);
271 }
272 
GetPickerTextStyle(uint32_t color,const char * fontInfo,int32_t styleVal,NG::PickerTextStyle & textStyle)273 void GetPickerTextStyle(uint32_t color, const char* fontInfo, int32_t styleVal, NG::PickerTextStyle& textStyle)
274 {
275     textStyle.textColor = Color(color);
276 
277     std::vector<std::string> res;
278     std::string fontValues = std::string(fontInfo);
279     StringUtils::StringSplitter(fontValues, DEFAULT_DELIMITER, res);
280 
281     if (res.size() != SIZE_OF_THREE) {
282         return;
283     }
284 
285     if (res[POS_0] != "-1") {
286         textStyle.fontSize = StringUtils::StringToCalcDimension(res[POS_0], false, DimensionUnit::FP);
287     } else {
288         textStyle.fontSize = Dimension(-1);
289     }
290 
291     if (res[POS_1] != "-1") {
292         textStyle.fontWeight = StringUtils::StringToFontWeight(res[POS_1], FontWeight::NORMAL);
293     }
294 
295     if (res[POS_2] != "-1") {
296         textStyle.fontFamily = Framework::ConvertStrToFontFamilies(res[POS_2]);
297     }
298     textStyle.fontStyle = static_cast<Ace::FontStyle>(styleVal);
299 }
300 
GetTextpickerModifier()301 ArkUITextpickerModifierAPI GetTextpickerModifier()
302 {
303     static const ArkUITextpickerModifierAPI modifier = { SetTextpickerBackgroundColor, SetTextpickerCanLoop,
304         SetTextpickerSelectedIndex, SetTextpickerTextStyle, SetTextpickerSelectedTextStyle,
305         SetTextpickerDisappearTextStyle, SetTextpickerDefaultPickerItemHeight, ResetTextpickerCanLoop,
306         ResetTextpickerSelected, ResetTextpickerTextStyle, ResetTextpickerSelectedTextStyle,
307         ResetTextpickerDisappearTextStyle, ResetTextpickerDefaultPickerItemHeight, ResetTextpickerBackgroundColor };
308 
309     return modifier;
310 }
311 }