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 #include "core/components_ng/manager/drag_drop/drag_drop_proxy.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components_ng/event/focus_hub.h"
20 #include "core/components_ng/property/measure_utils.h"
21 #include "core/pipeline_ng/pipeline_context.h"
22 
23 namespace OHOS::Ace::NG {
24 
OnTextDragStart(const std::string & extraInfo)25 void DragDropProxy::OnTextDragStart(const std::string& extraInfo)
26 {
27     auto pipeline = PipelineContext::GetCurrentContext();
28     CHECK_NULL_VOID(pipeline);
29     auto manager = pipeline->GetDragDropManager();
30     CHECK_NULL_VOID(manager);
31     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
32 }
33 
OnDragStart(const GestureEvent & info,const std::string & extraInfo,const RefPtr<FrameNode> & frameNode)34 void DragDropProxy::OnDragStart(
35     const GestureEvent& info, const std::string& extraInfo, const RefPtr<FrameNode>& frameNode)
36 {
37     auto pipeline = PipelineContext::GetCurrentContext();
38     CHECK_NULL_VOID(pipeline);
39     auto manager = pipeline->GetDragDropManager();
40     CHECK_NULL_VOID(manager);
41     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
42 
43     auto point = Point(info.GetGlobalPoint().GetX(), info.GetGlobalPoint().GetY(), info.GetScreenLocation().GetX(),
44         info.GetScreenLocation().GetY());
45     auto pointerEvent = PointerEvent(info.GetGlobalPoint().GetX(), info.GetGlobalPoint().GetY(),
46         info.GetScreenLocation().GetX(), info.GetScreenLocation().GetY());
47     manager->OnDragStart(point, frameNode);
48     manager->OnDragMove(pointerEvent, extraInfo);
49     manager->SetExtraInfo(extraInfo);
50 }
51 
OnDragMove(const GestureEvent & info)52 void DragDropProxy::OnDragMove(const GestureEvent& info)
53 {
54     auto pipeline = PipelineContext::GetCurrentContext();
55     CHECK_NULL_VOID(pipeline);
56     auto manager = pipeline->GetDragDropManager();
57     CHECK_NULL_VOID(manager);
58     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
59 
60     std::string extraInfo = manager->GetExtraInfo();
61     manager->OnDragMove(PointerEvent(info.GetGlobalPoint().GetX(), info.GetGlobalPoint().GetY(),
62         info.GetScreenLocation().GetX(), info.GetScreenLocation().GetY()), extraInfo);
63 }
64 
OnDragEnd(const GestureEvent & info,bool isTextDragEnd)65 void DragDropProxy::OnDragEnd(const GestureEvent& info, bool isTextDragEnd)
66 {
67     auto pipeline = PipelineContext::GetCurrentContext();
68     CHECK_NULL_VOID(pipeline);
69     auto manager = pipeline->GetDragDropManager();
70     CHECK_NULL_VOID(manager);
71     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
72     std::string extraInfo = manager->GetExtraInfo();
73     if (isTextDragEnd) {
74         manager->OnTextDragEnd(static_cast<float>(info.GetGlobalPoint().GetX()),
75             static_cast<float>(info.GetGlobalPoint().GetY()), extraInfo);
76     } else {
77         manager->OnDragEnd(PointerEvent(info.GetGlobalPoint().GetX(), info.GetGlobalPoint().GetY(),
78             info.GetScreenLocation().GetX(), info.GetScreenLocation().GetY()), extraInfo);
79     }
80 }
81 
onDragCancel()82 void DragDropProxy::onDragCancel()
83 {
84     auto pipeline = PipelineContext::GetCurrentContext();
85     CHECK_NULL_VOID(pipeline);
86     auto manager = pipeline->GetDragDropManager();
87     CHECK_NULL_VOID(manager);
88     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
89     manager->onDragCancel();
90 }
91 
OnItemDragStart(const GestureEvent & info,const RefPtr<FrameNode> & frameNode)92 void DragDropProxy::OnItemDragStart(const GestureEvent& info, const RefPtr<FrameNode>& frameNode)
93 {
94     auto pipeline = PipelineContext::GetCurrentContext();
95     CHECK_NULL_VOID(pipeline);
96     auto manager = pipeline->GetDragDropManager();
97     CHECK_NULL_VOID(manager);
98     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
99 
100     manager->OnItemDragStart(
101         static_cast<float>(info.GetGlobalPoint().GetX()), static_cast<float>(info.GetGlobalPoint().GetY()), frameNode);
102 }
103 
OnItemDragMove(const GestureEvent & info,int32_t draggedIndex,DragType dragType)104 void DragDropProxy::OnItemDragMove(const GestureEvent& info, int32_t draggedIndex, DragType dragType)
105 {
106     auto pipeline = PipelineContext::GetCurrentContext();
107     CHECK_NULL_VOID(pipeline);
108     auto manager = pipeline->GetDragDropManager();
109     CHECK_NULL_VOID(manager);
110     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
111 
112     manager->OnItemDragMove(static_cast<float>(info.GetGlobalPoint().GetX()),
113         static_cast<float>(info.GetGlobalPoint().GetY()), draggedIndex, dragType);
114 }
115 
OnItemDragEnd(const GestureEvent & info,int32_t draggedIndex,DragType dragType)116 void DragDropProxy::OnItemDragEnd(const GestureEvent& info, int32_t draggedIndex, DragType dragType)
117 {
118     auto pipeline = PipelineContext::GetCurrentContext();
119     CHECK_NULL_VOID(pipeline);
120     auto manager = pipeline->GetDragDropManager();
121     CHECK_NULL_VOID(manager);
122     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
123 
124     manager->OnItemDragEnd(static_cast<float>(info.GetGlobalPoint().GetX()),
125         static_cast<float>(info.GetGlobalPoint().GetY()), draggedIndex, dragType);
126 }
127 
onItemDragCancel()128 void DragDropProxy::onItemDragCancel()
129 {
130     auto pipeline = PipelineContext::GetCurrentContext();
131     CHECK_NULL_VOID(pipeline);
132     auto manager = pipeline->GetDragDropManager();
133     CHECK_NULL_VOID(manager);
134     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
135 
136     manager->onItemDragCancel();
137 }
138 
DestroyDragWindow()139 void DragDropProxy::DestroyDragWindow()
140 {
141     auto pipeline = PipelineContext::GetCurrentContext();
142     CHECK_NULL_VOID(pipeline);
143     auto manager = pipeline->GetDragDropManager();
144     CHECK_NULL_VOID(manager);
145     CHECK_NULL_VOID(manager->CheckDragDropProxy(id_));
146 
147     manager->DestroyDragWindow();
148 }
149 
150 } // namespace OHOS::Ace::NG