1 /*
2 * Copyright (c) 2022-2023 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/checkbox_model_impl.h"
17
18 #include <utility>
19 #include "base/memory/referenced.h"
20 #include "bridge/declarative_frontend/view_stack_processor.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
23 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
24 #include "core/components/checkable/checkable_component.h"
25 #include "core/components/checkable/checkable_theme.h"
26
27 namespace OHOS::Ace::Framework {
Create(const std::optional<std::string> & name,const std::optional<std::string> & groupName,const std::string & tagName)28 void CheckBoxModelImpl::Create(
29 const std::optional<std::string>& name, const std::optional<std::string>& groupName, const std::string& tagName)
30 {
31 RefPtr<CheckboxTheme> checkBoxTheme = JSViewAbstract::GetTheme<CheckboxTheme>();
32 auto checkboxComponent = AceType::MakeRefPtr<OHOS::Ace::CheckboxComponent>(checkBoxTheme);
33
34 if (name.has_value()) {
35 const auto& checkboxName = name.value();
36 checkboxComponent->SetCheckboxName(checkboxName);
37 }
38 if (groupName.has_value()) {
39 const auto& checkboxGroup = groupName.value();
40 checkboxComponent->SetBelongGroup(checkboxGroup);
41 auto& checkboxGroupmap = CheckboxComponent::GetCheckboxGroupComponent();
42 auto item = checkboxGroupmap.find(checkboxGroup);
43 if (item != checkboxGroupmap.end()) {
44 item->second->AddCheckbox(checkboxComponent);
45 checkboxComponent->SetGroup(item->second);
46 } else {
47 auto& ungroupedCheckboxs = CheckboxComponent::GetUngroupedCheckboxs();
48 auto retPair = ungroupedCheckboxs.try_emplace(checkboxGroup, std::list<WeakPtr<CheckboxComponent>>());
49 retPair.first->second.push_back(checkboxComponent);
50 }
51 }
52
53 checkboxComponent->SetInspectorTag("Checkbox");
54 checkboxComponent->SetMouseAnimationType(HoverAnimationType::NONE);
55 ViewStackProcessor::GetInstance()->ClaimElementId(checkboxComponent);
56 ViewStackProcessor::GetInstance()->Push(checkboxComponent);
57
58 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
59 auto horizontalPadding = checkBoxTheme->GetHotZoneHorizontalPadding();
60 auto verticalPadding = checkBoxTheme->GetHotZoneVerticalPadding();
61 checkboxComponent->SetWidth(checkBoxTheme->GetWidth() - horizontalPadding * 2);
62 checkboxComponent->SetHeight(checkBoxTheme->GetHeight() - verticalPadding * 2);
63 box->SetWidth(checkBoxTheme->GetWidth());
64 box->SetHeight(checkBoxTheme->GetHeight());
65 }
66
SetSelect(bool isSelected)67 void CheckBoxModelImpl::SetSelect(bool isSelected)
68 {
69 auto *stack = ViewStackProcessor::GetInstance();
70 auto checkboxComponent = AceType::DynamicCast<CheckboxComponent>(stack->GetMainComponent());
71 checkboxComponent->SetValue(isSelected);
72 }
73
SetSelectedColor(const Color & color)74 void CheckBoxModelImpl::SetSelectedColor(const Color& color)
75 {
76 auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
77 auto checkable = AceType::DynamicCast<CheckboxComponent>(mainComponent);
78 if (checkable) {
79 checkable->SetActiveColor(color);
80 return;
81 }
82 }
83
SetOnChange(NG::ChangeEvent && onChange)84 void CheckBoxModelImpl::SetOnChange(NG::ChangeEvent&& onChange)
85 {
86 JSViewSetProperty(&CheckboxComponent::SetOnChange, std::move(onChange));
87 }
88
SetWidth(const Dimension & width)89 void CheckBoxModelImpl::SetWidth(const Dimension& width)
90 {
91 auto *stack = ViewStackProcessor::GetInstance();
92 Dimension padding;
93 auto box = stack->GetBoxComponent();
94 auto checkboxComponent = AceType::DynamicCast<CheckboxComponent>(stack->GetMainComponent());
95 if (checkboxComponent) {
96 padding = checkboxComponent->GetHotZoneHorizontalPadding();
97 checkboxComponent->SetWidth(width);
98 box->SetWidth(width + padding * 2);
99 }
100 }
101
SetHeight(const Dimension & height)102 void CheckBoxModelImpl::SetHeight(const Dimension& height)
103 {
104 auto *stack = ViewStackProcessor::GetInstance();
105 auto box = stack->GetBoxComponent();
106 Dimension padding;
107 auto checkboxComponent = AceType::DynamicCast<CheckboxComponent>(stack->GetMainComponent());
108 if (checkboxComponent) {
109 padding = checkboxComponent->GetHotZoneVerticalPadding();
110 checkboxComponent->SetHeight(height);
111 box->SetHeight(height + padding * 2);
112 }
113 }
114
SetPadding(const NG::PaddingPropertyF & args,const NG::PaddingProperty & newArgs,bool flag)115 void CheckBoxModelImpl::SetPadding(const NG::PaddingPropertyF& args, const NG::PaddingProperty& newArgs, bool flag)
116 {
117 if (!flag) {
118 return;
119 }
120
121 auto* stack = ViewStackProcessor::GetInstance();
122 auto box = stack->GetBoxComponent();
123 auto checkboxComponent = AceType::DynamicCast<CheckboxComponent>(stack->GetMainComponent());
124 if (checkboxComponent) {
125 auto width = checkboxComponent->GetWidth();
126 auto height = checkboxComponent->GetHeight();
127 checkboxComponent->SetHeight(height);
128 checkboxComponent->SetWidth(width);
129 box->SetHeight(height + Dimension(args.top.value(), DimensionUnit::VP) * 2);
130 box->SetWidth(width + Dimension(args.top.value(), DimensionUnit::VP) * 2);
131 checkboxComponent->SetHotZoneVerticalPadding(Dimension(args.top.value(), DimensionUnit::VP));
132 checkboxComponent->SetHorizontalPadding(Dimension(args.top.value(), DimensionUnit::VP));
133 }
134 }
135 } // namespace OHOS::Ace::Framework
136