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