• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "core/components_ng/pattern/radio/radio_model_ng.h"
17 
18 #include "core/common/resource/resource_parse_utils.h"
19 #include "core/components_ng/base/view_abstract.h"
20 #include "core/components_ng/base/view_stack_processor.h"
21 #include "core/components_ng/pattern/radio/radio_pattern.h"
22 
23 namespace OHOS::Ace::NG {
24 
Create(const std::optional<std::string> & value,const std::optional<std::string> & group,const std::optional<int32_t> & indicator)25 void RadioModelNG::Create(const std::optional<std::string>& value, const std::optional<std::string>& group,
26     const std::optional<int32_t>& indicator)
27 {
28     auto* stack = ViewStackProcessor::GetInstance();
29     CHECK_NULL_VOID(stack);
30     int32_t nodeId = stack->ClaimNodeId();
31     ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::RADIO_ETS_TAG, nodeId);
32     auto frameNode = FrameNode::GetOrCreateFrameNode(
33         V2::RADIO_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<RadioPattern>(); });
34     ViewStackProcessor::GetInstance()->Push(frameNode);
35     auto eventHub = frameNode->GetOrCreateEventHub<NG::RadioEventHub>();
36     CHECK_NULL_VOID(eventHub);
37     if (value.has_value()) {
38         eventHub->SetValue(value.value());
39     }
40     if (group.has_value()) {
41         eventHub->SetGroup(group.value());
42     }
43     if (indicator.has_value()) {
44         SetRadioIndicator(indicator.value());
45     }
46     auto props = frameNode->GetPaintPropertyPtr<RadioPaintProperty>();
47     if (props) {
48         props->ResetRadioCheckedBackgroundColorSetByUser();
49         props->ResetRadioUncheckedBorderColorSetByUser();
50         props->ResetRadioIndicatorColorSetByUser();
51     }
52 }
53 
CreateFrameNode(int32_t nodeId)54 RefPtr<FrameNode> RadioModelNG::CreateFrameNode(int32_t nodeId)
55 {
56     auto frameNode = FrameNode::CreateFrameNode(V2::RADIO_ETS_TAG, nodeId, AceType::MakeRefPtr<RadioPattern>());
57     CHECK_NULL_RETURN(frameNode, nullptr);
58     return frameNode;
59 }
60 
SetBuilder(std::function<void ()> && buildFunc)61 void RadioModelNG::SetBuilder(std::function<void()>&& buildFunc)
62 {
63     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
64     CHECK_NULL_VOID(frameNode);
65     auto radioPattern = frameNode->GetPattern<RadioPattern>();
66     CHECK_NULL_VOID(radioPattern);
67     radioPattern->SetBuilder(std::move(buildFunc));
68 }
69 
SetRadioIndicator(int32_t indicator)70 void RadioModelNG::SetRadioIndicator(int32_t indicator)
71 {
72     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioIndicator, indicator);
73 }
74 
SetChecked(bool isChecked)75 void RadioModelNG::SetChecked(bool isChecked)
76 {
77     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
78     CHECK_NULL_VOID(frameNode);
79     TAG_LOGD(
80         AceLogTag::ACE_SELECT_COMPONENT, "radio node %{public}d set checked %{public}d", frameNode->GetId(), isChecked);
81     auto eventHub = frameNode->GetOrCreateEventHub<RadioEventHub>();
82     CHECK_NULL_VOID(eventHub);
83     eventHub->SetCurrentUIState(UI_STATE_SELECTED, isChecked);
84 
85     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioCheck, isChecked);
86 }
87 
SetOnChange(ChangeEvent && onChange)88 void RadioModelNG::SetOnChange(ChangeEvent&& onChange)
89 {
90     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
91     CHECK_NULL_VOID(frameNode);
92     auto eventHub = frameNode->GetOrCreateEventHub<RadioEventHub>();
93     CHECK_NULL_VOID(eventHub);
94     eventHub->SetOnChange(std::move(onChange));
95 }
96 
SetOnChange(FrameNode * frameNode,ChangeEvent && onChange)97 void RadioModelNG::SetOnChange(FrameNode* frameNode, ChangeEvent&& onChange)
98 {
99     CHECK_NULL_VOID(frameNode);
100     auto eventHub = frameNode->GetOrCreateEventHub<RadioEventHub>();
101     CHECK_NULL_VOID(eventHub);
102     eventHub->SetOnChange(std::move(onChange));
103 }
104 
SetWidth(const Dimension & width)105 void RadioModelNG::SetWidth(const Dimension& width)
106 {
107     NG::ViewAbstract::SetWidth(NG::CalcLength(width));
108 }
109 
SetHeight(const Dimension & height)110 void RadioModelNG::SetHeight(const Dimension& height)
111 {
112     NG::ViewAbstract::SetHeight(NG::CalcLength(height));
113 }
114 
SetPadding(const NG::PaddingPropertyF &,const NG::PaddingProperty & newArgs)115 void RadioModelNG::SetPadding(const NG::PaddingPropertyF& /*args*/, const NG::PaddingProperty& newArgs)
116 {
117     NG::ViewAbstract::SetPadding(newArgs);
118 }
119 
SetCheckedBackgroundColor(const Color & color)120 void RadioModelNG::SetCheckedBackgroundColor(const Color& color)
121 {
122     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioCheckedBackgroundColor, color);
123 }
124 
SetCheckedBackgroundColorSetByUser(bool flag)125 void RadioModelNG::SetCheckedBackgroundColorSetByUser(bool flag)
126 {
127     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioCheckedBackgroundColorSetByUser, flag);
128 }
129 
SetUncheckedBorderColor(const Color & color)130 void RadioModelNG::SetUncheckedBorderColor(const Color& color)
131 {
132     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioUncheckedBorderColor, color);
133 }
134 
SetUncheckedBorderColorSetByUser(bool flag)135 void RadioModelNG::SetUncheckedBorderColorSetByUser(bool flag)
136 {
137     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioUncheckedBorderColorSetByUser, flag);
138 }
139 
SetIndicatorColor(const Color & color)140 void RadioModelNG::SetIndicatorColor(const Color& color)
141 {
142     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioIndicatorColor, color);
143 }
144 
SetIndicatorColorSetByUser(bool flag)145 void RadioModelNG::SetIndicatorColorSetByUser(bool flag)
146 {
147     ACE_UPDATE_PAINT_PROPERTY(RadioPaintProperty, RadioIndicatorColorSetByUser, flag);
148 }
149 
SetOnChangeEvent(ChangeEvent && onChangeEvent)150 void RadioModelNG::SetOnChangeEvent(ChangeEvent&& onChangeEvent)
151 {
152     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
153     CHECK_NULL_VOID(frameNode);
154     auto eventHub = frameNode->GetOrCreateEventHub<RadioEventHub>();
155     CHECK_NULL_VOID(eventHub);
156     eventHub->SetOnChangeEvent(std::move(onChangeEvent));
157 }
158 
SetResponseRegion(const std::vector<DimensionRect> & responseRegion)159 void RadioModelNG::SetResponseRegion(const std::vector<DimensionRect>& responseRegion)
160 {
161     NG::ViewAbstract::SetResponseRegion(responseRegion);
162     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
163     CHECK_NULL_VOID(frameNode);
164     auto pattern = frameNode->GetPattern<RadioPattern>();
165     CHECK_NULL_VOID(pattern);
166     pattern->SetIsUserSetResponseRegion(true);
167 }
168 
SetHoverEffect(HoverEffectType hoverEffect)169 void RadioModelNG::SetHoverEffect(HoverEffectType hoverEffect)
170 {
171     NG::ViewAbstract::SetHoverEffect(hoverEffect);
172     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
173     CHECK_NULL_VOID(frameNode);
174     auto pattern = frameNode->GetPattern<RadioPattern>();
175     CHECK_NULL_VOID(pattern);
176     pattern->SetShowHoverEffect(hoverEffect != HoverEffectType::NONE);
177 }
178 
SetChecked(FrameNode * frameNode,bool isChecked)179 void RadioModelNG::SetChecked(FrameNode* frameNode, bool isChecked)
180 {
181     CHECK_NULL_VOID(frameNode);
182     auto eventHub = frameNode->GetOrCreateEventHub<RadioEventHub>();
183     TAG_LOGD(AceLogTag::ACE_SELECT_COMPONENT,
184         "radio frame node %{public}d set checked %{public}d", frameNode->GetId(), isChecked);
185     CHECK_NULL_VOID(eventHub);
186     eventHub->SetCurrentUIState(UI_STATE_SELECTED, isChecked);
187     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioCheck, isChecked, frameNode);
188 }
189 
SetCheckedBackgroundColor(FrameNode * frameNode,const Color & color)190 void RadioModelNG::SetCheckedBackgroundColor(FrameNode* frameNode, const Color& color)
191 {
192     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioCheckedBackgroundColor, color, frameNode);
193 }
194 
SetCheckedBackgroundColorSetByUser(FrameNode * frameNode,bool flag)195 void RadioModelNG::SetCheckedBackgroundColorSetByUser(FrameNode* frameNode, bool flag)
196 {
197     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioCheckedBackgroundColorSetByUser, flag, frameNode);
198 }
199 
SetUncheckedBorderColor(FrameNode * frameNode,const Color & color)200 void RadioModelNG::SetUncheckedBorderColor(FrameNode* frameNode, const Color& color)
201 {
202     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioUncheckedBorderColor, color, frameNode);
203 }
204 
SetUncheckedBorderColorSetByUser(FrameNode * frameNode,bool flag)205 void RadioModelNG::SetUncheckedBorderColorSetByUser(FrameNode* frameNode, bool flag)
206 {
207     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioUncheckedBorderColorSetByUser, flag, frameNode);
208 }
209 
SetIndicatorColor(FrameNode * frameNode,const Color & color)210 void RadioModelNG::SetIndicatorColor(FrameNode* frameNode, const Color& color)
211 {
212     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioIndicatorColor, color, frameNode);
213 }
214 
SetIndicatorColorSetByUser(FrameNode * frameNode,bool flag)215 void RadioModelNG::SetIndicatorColorSetByUser(FrameNode* frameNode, bool flag)
216 {
217     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioIndicatorColorSetByUser, flag, frameNode);
218 }
219 
SetWidth(FrameNode * frameNode,const Dimension & width)220 void RadioModelNG::SetWidth(FrameNode* frameNode, const Dimension& width)
221 {
222     NG::ViewAbstract::SetWidth(frameNode, NG::CalcLength(width));
223 }
224 
SetHeight(FrameNode * frameNode,const Dimension & height)225 void RadioModelNG::SetHeight(FrameNode* frameNode, const Dimension& height)
226 {
227     NG::ViewAbstract::SetHeight(frameNode, NG::CalcLength(height));
228 }
229 
SetHoverEffect(FrameNode * frameNode,HoverEffectType hoverEffect)230 void RadioModelNG::SetHoverEffect(FrameNode* frameNode, HoverEffectType hoverEffect)
231 {
232     NG::ViewAbstract::SetHoverEffect(frameNode, hoverEffect);
233     CHECK_NULL_VOID(frameNode);
234     auto pattern = frameNode->GetPattern<RadioPattern>();
235     CHECK_NULL_VOID(pattern);
236     pattern->SetShowHoverEffect(hoverEffect != HoverEffectType::NONE);
237 }
238 
SetPadding(FrameNode * frameNode,const NG::PaddingProperty & newArgs)239 void RadioModelNG::SetPadding(FrameNode* frameNode, const NG::PaddingProperty& newArgs)
240 {
241     NG::ViewAbstract::SetPadding(frameNode, newArgs);
242 }
243 
SetResponseRegion(FrameNode * frameNode,const std::vector<DimensionRect> & responseRegion)244 void RadioModelNG::SetResponseRegion(FrameNode* frameNode, const std::vector<DimensionRect>& responseRegion)
245 {
246     NG::ViewAbstract::SetResponseRegion(frameNode, responseRegion);
247     CHECK_NULL_VOID(frameNode);
248     auto pattern = frameNode->GetPattern<RadioPattern>();
249     CHECK_NULL_VOID(pattern);
250     pattern->SetIsUserSetResponseRegion(true);
251 }
252 
SetBuilderFunc(FrameNode * frameNode,NG::RadioMakeCallback && makeFunc)253 void RadioModelNG::SetBuilderFunc(FrameNode* frameNode, NG::RadioMakeCallback&& makeFunc)
254 {
255     CHECK_NULL_VOID(frameNode);
256     auto pattern = frameNode->GetPattern<RadioPattern>();
257     CHECK_NULL_VOID(pattern);
258     pattern->SetBuilderFunc(std::move(makeFunc));
259 }
260 
SetChangeValue(FrameNode * frameNode,bool value)261 void RadioModelNG::SetChangeValue(FrameNode* frameNode, bool value)
262 {
263     auto pattern = frameNode->GetPattern<RadioPattern>();
264     CHECK_NULL_VOID(pattern);
265     pattern->SetRadioChecked(value);
266 }
267 
GetChecked(FrameNode * frameNode)268 bool RadioModelNG::GetChecked(FrameNode* frameNode)
269 {
270     bool value = false;
271     ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(RadioPaintProperty, RadioCheck, value, frameNode, value);
272     return value;
273 }
274 
GetCheckedBackgroundColor(FrameNode * frameNode)275 Color RadioModelNG::GetCheckedBackgroundColor(FrameNode* frameNode)
276 {
277     Color color(0xFF007DFF);
278     ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(
279         RadioPaintProperty, RadioCheckedBackgroundColor, color, frameNode, color);
280     return color;
281 }
282 
GetUncheckedBorderColor(FrameNode * frameNode)283 Color RadioModelNG::GetUncheckedBorderColor(FrameNode* frameNode)
284 {
285     Color color(0xFF182431);
286     ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(
287         RadioPaintProperty, RadioUncheckedBorderColor, color, frameNode, color);
288     return color;
289 }
290 
GetIndicatorColor(FrameNode * frameNode)291 Color RadioModelNG::GetIndicatorColor(FrameNode* frameNode)
292 {
293     Color color(0xFFFFFFFF);
294     ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(RadioPaintProperty, RadioIndicatorColor, color, frameNode, color);
295     return color;
296 }
297 
SetRadioValue(FrameNode * frameNode,const std::string & value)298 void RadioModelNG::SetRadioValue(FrameNode* frameNode, const std::string& value)
299 {
300     CHECK_NULL_VOID(frameNode);
301     auto eventHub = frameNode->GetOrCreateEventHub<NG::RadioEventHub>();
302     CHECK_NULL_VOID(eventHub);
303     if (!value.empty()) {
304         eventHub->SetValue(value);
305     }
306 }
307 
GetRadioValue(FrameNode * frameNode)308 std::string RadioModelNG::GetRadioValue(FrameNode* frameNode)
309 {
310     CHECK_NULL_RETURN(frameNode, nullptr);
311     auto eventHub = frameNode->GetOrCreateEventHub<NG::RadioEventHub>();
312     CHECK_NULL_RETURN(eventHub, nullptr);
313     return eventHub->GetValue();
314 }
315 
SetRadioGroup(FrameNode * frameNode,const std::string & value)316 void RadioModelNG::SetRadioGroup(FrameNode* frameNode, const std::string& value)
317 {
318     CHECK_NULL_VOID(frameNode);
319     auto eventHub = frameNode->GetOrCreateEventHub<NG::RadioEventHub>();
320     CHECK_NULL_VOID(eventHub);
321     if (!value.empty()) {
322         eventHub->SetGroup(value);
323     }
324 }
325 
GetRadioGroup(FrameNode * frameNode)326 std::string RadioModelNG::GetRadioGroup(FrameNode* frameNode)
327 {
328     CHECK_NULL_RETURN(frameNode, nullptr);
329     auto eventHub = frameNode->GetOrCreateEventHub<NG::RadioEventHub>();
330     CHECK_NULL_RETURN(eventHub, nullptr);
331     return eventHub->GetGroup();
332 }
333 
SetRadioOptions(FrameNode * frameNode,const std::string & value,const std::string & group,int32_t indicator)334 void RadioModelNG::SetRadioOptions(FrameNode* frameNode, const std::string& value,
335     const std::string& group, int32_t indicator)
336 {
337     CHECK_NULL_VOID(frameNode);
338     auto eventHub = frameNode->GetOrCreateEventHub<NG::RadioEventHub>();
339     CHECK_NULL_VOID(eventHub);
340     eventHub->SetValue(value);
341     eventHub->SetGroup(group);
342     ACE_UPDATE_NODE_PAINT_PROPERTY(RadioPaintProperty, RadioIndicator, indicator, frameNode);
343 }
344 
CreateWithColorResourceObj(const RefPtr<ResourceObject> & resObj,const RadioColorType radioColorType)345 void RadioModelNG::CreateWithColorResourceObj(const RefPtr<ResourceObject>& resObj,
346     const RadioColorType radioColorType)
347 {
348     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
349     CHECK_NULL_VOID(frameNode);
350     CreateWithColorResourceObj(frameNode, resObj, radioColorType);
351 }
352 
CreateWithColorResourceObj(FrameNode * frameNode,const RefPtr<ResourceObject> & resObj,const RadioColorType radioColorType)353 void RadioModelNG::CreateWithColorResourceObj(FrameNode* frameNode, const RefPtr<ResourceObject>& resObj,
354     const RadioColorType radioColorType)
355 {
356     CHECK_NULL_VOID(frameNode);
357     auto pattern = frameNode->GetPattern<RadioPattern>();
358     CHECK_NULL_VOID(pattern);
359     std::string key = "radio" + ColorTypeToString(radioColorType);
360     if (resObj) {
361         auto&& updateFunc = [radioColorType, weak = AceType::WeakClaim(AceType::RawPtr(pattern))](
362             const RefPtr<ResourceObject>& resObj) {
363             auto pattern = weak.Upgrade();
364             CHECK_NULL_VOID(pattern);
365             Color result;
366             if (ResourceParseUtils::ParseResColor(resObj, result)) {
367                 pattern->UpdateRadioComponentColor(result, radioColorType);
368             }
369         };
370         pattern->AddResObj(key, resObj, std::move(updateFunc));
371     } else {
372         pattern->RemoveResObj(key);
373     }
374 }
375 
ColorTypeToString(const RadioColorType radioColorType)376 std::string RadioModelNG::ColorTypeToString(const RadioColorType radioColorType)
377 {
378     switch (radioColorType) {
379         case RadioColorType::CHECKED_BACKGROUND_COLOR:
380             return "CheckedBackgroundColor";
381         case RadioColorType::UNCHECKED_BORDER_COLOR:
382             return "UncheckedBorderColor";
383         case RadioColorType::INDICATOR_COLOR:
384             return "IndicatorColor";
385         default:
386             return "";
387     }
388     return "Unknown";
389 }
390 
SetIsUserSetMargin(bool isUserSet)391 void RadioModelNG::SetIsUserSetMargin(bool isUserSet)
392 {
393     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
394     CHECK_NULL_VOID(frameNode);
395     SetIsUserSetMargin(frameNode, isUserSet);
396 }
397 
SetIsUserSetMargin(FrameNode * frameNode,bool isUserSet)398 void RadioModelNG::SetIsUserSetMargin(FrameNode* frameNode, bool isUserSet)
399 {
400     CHECK_NULL_VOID(frameNode);
401     auto pattern = frameNode->GetPattern<RadioPattern>();
402     CHECK_NULL_VOID(pattern);
403     pattern->SetIsUserSetMargin(isUserSet);
404 }
405 
SetUncheckedBorderColorByJSRadioTheme(bool flag)406 void RadioModelNG::SetUncheckedBorderColorByJSRadioTheme(bool flag)
407 {
408     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
409     CHECK_NULL_VOID(frameNode);
410     auto pattern = frameNode->GetPattern<RadioPattern>();
411     CHECK_NULL_VOID(pattern);
412     pattern->SetUncheckedBorderColorByJSRadioTheme(flag);
413 }
414 
SetIndicatorColorByJSRadioTheme(bool flag)415 void RadioModelNG::SetIndicatorColorByJSRadioTheme(bool flag)
416 {
417     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
418     CHECK_NULL_VOID(frameNode);
419     auto pattern = frameNode->GetPattern<RadioPattern>();
420     CHECK_NULL_VOID(pattern);
421     pattern->SetIndicatorColorByJSRadioTheme(flag);
422 }
423 } // namespace OHOS::Ace::NG
424