1 /*
2 * Copyright (c) 2022 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 "bridge/declarative_frontend/jsview/models/radio_model_impl.h"
17
18 #include <utility>
19
20 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
21 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
22 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "bridge/declarative_frontend/view_stack_processor.h"
24 #include "core/components/checkable/checkable_component.h"
25 #include "core/components/checkable/checkable_theme.h"
26
27 namespace OHOS::Ace::Framework {
28
Create(const std::optional<std::string> & value,const std::optional<std::string> & group,const std::optional<int32_t> & indicator)29 void RadioModelImpl::Create(const std::optional<std::string>& value, const std::optional<std::string>& group,
30 const std::optional<int32_t>& indicator)
31 {
32 RefPtr<RadioTheme> radioTheme = JSViewAbstract::GetTheme<RadioTheme>();
33 auto radioComponent = AceType::MakeRefPtr<OHOS::Ace::RadioComponent<std::string>>(radioTheme);
34
35 if (value.has_value()) {
36 const auto& radioValue = value.value();
37 radioComponent->SetValue(radioValue);
38 }
39 if (group.has_value()) {
40 const auto& radioGroupName = group.value();
41 radioComponent->SetGroupName(radioGroupName);
42 auto radioGroupComponent = ViewStackProcessor::GetInstance()->GetRadioGroupComponent();
43 auto& radioGroup = (*radioGroupComponent)[radioGroupName];
44 radioGroup.SetIsDeclarative(true);
45 radioGroup.AddRadio(radioComponent);
46 }
47
48 radioComponent->SetMouseAnimationType(HoverAnimationType::NONE);
49 ViewStackProcessor::GetInstance()->Push(radioComponent);
50 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
51 auto horizontalPadding = radioTheme->GetHotZoneHorizontalPadding();
52 auto verticalPadding = radioTheme->GetHotZoneVerticalPadding();
53 radioComponent->SetWidth(radioTheme->GetWidth() - horizontalPadding * 2);
54 radioComponent->SetHeight(radioTheme->GetHeight() - verticalPadding * 2);
55 box->SetDeliverMinToChild(true);
56 box->SetWidth(radioTheme->GetWidth());
57 box->SetHeight(radioTheme->GetHeight());
58 }
59
SetChecked(bool isChecked)60 void RadioModelImpl::SetChecked(bool isChecked)
61 {
62 auto* stack = ViewStackProcessor::GetInstance();
63 auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
64 if (isChecked) {
65 radioComponent->SetGroupValue(radioComponent->GetValue());
66 radioComponent->SetOriginChecked(isChecked);
67 } else {
68 radioComponent->SetGroupValue("");
69 }
70 }
71
SetOnChange(NG::ChangeEvent && onChange)72 void RadioModelImpl::SetOnChange(NG::ChangeEvent&& onChange)
73 {
74 JSViewSetProperty(&CheckableComponent::SetOnChange, std::move(onChange));
75 }
76
SetWidth(const Dimension & width)77 void RadioModelImpl::SetWidth(const Dimension& width)
78 {
79 auto* stack = ViewStackProcessor::GetInstance();
80 auto box = stack->GetBoxComponent();
81 auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
82 if (radioComponent) {
83 auto padding = radioComponent->GetHotZoneHorizontalPadding();
84 radioComponent->SetWidth(width);
85 box->SetWidth(width + padding * 2);
86 }
87 }
88
SetHeight(const Dimension & height)89 void RadioModelImpl::SetHeight(const Dimension& height)
90 {
91 auto* stack = ViewStackProcessor::GetInstance();
92 auto box = stack->GetBoxComponent();
93 auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
94 if (radioComponent) {
95 auto padding = radioComponent->GetHotZoneVerticalPadding();
96 radioComponent->SetHeight(height);
97 box->SetHeight(height + padding * 2);
98 }
99 }
100
SetPadding(const NG::PaddingPropertyF & args,const NG::PaddingProperty &)101 void RadioModelImpl::SetPadding(const NG::PaddingPropertyF& args, const NG::PaddingProperty& /*newArgs*/)
102 {
103 auto* stack = ViewStackProcessor::GetInstance();
104 auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
105 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
106
107 if (radioComponent) {
108 auto width = radioComponent->GetWidth();
109 auto height = radioComponent->GetHeight();
110 radioComponent->SetHeight(height);
111 radioComponent->SetWidth(width);
112 box->SetHeight(height + Dimension(args.top.value(), DimensionUnit::VP) * 2);
113 box->SetWidth(width + Dimension(args.left.value(), DimensionUnit::VP) * 2);
114 radioComponent->SetHotZoneVerticalPadding(Dimension(args.top.value(), DimensionUnit::VP));
115 radioComponent->SetHorizontalPadding(Dimension(args.left.value(), DimensionUnit::VP));
116 }
117 }
118
SetOnClickEvent(std::function<void ()> && onClick)119 void RadioModelImpl::SetOnClickEvent(std::function<void()>&& onClick)
120 {
121 auto component = AceType::DynamicCast<CheckableComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
122 if (!component) {
123 LOGW("Failed to get '%{public}s' in view stack", AceType::TypeName<CheckableComponent>());
124 return;
125 }
126 component->SetOnClick(std::move(onClick));
127 }
128
SetResponseRegion(const std::vector<DimensionRect> & responseRegion)129 void RadioModelImpl::SetResponseRegion(const std::vector<DimensionRect>& responseRegion)
130 {
131 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
132 auto renderComponent = AceType::DynamicCast<RenderComponent>(component);
133 if (renderComponent) {
134 renderComponent->SetResponseRegion(responseRegion);
135 renderComponent->MarkResponseRegion(true);
136 }
137 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
138 box->SetResponseRegion(responseRegion);
139 box->MarkResponseRegion(true);
140 if (ViewStackProcessor::GetInstance()->HasClickGestureListenerComponent()) {
141 auto click = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent();
142 click->SetResponseRegion(responseRegion);
143 click->MarkResponseRegion(true);
144 }
145 if (ViewStackProcessor::GetInstance()->HasTouchListenerComponent()) {
146 auto touch = ViewStackProcessor::GetInstance()->GetTouchListenerComponent();
147 touch->SetResponseRegion(responseRegion);
148 touch->MarkResponseRegion(true);
149 }
150 }
151 } // namespace OHOS::Ace::Framework
152