• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "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) {
62             toggle.toggleChild_->SetDisabled(StringToBool(value)); } },
63         { DOM_TEXT_VALUE, [](DOMToggle& toggle, const std::string& value) { toggle.textChild_->SetData(value); } },
64     };
65     auto operatorIter = BinarySearchFindIndex(toggleAttrOperators, ArraySize(toggleAttrOperators), attr.first.c_str());
66     if (operatorIter != -1) {
67         toggleAttrOperators[operatorIter].value(*this, attr.second);
68         return true;
69     }
70     return false;
71 }
72 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)73 bool DOMToggle::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
74 {
75     const static LinearMapNode<void (*)(DOMToggle&, const std::string&)> toggleStyleOperators[] = {
76         { DOM_TEXT_ALLOW_SCALE,
77             [](DOMToggle& toggle, const std::string& value) { toggle.textStyle_.SetAllowScale(StringToBool(value)); } },
78         { DOM_TOGGLE_BACKGROUND_COLOR,
79             [](DOMToggle& toggle, const std::string& value) {
80                 if (toggle.HasCheckedPseudo()) {
81                     toggle.toggleChild_->SetCheckedColor(toggle.ParseColor(value));
82                 } else {
83                     toggle.toggleChild_->SetBackgroundColor(toggle.ParseColor(value));
84                 }
85             } },
86         { DOM_TOGGLE_CHECKED_COLOR,
87             [](DOMToggle& toggle, const std::string& value) {
88                 toggle.toggleChild_->SetCheckedColor(toggle.ParseColor(value));
89             } },
90         { DOM_TEXT_FONT_FAMILY,
91             [](DOMToggle& toggle, const std::string& value) {
92                 toggle.textStyle_.SetFontFamilies(toggle.ParseFontFamilies(value));
93             } },
94         { DOM_TEXT_FONT_SIZE,
95             [](DOMToggle& toggle, const std::string& value) {
96                 toggle.textStyle_.SetFontSize(toggle.ParseDimension(value));
97                 toggle.toggleChild_->SetFontDefinedState(true);
98             } },
99         { DOM_TEXT_FONT_STYLE,
100             [](DOMToggle& toggle, const std::string& value) {
101                 toggle.textStyle_.SetFontStyle(ConvertStrToFontStyle(value));
102             } },
103         { DOM_TEXT_FONT_WEIGHT,
104             [](DOMToggle& toggle, const std::string& value) {
105                 toggle.textStyle_.SetFontWeight(ConvertStrToFontWeight(value));
106             } },
107         { DOM_TOGGLE_HEIGHT,
108             [](DOMToggle& toggle, const std::string& value) {
109                 toggle.toggleChild_->SetHeight(toggle.ParseDimension(value));
110             } },
111         { DOM_TOGGLE_TEXT_COLOR,
112             [](DOMToggle& toggle, const std::string& value) {
113                 toggle.textStyle_.SetTextColor(toggle.ParseColor(value));
114             } },
115         { DOM_TOGGLE_WIDTH,
116             [](DOMToggle& toggle, const std::string& value) {
117                 toggle.toggleChild_->SetWidth(toggle.ParseDimension(value));
118             } },
119     };
120     auto operatorIter =
121         BinarySearchFindIndex(toggleStyleOperators, ArraySize(toggleStyleOperators), style.first.c_str());
122     if (operatorIter != -1) {
123         toggleStyleOperators[operatorIter].value(*this, style.second);
124         return true;
125     }
126     return false;
127 }
128 
AddSpecializedEvent(int32_t pageId,const std::string & event)129 bool DOMToggle::AddSpecializedEvent(int32_t pageId, const std::string& event)
130 {
131     static const LinearMapNode<void (*)(DOMToggle&, const EventMarker&)> toggleEventOperators[] = {
132         { DOM_CATCH_BUBBLE_CLICK,
133             [](DOMToggle& toggle, const EventMarker& event) {
134                 EventMarker eventMarker(event);
135                 eventMarker.SetCatchMode(true);
136                 toggle.toggleChild_->SetClickEvent(eventMarker);
137             } },
138         { DOM_CHANGE, [](DOMToggle& toggle, const EventMarker& event) { toggle.toggleChild_->SetChangeEvent(event); } },
139         { DOM_CLICK,
140             [](DOMToggle& toggle, const EventMarker& event) {
141                 EventMarker eventMarker(event);
142                 eventMarker.SetCatchMode(false);
143                 toggle.toggleChild_->SetClickEvent(eventMarker);
144             } },
145     };
146     auto operatorIter = BinarySearchFindIndex(toggleEventOperators, ArraySize(toggleEventOperators), event.c_str());
147     if (operatorIter != -1) {
148         toggleEventOperators[operatorIter].value(*this, EventMarker(GetNodeIdForEvent(), event, pageId));
149         return true;
150     }
151     return false;
152 }
153 
PrepareSpecializedComponent()154 void DOMToggle::PrepareSpecializedComponent()
155 {
156     textChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
157     ResetColorStyle();
158     if (toggleChild_->IsDisabled()) {
159         PrepareDisabledStyle();
160     }
161     textStyle_.SetAdaptTextSize(textStyle_.GetFontSize(), textStyle_.GetFontSize());
162     textChild_->SetTextStyle(textStyle_);
163     textChild_->SetFocusColor(textStyle_.GetTextColor());
164     if (!boxComponent_) {
165         return;
166     }
167     boxComponent_->SetDeliverMinToChild(true);
168     if (!NearZero(toggleChild_->GetWidth().Value())) {
169         boxComponent_->SetWidth(toggleChild_->GetWidth().Value(), toggleChild_->GetWidth().Unit());
170     }
171     if (!NearZero(toggleChild_->GetHeight().Value())) {
172         boxComponent_->SetHeight(toggleChild_->GetHeight().Value(), toggleChild_->GetHeight().Unit());
173     } else {
174         if (!toggleChild_->GetFontDefinedState()) {
175             boxComponent_->SetHeight(toggleTheme_->GetHeight().Value(), toggleTheme_->GetHeight().Unit());
176         }
177     }
178     auto backDecoration = boxComponent_->GetBackDecoration();
179     if (backDecoration) {
180         if (backDecoration->GetImage()) {
181             toggleChild_->SetBackgroundColor(Color::TRANSPARENT);
182         }
183         backDecoration->SetBorderRadius(Radius(boxComponent_->GetHeightDimension() / 2.0));
184     }
185 
186     // Set the padding on the box to the toggle
187     auto edge = boxComponent_->GetPadding();
188     if (edge == Edge::NONE) {
189         return;
190     }
191     paddingChild_->SetPadding(edge);
192     boxComponent_->SetPadding(Edge());
193 }
194 
ResetColorStyle()195 void DOMToggle::ResetColorStyle()
196 {
197     if (!colorChanged_) {
198         textColor_ = textStyle_.GetTextColor();
199         backgroundColor_ = toggleChild_->GetBackgroundColor();
200         checkedColor_ = toggleChild_->GetCheckedColor();
201     }
202     textStyle_.SetTextColor(textColor_);
203     toggleChild_->SetBackgroundColor(backgroundColor_);
204     toggleChild_->SetCheckedColor(checkedColor_);
205 }
206 
PrepareDisabledStyle()207 void DOMToggle::PrepareDisabledStyle()
208 {
209     if (HasDisabledPseudo()) {
210         return;
211     }
212     textStyle_.SetTextColor(textStyle_.GetTextColor().BlendOpacity(blendOpacity_));
213     toggleChild_->SetBackgroundColor(toggleChild_->GetBackgroundColor().BlendOpacity(blendOpacity_));
214     toggleChild_->SetCheckedColor(toggleChild_->GetCheckedColor().BlendOpacity(blendOpacity_));
215     colorChanged_ = true;
216 }
217 
218 } // namespace OHOS::Ace::Framework
219