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