1 /*
2  * Copyright (c) 2024 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_LIST_LIST_ITEM_DRAG_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LIST_LIST_ITEM_DRAG_MANAGER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/base/ui_node.h"
21 #include "core/components_ng/syntax/for_each_base_node.h"
22 
23 namespace OHOS::Ace::NG {
24 class ListItemDragManager : public AceType {
25     DECLARE_ACE_TYPE(ListItemDragManager, AceType)
26 
27 public:
28     struct ScaleResult {
29         bool needMove;
30         float scale;
31     };
ListItemDragManager(RefPtr<FrameNode> host,RefPtr<ForEachBaseNode> forEach)32     ListItemDragManager(RefPtr<FrameNode> host, RefPtr<ForEachBaseNode> forEach)
33     {
34         frameNode_ = host;
35         forEachNode_ = forEach;
36         listNode_ = GetListFrameNode();
37     }
38     ~ListItemDragManager() override = default;
39 
GetHost()40     RefPtr<FrameNode> GetHost() const
41     {
42         return frameNode_.Upgrade();
43     }
44 
45     void InitDragDropEvent();
46     void DeInitDragDropEvent();
47 private:
48     void HandleOnItemLongPress(const GestureEvent& info);
49     void HandleOnItemDragStart(const GestureEvent& info);
50     void HandleOnItemDragUpdate(const GestureEvent& info);
51     void HandleOnItemDragEnd(const GestureEvent& info);
52     void HandleOnItemDragCancel();
53     void HandleDragEndAnimation();
54     void HandleScrollCallback();
55     void HandleSwapAnimation(int32_t from, int32_t to);
56     void SetNearbyNodeScale(RefPtr<FrameNode> node, float scale);
57     ScaleResult ScaleAxisNearItem(int32_t index, const RectF& rect, const OffsetF& delta, Axis axis);
58     void ScaleDiagonalItem(int32_t index, const RectF& rect, const OffsetF& delta);
59     void HandleAutoScroll(int32_t index, const PointF& point, const RectF& frameRect);
60     void SetPosition(const OffsetF& offset);
61     void ResetPrevScaleNode();
62     int32_t ScaleNearItem(int32_t index, const RectF& rect, const OffsetF& delta);
63     int32_t GetIndex() const;
64     int32_t GetLanes() const;
65     bool IsInHotZone(int32_t index, const RectF& frameRect) const;
66     RefPtr<FrameNode> GetListFrameNode() const;
67 
68     OffsetF dragOffset_;
69     WeakPtr<FrameNode> frameNode_;
70     WeakPtr<ForEachBaseNode> forEachNode_;
71     WeakPtr<FrameNode> listNode_;
72 
73     std::map<WeakPtr<RenderContext>, VectorF> scaleNode_;
74     std::map<WeakPtr<RenderContext>, VectorF> prevScaleNode_;
75     Axis axis_ = Axis::VERTICAL;
76     int32_t totalCount_ = -1;
77     int32_t lanes_ = 1;
78     bool scrolling_ = false;
79     OffsetF realOffset_;
80 
81     int32_t fromIndex_ = -1;
82 
83     VectorF prevScale_{ 1.f, 1.f };
84     Shadow prevShadow_;
85     int32_t prevZIndex_ = 0;
86 };
87 } // namespace OHOS::Ace::NG
88 
89 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LIST_LIST_ITEM_DRAG_MANAGER_H
90