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