DragAndDrop : don't reuse drag window
[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 object and data.
28     /// </summary>
29     /// <since_tizen> 10 </since_tizen>
30     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000: Dispose objects before losing scope", Justification = "It does not have ownership.")]
31     public class DragAndDrop : BaseHandle
32     {
33         public delegate void SourceEventHandler(DragSourceEventType sourceEventType);
34         private delegate void InternalSourceEventHandler(int sourceEventType);
35         public delegate void DragAndDropEventHandler(View targetView, DragEvent dragEvent);
36         private delegate void InternalDragAndDropEventHandler(global::System.IntPtr dragEvent);
37         private InternalSourceEventHandler sourceEventCb;
38         private Dictionary<View, InternalDragAndDropEventHandler> targetEventDictionary = new Dictionary<View, InternalDragAndDropEventHandler>();
39         private View mShadowView;
40         private Window mDragWindow;
41         private int shadowWidth = 100;
42         private int shadowHeight = 100;
43
44         private bool initDrag = false;
45
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         private void ReleaseDragWindow()
57         {
58             if (mDragWindow)
59             {                        
60                 if (mShadowView)
61                 {
62                     //Application has Shadow View ownership, so DnD doesn't dispose Shadow View
63                     mDragWindow.Remove(mShadowView);
64                     mShadowView = null;
65                 }
66             
67                 mDragWindow.Dispose();
68                 mDragWindow = null;                      
69             }         
70         }
71
72         /// <summary>
73         /// Gets the singleton instance of DragAndDrop.
74         /// </summary>
75         /// <since_tizen> 10 </since_tizen>
76         public static DragAndDrop Instance { get; } = new DragAndDrop();
77
78         /// <summary>
79         /// Starts drag and drop.
80         /// </summary>
81         /// <param name="sourceView">The soruce view</param>
82         /// <param name="shadowView">The shadow view for drag object</param>
83         /// <param name="dragData">The data to send</param>
84         /// <param name="callback">The source event callback</param>
85         /// <since_tizen> 10 </since_tizen>
86         public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
87         {            
88             if (initDrag)
89             {
90                  Tizen.Log.Fatal("NUI", "Start Drag And Drop Initializing...");
91                  return;
92             }
93             initDrag = true;
94
95             if (Window.IsSupportedMultiWindow() == false)
96             {
97                 throw new NotSupportedException("This device does not support surfaceless_context. So Window cannot be created.");
98             }
99
100             if (null == shadowView)
101             {
102                 throw new ArgumentNullException(nameof(shadowView));
103             }
104
105             ReleaseDragWindow();
106
107             shadowWidth = (int)shadowView.Size.Width;
108             shadowHeight = (int)shadowView.Size.Height;
109
110             // Prevents shadowView size from being smaller than 100 pixel
111             if (shadowView.Size.Width < 100)
112             {
113                 shadowWidth = 100;
114             }
115
116             if (shadowView.Size.Height < 100)
117             {
118                 shadowHeight = 100;
119             }
120
121             mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
122             {
123                 BackgroundColor = Color.Transparent,
124             };
125
126             if (mDragWindow)
127             {
128                 //Initialize Drag Window Size based on Shadow View Size,
129                 //Don't set Drag Window Posiiton, Window Server sets Position Internally
130                 mDragWindow.SetWindowSize(new Size(shadowWidth, shadowHeight));
131
132                 //Make Shadow View Transparent
133                 shadowView.SetOpacity(0.9f);
134
135                 //Make Position 0, 0 for Moving into Drag Window
136                 shadowView.Position = new Position(0, 0);
137             
138                 mShadowView = shadowView;
139                 mDragWindow.Add(mShadowView);
140            
141                 sourceEventCb = (sourceEventType) =>
142                 {   
143                     if ((DragSourceEventType)sourceEventType != DragSourceEventType.Start)
144                     {     
145                         Tizen.Log.Fatal("NUI", "DnD Source Event is Called");  
146                         ReleaseDragWindow();                
147                     }
148
149                     callback((DragSourceEventType)sourceEventType);
150                 };
151
152                 //Show Drag Window before StartDragAndDrop
153                 mDragWindow.Show();
154
155                 if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
156                                                         new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
157                 {
158                     throw new InvalidOperationException("Fail to StartDragAndDrop");
159                 }
160
161             }         
162             
163             initDrag = false;
164         }
165
166         /// <summary>
167         /// Adds listener for drop targets
168         /// </summary>
169         /// <param name="targetView">The target view</param>
170         /// <param name="callback">The callback function to get drag event when the drag source enters, moves, leaves and drops on the drop target</param>
171         /// <since_tizen> 10 </since_tizen>
172         public void AddListener(View targetView, DragAndDropEventHandler callback)
173         {
174             InternalDragAndDropEventHandler cb = (dragEvent) =>
175             {
176                 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
177                 DragEvent ev = new DragEvent();
178                 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
179                 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
180
181                 if (type == DragType.Enter)
182                 {
183                     ev.DragType = type;
184                     callback(targetView, ev);
185                 }
186                 else if (type == DragType.Leave)
187                 {
188                     ev.DragType = type;
189                     callback(targetView, ev);
190                 }
191                 else if (type == DragType.Move)
192                 {
193                     ev.DragType = type;
194                     callback(targetView, ev);
195                 }
196                 else if (type == DragType.Drop)
197                 {
198                     ev.DragType = type;
199                     ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
200                     ev.Data = Interop.DragAndDrop.GetData(dragEvent);
201                     callback(targetView, ev);
202                 }
203             };
204
205             targetEventDictionary.Add(targetView, cb);
206
207             if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
208                                                  new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
209             {
210                  throw new InvalidOperationException("Fail to AddListener");
211             }
212         }
213
214         /// <summary>
215         /// Removes listener for drop targets
216         /// </summary>
217         /// <param name="targetView">The target view</param>
218         /// <param name="callback">The callback function to remove</param>
219         /// <since_tizen> 10 </since_tizen>
220         public void RemoveListener(View targetView, DragAndDropEventHandler callback)
221         {
222             if (!targetEventDictionary.ContainsKey(targetView))
223             {
224                  throw new InvalidOperationException("Fail to RemoveListener");
225             }
226
227             InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
228             targetEventDictionary.Remove(targetView);
229             if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
230                                                     new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
231             {
232                  throw new InvalidOperationException("Fail to RemoveListener");
233             }
234         }
235     }
236 }