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