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