• 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/common/properties/page_transition_option.h"
17 
18 #include "base/utils/device_type.h"
19 #include "base/utils/system_properties.h"
20 #include "core/animation/animator.h"
21 #include "core/animation/curve_animation.h"
22 #include "core/animation/property_animatable.h"
23 
24 namespace OHOS::Ace {
25 namespace {
26 
27 constexpr float TRANSITION_FACTOR = 0.8f;
28 constexpr float TRANSITION_FACTOR_IN = 0.5f;
29 constexpr float TRANSITION_FACTOR_OUT = 0.2f;
30 constexpr float BLACK_OPACITY = 0.3f;
31 constexpr float SHARED_TRANSITION_ENTER_START_OFFSET_X = 68.0f;
32 
CreateSharedInOption(bool isRightToLeft,double deviceViewScale,double deviceWidth,TweenOption & option)33 void CreateSharedInOption(bool isRightToLeft, double deviceViewScale, double deviceWidth, TweenOption& option)
34 {
35     // opacity. parameters only have local meaning, no need to define const variables.
36     auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f);   // start with invisible
37     auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.428f, 0.0f); // invisible at 42.8% of total duration
38     opacityKeyframe2->SetCurve(Curves::LINEAR);
39     auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // finally visible
40     opacityKeyframe3->SetCurve(Curves::FRICTION);
41     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
42     opacityAnimation->AddKeyframe(opacityKeyframe1);
43     opacityAnimation->AddKeyframe(opacityKeyframe2);
44     opacityAnimation->AddKeyframe(opacityKeyframe3);
45     option.SetOpacityAnimation(opacityAnimation);
46     // translate. parameters only have local meaning, no need to define const variables.
47     double startX = SHARED_TRANSITION_ENTER_START_OFFSET_X / deviceViewScale - deviceWidth;
48     double endX = 0.0;
49     if (isRightToLeft) {
50         startX = -1.0 * startX;
51         endX = 0.0;
52     }
53     auto translateXKeyframe1 =
54         AceType::MakeRefPtr<Keyframe<DimensionOffset>>(0.0f, Offset(startX, 0)); // start with startX
55     auto translateXKeyframe2 =
56         AceType::MakeRefPtr<Keyframe<DimensionOffset>>(0.428f, Offset(startX, 0)); // start at 42.8% of total duration
57     translateXKeyframe2->SetCurve(Curves::LINEAR);
58     auto translateXKeyframe3 = AceType::MakeRefPtr<Keyframe<DimensionOffset>>(1.0f, Offset(endX, 0)); // finally at endX
59     translateXKeyframe3->SetCurve(Curves::FRICTION);
60     auto translateXAnimation = AceType::MakeRefPtr<KeyframeAnimation<DimensionOffset>>();
61     translateXAnimation->AddKeyframe(translateXKeyframe1);
62     translateXAnimation->AddKeyframe(translateXKeyframe2);
63     translateXAnimation->AddKeyframe(translateXKeyframe3);
64     option.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateXAnimation);
65 }
66 
CreateSharedOutOption(TweenOption & option)67 void CreateSharedOutOption(TweenOption& option)
68 {
69     // opacity. parameters only have local meaning, no need to define const variables.
70     auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 1.0f);   // start with invisible
71     auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.714f, 0.0f); // invisible at 71.4% of total duration
72     opacityKeyframe2->SetCurve(Curves::FRICTION);
73     auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 0.0f); // finally visible
74     opacityKeyframe3->SetCurve(Curves::LINEAR);
75     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
76     opacityAnimation->AddKeyframe(opacityKeyframe1);
77     opacityAnimation->AddKeyframe(opacityKeyframe2);
78     opacityAnimation->AddKeyframe(opacityKeyframe3);
79     option.SetOpacityAnimation(opacityAnimation);
80 }
81 
82 // Follow Watch's UI Standard.
CreateSharedStaticOption(TweenOption & option)83 void CreateSharedStaticOption(TweenOption& option)
84 {
85     // opacity. parameters only have local meaning, no need to define const variables.
86     auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f);   // start with invisible
87     auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.428f, 0.0f); // invisible at 42.8% of total duration
88     opacityKeyframe2->SetCurve(Curves::LINEAR);
89     auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // finally visible
90     opacityKeyframe3->SetCurve(Curves::FRICTION);
91     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
92     opacityAnimation->AddKeyframe(opacityKeyframe1);
93     opacityAnimation->AddKeyframe(opacityKeyframe2);
94     opacityAnimation->AddKeyframe(opacityKeyframe3);
95     option.SetOpacityAnimation(opacityAnimation);
96 }
97 
98 } // namespace
99 
100 constexpr int32_t TRANSITION_PHONE_DURATION = 300; // 0.3s
101 constexpr int32_t TRANSITION_WATCH_DURATION = 300; // 0.3s
102 constexpr int32_t TRANSITION_TV_DURATION = 1000;   // 1s
103 constexpr double TRANSITION_DEFAULT_WIDTH = 750.0;
104 constexpr double TRANSITION_DEFAULT_HEIGHT = 1900.0;
105 constexpr double TRANSITION_DEFAULT_CORNER_RADIUS = 0.0;
106 
TransitionTweenOption(bool isRightToLeft,const WeakPtr<PipelineContext> & context)107 TransitionTweenOption::TransitionTweenOption(bool isRightToLeft, const WeakPtr<PipelineContext>& context)
108 {
109     auto pipelineContext = context.Upgrade();
110     if (pipelineContext) {
111         auto deviceWidth = pipelineContext->GetStageRect().Width();
112         deviceWidth_ = deviceWidth > 0.0 ? deviceWidth : deviceWidth_;
113         auto deviceHeight = pipelineContext->GetStageRect().Height();
114         deviceHeight_ = deviceHeight > 0.0 ? deviceHeight : deviceHeight_;
115         auto viewScale = pipelineContext->GetViewScale();
116         deviceViewScale_ = viewScale > 0.0 ? viewScale : deviceViewScale_;
117         isRightToLeft_ = isRightToLeft;
118         windowModal_ = pipelineContext->GetWindowModal();
119     }
120 }
121 
GetTransitionContentInOption() const122 const TweenOption& TransitionTweenOption::GetTransitionContentInOption() const
123 {
124     return contentInOption_;
125 }
126 
GetTransitionContentOutOption() const127 const TweenOption& TransitionTweenOption::GetTransitionContentOutOption() const
128 {
129     return contentOutOption_;
130 }
131 
GetTransitionBackgroundInOption() const132 const TweenOption& TransitionTweenOption::GetTransitionBackgroundInOption() const
133 {
134     return backgroundInOption_;
135 }
136 
GetTransitionBackgroundOutOption() const137 const TweenOption& TransitionTweenOption::GetTransitionBackgroundOutOption() const
138 {
139     return backgroundOutOption_;
140 }
141 
GetTransitionFrontDecorationOption() const142 const TweenOption& TransitionTweenOption::GetTransitionFrontDecorationOption() const
143 {
144     return frontDecorationOption_;
145 }
146 
GetSharedTransitionFrontDecorationOption() const147 const TweenOption& TransitionTweenOption::GetSharedTransitionFrontDecorationOption() const
148 {
149     return sharedFrontDecorationOption_;
150 }
151 
GetSharedInOption() const152 const TweenOption& TransitionTweenOption::GetSharedInOption() const
153 {
154     return sharedTransitionInOption_;
155 }
156 
GetSharedOutOption() const157 const TweenOption& TransitionTweenOption::GetSharedOutOption() const
158 {
159     return sharedTransitionOutOption_;
160 }
161 
TransitionTvTweenOption(bool isRightToLeft,const WeakPtr<PipelineContext> & context)162 TransitionTvTweenOption::TransitionTvTweenOption(bool isRightToLeft, const WeakPtr<PipelineContext>& context)
163     : TransitionTweenOption(isRightToLeft, context)
164 {
165     CreateTransitionInOption();
166     CreateTransitionOutOption();
167     // Currently, Tv Do not have UI standard, no need to create special shared transition.
168 }
169 
CreateTransitionInOption()170 void TransitionTvTweenOption::CreateTransitionInOption()
171 {
172     CreateTransitionInContentOption();
173     CreateTransitionInBackgroundOption();
174 }
175 
CreateTransitionInContentOption()176 void TransitionTvTweenOption::CreateTransitionInContentOption()
177 {
178     // content opacity. parameters only have local meaning, no need to define const variables.
179     auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f);  // start with invisible
180     auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.55f, 0.0f); // invisible at 55% of total duration
181     opacityKeyframe2->SetCurve(Curves::FRICTION);
182     auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // finally visible
183     opacityKeyframe3->SetCurve(Curves::FRICTION);
184     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
185     opacityAnimation->AddKeyframe(opacityKeyframe1);
186     opacityAnimation->AddKeyframe(opacityKeyframe2);
187     opacityAnimation->AddKeyframe(opacityKeyframe3);
188     contentInOption_.SetOpacityAnimation(opacityAnimation);
189 
190     // content scale. parameters only have local meaning, no need to define const variables.
191     auto scaleKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f);  // start with invisible
192     auto scaleKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.55f, 0.9f); // 90% scale at 55% of total duration
193     scaleKeyframe2->SetCurve(Curves::FRICTION);
194     auto scaleKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // finally 100% scale
195     scaleKeyframe3->SetCurve(Curves::FRICTION);
196     auto scaleAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
197     scaleAnimation->AddKeyframe(scaleKeyframe1);
198     scaleAnimation->AddKeyframe(scaleKeyframe2);
199     scaleAnimation->AddKeyframe(scaleKeyframe3);
200     contentInOption_.SetTransformFloatAnimation(AnimationType::SCALE, scaleAnimation);
201     contentInOption_.SetDuration(TRANSITION_TV_DURATION);
202 }
203 
CreateTransitionInBackgroundOption()204 void TransitionTvTweenOption::CreateTransitionInBackgroundOption()
205 {
206     // background opacity. parameters only have local meaning, no need to define const variables.
207     auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f); // start with invisible
208     auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.5f, 0.0f); // invisible at 50% of total duration
209     opacityKeyframe2->SetCurve(Curves::FRICTION);
210     auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(0.95f, 1.0f); // visible at 95% of total duration
211     opacityKeyframe3->SetCurve(Curves::FRICTION);
212     auto opacityKeyframe4 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // finally visible
213     opacityKeyframe4->SetCurve(Curves::FRICTION);
214     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
215     opacityAnimation->AddKeyframe(opacityKeyframe1);
216     opacityAnimation->AddKeyframe(opacityKeyframe2);
217     opacityAnimation->AddKeyframe(opacityKeyframe3);
218     opacityAnimation->AddKeyframe(opacityKeyframe4);
219     backgroundInOption_.SetOpacityAnimation(opacityAnimation);
220 
221     // background scale. parameters only have local meaning, no need to define const variables.
222     auto scaleKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f); // start with invisible
223     auto scaleKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.5f, 1.1f); // 110% scale at 50% of total duration
224     scaleKeyframe2->SetCurve(Curves::FRICTION);
225     auto scaleKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(0.95f, 1.0f); // 100% scale at 95% of total duration
226     scaleKeyframe3->SetCurve(Curves::FRICTION);
227     auto scaleKeyframe4 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // finally 100% scale
228     scaleKeyframe4->SetCurve(Curves::FRICTION);
229     auto scaleAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
230     scaleAnimation->AddKeyframe(scaleKeyframe1);
231     scaleAnimation->AddKeyframe(scaleKeyframe2);
232     scaleAnimation->AddKeyframe(scaleKeyframe3);
233     scaleAnimation->AddKeyframe(scaleKeyframe4);
234     backgroundInOption_.SetTransformFloatAnimation(AnimationType::SCALE, scaleAnimation);
235 }
236 
CreateTransitionOutOption()237 void TransitionTvTweenOption::CreateTransitionOutOption()
238 {
239     CreateTransitionOutContentOption();
240     CreateTransitionOutBackgroundOption();
241 }
242 
CreateTransitionOutContentOption()243 void TransitionTvTweenOption::CreateTransitionOutContentOption()
244 {
245     CreatTransitionOutOption(contentOutOption_);
246 }
247 
CreateTransitionOutBackgroundOption()248 void TransitionTvTweenOption::CreateTransitionOutBackgroundOption()
249 {
250     CreatTransitionOutOption(backgroundOutOption_);
251 }
252 
CreatTransitionOutOption(TweenOption & option)253 void TransitionTvTweenOption::CreatTransitionOutOption(TweenOption& option)
254 {
255     // out opacity. parameters only have local meaning, no need to define const variables.
256     auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 1.0f); // start with visible
257     auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.1f, 1.0f); // visible at 10% of total duration
258     opacityKeyframe2->SetCurve(Curves::FRICTION);
259     auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(0.55f, 0.0f); // invisible at 55% of total duration
260     opacityKeyframe3->SetCurve(Curves::FRICTION);
261     auto opacityKeyframe4 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 0.0f); // finally invisible
262     opacityKeyframe4->SetCurve(Curves::FRICTION);
263     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
264     opacityAnimation->AddKeyframe(opacityKeyframe1);
265     opacityAnimation->AddKeyframe(opacityKeyframe2);
266     opacityAnimation->AddKeyframe(opacityKeyframe3);
267     opacityAnimation->AddKeyframe(opacityKeyframe4);
268     option.SetOpacityAnimation(opacityAnimation);
269 
270     // out scale. parameters only have local meaning, no need to define const variables.
271     auto scaleKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 1.0f); // 100% scale at start
272     auto scaleKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.1f, 1.0f); // 100% scale at 10% of total duration
273     scaleKeyframe2->SetCurve(Curves::FRICTION);
274     auto scaleKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(0.55f, 1.1f); // 110% scale at 55% of total duration
275     scaleKeyframe3->SetCurve(Curves::FRICTION);
276     auto scaleKeyframe4 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f); // 100% scale at 100% of total duration
277     scaleKeyframe4->SetCurve(Curves::FRICTION);
278     auto scaleAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
279     scaleAnimation->AddKeyframe(scaleKeyframe1);
280     scaleAnimation->AddKeyframe(scaleKeyframe2);
281     scaleAnimation->AddKeyframe(scaleKeyframe3);
282     scaleAnimation->AddKeyframe(scaleKeyframe4);
283     option.SetTransformFloatAnimation(AnimationType::SCALE, scaleAnimation);
284     option.SetDuration(TRANSITION_TV_DURATION);
285 }
286 
CreateTransitionTweenOption(DeviceType deviceType,TransitionEvent event,bool isRightToLeft,const RRect & rrect,const WeakPtr<PipelineContext> & context)287 RefPtr<TransitionTweenOption> TransitionTweenOptionFactory::CreateTransitionTweenOption(
288     DeviceType deviceType, TransitionEvent event, bool isRightToLeft, const RRect& rrect,
289     const WeakPtr<PipelineContext>& context)
290 {
291     switch (deviceType) {
292         case DeviceType::PHONE:
293             if (rrect.GetRect().IsValid()) {
294                 return AceType::MakeRefPtr<TransitionPhoneTweenOption>(event, isRightToLeft, rrect, context);
295             }
296             return AceType::MakeRefPtr<TransitionPhoneTweenOption>(event, isRightToLeft, context);
297         case DeviceType::TV:
298             return AceType::MakeRefPtr<TransitionTvTweenOption>(isRightToLeft, context);
299         case DeviceType::WATCH:
300             return AceType::MakeRefPtr<TransitionWatchTweenOption>(event, isRightToLeft, context);
301         default:
302             LOGE("unknown transition type: %{public}d, use default type", deviceType);
303             return AceType::MakeRefPtr<TransitionPhoneTweenOption>(event, isRightToLeft, context);
304     }
305 }
306 
CreateSharedTweenOption(SharedTransitionEffectType type,TweenOption & option)307 void TransitionTweenOptionFactory::CreateSharedTweenOption(SharedTransitionEffectType type, TweenOption& option)
308 {
309     if (type == SharedTransitionEffectType::SHARED_EFFECT_STATIC) {
310         CreateSharedStaticOption(option);
311     } else {
312         LOGW("Unsupported Shared transition effect type: %{public}d", type);
313     }
314 }
315 
TransitionPhoneTweenOption(TransitionEvent event,bool isRightToLeft,const WeakPtr<PipelineContext> & context)316 TransitionPhoneTweenOption::TransitionPhoneTweenOption(
317     TransitionEvent event, bool isRightToLeft, const WeakPtr<PipelineContext>& context)
318     : TransitionTweenOption(isRightToLeft, context)
319 {
320     if (windowModal_ == WindowModal::DIALOG_MODAL) {
321         CreateDialogModalTransitionInOption(event);
322         CreateDialogModalTransitionOutOption(event);
323     } else {
324         CreateTransitionInOption(event);
325         CreateTransitionOutOption(event);
326     }
327     CreateSharedInOption(isRightToLeft_, deviceViewScale_, deviceWidth_, sharedTransitionInOption_);
328     CreateSharedOutOption(sharedTransitionOutOption_);
329     // extra options for phone
330     auto widthAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(deviceWidth_, deviceWidth_, Curves::FRICTION);
331     sharedTransitionOutOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_WIDTH, widthAnimation);
332 
333     auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
334         DimensionOffset(Offset()), DimensionOffset(Offset()), Curves::LINEAR);
335     sharedTransitionOutOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
336 
337     auto colorAnimation = AceType::MakeRefPtr<CurveAnimation<Color>>(
338         Color::FromRGBO(0, 0, 0, 0.0), Color::FromRGBO(0, 0, 0, 0.0), Curves::FRICTION);
339     sharedFrontDecorationOption_.SetColorAnimation(colorAnimation);
340     sharedFrontDecorationOption_.SetIsBackground(false);
341 }
342 
TransitionPhoneTweenOption(TransitionEvent event,bool isRightToLeft,const RRect & rrect,const WeakPtr<PipelineContext> & context)343 TransitionPhoneTweenOption::TransitionPhoneTweenOption(
344     TransitionEvent event, bool isRightToLeft, const RRect& rrect, const WeakPtr<PipelineContext>& context)
345     : TransitionTweenOption(isRightToLeft, context)
346 {
347     CreateCardTransitionInOption(event, rrect);
348     CreateCardTransitionOutOption(event);
349 
350     CreateSharedInOption(isRightToLeft_, deviceViewScale_, deviceWidth_, sharedTransitionInOption_);
351     CreateSharedOutOption(sharedTransitionOutOption_);
352     // extra options for phone
353     auto widthAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(deviceWidth_, deviceWidth_, Curves::FRICTION);
354     sharedTransitionOutOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_WIDTH, widthAnimation);
355 
356     auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
357         DimensionOffset(Offset()), DimensionOffset(Offset()), Curves::LINEAR);
358     sharedTransitionOutOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
359 
360     auto colorAnimation = AceType::MakeRefPtr<CurveAnimation<Color>>(
361         Color::FromRGBO(0, 0, 0, 0.0), Color::FromRGBO(0, 0, 0, 0.0), Curves::FRICTION);
362     sharedFrontDecorationOption_.SetColorAnimation(colorAnimation);
363     sharedFrontDecorationOption_.SetIsBackground(false);
364 }
365 
CreateDialogModalTransitionInOption(TransitionEvent event)366 void TransitionPhoneTweenOption::CreateDialogModalTransitionInOption(TransitionEvent event)
367 {
368     RefPtr<CurveAnimation<DimensionOffset>> translateAnimation;
369     if (isRightToLeft_) {
370         translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
371             DimensionOffset(Dimension(-deviceWidth_, DimensionUnit::PX), Dimension()),
372             DimensionOffset(Dimension(), Dimension()), Curves::FRICTION);
373     } else {
374         translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
375             DimensionOffset(Dimension(deviceWidth_, DimensionUnit::PX), Dimension()),
376             DimensionOffset(Dimension(), Dimension()), Curves::FRICTION);
377     }
378     contentInOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
379     contentInOption_.SetDuration(TRANSITION_PHONE_DURATION);
380 }
381 
CreateTransitionInOption(TransitionEvent event)382 void TransitionPhoneTweenOption::CreateTransitionInOption(TransitionEvent event)
383 {
384     if (isRightToLeft_) {
385         auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
386             DimensionOffset(Dimension(-deviceWidth_ * TRANSITION_FACTOR_IN, DimensionUnit::PX), Dimension()),
387             DimensionOffset(Dimension(), Dimension()), Curves::FRICTION);
388         auto widthAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(
389             deviceWidth_ * TRANSITION_FACTOR_IN, deviceWidth_, Curves::FRICTION);
390         contentInOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_WIDTH, widthAnimation);
391         contentInOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
392     } else {
393         auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
394             DimensionOffset(Dimension(deviceWidth_ * TRANSITION_FACTOR_IN, DimensionUnit::PX), Dimension()),
395             DimensionOffset(Dimension(), Dimension()), Curves::FRICTION);
396         auto offsetXAnimation =
397             AceType::MakeRefPtr<CurveAnimation<float>>(deviceWidth_ * TRANSITION_FACTOR_IN, 0.0f, Curves::FRICTION);
398         contentInOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_OFFSET_X, offsetXAnimation);
399         contentInOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
400     }
401     if (event == TransitionEvent::POP_START) {
402         contentInOption_.SetCurve(Curves::FAST_OUT_SLOW_IN);
403     }
404     contentInOption_.SetDuration(TRANSITION_PHONE_DURATION);
405 }
406 
CreateDialogModalTransitionOutOption(TransitionEvent event)407 void TransitionPhoneTweenOption::CreateDialogModalTransitionOutOption(TransitionEvent event)
408 {
409     RefPtr<CurveAnimation<DimensionOffset>> translateAnimation;
410     if (isRightToLeft_) {
411         translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(DimensionOffset(Offset()),
412             DimensionOffset(Offset(deviceWidth_, 0)), Curves::FRICTION);
413     } else {
414         translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(DimensionOffset(Offset()),
415             DimensionOffset(Offset(-deviceWidth_, 0)), Curves::FRICTION);
416     }
417     auto opacityAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(1.0f, 1.0f, Curves::LINEAR);
418     contentOutOption_.SetOpacityAnimation(opacityAnimation);
419     contentOutOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
420 }
421 
CreateTransitionOutOption(TransitionEvent event)422 void TransitionPhoneTweenOption::CreateTransitionOutOption(TransitionEvent event)
423 {
424     if (isRightToLeft_) {
425         auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(DimensionOffset(Offset()),
426             DimensionOffset(Offset(deviceWidth_ * TRANSITION_FACTOR_OUT, 0)), Curves::FRICTION);
427         auto offsetXAnimation =
428             AceType::MakeRefPtr<CurveAnimation<float>>(0.0f, deviceWidth_ * TRANSITION_FACTOR, Curves::FRICTION);
429         contentOutOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_OFFSET_X, offsetXAnimation);
430         contentOutOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
431     } else {
432         auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(DimensionOffset(Offset()),
433             DimensionOffset(Offset(-deviceWidth_ * TRANSITION_FACTOR_OUT, 0)), Curves::FRICTION);
434         auto widthAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(
435             deviceWidth_, deviceWidth_ * TRANSITION_FACTOR_OUT, Curves::FRICTION);
436         contentOutOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_WIDTH, widthAnimation);
437         contentOutOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
438     }
439     auto opacityAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(1.0f, 1.0f, Curves::LINEAR);
440     contentOutOption_.SetOpacityAnimation(opacityAnimation);
441 
442     auto colorAnimation = AceType::MakeRefPtr<CurveAnimation<Color>>(
443         Color::FromRGBO(0, 0, 0, 0.0), Color::FromRGBO(0, 0, 0, BLACK_OPACITY), Curves::FRICTION);
444     frontDecorationOption_.SetColorAnimation(colorAnimation);
445     frontDecorationOption_.SetIsBackground(false);
446 
447     if (event == TransitionEvent::POP_START) {
448         contentOutOption_.SetCurve(Curves::FAST_OUT_SLOW_IN);
449         frontDecorationOption_.SetCurve(Curves::FAST_OUT_SLOW_IN);
450     }
451 }
452 
TransitionWatchTweenOption(TransitionEvent event,bool isRightToLeft,const WeakPtr<PipelineContext> & context)453 TransitionWatchTweenOption::TransitionWatchTweenOption(
454     TransitionEvent event, bool isRightToLeft, const WeakPtr<PipelineContext>& context)
455     : TransitionTweenOption(isRightToLeft, context)
456 {
457     CreateTransitionInOption(event);
458     CreateTransitionOutOption(event);
459     CreateSharedInOption(isRightToLeft_, deviceViewScale_, deviceWidth_, sharedTransitionInOption_);
460     CreateSharedOutOption(sharedTransitionOutOption_);
461 }
462 
CreateTransitionInOption(TransitionEvent event)463 void TransitionWatchTweenOption::CreateTransitionInOption(TransitionEvent event)
464 {
465     RefPtr<CurveAnimation<DimensionOffset>> translateAnimation;
466     if (isRightToLeft_) {
467         translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
468             DimensionOffset(Dimension(-deviceWidth_, DimensionUnit::PX), Dimension()),
469             DimensionOffset(Dimension(), Dimension()), Curves::FRICTION);
470     } else {
471         translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(
472             DimensionOffset(Dimension(deviceWidth_, DimensionUnit::PX), Dimension()),
473             DimensionOffset(Dimension(), Dimension()), Curves::FRICTION);
474     }
475     contentInOption_.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
476     contentInOption_.SetDuration(TRANSITION_WATCH_DURATION);
477 
478     if (event == TransitionEvent::POP_START) {
479         contentInOption_.SetCurve(Curves::SMOOTH);
480     }
481 }
482 
CreateTransitionOutOption(TransitionEvent event)483 void TransitionWatchTweenOption::CreateTransitionOutOption(TransitionEvent event)
484 {
485     if (isRightToLeft_) {
486         auto offsetXAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(0.0f, deviceWidth_, Curves::FRICTION);
487         contentOutOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_OFFSET_X, offsetXAnimation);
488     } else {
489         auto widthAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(deviceWidth_, 0.0f, Curves::FRICTION);
490         contentOutOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_WIDTH, widthAnimation);
491     }
492     auto opacityAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(1.0f, 1.0f, Curves::FRICTION);
493     contentOutOption_.SetOpacityAnimation(opacityAnimation);
494 
495     if (event == TransitionEvent::POP_START) {
496         contentOutOption_.SetCurve(Curves::SMOOTH);
497     }
498 }
499 
CreateCardTransitionOutOption(TransitionEvent event)500 void TransitionPhoneTweenOption::CreateCardTransitionOutOption(TransitionEvent event)
501 {
502     contentOutOption_.SetDuration(TRANSITION_PHONE_DURATION);
503     // blackening treatment
504     if (event == TransitionEvent::POP_START) {
505         auto colorAnimation = AceType::MakeRefPtr<CurveAnimation<Color>>(Color::FromRGBO(0, 0, 0, 0.2),
506             Color::FromRGBO(0, 0, 0, 0.0), Curves::FRICTION);
507         frontDecorationOption_.SetColorAnimation(colorAnimation);
508     } else {
509         auto colorAnimation = AceType::MakeRefPtr<KeyframeAnimation<Color>>();
510         auto colorKeyframe1 = AceType::MakeRefPtr<Keyframe<Color>>(0.0f, Color::FromRGBO(0, 0, 0, 0.0));
511         auto colorKeyframe2 = AceType::MakeRefPtr<Keyframe<Color>>(0.571f, Color::FromRGBO(0, 0, 0, 0.2));
512         auto colorKeyframe3 = AceType::MakeRefPtr<Keyframe<Color>>(1.0f, Color::FromRGBO(0, 0, 0, 0.2));
513         colorKeyframe2->SetCurve(Curves::SHARP);
514         colorAnimation->AddKeyframe(colorKeyframe1);
515         colorAnimation->AddKeyframe(colorKeyframe2);
516         colorAnimation->AddKeyframe(colorKeyframe3);
517         frontDecorationOption_.SetColorAnimation(colorAnimation);
518     }
519     frontDecorationOption_.SetIsBackground(false);
520 }
521 
CreateCardTransitionInOption(TransitionEvent event,const RRect & rrect)522 void TransitionPhoneTweenOption::CreateCardTransitionInOption(TransitionEvent event, const RRect& rrect)
523 {
524     const Offset& position = rrect.GetRect().GetOffset();
525     const Size& size = rrect.GetRect().GetSize();
526     contentInOption_.SetTransformOrigin(Dimension(), Dimension());
527     if (event == TransitionEvent::POP_START) {
528         auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(DimensionOffset(Offset()),
529             DimensionOffset(position), Curves::FRICTION);
530         auto heightAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(deviceHeight_,
531             size.Height() * deviceWidth_ / size.Width(), Curves::FRICTION);
532         auto scaleAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(1.0f, size.Width() / deviceWidth_,
533             Curves::FRICTION);
534         contentInOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_HEIGHT, heightAnimation);
535         contentInOption_.SetTranslateAnimations(AnimationType::TRANSLATE, translateAnimation);
536         contentInOption_.SetTransformFloatAnimation(AnimationType::SCALE, scaleAnimation);
537     } else {
538         auto translateAnimation = AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(DimensionOffset(position),
539             DimensionOffset(Offset()), Curves::FRICTION);
540         auto heightAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(size.Height() * deviceWidth_ / size.Width(),
541             deviceHeight_, Curves::FRICTION);
542         auto scaleAnimation = AceType::MakeRefPtr<CurveAnimation<float>>(size.Width() / deviceWidth_, 1.0f,
543             Curves::FRICTION);
544         contentInOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_HEIGHT, heightAnimation);
545         contentInOption_.SetTranslateAnimations(AnimationType::TRANSLATE, translateAnimation);
546         contentInOption_.SetTransformFloatAnimation(AnimationType::SCALE, scaleAnimation);
547     }
548     CreateCornerAnimationInOption(event, rrect);
549     CreateCardOpacityAnimationInOption(event);
550     contentInOption_.SetDuration(TRANSITION_PHONE_DURATION);
551 }
552 
CreateCornerAnimationInOption(TransitionEvent event,const RRect & rrect)553 void TransitionPhoneTweenOption::CreateCornerAnimationInOption(TransitionEvent event, const RRect& rrect)
554 {
555     auto cornerRadiusAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
556     auto cornerRadius = rrect.GetCorner().topLeftRadius.GetX().Value() * deviceWidth_ / rrect.Width();
557     if (event == TransitionEvent::POP_START) {
558         auto cornerKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, TRANSITION_DEFAULT_CORNER_RADIUS);
559         auto cornerKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.428f, cornerRadius);
560         auto cornerKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, cornerRadius);
561         cornerKeyframe2->SetCurve(Curves::FRICTION);
562         cornerRadiusAnimation->AddKeyframe(cornerKeyframe1);
563         cornerRadiusAnimation->AddKeyframe(cornerKeyframe2);
564         cornerRadiusAnimation->AddKeyframe(cornerKeyframe3);
565     } else {
566         auto cornerKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, cornerRadius);
567         auto cornerKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.571f, cornerRadius);
568         auto cornerKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, TRANSITION_DEFAULT_CORNER_RADIUS);
569         cornerKeyframe3->SetCurve(Curves::FRICTION);
570         cornerRadiusAnimation->AddKeyframe(cornerKeyframe1);
571         cornerRadiusAnimation->AddKeyframe(cornerKeyframe2);
572         cornerRadiusAnimation->AddKeyframe(cornerKeyframe3);
573     }
574     contentInOption_.SetPropertyAnimationFloat(PropertyAnimatableType::PROPERTY_BORDER_RADIUS, cornerRadiusAnimation);
575 }
576 
CreateCardOpacityAnimationInOption(TransitionEvent event)577 void TransitionPhoneTweenOption::CreateCardOpacityAnimationInOption(TransitionEvent event)
578 {
579     auto opacityAnimation = AceType::MakeRefPtr<KeyframeAnimation<float>>();
580     if (event == TransitionEvent::POP_START) {
581         auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 1.0f);
582         auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.571f, 1.0f);
583         auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 0.0f);
584         opacityKeyframe3->SetCurve(Curves::SHARP);
585         opacityAnimation->AddKeyframe(opacityKeyframe1);
586         opacityAnimation->AddKeyframe(opacityKeyframe2);
587         opacityAnimation->AddKeyframe(opacityKeyframe3);
588     } else {
589         auto opacityKeyframe1 = AceType::MakeRefPtr<Keyframe<float>>(0.0f, 0.0f);
590         auto opacityKeyframe2 = AceType::MakeRefPtr<Keyframe<float>>(0.286f, 1.0f);
591         auto opacityKeyframe3 = AceType::MakeRefPtr<Keyframe<float>>(1.0f, 1.0f);
592         opacityKeyframe2->SetCurve(Curves::SHARP);
593         opacityAnimation->AddKeyframe(opacityKeyframe1);
594         opacityAnimation->AddKeyframe(opacityKeyframe2);
595         opacityAnimation->AddKeyframe(opacityKeyframe3);
596     }
597     contentInOption_.SetOpacityAnimation(opacityAnimation);
598 }
599 
CreateSlideEffectAnimation(TweenOption & tweenOption,SlideEffect effect,PageTransitionType type,TransitionDirection direction)600 void TransitionDeclarativeTweenOption::CreateSlideEffectAnimation(
601     TweenOption& tweenOption, SlideEffect effect, PageTransitionType type, TransitionDirection direction)
602 {
603     bool exitFlag = false;
604     if (type == PageTransitionType::EXIT || type == PageTransitionType::EXIT_POP ||
605         type == PageTransitionType::EXIT_PUSH) {
606         exitFlag = true;
607     }
608 
609     if (effect == SlideEffect::LEFT) {
610         auto init = DimensionOffset(Dimension(-deviceWidth_, DimensionUnit::PX), Dimension());
611         auto target = DimensionOffset(Dimension(), Dimension());
612         if (exitFlag) {
613             std::swap(init, target);
614         }
615         auto translateAnimation =
616             AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(init, target, tweenOption.GetCurve());
617         tweenOption.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
618     } else if (effect == SlideEffect::RIGHT) {
619         auto init = DimensionOffset(Dimension(deviceWidth_, DimensionUnit::PX), Dimension());
620         auto target = DimensionOffset(Dimension(), Dimension());
621         if (exitFlag) {
622             std::swap(init, target);
623         }
624         auto translateAnimation =
625             AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(init, target, tweenOption.GetCurve());
626         tweenOption.SetTranslateAnimations(AnimationType::TRANSLATE_X, translateAnimation);
627     } else if (effect == SlideEffect::TOP) {
628         auto init = DimensionOffset(Dimension(), Dimension(-deviceHeight_, DimensionUnit::PX));
629         auto target = DimensionOffset(Dimension(), Dimension());
630         if (exitFlag) {
631             std::swap(init, target);
632         }
633         auto translateAnimation =
634             AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(init, target, tweenOption.GetCurve());
635         tweenOption.SetTranslateAnimations(AnimationType::TRANSLATE_Y, translateAnimation);
636     } else if (effect == SlideEffect::BOTTOM) {
637         auto init = DimensionOffset(Dimension(), Dimension(deviceHeight_, DimensionUnit::PX));
638         auto target = DimensionOffset(Dimension(), Dimension());
639         if (exitFlag) {
640             std::swap(init, target);
641         }
642         auto translateAnimation =
643             AceType::MakeRefPtr<CurveAnimation<DimensionOffset>>(init, target, tweenOption.GetCurve());
644         tweenOption.SetTranslateAnimations(AnimationType::TRANSLATE_Y, translateAnimation);
645     }
646 }
647 
648 } // namespace OHOS::Ace
649