• 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_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)29 void RadioModelImpl::Create(const std::optional<std::string>& value, const std::optional<std::string>& group)
30 {
31     RefPtr<RadioTheme> radioTheme = JSViewAbstract::GetTheme<RadioTheme>();
32     auto radioComponent = AceType::MakeRefPtr<OHOS::Ace::RadioComponent<std::string>>(radioTheme);
33 
34     if (value.has_value()) {
35         const auto& radioValue = value.value();
36         radioComponent->SetValue(radioValue);
37     }
38     if (group.has_value()) {
39         const auto& radioGroupName = group.value();
40         radioComponent->SetGroupName(radioGroupName);
41         auto radioGroupComponent = ViewStackProcessor::GetInstance()->GetRadioGroupComponent();
42         auto& radioGroup = (*radioGroupComponent)[radioGroupName];
43         radioGroup.SetIsDeclarative(true);
44         radioGroup.AddRadio(radioComponent);
45     }
46 
47     radioComponent->SetMouseAnimationType(HoverAnimationType::NONE);
48     ViewStackProcessor::GetInstance()->Push(radioComponent);
49     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
50     auto horizontalPadding = radioTheme->GetHotZoneHorizontalPadding();
51     auto verticalPadding = radioTheme->GetHotZoneVerticalPadding();
52     radioComponent->SetWidth(radioTheme->GetWidth() - horizontalPadding * 2);
53     radioComponent->SetHeight(radioTheme->GetHeight() - verticalPadding * 2);
54     box->SetDeliverMinToChild(true);
55     box->SetWidth(radioTheme->GetWidth());
56     box->SetHeight(radioTheme->GetHeight());
57 }
58 
SetChecked(bool isChecked)59 void RadioModelImpl::SetChecked(bool isChecked)
60 {
61     auto* stack = ViewStackProcessor::GetInstance();
62     auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
63     if (isChecked) {
64         radioComponent->SetGroupValue(radioComponent->GetValue());
65         radioComponent->SetOriginChecked(isChecked);
66     } else {
67         radioComponent->SetGroupValue("");
68     }
69 }
70 
SetOnChange(NG::ChangeEvent && onChange)71 void RadioModelImpl::SetOnChange(NG::ChangeEvent&& onChange)
72 {
73     JSViewSetProperty(&CheckableComponent::SetOnChange, std::move(onChange));
74 }
75 
SetWidth(const Dimension & width)76 void RadioModelImpl::SetWidth(const Dimension& width)
77 {
78     auto* stack = ViewStackProcessor::GetInstance();
79     auto box = stack->GetBoxComponent();
80     auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
81     if (radioComponent) {
82         auto padding = radioComponent->GetHotZoneHorizontalPadding();
83         radioComponent->SetWidth(width);
84         box->SetWidth(width + padding * 2);
85     }
86 }
87 
SetHeight(const Dimension & height)88 void RadioModelImpl::SetHeight(const Dimension& height)
89 {
90     auto* stack = ViewStackProcessor::GetInstance();
91     auto box = stack->GetBoxComponent();
92     auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
93     if (radioComponent) {
94         auto padding = radioComponent->GetHotZoneVerticalPadding();
95         radioComponent->SetHeight(height);
96         box->SetHeight(height + padding * 2);
97     }
98 }
99 
SetPadding(const NG::PaddingPropertyF & args)100 void RadioModelImpl::SetPadding(const NG::PaddingPropertyF& args)
101 {
102     auto* stack = ViewStackProcessor::GetInstance();
103     auto radioComponent = AceType::DynamicCast<RadioComponent<std::string>>(stack->GetMainComponent());
104     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
105 
106     if (radioComponent) {
107         auto width = radioComponent->GetWidth();
108         auto height = radioComponent->GetHeight();
109         radioComponent->SetHeight(height);
110         radioComponent->SetWidth(width);
111         box->SetHeight(height + Dimension(args.top.value(), DimensionUnit::VP) * 2);
112         box->SetWidth(width + Dimension(args.left.value(), DimensionUnit::VP) * 2);
113         radioComponent->SetHotZoneVerticalPadding(Dimension(args.top.value(), DimensionUnit::VP));
114         radioComponent->SetHorizontalPadding(Dimension(args.left.value(), DimensionUnit::VP));
115     }
116 }
117 
118 } // namespace OHOS::Ace::Framework
119