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_marquee.h"
17
18 #include "base/log/event_report.h"
19 #include "base/utils/linear_map.h"
20 #include "base/utils/utils.h"
21 #include "core/components/marquee/marquee_theme.h"
22 #include "core/components/theme/theme_manager.h"
23 #include "frameworks/bridge/common/dom/dom_type.h"
24 #include "frameworks/bridge/common/utils/utils.h"
25
26 namespace OHOS::Ace::Framework {
27
DOMMarquee(NodeId nodeId,const std::string & nodeName)28 DOMMarquee::DOMMarquee(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
29 {
30 marqueeChild_ = AceType::MakeRefPtr<MarqueeComponent>();
31 }
32
InitializeStyle()33 void DOMMarquee::InitializeStyle()
34 {
35 ResetInitializedStyle();
36 }
37
ResetInitializedStyle()38 void DOMMarquee::ResetInitializedStyle()
39 {
40 RefPtr<MarqueeTheme> theme = GetTheme<MarqueeTheme>();
41 // the marquee text lines is only one.
42 textStyle_.SetMaxLines(1);
43 if (theme) {
44 textStyle_.SetFontSize(theme->GetFontSize());
45 textStyle_.SetTextColor(theme->GetTextColor());
46 marqueeChild_->SetTextStyle(textStyle_);
47 }
48 }
49
CallSpecializedMethod(const std::string & method,const std::string & args)50 void DOMMarquee::CallSpecializedMethod(const std::string& method, const std::string& args)
51 {
52 auto controller = marqueeChild_->GetController();
53 if (!controller) {
54 LOGE("get controller failed");
55 EventReport::SendComponentException(ComponentExcepType::MARQUEE_ERR);
56 return;
57 }
58 if (method == DOM_MARQUEE_METHOD_START) {
59 controller->Start();
60 } else if (method == DOM_MARQUEE_METHOD_STOP) {
61 controller->Stop();
62 } else {
63 LOGE("Method not support: %{private}s", method.c_str());
64 }
65 }
66
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)67 bool DOMMarquee::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
68 {
69 static const LinearMapNode<void (*)(MarqueeComponent&, const std::string&)> attrOperators[] = {
70 { DOM_MARQUEE_DIRECTION,
71 [](MarqueeComponent& marquee, const std::string& val) {
72 marquee.SetDirection(
73 val == DOM_MARQUEE_DIRECTION_RIGHT ? MarqueeDirection::RIGHT : MarqueeDirection::LEFT);
74 } },
75 { DOM_MARQUEE_LOOP,
76 [](MarqueeComponent& marquee, const std::string& val) { marquee.SetLoop(StringToInt(val)); } },
77 { DOM_MARQUEE_SCROLL_AMOUNT,
78 [](MarqueeComponent& marquee, const std::string& val) { marquee.SetScrollAmount(StringToDouble(val)); } },
79 { DOM_MARQUEE_VALUE, [](MarqueeComponent& marquee, const std::string& val) { marquee.SetValue(val); } },
80 };
81 auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), attr.first.c_str());
82 if (operatorIter != -1) {
83 attrOperators[operatorIter].value(*marqueeChild_, attr.second);
84 return true;
85 }
86 return false;
87 }
88
SetSpecializedStyle(const std::pair<std::string,std::string> & style)89 bool DOMMarquee::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
90 {
91 static const LinearMapNode<void (*)(TextStyle&, const std::string&, const DOMMarquee&)> textStyleOperators[] = {
92 { DOM_TEXT_ALLOW_SCALE, [](TextStyle& textStyle, const std::string& val,
93 const DOMMarquee&) { textStyle.SetAllowScale(StringToBool(val)); } },
94 { DOM_MARQUEE_COLOR, [](TextStyle& textStyle, const std::string& val,
95 const DOMMarquee& node) { textStyle.SetTextColor(node.ParseColor(val)); } },
96 { DOM_MARQUEE_FONT_FAMILY,
97 [](TextStyle& textStyle, const std::string& val, const DOMMarquee& node) {
98 textStyle.SetFontFamilies(node.ParseFontFamilies(val));
99 } },
100 { DOM_MARQUEE_FONT_SIZE, [](TextStyle& textStyle, const std::string& val,
101 const DOMMarquee& node) { textStyle.SetFontSize(node.ParseDimension(val)); } },
102 { DOM_MARQUEE_FONT_WEIGHT, [](TextStyle& textStyle, const std::string& val,
103 const DOMMarquee&) { textStyle.SetFontWeight(ConvertStrToFontWeight(val)); } },
104 { DOM_MARQUEE_TEXT_ALIGN,
105 [](TextStyle& textStyle, const std::string& val, const DOMMarquee&) {
106 textStyle.SetTextAlign(ConvertStrToTextAlign(val));
107 } },
108 };
109 auto operatorIter = BinarySearchFindIndex(textStyleOperators, ArraySize(textStyleOperators), style.first.c_str());
110 if (operatorIter != -1) {
111 textStyleOperators[operatorIter].value(textStyle_, style.second, *this);
112 marqueeChild_->SetTextStyle(textStyle_);
113 return true;
114 }
115 return false;
116 }
117
AddSpecializedEvent(int32_t pageId,const std::string & event)118 bool DOMMarquee::AddSpecializedEvent(int32_t pageId, const std::string& event)
119 {
120 if (event == DOM_MARQUEE_EVENT_BOUNCE) {
121 marqueeChild_->SetBounceEventId(EventMarker(GetNodeIdForEvent(), event, pageId));
122 } else if (event == DOM_MARQUEE_EVENT_FINISH) {
123 marqueeChild_->SetFinishEventId(EventMarker(GetNodeIdForEvent(), event, pageId));
124 } else if (event == DOM_MARQUEE_EVENT_START) {
125 marqueeChild_->SetStartEventId(EventMarker(GetNodeIdForEvent(), event, pageId));
126 } else {
127 return false;
128 }
129 return true;
130 }
131
PrepareSpecializedComponent()132 void DOMMarquee::PrepareSpecializedComponent() {}
133
134 } // namespace OHOS::Ace::Framework