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