• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/bridge/common/dom/dom_toggle.h"
17 
18 #include "frameworks/bridge/common/utils/utils.h"
19 
20 namespace OHOS::Ace::Framework {
21 
DOMToggle(NodeId nodeId,const std::string & nodeName)22 DOMToggle::DOMToggle(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
23 {
24     toggleChild_ = AceType::MakeRefPtr<ToggleComponent>();
25     textChild_ = AceType::MakeRefPtr<TextComponent>("");
26     paddingChild_ = AceType::MakeRefPtr<PaddingComponent>();
27     paddingChild_->SetChild(textChild_);
28     toggleChild_->SetChild(paddingChild_);
29 }
30 
InitializeStyle()31 void DOMToggle::InitializeStyle()
32 {
33     ResetInitializedStyle();
34 }
35 
ResetInitializedStyle()36 void DOMToggle::ResetInitializedStyle()
37 {
38     toggleTheme_ = GetTheme<ToggleTheme>();
39     if (!toggleTheme_) {
40         LOGE("toggleTheme is null");
41         return;
42     }
43     paddingChild_->SetPadding(toggleTheme_->GetPadding());
44     toggleChild_->SetBackgroundColor(toggleTheme_->GetBackgroundColor());
45     toggleChild_->SetCheckedColor(toggleTheme_->GetCheckedColor());
46     toggleChild_->SetPressedBlendColor(toggleTheme_->GetPressedBlendColor());
47     textStyle_ = toggleTheme_->GetTextStyle();
48     textStyle_.SetMaxLines(1);
49     textStyle_.SetTextOverflow(TextOverflow::ELLIPSIS);
50     textStyle_.SetTextAlign(TextAlign::LEFT);
51     blendOpacity_ = toggleTheme_->GetDisabledAlpha();
52 }
53 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)54 bool DOMToggle::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
55 {
56     static const LinearMapNode<void (*)(DOMToggle&, const std::string&)> toggleAttrOperators[] = {
57         { DOM_TOGGLE_CHECKED_STATE,
58             [](DOMToggle& toggle, const std::string& value) {
59                 toggle.toggleChild_->SetCheckedState(StringToBool(value));
60             } },
61         { DOM_DISABLED, [](DOMToggle& toggle, const std::string& value) { toggle.isDisabled_ = StringToBool(value); } },
62         { DOM_TEXT_VALUE, [](DOMToggle& toggle, const std::string& value) { toggle.textChild_->SetData(value); } },
63     };
64     auto operatorIter = BinarySearchFindIndex(toggleAttrOperators, ArraySize(toggleAttrOperators), attr.first.c_str());
65     if (operatorIter != -1) {
66         toggleAttrOperators[operatorIter].value(*this, attr.second);
67         return true;
68     }
69     return false;
70 }
71 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)72 bool DOMToggle::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
73 {
74     const static LinearMapNode<void (*)(DOMToggle&, const std::string&)> toggleStyleOperators[] = {
75         { DOM_TEXT_ALLOW_SCALE,
76             [](DOMToggle& toggle, const std::string& value) { toggle.textStyle_.SetAllowScale(StringToBool(value)); } },
77         { DOM_TOGGLE_BACKGROUND_COLOR,
78             [](DOMToggle& toggle, const std::string& value) {
79                 if (toggle.HasCheckedPseudo()) {
80                     toggle.toggleChild_->SetCheckedColor(toggle.ParseColor(value));
81                 } else {
82                     toggle.toggleChild_->SetBackgroundColor(toggle.ParseColor(value));
83                 }
84             } },
85         { DOM_TOGGLE_CHECKED_COLOR,
86             [](DOMToggle& toggle, const std::string& value) {
87                 toggle.toggleChild_->SetCheckedColor(toggle.ParseColor(value));
88             } },
89         { DOM_TEXT_FONT_FAMILY,
90             [](DOMToggle& toggle, const std::string& value) {
91                 toggle.textStyle_.SetFontFamilies(toggle.ParseFontFamilies(value));
92             } },
93         { DOM_TEXT_FONT_SIZE,
94             [](DOMToggle& toggle, const std::string& value) {
95                 toggle.textStyle_.SetFontSize(toggle.ParseDimension(value));
96                 toggle.toggleChild_->SetFontDefinedState(true);
97             } },
98         { DOM_TEXT_FONT_STYLE,
99             [](DOMToggle& toggle, const std::string& value) {
100                 toggle.textStyle_.SetFontStyle(ConvertStrToFontStyle(value));
101             } },
102         { DOM_TEXT_FONT_WEIGHT,
103             [](DOMToggle& toggle, const std::string& value) {
104                 toggle.textStyle_.SetFontWeight(ConvertStrToFontWeight(value));
105             } },
106         { DOM_TOGGLE_HEIGHT,
107             [](DOMToggle& toggle, const std::string& value) {
108                 toggle.toggleChild_->SetHeight(toggle.ParseDimension(value));
109             } },
110         { DOM_TOGGLE_TEXT_COLOR,
111             [](DOMToggle& toggle, const std::string& value) {
112                 toggle.textStyle_.SetTextColor(toggle.ParseColor(value));
113             } },
114         { DOM_TOGGLE_WIDTH,
115             [](DOMToggle& toggle, const std::string& value) {
116                 toggle.toggleChild_->SetWidth(toggle.ParseDimension(value));
117             } },
118     };
119     auto operatorIter =
120         BinarySearchFindIndex(toggleStyleOperators, ArraySize(toggleStyleOperators), style.first.c_str());
121     if (operatorIter != -1) {
122         toggleStyleOperators[operatorIter].value(*this, style.second);
123         return true;
124     }
125     return false;
126 }
127 
AddSpecializedEvent(int32_t pageId,const std::string & event)128 bool DOMToggle::AddSpecializedEvent(int32_t pageId, const std::string& event)
129 {
130     static const LinearMapNode<void (*)(DOMToggle&, const EventMarker&)> toggleEventOperators[] = {
131         { DOM_CATCH_BUBBLE_CLICK,
132             [](DOMToggle& toggle, const EventMarker& event) {
133                 EventMarker eventMarker(event);
134                 eventMarker.SetCatchMode(true);
135                 toggle.toggleChild_->SetClickEvent(eventMarker);
136             } },
137         { DOM_CHANGE, [](DOMToggle& toggle, const EventMarker& event) { toggle.toggleChild_->SetChangeEvent(event); } },
138         { DOM_CLICK,
139             [](DOMToggle& toggle, const EventMarker& event) {
140                 EventMarker eventMarker(event);
141                 eventMarker.SetCatchMode(false);
142                 toggle.toggleChild_->SetClickEvent(eventMarker);
143             } },
144     };
145     auto operatorIter = BinarySearchFindIndex(toggleEventOperators, ArraySize(toggleEventOperators), event.c_str());
146     if (operatorIter != -1) {
147         toggleEventOperators[operatorIter].value(*this, EventMarker(GetNodeIdForEvent(), event, pageId));
148         return true;
149     }
150     return false;
151 }
152 
PrepareSpecializedComponent()153 void DOMToggle::PrepareSpecializedComponent()
154 {
155     textChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
156     ResetColorStyle();
157     if (isDisabled_) {
158         PrepareDisabledStyle();
159     }
160     textStyle_.SetAdaptTextSize(textStyle_.GetFontSize(), textStyle_.GetFontSize());
161     textChild_->SetTextStyle(textStyle_);
162     textChild_->SetFocusColor(textStyle_.GetTextColor());
163     if (!boxComponent_) {
164         return;
165     }
166     boxComponent_->SetDeliverMinToChild(true);
167     if (!NearZero(toggleChild_->GetWidth().Value())) {
168         boxComponent_->SetWidth(toggleChild_->GetWidth().Value(), toggleChild_->GetWidth().Unit());
169     }
170     if (!NearZero(toggleChild_->GetHeight().Value())) {
171         boxComponent_->SetHeight(toggleChild_->GetHeight().Value(), toggleChild_->GetHeight().Unit());
172     } else {
173         if (!toggleChild_->GetFontDefinedState()) {
174             boxComponent_->SetHeight(toggleTheme_->GetHeight().Value(), toggleTheme_->GetHeight().Unit());
175         }
176     }
177     auto backDecoration = boxComponent_->GetBackDecoration();
178     if (backDecoration) {
179         if (backDecoration->GetImage()) {
180             toggleChild_->SetBackgroundColor(Color::TRANSPARENT);
181         }
182         backDecoration->SetBorderRadius(Radius(boxComponent_->GetHeightDimension() / 2.0));
183     }
184 
185     // Set the padding on the box to the toggle
186     auto edge = boxComponent_->GetPadding();
187     if (edge == Edge::NONE) {
188         return;
189     }
190     paddingChild_->SetPadding(edge);
191     boxComponent_->SetPadding(Edge());
192 }
193 
ResetColorStyle()194 void DOMToggle::ResetColorStyle()
195 {
196     if (!colorChanged_) {
197         textColor_ = textStyle_.GetTextColor();
198         backgroundColor_ = toggleChild_->GetBackgroundColor();
199         checkedColor_ = toggleChild_->GetCheckedColor();
200     }
201     textStyle_.SetTextColor(textColor_);
202     toggleChild_->SetBackgroundColor(backgroundColor_);
203     toggleChild_->SetCheckedColor(checkedColor_);
204 }
205 
PrepareDisabledStyle()206 void DOMToggle::PrepareDisabledStyle()
207 {
208     if (HasDisabledPseudo()) {
209         return;
210     }
211     textStyle_.SetTextColor(textStyle_.GetTextColor().BlendOpacity(blendOpacity_));
212     toggleChild_->SetBackgroundColor(toggleChild_->GetBackgroundColor().BlendOpacity(blendOpacity_));
213     toggleChild_->SetCheckedColor(toggleChild_->GetCheckedColor().BlendOpacity(blendOpacity_));
214     colorChanged_ = true;
215 }
216 
217 } // namespace OHOS::Ace::Framework
218