1 /*
2  * Copyright (c) 2022-2023 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_PATTERN_PANEL_PANEL_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PANEL_PANEL_PATTERN_H
18 
19 #include <optional>
20 
21 #include "base/geometry/axis.h"
22 #include "base/memory/referenced.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components/panel/panel_component.h"
25 #include "core/components_ng/event/event_hub.h"
26 #include "core/components_ng/manager/focus/focus_view.h"
27 #include "core/components_ng/pattern/panel/close_icon_pattern.h"
28 #include "core/components_ng/pattern/panel/drag_bar_pattern.h"
29 #include "core/components_ng/pattern/panel/sliding_panel_event_hub.h"
30 #include "core/components_ng/pattern/panel/sliding_panel_layout_algorithm.h"
31 #include "core/components_ng/pattern/panel/sliding_panel_layout_property.h"
32 #include "core/components_ng/pattern/pattern.h"
33 
34 namespace OHOS::Ace::NG {
35 class InspectorFilter;
36 
37 class SlidingPanelPattern : public Pattern, public FocusView {
38     DECLARE_ACE_TYPE(SlidingPanelPattern, Pattern, FocusView);
39 
40 public:
41     SlidingPanelPattern() = default;
42     ~SlidingPanelPattern() override = default;
43 
IsAtomicNode()44     bool IsAtomicNode() const override
45     {
46         return false;
47     }
48 
CreateLayoutProperty()49     RefPtr<LayoutProperty> CreateLayoutProperty() override
50     {
51         return MakeRefPtr<SlidingPanelLayoutProperty>();
52     }
53 
CreateLayoutAlgorithm()54     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
55     {
56         auto layoutAlgorithm = MakeRefPtr<SlidingPanelLayoutAlgorithm>();
57         layoutAlgorithm->SetCurrentOffset(currentOffset_);
58         layoutAlgorithm->SetIsFirstLayout(isFirstLayout_);
59         layoutAlgorithm->SetInvisibleFlag(invisibleFlag_.value_or(false));
60         return layoutAlgorithm;
61     }
62 
CreateEventHub()63     RefPtr<EventHub> CreateEventHub() override
64     {
65         return MakeRefPtr<SlidingPanelEventHub>();
66     }
67 
GetFocusPattern()68     FocusPattern GetFocusPattern() const override
69     {
70         return { FocusType::SCOPE, true };
71     }
72 
GetRouteOfFirstScope()73     std::list<int32_t> GetRouteOfFirstScope() override
74     {
75         return { 0, 0, 0 };
76     }
77 
SetLastOffset(float lastOffset)78     void SetLastOffset(float lastOffset)
79     {
80         lastOffset_ = lastOffset;
81     }
82 
GetLastOffset()83     float GetLastOffset() const
84     {
85         return lastOffset_;
86     }
87 
88     void OnAnimationStop();
89     void UpdateCurrentOffset(float offset);
90     void UpdateCurrentOffsetOnAnimate(float currentOffset);
91     void MarkDirtyNode(PropertyChangeFlag extraFlag);
92     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
93 
94 private:
95     void OnModifyDone() override;
96     void OnAttachToFrameNode() override;
97     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
98     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
99 
100     // Init pan recognizer to move items when drag update, play translate animation when drag end.
101     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
102     void Update();
103     void UpdatePanelRenderContext();
104     // Init LayoutProperties
105     void InitializeLayoutProps();
106 
107     void FireSizeChangeEvent();
108     void FireHeightChangeEvent();
109     void HandleDragStart(const Offset& startPoint);
110     void HandleDragUpdate(const GestureEvent& info);
111     void HandleDragEnd(float dragVelocity);
112     void CalculateModeTypeMini(float dragLen, float dragVelocity);
113     void CalculateModeTypeFold(float dragLen, float dragVelocity);
114     void CalculateModeTypeTemp(float dragLen, float dragVelocity);
115     void AnimateTo(float targetLocation, PanelMode mode);
116     void AppendBlankHeightAnimation(float targetLocation, PanelMode mode);
117     int32_t GetAnimationDuration(float delta, float dragRange) const;
118     void CheckHeightValidity();
119     void CheckPanelModeAndType();
120     void FirstLayout();
121     void IsShowChanged(bool isShow);
122     void HeightDynamicUpdate();
123     void SetDragBarCallBack();
124     void SetCloseIconCallBack();
125 
126     PanelType GetPanelType() const;
127     PanelMode GetPanelMode() const;
128     RefPtr<FrameNode> GetDragBarNode();
129     void FireChangeEvent() const;
130     RefPtr<FrameNode> GetCloseIconNode();
131     RefPtr<UINode> GetChildNodeByTag(const std::string& tagName) const;
132     void AddOrRemoveDragBarNode(const RefPtr<SlidingPanelLayoutProperty>& layoutProperty) const;
133     void AddOrRemoveCloseIconNode(const RefPtr<SlidingPanelLayoutProperty>& layoutProperty) const;
134     void ResetLayoutWeight();
135     bool IsNeedResetPanEvent(const RefPtr<GestureEventHub>& gestureHub);
136 
137     RefPtr<PanEvent> panEvent_;
138     RefPtr<Animator> animator_;
139     std::unordered_map<PanelMode, double> defaultBlankHeights_;
140 
141     bool isAnimating_ = false;
142     bool isFirstLayout_ = true;
143 
144     std::optional<PanelMode> mode_;
145     PanelMode previousMode_ = PanelMode::HALF;
146     PanelType type_ = PanelType::FOLDABLE_BAR;
147     float fullHalfBoundary_ = 0.0f;
148     float halfMiniBoundary_ = 0.0f;
149     float fullMiniBoundary_ = 0.0f;
150 
151     Dimension fullHeight_;
152     Dimension halfHeight_;
153     Dimension miniHeight_;
154     CalcDimension customHeight_;
155     float maxWidth_ = 0.0f;
156     SizeF maxSize_;
157 
158     float minBlankHeight_ = 0.0;
159     float currentOffset_ = 0.0f;
160     float dragStartCurrentOffset_ = 0.0f;
161 
162     std::optional<bool> isShow_;
163     bool isDrag_ = false;
164     std::optional<bool> invisibleFlag_;
165     std::queue<bool> isShowQueue_;
166     bool isClosePanel_ = false;
167     bool preAnimateFlag_ = false;
168     float lastOffset_ = 0.0f;
169 
170     ACE_DISALLOW_COPY_AND_MOVE(SlidingPanelPattern);
171 };
172 } // namespace OHOS::Ace::NG
173 
174 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PANEL_PANEL_PATTERN_H
175