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