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