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