1 /*
2  * Copyright (c) 2020 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 #include "marquee_component.h"
16 #include "ace_log.h"
17 #include "js_app_context.h"
18 #include "key_parser.h"
19 #include "keys.h"
20 
21 namespace OHOS {
22 namespace ACELite {
23 static constexpr uint16_t MILLISECONDS_PER_SECOND = 1000;
24 
MarqueeComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)25 MarqueeComponent::MarqueeComponent(jerry_value_t options, jerry_value_t children, AppStyleManager *styleManager)
26     : TextComponent(options, children, styleManager)
27 {
28     SetComponentName(K_MARQUEE);
29 }
30 
CreateNativeViews()31 bool MarqueeComponent::CreateNativeViews()
32 {
33     if (TextComponent::CreateNativeViews()) {
34         UI_LABEL_TYPE_WRAPPER *uiLabel = TextComponent::GetUILabelView();
35         if (uiLabel != nullptr) {
36             // default mode, speed
37             uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
38             SetRollSpeed();
39             return true;
40         }
41     }
42 
43     return false;
44 }
45 
SetPrivateAttribute(uint16_t attrKeyId,jerry_value_t attrValue)46 bool MarqueeComponent::SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue)
47 {
48     bool isSuccess = TextComponent::SetPrivateAttribute(attrKeyId, attrValue);
49     if (!isSuccess) {
50         switch (attrKeyId) {
51             case K_SCROLLAMOUNT: {
52                 SetScrollamount(IntegerOf(attrValue));
53                 break;
54             }
55             case K_SCROLLDELAY: {
56                 SetScrolldelay(IntegerOf(attrValue));
57                 break;
58             }
59             default: {
60                 return false;
61             }
62         }
63     }
64     return true;
65 }
66 
SetScrollamount(uint16_t scrollamount)67 void MarqueeComponent::SetScrollamount(uint16_t scrollamount)
68 {
69     scrollamount_ = scrollamount;
70     SetRollSpeed();
71 }
72 
SetScrolldelay(uint16_t scrolldelay)73 void MarqueeComponent::SetScrolldelay(uint16_t scrolldelay)
74 {
75     const int16_t minScrolldelay = 60;
76     if (scrolldelay >= minScrolldelay) {
77         scrolldelay_ = scrolldelay;
78     } else {
79         scrolldelay_ = minScrolldelay;
80     }
81     SetRollSpeed();
82 }
83 
SetRollSpeed()84 void MarqueeComponent::SetRollSpeed()
85 {
86     UI_LABEL_TYPE_WRAPPER *uiLabel = TextComponent::GetUILabelView();
87     if (uiLabel != nullptr) {
88         const int32_t supportScrolldelayApiVersion = 5;
89         if (JsAppContext::GetInstance()->GetTargetApi() >= supportScrolldelayApiVersion) {
90             uint32_t rollSpeed = (uint32_t)scrollamount_ * MILLISECONDS_PER_SECOND / scrolldelay_;
91             if (rollSpeed > UINT16_MAX) {
92                 rollSpeed = UINT16_MAX;
93             }
94             uiLabel->SetRollSpeed(rollSpeed);
95         } else {
96             uiLabel->SetRollSpeed(scrollamount_);
97         }
98     }
99 }
100 } // namespace ACELite
101 } // namespace OHOS
102