• 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/toggle_model_impl.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "core/components/toggle/toggle_component.h"
20 #include "core/components/toggle/toggle_theme.h"
21 
22 namespace OHOS::Ace::Framework {
23 
Create(NG::ToggleType toggleType,bool isOn)24 void ToggleModelImpl::Create(NG::ToggleType toggleType, bool isOn)
25 {
26     RefPtr<Component> component;
27     if (toggleType == NG::ToggleType::CHECKBOX) {
28         RefPtr<CheckboxTheme> checkBoxTheme = JSViewAbstract::GetTheme<CheckboxTheme>();
29         if (!checkBoxTheme) {
30             return;
31         }
32         RefPtr<CheckboxComponent> checkboxComponent = AceType::MakeRefPtr<OHOS::Ace::CheckboxComponent>(checkBoxTheme);
33         checkboxComponent->SetValue(isOn);
34         checkboxComponent->SetMouseAnimationType(HoverAnimationType::NONE);
35         auto horizontalPadding = checkBoxTheme->GetHotZoneHorizontalPadding();
36         auto verticalPadding = checkBoxTheme->GetHotZoneVerticalPadding();
37         checkboxComponent->SetWidth(checkBoxTheme->GetWidth() - horizontalPadding * 2);
38         checkboxComponent->SetHeight(checkBoxTheme->GetHeight() - verticalPadding * 2);
39         component = checkboxComponent;
40     } else if (toggleType == NG::ToggleType::SWITCH) {
41         RefPtr<SwitchTheme> switchTheme = JSViewAbstract::GetTheme<SwitchTheme>();
42         if (!switchTheme) {
43             return;
44         }
45         RefPtr<SwitchComponent> switchComponent = AceType::MakeRefPtr<OHOS::Ace::SwitchComponent>(switchTheme);
46         switchComponent->SetValue(isOn);
47         switchComponent->SetMouseAnimationType(HoverAnimationType::NONE);
48         auto horizontalPadding = switchTheme->GetHotZoneHorizontalPadding();
49         auto verticalPadding = switchTheme->GetHotZoneVerticalPadding();
50         switchComponent->SetWidth(switchTheme->GetWidth() - horizontalPadding * 2);
51         switchComponent->SetHeight(switchTheme->GetHeight() - verticalPadding * 2);
52         component = switchComponent;
53     } else {
54         RefPtr<ToggleTheme> toggleTheme = JSViewAbstract::GetTheme<ToggleTheme>();
55         if (!toggleTheme) {
56             return;
57         }
58         RefPtr<ToggleComponent> toggleComponent = AceType::MakeRefPtr<ToggleComponent>();
59         toggleComponent->SetBackgroundColor(toggleTheme->GetBackgroundColor());
60         toggleComponent->SetCheckedColor(toggleTheme->GetCheckedColor());
61         toggleComponent->SetPressedBlendColor(toggleTheme->GetPressedBlendColor());
62         toggleComponent->SetCheckedState(isOn);
63         component = toggleComponent;
64     }
65 
66     ViewStackProcessor::GetInstance()->ClaimElementId(component);
67     ViewStackProcessor::GetInstance()->Push(component);
68     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
69     box->SetDeliverMinToChild(true);
70     if (toggleType == NG::ToggleType::CHECKBOX) {
71         RefPtr<CheckboxTheme> checkBoxTheme = JSViewAbstract::GetTheme<CheckboxTheme>();
72         if (!checkBoxTheme) {
73             return;
74         }
75         box->SetWidth(checkBoxTheme->GetWidth());
76         box->SetHeight(checkBoxTheme->GetHeight());
77     } else if (toggleType == NG::ToggleType::SWITCH) {
78         RefPtr<SwitchTheme> switchTheme = JSViewAbstract::GetTheme<SwitchTheme>();
79         if (!switchTheme) {
80             return;
81         }
82         box->SetWidth(switchTheme->GetWidth());
83         box->SetHeight(switchTheme->GetHeight());
84     } else {
85         RefPtr<ToggleTheme> toggleTheme = JSViewAbstract::GetTheme<ToggleTheme>();
86         if (!toggleTheme) {
87             return;
88         }
89         box->SetHeight(toggleTheme->GetHeight().Value(), toggleTheme->GetHeight().Unit());
90     }
91 }
SetSelectedColor(const std::optional<Color> & selectedColor)92 void ToggleModelImpl::SetSelectedColor(const std::optional<Color>& selectedColor)
93 {
94     if (!selectedColor.has_value()) {
95         return;
96     }
97     Color color = selectedColor.value();
98     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
99     auto toggle = AceType::DynamicCast<ToggleComponent>(mainComponent);
100     if (toggle) {
101         toggle->SetCheckedColor(color);
102         return;
103     }
104     auto checkable = AceType::DynamicCast<CheckableComponent>(mainComponent);
105     if (checkable) {
106         checkable->SetActiveColor(color);
107         return;
108     }
109 }
SetSwitchPointColor(const std::optional<Color> & switchPointColor)110 void ToggleModelImpl::SetSwitchPointColor(const std::optional<Color>& switchPointColor)
111 {
112     if (!switchPointColor.has_value()) {
113         return;
114     }
115     Color color = switchPointColor.value();
116     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
117     auto switchComponent = AceType::DynamicCast<SwitchComponent>(mainComponent);
118     if (!switchComponent) {
119         LOGE("pointstyle only support switch");
120         return;
121     }
122 
123     switchComponent->SetPointColor(color);
124 }
OnChange(NG::ChangeEvent && onChange)125 void ToggleModelImpl::OnChange(NG::ChangeEvent&& onChange)
126 {
127     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
128     auto toggle = AceType::DynamicCast<ToggleComponent>(mainComponent);
129     if (toggle) {
130         JSViewSetProperty(&ToggleComponent::SetOnChange, std::move(onChange));
131     }
132     auto checkable = AceType::DynamicCast<CheckableComponent>(mainComponent);
133     if (checkable) {
134         JSViewSetProperty(&CheckableComponent::SetOnChange, std::move(onChange));
135     }
136 }
137 
SetWidth(const Dimension & width)138 void ToggleModelImpl::SetWidth(const Dimension& width)
139 {
140     auto* stack = ViewStackProcessor::GetInstance();
141     Dimension padding;
142     auto box = stack->GetBoxComponent();
143     auto checkableComponent = AceType::DynamicCast<CheckableComponent>(stack->GetMainComponent());
144     if (checkableComponent) {
145         padding = checkableComponent->GetHotZoneHorizontalPadding();
146         checkableComponent->SetWidth(width);
147         box->SetWidth(width + padding * 2);
148     }
149 
150     auto toggleComponent = AceType::DynamicCast<ToggleComponent>(stack->GetMainComponent());
151     if (toggleComponent) {
152         toggleComponent->SetWidth(width);
153         box->SetWidth(width);
154     }
155 }
156 
SetHeight(const Dimension & height)157 void ToggleModelImpl::SetHeight(const Dimension& height)
158 {
159     auto* stack = ViewStackProcessor::GetInstance();
160     auto box = stack->GetBoxComponent();
161     Dimension padding;
162     auto checkableComponent = AceType::DynamicCast<CheckableComponent>(stack->GetMainComponent());
163     if (checkableComponent) {
164         padding = checkableComponent->GetHotZoneVerticalPadding();
165         checkableComponent->SetHeight(height);
166         box->SetHeight(height + padding * 2);
167     }
168 
169     auto toggleComponent = AceType::DynamicCast<ToggleComponent>(stack->GetMainComponent());
170     if (toggleComponent) {
171         toggleComponent->SetHeight(height);
172         box->SetHeight(height);
173     }
174 }
175 
IsToggle()176 bool ToggleModelImpl::IsToggle()
177 {
178     auto* stack = ViewStackProcessor::GetInstance();
179     auto toggleComponent = AceType::DynamicCast<ToggleComponent>(stack->GetMainComponent());
180     auto box = stack->GetBoxComponent();
181     return toggleComponent;
182 }
183 
SetPadding(const NG::PaddingPropertyF & args,const NG::PaddingProperty &)184 void ToggleModelImpl::SetPadding(const NG::PaddingPropertyF& args, const NG::PaddingProperty& /*newArgs*/)
185 {
186     auto* stack = ViewStackProcessor::GetInstance();
187     auto box = stack->GetBoxComponent();
188 
189     auto checkableComponent = AceType::DynamicCast<CheckableComponent>(stack->GetMainComponent());
190     if (checkableComponent) {
191         auto width = checkableComponent->GetWidth();
192         auto height = checkableComponent->GetHeight();
193         checkableComponent->SetHeight(height);
194         checkableComponent->SetWidth(width);
195         box->SetHeight(height + Dimension(args.top.value(), DimensionUnit::VP) * 2);
196         box->SetWidth(width + Dimension(args.left.value(), DimensionUnit::VP) * 2);
197         checkableComponent->SetHotZoneVerticalPadding(Dimension(args.top.value(), DimensionUnit::VP));
198         checkableComponent->SetHorizontalPadding(Dimension(args.left.value(), DimensionUnit::VP));
199     }
200 }
201 
Pop()202 void ToggleModelImpl::Pop()
203 {
204     ViewStackProcessor::GetInstance()->Pop();
205 }
206 } // namespace OHOS::Ace::Framework
207