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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NAVIGATION_BAR_RENDER_COLLAPSING_NAVIGATION_BAR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NAVIGATION_BAR_RENDER_COLLAPSING_NAVIGATION_BAR_H
18 
19 #include "core/animation/animation.h"
20 #include "core/animation/animator.h"
21 #include "core/gestures/drag_recognizer.h"
22 #include "core/pipeline/base/related_node.h"
23 #include "core/pipeline/base/render_node.h"
24 
25 namespace OHOS::Ace {
26 
27 using ValueChangedCallback = std::function<void(double)>;
28 class RenderCollapsingNavigationBar : public RenderNode, public RelatedChild {
29     DECLARE_ACE_TYPE(RenderCollapsingNavigationBar, RenderNode);
30 
31 public:
32     static RefPtr<RenderNode> Create();
33     void Update(const RefPtr<Component>& component) override;
34     void PerformLayout() override;
GetPositionY()35     double GetPositionY() const
36     {
37         return positionY_.value;
38     }
39     void OnRelatedStart();
40     void OnRelatedPreScroll(const Offset& delta, Offset& consumed);
41     void OnRelatedScroll(const Offset& delta, Offset& consumed);
42     void OnRelatedEnd();
43 
44 protected:
45     void OnTouchTestHit(
46         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
47 
48 private:
IsNotSiblingAddRecognizerToResult()49     bool IsNotSiblingAddRecognizerToResult() override
50     {
51         return false;
52     }
53     void Initialize(const WeakPtr<PipelineContext>& context);
54     void AttachToContainer();
55     void ScrollBy(double dy, double maxPosition);
56     void HandleDragStart(const DragStartInfo& info);
57     void HandleDragUpdate(const DragUpdateInfo& info);
58     void HandleDragEnd();
59     void PrepareTitleSizeTranslate(double expand, double collapse);
60     void PrepareSubtitleSizeTranslate(double expand, double collapse);
61     void PreparePositionTranslate(double expand, double collapse);
62 
NeedHidden(double dy)63     bool NeedHidden(double dy)
64     {
65         return !NearZero(dy) && LessNotEqual(dy, 0.0) && LessNotEqual(-positionY_.value, scrollableHeight_);
66     }
67 
NeedShow(double dy)68     bool NeedShow(double dy)
69     {
70         bool canExpand = GreatNotEqual(dy, 0.0) && LessNotEqual(positionY_.value, positionY_.bigger);
71         bool needRestore = LessNotEqual(dy, 0.0) && GreatNotEqual(positionY_.value, positionY_.expand);
72         return !NearZero(dy) && (canExpand || needRestore);
73     }
74 
75     bool relateEvent_ = false;
76     double dipScale_ = 0.0;
77     bool lastUpScroll_ = true;
78     Dimension minHeight_;
79     double scrollableHeight_ = 0.0;
80     double lastTitleScale_ = 1.0;
81     bool barIsMini_ = false;
82     std::function<void(const std::shared_ptr<BaseEventInfo>&)> changeEvent_;
83 
84     struct ChangedKeyframe {
85         ChangedKeyframe() = default;
ChangedKeyframeChangedKeyframe86         ChangedKeyframe(double collapse, double expand, double bigger)
87             : collapse(collapse), expand(expand), bigger(bigger), value(expand), expandDis(expand - collapse),
88               biggerDis(bigger - expand)
89         {}
90         ~ChangedKeyframe() = default;
91         double collapse = 0.0;
92         double expand = 0.0;
93         double bigger = 0.0;
94         double value = 0.0;
95         double expandDis = 0.0;
96         double biggerDis = 0.0;
97         void Update(double collapseValue, double expandValue, double biggerValue = 0.0)
98         {
99             collapse = collapseValue;
100             expand = expandValue;
101             bigger = biggerValue;
102             expandDis = expand - collapse;
103             biggerDis = bigger - expand;
104         }
105     };
106     ChangedKeyframe titleSize_;
107     ChangedKeyframe subtitleOpacity_;
108     ChangedKeyframe positionY_;
109     ChangedKeyframe titlePositionY_;
110     RefPtr<DragRecognizer> dragRecognizer_;
111     RefPtr<Animator> controller_;
112     RefPtr<Animation<double>> positionTranslate_;
113     RefPtr<Animation<double>> titleSizeTranslate_;
114     RefPtr<Animation<double>> subtitleSizeTranslate_;
115     ValueChangedCallback titleChangedCallback_;
116     ValueChangedCallback subtitleChangedCallback_;
117 };
118 
119 } // namespace OHOS::Ace
120 
121 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NAVIGATION_BAR_RENDER_COLLAPSING_NAVIGATION_BAR_H
122