• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/components/toast/toast_component.h"
17 
18 #include <atomic>
19 
20 #include "base/utils/string_utils.h"
21 #include "base/utils/system_properties.h"
22 #include "core/components/align/align_component.h"
23 #include "core/components/common/layout/grid_system_manager.h"
24 #include "core/components/common/properties/shadow_config.h"
25 #include "core/components/flex/flex_component.h"
26 #include "core/components/positioned/positioned_component.h"
27 #include "core/components/stage/stage_element.h"
28 #include "core/components/tween/tween_component.h"
29 
30 namespace OHOS::Ace {
31 namespace {
32 
33 constexpr double START_FRAME_TIME = 0.0;
34 constexpr double END_FRAME_TIME = 0.9;
35 constexpr double START_FRAME_OPACITY = 0.0;
36 constexpr double MID_FRAME_OPACITY = 1.0;
37 constexpr double END_FRAME_OPACITY = 0.0;
38 constexpr float TOAST_ANIMATION_TIME = 100.0f;
39 constexpr char TOAST_TWEEN_NAME[] = "toast";
40 
41 } // namespace
42 
43 ToastComponent::ToastComponent() = default;
44 ToastComponent::~ToastComponent() = default;
45 
46 static std::atomic<int32_t> g_toastId(1);
47 
GenerateNextToastId()48 int32_t ToastComponent::GenerateNextToastId()
49 {
50     return g_toastId.fetch_add(1, std::memory_order_relaxed);
51 }
52 
InitToastAnimation()53 void ToastComponent::InitToastAnimation()
54 {
55     if (NearZero(toastDurationTime_)) {
56         return;
57     }
58     float midFrameTime = TOAST_ANIMATION_TIME / toastDurationTime_;
59     float stopFrameTime = END_FRAME_TIME - TOAST_ANIMATION_TIME / toastDurationTime_;
60     auto opacityKeyframeStart = AceType::MakeRefPtr<Keyframe<float>>(START_FRAME_TIME, START_FRAME_OPACITY);
61     auto opacityKeyframeMid = AceType::MakeRefPtr<Keyframe<float>>(midFrameTime, MID_FRAME_OPACITY);
62     opacityKeyframeMid->SetCurve(Curves::FRICTION);
63     auto opacityKeyframeStop = AceType::MakeRefPtr<Keyframe<float>>(stopFrameTime, MID_FRAME_OPACITY);
64     opacityKeyframeStop->SetCurve(Curves::LINEAR);
65     auto opacityKeyframeEnd = AceType::MakeRefPtr<Keyframe<float>>(END_FRAME_TIME, END_FRAME_OPACITY);
66     opacityKeyframeEnd->SetCurve(Curves::FRICTION);
67     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
68     opacityAnimation->AddKeyframe(opacityKeyframeStart);
69     opacityAnimation->AddKeyframe(opacityKeyframeMid);
70     opacityAnimation->AddKeyframe(opacityKeyframeStop);
71     opacityAnimation->AddKeyframe(opacityKeyframeEnd);
72     tweenOption_.SetOpacityAnimation(opacityAnimation);
73     tweenOption_.SetDuration(toastDurationTime_);
74     tweenOption_.SetFillMode(FillMode::FORWARDS);
75 }
76 
BuildToastContent(const RefPtr<TextComponent> & text,const RefPtr<ToastTheme> & toastTheme)77 void ToastComponent::BuildToastContent(const RefPtr<TextComponent>& text, const RefPtr<ToastTheme>& toastTheme)
78 {
79     if (!text || !toastTheme) {
80         return;
81     }
82     TextStyle toastTextStyle = toastTheme->GetTextStyle();
83     auto deviceType = SystemProperties::GetDeviceType();
84     if (deviceType == DeviceType::WATCH) {
85         toastTextStyle.SetAdaptTextSize(toastTextStyle.GetFontSize(), toastTheme->GetMinFontSize());
86         toastTextStyle.SetMaxLines(toastTheme->GetTextMaxLines());
87         toastTextStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
88         toastTextStyle.SetTextAlign(TextAlign::CENTER);
89     }
90     text->SetTextStyle(toastTextStyle);
91 }
92 
BuildPackageBox(const RefPtr<PipelineContext> & context,const RefPtr<BoxComponent> & box,const RefPtr<TextComponent> & text,const RefPtr<ToastTheme> & toastTheme)93 void ToastComponent::BuildPackageBox(const RefPtr<PipelineContext>& context, const RefPtr<BoxComponent>& box,
94     const RefPtr<TextComponent>& text, const RefPtr<ToastTheme>& toastTheme)
95 {
96     if (!context || !box || !text || !toastTheme) {
97         return;
98     }
99     // create base box for background of toast
100     RefPtr<BoxComponent> baseBox = AceType::MakeRefPtr<BoxComponent>();
101     // baseBox set back decoration
102     RefPtr<Decoration> backDecoration = AceType::MakeRefPtr<Decoration>();
103     backDecoration->SetBackgroundColor(toastTheme->GetBackgroundColor());
104     backDecoration->AddShadow(ShadowConfig::DefaultShadowL);
105     Border border;
106     border.SetBorderRadius(toastTheme->GetRadius());
107     backDecoration->SetBorder(border);
108     baseBox->SetBackDecoration(backDecoration);
109     // baseBox set padding
110     baseBox->SetPadding(toastTheme->GetPadding());
111     auto deviceType = SystemProperties::GetDeviceType();
112     if (deviceType == DeviceType::WATCH) {
113         // baseBox set constraints
114         LayoutParam constraints;
115         constraints.SetMinSize(Size(
116             context->NormalizeToPx(toastTheme->GetMinWidth()), context->NormalizeToPx(toastTheme->GetMinHeight())));
117         constraints.SetMaxSize(Size(Size::INFINITE_SIZE, Size::INFINITE_SIZE));
118         baseBox->SetConstraints(constraints);
119     } else {
120         baseBox->SetGridLayoutInfo(GridSystemManager::GetInstance().GetInfoByType(GridColumnType::TOAST));
121     }
122     // baseBox set child
123     if (deviceType == DeviceType::WATCH) {
124         baseBox->SetMargin(toastTheme->GetMarging());
125         baseBox->SetChild(text);
126     } else {
127         // Single line center alignment, multiple lines Left alignment.
128         std::list<RefPtr<Component>> rowChildren;
129         rowChildren.emplace_back(text);
130         RefPtr<RowComponent> row = AceType::MakeRefPtr<RowComponent>(FlexAlign::CENTER, FlexAlign::CENTER, rowChildren);
131         row->SetMainAxisSize(MainAxisSize::MIN);
132         baseBox->SetAlignment(Alignment::CENTER);
133         baseBox->SetChild(row);
134     }
135 
136     box->SetFlex(BoxFlex::FLEX_X);
137     box->SetChild(baseBox);
138 }
139 
Show(const RefPtr<PipelineContext> & context,const std::string & message,int32_t duration,const std::string & bottom,bool isRightToLeft)140 void ToastComponent::Show(const RefPtr<PipelineContext>& context, const std::string& message, int32_t duration,
141     const std::string& bottom, bool isRightToLeft)
142 {
143     if (!context) {
144         LOGE("fail to show toast due to context is null");
145         return;
146     }
147     auto stackElement = context->GetLastStack();
148     if (!stackElement) {
149         return;
150     }
151 
152     auto themeManager = context->GetThemeManager();
153     if (!themeManager) {
154         return;
155     }
156     auto toastTheme = themeManager->GetTheme<ToastTheme>();
157     if (!toastTheme) {
158         return;
159     }
160 
161     RefPtr<TextComponent> text = AceType::MakeRefPtr<TextComponent>(message);
162     text->SetTextDirection((isRightToLeft ? TextDirection::RTL : TextDirection::LTR));
163     BuildToastContent(text, toastTheme);
164     RefPtr<BoxComponent> box = AceType::MakeRefPtr<BoxComponent>();
165     BuildPackageBox(context, box, text, toastTheme);
166 
167     int32_t toastId = GenerateNextToastId();
168     auto deviceType = SystemProperties::GetDeviceType();
169     // get toast animation playing time
170     toastDurationTime_ = duration;
171     Dimension bottomPosition = StringUtils::StringToDimension(bottom);
172     RefPtr<TweenComponent> tween =
173         AceType::MakeRefPtr<TweenComponent>(TweenComponent::AllocTweenComponentId(), TOAST_TWEEN_NAME, box);
174     InitToastAnimation();
175     tween->SetTweenOption(tweenOption_);
176     tween->SetAnimationOperation(AnimationOperation::PLAY);
177     // to prevent flicking when play animation
178     tween->SetIsFirstFrameShow(false);
179 
180     // prevent layer from clipping shadows
181     tween->SetShadow(ShadowConfig::DefaultShadowL);
182 
183     if (deviceType == DeviceType::WATCH) {
184         // center alignment
185         std::list<RefPtr<Component>> alignChildren;
186         alignChildren.emplace_back(tween);
187         RefPtr<AlignComponent> align = AceType::MakeRefPtr<AlignComponent>(alignChildren, Alignment::CENTER);
188         stackElement->PushToastComponent(align, toastId);
189     } else {
190         RefPtr<PositionedComponent> positioned = AceType::MakeRefPtr<PositionedComponent>(tween);
191         positioned->SetBottom(bottomPosition.IsValid() ? bottomPosition : toastTheme->GetBottom());
192         stackElement->PushToastComponent(positioned, toastId);
193     }
194 
195     WeakPtr<StackElement> weak = stackElement;
196     context->GetTaskExecutor()->PostDelayedTask([weak, toastId] {
197             auto ref = weak.Upgrade();
198             if (ref == nullptr) {
199                 return;
200             }
201             ref->PopToastComponent(toastId);
202         },
203         TaskExecutor::TaskType::UI, duration);
204 }
205 
206 } // namespace OHOS::Ace
207