[NUI] remove previous shadowView before adding new shadowView
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / DragAndDrop / DragAndDrop.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21 using System.Diagnostics.CodeAnalysis;
22 using Tizen.NUI.BaseComponents;
23
24 namespace Tizen.NUI
25 {
26     /// <summary>
27     /// DragAndDrop controls the drag objet and data.
28     /// </summary>
29     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000: Dispose objects before losing scope", Justification = "It does not have ownership.")]
30     [EditorBrowsable(EditorBrowsableState.Never)]
31     public class DragAndDrop : BaseHandle
32     {
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public delegate void SourceEventHandler(SourceEventType sourceEventType);
35         private delegate void InternalSourceEventHandler(int sourceEventType);
36         public delegate void DragAndDropEventHandler(View targetView, DragEvent dragEvent);
37         private delegate void InternalDragAndDropEventHandler(global::System.IntPtr dragEvent);
38         private InternalSourceEventHandler sourceEventCb;
39         private Dictionary<View, InternalDragAndDropEventHandler> targetEventDictionary = new Dictionary<View, InternalDragAndDropEventHandler>();
40         private View mShadowView;
41         private Window mDragWindow;
42         private const int shadowWidth = 150;
43         private const int shadowHeight = 150;
44
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         private DragAndDrop() : this(Interop.DragAndDrop.New(), true)
47         {
48             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
49         }
50
51         private DragAndDrop(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
52         {
53
54         }
55
56         /// <summary>
57         /// Gets the singleton instance of DragAndDrop.
58         /// </summary>
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public static DragAndDrop Instance { get; } = new DragAndDrop();
61
62         /// <summary>
63         /// Starts drag and drop.
64         /// </summary>
65         /// <param name="sourceView">The soruce view</param>
66         /// <param name="shadowView">The shadow view for drag object</param>
67         /// <param name="dragData">The data to send</param>
68         /// <param name="callback">The source event callback</param>
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
71         {
72             if (null == shadowView)
73             {
74                 throw new ArgumentNullException(nameof(shadowView));
75             }
76
77             if (null == mDragWindow)
78             {
79                 mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
80                 {
81                     BackgroundColor = Color.Transparent,
82                 };
83             }
84
85             shadowView.SetSize(shadowWidth, shadowHeight);
86             shadowView.SetOpacity(0.9f);
87
88             if (mShadowView)
89             {
90                 mDragWindow.Remove(mShadowView);
91             }
92
93             mShadowView = shadowView;
94             mDragWindow.Add(mShadowView);
95             mDragWindow.Show();
96
97             sourceEventCb = (sourceEventType) =>
98             {
99                 callback((SourceEventType)sourceEventType);
100             };
101
102             if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
103                                                       new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
104             {
105                 throw new InvalidOperationException("Fail to StartDragAndDrop");
106             }
107         }
108
109         /// <summary>
110         /// Adds listener for drop targets
111         /// </summary>
112         /// <param name="targetView">The target view</param>
113         /// <param name="callback">The callback function to get drag event</param>
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public void AddListener(View targetView, DragAndDropEventHandler callback)
116         {
117             InternalDragAndDropEventHandler cb = (dragEvent) =>
118             {
119                 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
120                 DragEvent ev = new DragEvent();
121                 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
122                 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
123
124                 if (type == DragType.Enter)
125                 {
126                     ev.DragType = type;
127                     callback(targetView, ev);
128                 }
129                 else if (type == DragType.Leave)
130                 {
131                     ev.DragType = type;
132                     callback(targetView, ev);
133                 }
134                 else if (type == DragType.Move)
135                 {
136                     ev.DragType = type;
137                     callback(targetView, ev);
138                 }
139                 else if (type == DragType.Drop)
140                 {
141                     ev.DragType = type;
142                     ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
143                     ev.Data = Interop.DragAndDrop.GetData(dragEvent);
144                     callback(targetView, ev);
145                 }
146             };
147
148             targetEventDictionary.Add(targetView, cb);
149
150             if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
151                                                  new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
152             {
153                  throw new InvalidOperationException("Fail to AddListener");
154             }
155         }
156
157         /// <summary>
158         /// Removes listener for drop targets
159         /// </summary>
160         /// <param name="targetView">The target view</param>
161         /// <param name="callback">The callback function to remove</param>
162         [EditorBrowsable(EditorBrowsableState.Never)]
163         public void RemoveListener(View targetView, DragAndDropEventHandler callback)
164         {
165             if (!targetEventDictionary.ContainsKey(targetView))
166             {
167                  throw new InvalidOperationException("Fail to RemoveListener");
168             }
169
170             InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
171             targetEventDictionary.Remove(targetView);
172             if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
173                                                     new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
174             {
175                  throw new InvalidOperationException("Fail to RemoveListener");
176             }
177         }
178     }
179 }