[NUI] DragAndDrop: Add NotSupportedException for Multi-Window Feature
[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 int shadowWidth = 100;
43         private int shadowHeight = 100;
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         /// <exception cref="NotSupportedException">The multi-window feature is not supported.</exception>
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
72         {
73             if (Window.IsSupportedMultiWindow() == false)
74             {
75                 throw new NotSupportedException("This device does not support surfaceless_context. So Window cannot be created.");
76             }
77
78             if (null == shadowView)
79             {
80                 throw new ArgumentNullException(nameof(shadowView));
81             }
82
83             shadowWidth = (int)shadowView.Size.Width;
84             shadowHeight = (int)shadowView.Size.Height;
85
86             // Prevents shadowView size from being smaller than 100 pixel
87             if (shadowView.Size.Width < 100)
88             {
89                 shadowWidth = 100;
90             }
91
92             if (shadowView.Size.Height < 100)
93             {
94                 shadowHeight = 100;
95             }
96
97             if (null == mDragWindow)
98             {
99                 mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
100                 {
101                     BackgroundColor = Color.Transparent,
102                 };
103             }
104
105             //Initialize Drag Window Position and Size based on Shadow View Position and Size
106             mDragWindow.SetPosition(new Position2D((int)shadowView.Position.X, (int)shadowView.Position.Y));
107             mDragWindow.SetWindowSize(new Size(shadowWidth, shadowHeight));
108
109             //Make Shadow View Transparent
110             shadowView.SetOpacity(0.9f);
111
112             //Make Position 0, 0 for Moving into Drag Window
113             shadowView.Position = new Position(0, 0);
114
115             if (mShadowView)
116             {
117                 mShadowView.Hide();
118                 mDragWindow.Remove(mShadowView);
119                 mShadowView.Dispose();
120             }
121
122             mShadowView = shadowView;
123             mDragWindow.Add(mShadowView);
124
125             //Update Window Directly
126             mDragWindow.VisibiltyChangedSignalEmit(true);
127             mDragWindow.RenderOnce();
128
129             sourceEventCb = (sourceEventType) =>
130             {
131                 if ((SourceEventType)sourceEventType == SourceEventType.Finish)
132                 {
133                     if (mShadowView)
134                     {
135                         mShadowView.Hide();
136                         mDragWindow.Remove(mShadowView);
137                         mShadowView.Dispose();
138                     }
139
140                     //Update Window Directly
141                     mDragWindow.VisibiltyChangedSignalEmit(true);
142                     mDragWindow.RenderOnce();
143                 }
144
145                 callback((SourceEventType)sourceEventType);
146             };
147
148             if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
149                                                       new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
150             {
151                 throw new InvalidOperationException("Fail to StartDragAndDrop");
152             }
153
154             mDragWindow.Show();
155         }
156
157         /// <summary>
158         /// Adds listener for drop targets
159         /// </summary>
160         /// <param name="targetView">The target view</param>
161         /// <param name="callback">The callback function to get drag event</param>
162         [EditorBrowsable(EditorBrowsableState.Never)]
163         public void AddListener(View targetView, DragAndDropEventHandler callback)
164         {
165             InternalDragAndDropEventHandler cb = (dragEvent) =>
166             {
167                 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
168                 DragEvent ev = new DragEvent();
169                 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
170                 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
171
172                 if (type == DragType.Enter)
173                 {
174                     ev.DragType = type;
175                     callback(targetView, ev);
176                 }
177                 else if (type == DragType.Leave)
178                 {
179                     ev.DragType = type;
180                     callback(targetView, ev);
181                 }
182                 else if (type == DragType.Move)
183                 {
184                     ev.DragType = type;
185                     callback(targetView, ev);
186                 }
187                 else if (type == DragType.Drop)
188                 {
189                     ev.DragType = type;
190                     ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
191                     ev.Data = Interop.DragAndDrop.GetData(dragEvent);
192                     callback(targetView, ev);
193                 }
194             };
195
196             targetEventDictionary.Add(targetView, cb);
197
198             if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
199                                                  new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
200             {
201                  throw new InvalidOperationException("Fail to AddListener");
202             }
203         }
204
205         /// <summary>
206         /// Removes listener for drop targets
207         /// </summary>
208         /// <param name="targetView">The target view</param>
209         /// <param name="callback">The callback function to remove</param>
210         [EditorBrowsable(EditorBrowsableState.Never)]
211         public void RemoveListener(View targetView, DragAndDropEventHandler callback)
212         {
213             if (!targetEventDictionary.ContainsKey(targetView))
214             {
215                  throw new InvalidOperationException("Fail to RemoveListener");
216             }
217
218             InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
219             targetEventDictionary.Remove(targetView);
220             if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
221                                                     new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
222             {
223                  throw new InvalidOperationException("Fail to RemoveListener");
224             }
225         }
226     }
227 }