2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21 using System.Diagnostics.CodeAnalysis;
22 using Tizen.NUI.BaseComponents;
27 /// DragAndDrop controls the drag objet and data.
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
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 Window mDragWindow;
41 private const int shadowWidth = 150;
42 private const int shadowHeight = 150;
44 [EditorBrowsable(EditorBrowsableState.Never)]
45 private DragAndDrop() : this(Interop.DragAndDrop.New(), true)
47 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
50 private DragAndDrop(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public static DragAndDrop Instance { get; } = new DragAndDrop();
59 /// Starts drag and drop.
61 /// <param name="sourceView">The soruce view</param>
62 /// <param name="shadowView">The shadow view for drag object</param>
63 /// <param name="dragData">The data to send</param>
64 /// <param name="callback">The source event callback</param>
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
68 if (null == shadowView)
70 throw new ArgumentNullException(nameof(shadowView));
76 mDragWindow.Dispose();
80 //FIXME: Drag window cannot be reused because the current window server does not allow reuse.
81 // When window server allows the drag window to be reused, makes the mDragWindow created once.
82 mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
84 BackgroundColor = Color.Transparent,
89 shadowView.SetSize(shadowWidth, shadowHeight);
90 shadowView.SetOpacity(0.9f);
92 mDragWindow.Add(shadowView);
95 sourceEventCb = (sourceEventType) =>
97 callback((SourceEventType)sourceEventType);
100 if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
101 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
103 throw new InvalidOperationException("Fail to StartDragAndDrop");
109 /// Adds listener for drop targets
111 /// <param name="targetView">The target view</param>
112 /// <param name="callback">The callback function to get drag event</param>
113 [EditorBrowsable(EditorBrowsableState.Never)]
114 public void AddListener(View targetView, DragAndDropEventHandler callback)
116 InternalDragAndDropEventHandler cb = (dragEvent) =>
118 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
119 DragEvent ev = new DragEvent();
120 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
121 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
123 if (type == DragType.Enter)
126 callback(targetView, ev);
128 else if (type == DragType.Leave)
131 callback(targetView, ev);
133 else if (type == DragType.Move)
136 callback(targetView, ev);
138 else if (type == DragType.Drop)
141 ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
142 ev.Data = Interop.DragAndDrop.GetData(dragEvent);
143 callback(targetView, ev);
147 targetEventDictionary.Add(targetView, cb);
149 if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
150 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
152 throw new InvalidOperationException("Fail to AddListener");
157 /// Removes listener for drop targets
159 /// <param name="targetView">The target view</param>
160 /// <param name="callback">The callback function to remove</param>
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public void RemoveListener(View targetView, DragAndDropEventHandler callback)
164 if (!targetEventDictionary.ContainsKey(targetView))
166 throw new InvalidOperationException("Fail to RemoveListener");
169 InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
170 targetEventDictionary.Remove(targetView);
171 if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
172 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
174 throw new InvalidOperationException("Fail to RemoveListener");