1 /*
2  * Copyright (c) 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 "bridge/declarative_frontend/jsview/models/marquee_model_impl.h"
17 
18 #include <optional>
19 
20 #include "base/geometry/dimension.h"
21 #include "base/utils/utils.h"
22 #include "bridge/declarative_frontend/view_stack_processor.h"
23 #include "core/components/common/properties/text_style.h"
24 #include "core/components/text/text_theme.h"
25 
26 namespace OHOS::Ace::Framework {
27 namespace {
28 constexpr Dimension DEFAULT_STEP = 6.0_vp;
29 }
Create()30 void MarqueeModelImpl::Create()
31 {
32     auto marqueeComponent = AceType::MakeRefPtr<MarqueeComponent>();
33     CHECK_NULL_VOID(marqueeComponent);
34     ViewStackProcessor::GetInstance()->ClaimElementId(marqueeComponent);
35     ViewStackProcessor::GetInstance()->Push(marqueeComponent);
36 }
37 
SetValue(const std::optional<std::string> & srcValue)38 void MarqueeModelImpl::SetValue(const std::optional<std::string>& srcValue)
39 {
40     auto component = GetComponent();
41     CHECK_NULL_VOID(component);
42     component->SetValue(srcValue.value_or(""));
43 }
44 
SetPlayerStatus(const std::optional<bool> & playerStatus)45 void MarqueeModelImpl::SetPlayerStatus(const std::optional<bool>& playerStatus)
46 {
47     auto component = GetComponent();
48     CHECK_NULL_VOID(component);
49     component->SetPlayerStatus(playerStatus.value_or(false));
50 }
51 
SetScrollAmount(const std::optional<double> & scrollAmount)52 void MarqueeModelImpl::SetScrollAmount(const std::optional<double>& scrollAmount)
53 {
54     auto component = GetComponent();
55     CHECK_NULL_VOID(component);
56     component->SetScrollAmount(scrollAmount.value_or(DEFAULT_STEP.ConvertToPx()));
57 }
58 
SetLoop(const std::optional<int32_t> & loop)59 void MarqueeModelImpl::SetLoop(const std::optional<int32_t>& loop)
60 {
61     auto component = GetComponent();
62     CHECK_NULL_VOID(component);
63     component->SetLoop(loop.value_or(-1));
64 }
65 
SetDirection(const std::optional<MarqueeDirection> & direction)66 void MarqueeModelImpl::SetDirection(const std::optional<MarqueeDirection>& direction)
67 {
68     auto component = GetComponent();
69     CHECK_NULL_VOID(component);
70     component->SetDirection(direction.value_or(MarqueeDirection::LEFT));
71 }
72 
SetTextColor(const std::optional<Color> & textColor)73 void MarqueeModelImpl::SetTextColor(const std::optional<Color>& textColor)
74 {
75     auto component = GetComponent();
76     CHECK_NULL_VOID(component);
77     auto textStyle = component->GetTextStyle();
78     auto pipelineContext = PipelineContext::GetCurrentContext();
79     CHECK_NULL_VOID(pipelineContext);
80     auto theme = pipelineContext->GetTheme<TextTheme>();
81     CHECK_NULL_VOID(theme);
82     textStyle.SetTextColor(textColor.value_or(theme->GetTextStyle().GetTextColor()));
83     component->SetTextStyle(textStyle);
84 }
85 
SetFontSize(const std::optional<Dimension> & fontSize)86 void MarqueeModelImpl::SetFontSize(const std::optional<Dimension>& fontSize)
87 {
88     auto component = GetComponent();
89     CHECK_NULL_VOID(component);
90     auto textStyle = component->GetTextStyle();
91     auto pipelineContext = PipelineContext::GetCurrentContext();
92     CHECK_NULL_VOID(pipelineContext);
93     auto theme = pipelineContext->GetTheme<TextTheme>();
94     CHECK_NULL_VOID(theme);
95     textStyle.SetFontSize(fontSize.value_or(theme->GetTextStyle().GetFontSize()));
96     component->SetTextStyle(textStyle);
97 }
98 
SetFontWeight(const std::optional<FontWeight> & fontWeight)99 void MarqueeModelImpl::SetFontWeight(const std::optional<FontWeight>& fontWeight)
100 {
101     auto component = GetComponent();
102     CHECK_NULL_VOID(component);
103     auto textStyle = component->GetTextStyle();
104     textStyle.SetFontWeight(fontWeight.value_or(FontWeight::NORMAL));
105     component->SetTextStyle(textStyle);
106 }
107 
SetFontFamily(const std::optional<std::vector<std::string>> & fontFamilies)108 void MarqueeModelImpl::SetFontFamily(const std::optional<std::vector<std::string>>& fontFamilies)
109 {
110     auto component = GetComponent();
111     CHECK_NULL_VOID(component);
112     auto textStyle = component->GetTextStyle();
113     if (fontFamilies.has_value()) {
114         textStyle.SetFontFamilies(fontFamilies.value());
115     }
116     component->SetTextStyle(textStyle);
117 }
118 
SetAllowScale(const std::optional<bool> & allowScale)119 void MarqueeModelImpl::SetAllowScale(const std::optional<bool>& allowScale)
120 {
121     auto component = GetComponent();
122     CHECK_NULL_VOID(component);
123     auto textStyle = component->GetTextStyle();
124     textStyle.SetAllowScale(allowScale.value_or(false));
125     component->SetTextStyle(textStyle);
126 }
127 
SetOnStart(std::function<void ()> && onChange)128 void MarqueeModelImpl::SetOnStart(std::function<void()>&& onChange)
129 {
130     auto component = GetComponent();
131     CHECK_NULL_VOID(component);
132     component->SetOnStart(move(onChange));
133 }
134 
SetOnBounce(std::function<void ()> && onChange)135 void MarqueeModelImpl::SetOnBounce(std::function<void()>&& onChange)
136 {
137     auto component = GetComponent();
138     CHECK_NULL_VOID(component);
139     component->SetOnBounce(move(onChange));
140 }
141 
SetOnFinish(std::function<void ()> && onChange)142 void MarqueeModelImpl::SetOnFinish(std::function<void()>&& onChange)
143 {
144     auto component = GetComponent();
145     CHECK_NULL_VOID(component);
146     component->SetOnFinish(move(onChange));
147 }
148 
GetComponent()149 RefPtr<MarqueeComponent> MarqueeModelImpl::GetComponent()
150 {
151     auto* stack = ViewStackProcessor::GetInstance();
152     if (!stack) {
153         return nullptr;
154     }
155     auto component = AceType::DynamicCast<MarqueeComponent>(stack->GetMainComponent());
156     return component;
157 }
158 } // namespace OHOS::Ace::Framework
159