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 "core/components/align/align_component.h"
19 #include "core/components/common/layout/grid_system_manager.h"
20 #include "core/components/common/properties/shadow_config.h"
21 #include "core/components/flex/flex_component.h"
22 #include "core/components/positioned/positioned_component.h"
23 #include "core/components/stage/stage_element.h"
24 #include "core/components/tween/tween_component.h"
25
26 namespace OHOS::Ace {
27 namespace {
28
29 constexpr double START_FRAME_TIME = 0.0;
30 constexpr double END_FRAME_TIME = 0.9;
31 constexpr double START_FRAME_OPACITY = 0.0;
32 constexpr double MID_FRAME_OPACITY = 1.0;
33 constexpr double END_FRAME_OPACITY = 0.0;
34 constexpr float TOAST_ANIMATION_TIME = 100.0f;
35 constexpr char TOAST_TWEEN_NAME[] = "toast";
36
37 } // namespace
38
39 ToastComponent::ToastComponent() = default;
40 ToastComponent::~ToastComponent() = default;
41
42 static std::atomic<int32_t> g_toastId(1);
43
GenerateNextToastId()44 int32_t ToastComponent::GenerateNextToastId()
45 {
46 return g_toastId.fetch_add(1, std::memory_order_relaxed);
47 }
48
InitToastAnimation()49 void ToastComponent::InitToastAnimation()
50 {
51 if (NearZero(toastDurationTime_)) {
52 return;
53 }
54 float midFrameTime = TOAST_ANIMATION_TIME / toastDurationTime_;
55 float stopFrameTime = END_FRAME_TIME - TOAST_ANIMATION_TIME / toastDurationTime_;
56 auto opacityKeyframeStart = AceType::MakeRefPtr<Keyframe<float>>(START_FRAME_TIME, START_FRAME_OPACITY);
57 auto opacityKeyframeMid = AceType::MakeRefPtr<Keyframe<float>>(midFrameTime, MID_FRAME_OPACITY);
58 opacityKeyframeMid->SetCurve(Curves::FRICTION);
59 auto opacityKeyframeStop = AceType::MakeRefPtr<Keyframe<float>>(stopFrameTime, MID_FRAME_OPACITY);
60 opacityKeyframeStop->SetCurve(Curves::LINEAR);
61 auto opacityKeyframeEnd = AceType::MakeRefPtr<Keyframe<float>>(END_FRAME_TIME, END_FRAME_OPACITY);
62 opacityKeyframeEnd->SetCurve(Curves::FRICTION);
63 auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
64 opacityAnimation->AddKeyframe(opacityKeyframeStart);
65 opacityAnimation->AddKeyframe(opacityKeyframeMid);
66 opacityAnimation->AddKeyframe(opacityKeyframeStop);
67 opacityAnimation->AddKeyframe(opacityKeyframeEnd);
68 tweenOption_.SetOpacityAnimation(opacityAnimation);
69 tweenOption_.SetDuration(toastDurationTime_);
70 tweenOption_.SetFillMode(FillMode::FORWARDS);
71 }
72
BuildToastContent(const RefPtr<TextComponent> & text,const RefPtr<ToastTheme> & toastTheme)73 void ToastComponent::BuildToastContent(const RefPtr<TextComponent>& text, const RefPtr<ToastTheme>& toastTheme)
74 {
75 if (!text || !toastTheme) {
76 return;
77 }
78 TextStyle toastTextStyle = toastTheme->GetTextStyle();
79 auto deviceType = SystemProperties::GetDeviceType();
80 #ifdef OHOS_PLATFORM
81 toastTextStyle.SetTextAlign(TextAlign::CENTER);
82 #endif
83 if (deviceType == DeviceType::WATCH) {
84 toastTextStyle.SetAdaptTextSize(toastTextStyle.GetFontSize(), toastTheme->GetMinFontSize());
85 toastTextStyle.SetMaxLines(toastTheme->GetTextMaxLines());
86 toastTextStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
87 toastTextStyle.SetTextAlign(TextAlign::CENTER);
88 }
89 toastTextStyle.SetTextAlign(TextAlign::START);
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 auto gridColumnInfo = GridSystemManager::GetInstance().GetInfoByType(GridColumnType::TOAST);
121 auto parent = gridColumnInfo->GetParent();
122 if (parent) {
123 parent->BuildColumnWidth();
124 }
125 baseBox->SetGridLayoutInfo(gridColumnInfo);
126 }
127 // baseBox set child
128 if (deviceType == DeviceType::WATCH) {
129 baseBox->SetMargin(toastTheme->GetMarging());
130 baseBox->SetChild(text);
131 } else {
132 // Single line center alignment, multiple lines Left alignment.
133 std::list<RefPtr<Component>> rowChildren;
134 rowChildren.emplace_back(text);
135 RefPtr<RowComponent> row = AceType::MakeRefPtr<RowComponent>(FlexAlign::CENTER, FlexAlign::CENTER, rowChildren);
136 row->SetMainAxisSize(MainAxisSize::MIN);
137 baseBox->SetAlignment(Alignment::CENTER);
138 baseBox->SetChild(row);
139 }
140
141 box->SetFlex(BoxFlex::FLEX_X);
142 box->SetChild(baseBox);
143 }
144
Show(const RefPtr<PipelineContext> & context,const std::string & message,int32_t duration,const std::string & bottom,bool isRightToLeft)145 void ToastComponent::Show(const RefPtr<PipelineContext>& context, const std::string& message, int32_t duration,
146 const std::string& bottom, bool isRightToLeft)
147 {
148 if (!context) {
149 LOGE("fail to show toast due to context is null");
150 return;
151 }
152 auto stackElement = context->GetLastStack();
153 if (!stackElement) {
154 return;
155 }
156
157 auto themeManager = context->GetThemeManager();
158 if (!themeManager) {
159 return;
160 }
161 auto toastTheme = themeManager->GetTheme<ToastTheme>();
162 if (!toastTheme) {
163 return;
164 }
165
166 RefPtr<TextComponent> text = AceType::MakeRefPtr<TextComponent>(message);
167 text->SetTextDirection((isRightToLeft ? TextDirection::RTL : TextDirection::LTR));
168 BuildToastContent(text, toastTheme);
169 RefPtr<BoxComponent> box = AceType::MakeRefPtr<BoxComponent>();
170 BuildPackageBox(context, box, text, toastTheme);
171
172 int32_t toastId = GenerateNextToastId();
173 auto deviceType = SystemProperties::GetDeviceType();
174 int32_t barrierfreeDuration = AceApplicationInfo::GetInstance().GetBarrierfreeDuration();
175 duration = duration > barrierfreeDuration ? duration : barrierfreeDuration;
176 // get toast animation playing time
177 toastDurationTime_ = duration;
178 Dimension bottomPosition = StringUtils::StringToDimensionWithThemeValue(bottom, true, toastTheme->GetBottom());
179 RefPtr<TweenComponent> tween =
180 AceType::MakeRefPtr<TweenComponent>(TweenComponent::AllocTweenComponentId(), TOAST_TWEEN_NAME, box);
181 InitToastAnimation();
182 tween->SetTweenOption(tweenOption_);
183 tween->SetAnimationOperation(AnimationOperation::PLAY);
184 // to prevent flicking when play animation
185 tween->SetIsFirstFrameShow(false);
186
187 // prevent layer from clipping shadows
188 tween->SetShadow(ShadowConfig::DefaultShadowL);
189
190 if (deviceType == DeviceType::WATCH) {
191 // center alignment
192 std::list<RefPtr<Component>> alignChildren;
193 alignChildren.emplace_back(tween);
194 RefPtr<AlignComponent> align = AceType::MakeRefPtr<AlignComponent>(alignChildren, Alignment::CENTER);
195 stackElement->PushToastComponent(align, toastId);
196 } else {
197 RefPtr<PositionedComponent> positioned = AceType::MakeRefPtr<PositionedComponent>(tween);
198 positioned->SetBottom(GreatOrEqual(bottomPosition.Value(), 0.0) ? bottomPosition : toastTheme->GetBottom());
199 stackElement->PushToastComponent(positioned, toastId);
200 }
201
202 WeakPtr<StackElement> weak = stackElement;
203 context->GetTaskExecutor()->PostDelayedTask(
204 [weak, toastId, stopCallback = stopCallback_] {
205 auto ref = weak.Upgrade();
206 if (ref == nullptr) {
207 return;
208 }
209 ref->PopToastComponent(toastId);
210 if (stopCallback) {
211 LOGI("Animator stop callback.");
212 stopCallback();
213 }
214 },
215 TaskExecutor::TaskType::UI, duration, "ArkUIToastAnimatorStopCallback");
216 }
217
SetToastStopListenerCallback(std::function<void ()> && stopCallback)218 void ToastComponent::SetToastStopListenerCallback(std::function<void()>&& stopCallback)
219 {
220 stopCallback_ = std::move(stopCallback);
221 }
222
223 } // namespace OHOS::Ace
224