1 /*
2  * Copyright (c) 2021 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 "core/components/checkable/checkable_component.h"
17 
18 #include "core/components/checkable/checkable_element.h"
19 #include "core/components/checkable/render_checkbox.h"
20 #include "core/components/checkable/render_radio.h"
21 #include "core/components/checkable/render_switch.h"
22 
23 namespace OHOS::Ace {
24 namespace {
25 
26 constexpr bool DEFAULT_SWITCH_VALUE = false;
27 constexpr bool DEFAULT_CHECKBOX_VALUE = false;
28 
29 } // namespace
30 
CheckableComponent(CheckableType type,const RefPtr<CheckableTheme> & theme)31 CheckableComponent::CheckableComponent(CheckableType type, const RefPtr<CheckableTheme>& theme) : checkableType_(type)
32 {
33     ApplyTheme(theme);
34 }
35 
ApplyTheme(const RefPtr<CheckableTheme> & theme)36 void CheckableComponent::ApplyTheme(const RefPtr<CheckableTheme>& theme)
37 {
38     if (theme) {
39         width_ = theme->GetWidth();
40         height_ = theme->GetHeight();
41         hotZoneHorizontalPadding_ = theme->GetHotZoneHorizontalPadding();
42         hotZoneVerticalPadding_ = theme->GetHotZoneVerticalPadding();
43         aspectRatio_ = theme->GetAspectRatio();
44         pointColor_ = theme->GetPointColor();
45         activeColor_ = theme->GetActiveColor();
46         inactiveColor_ = theme->GetInactiveColor();
47         focusColor_ = theme->GetFocusColor();
48         defaultWidth_ = theme->GetDefaultWidth();
49         defaultHeight_ = theme->GetDefaultHeight();
50         radioInnerSizeRatio_ = theme->GetRadioInnerSizeRatio();
51         needFocus_ = theme->GetNeedFocus();
52         backgroundSolid_ = theme->IsBackgroundSolid();
53         hoverColor_ = theme->GetHoverColor();
54         inactivePointColor_ = theme->GetInactivePointColor();
55         shadowColor_ = theme->GetShadowColor();
56         shadowWidth_ = theme->GetShadowWidth();
57         hoverRadius_ = theme->GetHoverRadius();
58         borderWidth_ = theme->GetBorderWidth();
59     }
60 }
61 
CreateRenderNode()62 RefPtr<RenderNode> CheckableComponent::CreateRenderNode()
63 {
64     if (checkableType_ == CheckableType::CHECKBOX) {
65         return RenderCheckbox::Create();
66     } else if (checkableType_ == CheckableType::SWITCH) {
67         return RenderSwitch::Create();
68     } else if (checkableType_ == CheckableType::RADIO) {
69         return RenderRadio::Create();
70     } else {
71         LOGW("Unknown checkable type!");
72         return nullptr;
73     }
74 }
75 
CreateElement()76 RefPtr<Element> CheckableComponent::CreateElement()
77 {
78     return AceType::MakeRefPtr<CheckableElement>();
79 }
80 
81 std::unordered_map<std::string, std::list<WeakPtr<CheckboxComponent>>> CheckboxComponent::ungroupedCheckboxs_;
82 std::unordered_map<std::string, RefPtr<CheckboxComponent>> CheckboxComponent::checkboxGroups_;
83 
CheckboxComponent(const RefPtr<CheckboxTheme> & theme)84 CheckboxComponent::CheckboxComponent(const RefPtr<CheckboxTheme>& theme)
85     : CheckableComponent(CheckableType::CHECKBOX, theme), CheckableValue<bool>(DEFAULT_CHECKBOX_VALUE)
86 {}
87 
SwitchComponent(const RefPtr<SwitchTheme> & theme)88 SwitchComponent::SwitchComponent(const RefPtr<SwitchTheme>& theme)
89     : CheckableComponent(CheckableType::SWITCH, theme), CheckableValue<bool>(DEFAULT_SWITCH_VALUE)
90 {
91     if (theme) {
92         backgroundSolid_ = theme->IsBackgroundSolid();
93     }
94 }
95 
96 } // namespace OHOS::Ace
97