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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MARQUEE_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MARQUEE_PATTERN_H
18 
19 #include "base/geometry/ng/offset_t.h"
20 #include "base/memory/referenced.h"
21 #include "base/utils/noncopyable.h"
22 #include "core/components_ng/pattern/marquee/marquee_accessibility_property.h"
23 #include "core/components_ng/pattern/marquee/marquee_event_hub.h"
24 #include "core/components_ng/pattern/marquee/marquee_layout_algorithm.h"
25 #include "core/components_ng/pattern/marquee/marquee_layout_property.h"
26 #include "core/components_ng/pattern/marquee/marquee_paint_property.h"
27 #include "core/components_ng/pattern/pattern.h"
28 #include "core/components_ng/property/property.h"
29 #include "core/components_ng/render/paint_property.h"
30 #include "core/pipeline/base/constants.h"
31 
32 namespace OHOS::Ace {
33     class AnimationOption;
34 }
35 
36 namespace OHOS::Ace::NG {
37 using TimeCallback = std::function<void()>;
38 struct LastAnimationParam {
39     uint64_t lastStartMilliseconds = 0;
40     float lastAnimationPosition = 0.0f;
41     float lastStep = 1.0f;
42     float lastDistance = 0.0f;
43     float lastDuration = 0.0f;
44     float lastStart = 0.0f;
45     float lastEnd = 0.0f;
46     MarqueeDirection lastDirection = MarqueeDirection::LEFT;
47 };
48 
49 class MarqueePattern : public Pattern {
50     DECLARE_ACE_TYPE(MarqueePattern, Pattern);
51 
52 public:
53     MarqueePattern() = default;
54     ~MarqueePattern() override;
55 
CreateLayoutProperty()56     RefPtr<LayoutProperty> CreateLayoutProperty() override
57     {
58         return MakeRefPtr<MarqueeLayoutProperty>();
59     }
60 
CreatePaintProperty()61     RefPtr<PaintProperty> CreatePaintProperty() override
62     {
63         return MakeRefPtr<MarqueePaintProperty>();
64     }
65 
CreateEventHub()66     RefPtr<EventHub> CreateEventHub() override
67     {
68         return MakeRefPtr<MarqueeEventHub>();
69     }
70 
CreateLayoutAlgorithm()71     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
72     {
73         return MakeRefPtr<MarqueeLayoutAlgorithm>();
74     }
75 
CreateAccessibilityProperty()76     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
77     {
78         return MakeRefPtr<MarqueeAccessibilityProperty>();
79     }
80 
81     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
82     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
83     void OnColorConfigurationUpdate() override;
84     void DumpInfo() override;
85     void OnVisibleChange(bool isVisible) override;
86     void OnWindowHide() override;
87     void OnWindowShow() override;
88     TextDirection GetTextDirection(const std::string& content, TextDirection direction);
SetMarqueeFrameRateRange(const RefPtr<FrameRateRange> & rateRange,MarqueeDynamicSyncSceneType type)89     void SetMarqueeFrameRateRange(const RefPtr<FrameRateRange>& rateRange, MarqueeDynamicSyncSceneType type)
90     {
91         frameRateRange_[type] = rateRange;
92     }
93 
94 protected:
95     void OnDetachFromFrameNode(FrameNode* frameNode) override;
96 
97 private:
98     void OnModifyDone() override;
99     void OnAttachToFrameNode() override;
100 
101     void FireStartEvent() const;
102     void FireBounceEvent() const;
103     void FireFinishEvent() const;
104 
105     void StartMarqueeAnimation();
106     void StopMarqueeAnimation(bool stopAndStart);
107     void SetTextOffset(float offsetX);
108     bool OnlyPlayStatusChange();
109     void ChangeAnimationPlayStatus();
110     void StoreProperties();
111     void PlayMarqueeAnimation(float start, int32_t playCount, bool needSecondPlay);
112     void OnAnimationFinish();
113     float CalculateStart();
114     float CalculateEnd();
115     float GetTextOffset();
116     float GetTextNodeWidth();
117     double GetScrollAmount();
118     void CheckTextDirectionChange(TextDirection direction);
119     TextDirection GetCurrentTextDirection();
120     void UpdateTextDirection(
121         const RefPtr<MarqueeLayoutProperty>& layoutProperty, const RefPtr<TextLayoutProperty>& textLayoutProperty);
122     void ActionAnimation(AnimationOption& option, float end, int32_t playCount, bool needSecondPlay);
123     bool IsRunMarquee();
124     bool measureChanged_ = false;
125     int32_t animationId_ = 0;
126     std::shared_ptr<AnimationUtils::Animation> animation_;
127     bool playStatus_ = false;
128     double scrollAmount_ = DEFAULT_MARQUEE_SCROLL_AMOUNT.ConvertToPx();
129     int32_t loop_ = -1;
130     MarqueeDirection direction_ = MarqueeDirection::LEFT;
131     TextDirection currentTextDirection_ = TextDirection::LTR;
132     ACE_DISALLOW_COPY_AND_MOVE(MarqueePattern);
133     LastAnimationParam lastAnimationParam_;
134     int32_t lastWindowHeight_ = 0.0;
135     int32_t lastWindowWidth_ = 0.0;
136     float marqueeWidth_ = 0.0f;
137     std::unordered_map<MarqueeDynamicSyncSceneType, RefPtr<FrameRateRange>> frameRateRange_ ;
138 };
139 } // namespace OHOS::Ace::NG
140 
141 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MARQUEE_PATTERN_H
142