DragAndDrop : don't reuse drag window
authorTaehyub Kim <taehyub.kim@samsung.com>
Tue, 14 Feb 2023 12:28:14 +0000 (21:28 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Mon, 6 Mar 2023 11:10:58 +0000 (20:10 +0900)
src/Tizen.NUI/src/public/DragAndDrop/DragAndDrop.cs
test/NUIDnDSource/NUIDnDSource.cs

index b9d5724..ebc2d10 100755 (executable)
@@ -41,6 +41,8 @@ namespace Tizen.NUI
         private int shadowWidth = 100;
         private int shadowHeight = 100;
 
+        private bool initDrag = false;
+
         private DragAndDrop() : this(Interop.DragAndDrop.New(), true)
         {
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -51,6 +53,22 @@ namespace Tizen.NUI
 
         }
 
+        private void ReleaseDragWindow()
+        {
+            if (mDragWindow)
+            {                        
+                if (mShadowView)
+                {
+                    //Application has Shadow View ownership, so DnD doesn't dispose Shadow View
+                    mDragWindow.Remove(mShadowView);
+                    mShadowView = null;
+                }
+            
+                mDragWindow.Dispose();
+                mDragWindow = null;                      
+            }         
+        }
+
         /// <summary>
         /// Gets the singleton instance of DragAndDrop.
         /// </summary>
@@ -66,7 +84,14 @@ namespace Tizen.NUI
         /// <param name="callback">The source event callback</param>
         /// <since_tizen> 10 </since_tizen>
         public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
-        {
+        {            
+            if (initDrag)
+            {
+                 Tizen.Log.Fatal("NUI", "Start Drag And Drop Initializing...");
+                 return;
+            }
+            initDrag = true;
+
             if (Window.IsSupportedMultiWindow() == false)
             {
                 throw new NotSupportedException("This device does not support surfaceless_context. So Window cannot be created.");
@@ -77,6 +102,8 @@ namespace Tizen.NUI
                 throw new ArgumentNullException(nameof(shadowView));
             }
 
+            ReleaseDragWindow();
+
             shadowWidth = (int)shadowView.Size.Width;
             shadowHeight = (int)shadowView.Size.Height;
 
@@ -91,64 +118,49 @@ namespace Tizen.NUI
                 shadowHeight = 100;
             }
 
-            if (null == mDragWindow)
+            mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
             {
-                mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
-                {
-                    BackgroundColor = Color.Transparent,
-                };
-            }
-
-            //Initialize Drag Window Position and Size based on Shadow View Position and Size
-            mDragWindow.SetPosition(new Position2D((int)shadowView.Position.X, (int)shadowView.Position.Y));
-            mDragWindow.SetWindowSize(new Size(shadowWidth, shadowHeight));
-
-            //Make Shadow View Transparent
-            shadowView.SetOpacity(0.9f);
-
-            //Make Position 0, 0 for Moving into Drag Window
-            shadowView.Position = new Position(0, 0);
+                BackgroundColor = Color.Transparent,
+            };
 
-            if (mShadowView)
+            if (mDragWindow)
             {
-                mShadowView.Hide();
-                mDragWindow.Remove(mShadowView);
-                mShadowView.Dispose();
-            }
+                //Initialize Drag Window Size based on Shadow View Size,
+                //Don't set Drag Window Posiiton, Window Server sets Position Internally
+                mDragWindow.SetWindowSize(new Size(shadowWidth, shadowHeight));
+
+                //Make Shadow View Transparent
+                shadowView.SetOpacity(0.9f);
+
+                //Make Position 0, 0 for Moving into Drag Window
+                shadowView.Position = new Position(0, 0);
+            
+                mShadowView = shadowView;
+                mDragWindow.Add(mShadowView);
+           
+                sourceEventCb = (sourceEventType) =>
+                {   
+                    if ((DragSourceEventType)sourceEventType != DragSourceEventType.Start)
+                    {     
+                        Tizen.Log.Fatal("NUI", "DnD Source Event is Called");  
+                        ReleaseDragWindow();                
+                    }
 
-            mShadowView = shadowView;
-            mDragWindow.Add(mShadowView);
+                    callback((DragSourceEventType)sourceEventType);
+                };
 
-            //Update Window Directly
-            mDragWindow.VisibiltyChangedSignalEmit(true);
-            mDragWindow.RenderOnce();
+                //Show Drag Window before StartDragAndDrop
+                mDragWindow.Show();
 
-            sourceEventCb = (sourceEventType) =>
-            {
-                if ((DragSourceEventType)sourceEventType == DragSourceEventType.Finish)
+                if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
+                                                        new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
                 {
-                    if (mShadowView)
-                    {
-                        mShadowView.Hide();
-                        mDragWindow.Remove(mShadowView);
-                        mShadowView.Dispose();
-                    }
-
-                    //Update Window Directly
-                    mDragWindow.VisibiltyChangedSignalEmit(true);
-                    mDragWindow.RenderOnce();
+                    throw new InvalidOperationException("Fail to StartDragAndDrop");
                 }
 
-                callback((DragSourceEventType)sourceEventType);
-            };
-
-            if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
-                                                      new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
-            {
-                throw new InvalidOperationException("Fail to StartDragAndDrop");
-            }
-
-            mDragWindow.Show();
+            }         
+            
+            initDrag = false;
         }
 
         /// <summary>
index 4fd294c..660db6c 100644 (file)
@@ -12,6 +12,8 @@ namespace NUIDnDSource
         ImageView targetViewA;
         ImageView targetViewB;
         DragAndDrop dnd;
+
+        LongPressGestureDetector longPressed;
         protected override void OnCreate()
         {
             base.OnCreate();
@@ -26,7 +28,6 @@ namespace NUIDnDSource
             Window.Instance.KeyEvent += OnKeyEvent;
             Window.Instance.WindowSize = new Size(900, 1080);
             Window.Instance.BackgroundColor = Color.White;
-            Window.Instance.TouchEvent += OnTouchEvent;
 
             TextLabel text = new TextLabel("DragSource Application");
             text.Position = new Position(0, 0);
@@ -79,6 +80,22 @@ namespace NUIDnDSource
 
             // Add Drop Target B
             dnd.AddListener(targetViewB, OnSourceAppDnDFuncB);
+
+            longPressed = new LongPressGestureDetector();
+            longPressed.Attach(sourceView);
+            longPressed.Detected += (s, e) =>
+            {
+              if(e.LongPressGesture.State == Gesture.StateType.Started)
+              {
+                Tizen.Log.Debug("NUIDnDSource", "StartDragAndDrop");
+                shadowView = new ImageView(Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "dragsource.png");
+                shadowView.Size = new Size(150, 150);
+                DragData dragData;
+                dragData.MimeType = "text/uri-list";
+                dragData.Data = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "dragsource.png";
+                dnd.StartDragAndDrop(sourceView, shadowView, dragData, OnSourceEventFunc);
+              }
+            };
         }
 
         public void OnSourceAppDnDFuncA(View targetView, DragEvent dragEvent)
@@ -151,20 +168,6 @@ namespace NUIDnDSource
           }
         }
 
-        private void OnTouchEvent(object source, Window.TouchEventArgs e)
-        {
-            if (e.Touch.GetState(0) == PointStateType.Down)
-            {
-                Tizen.Log.Debug("NUIDnDSource", "StartDragAndDrop");
-                shadowView = new ImageView(Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "dragsource.png");
-                shadowView.Size = new Size(150, 150);
-                DragData dragData;
-                dragData.MimeType = "text/uri-list";
-                dragData.Data = Tizen.Applications.Application.Current.DirectoryInfo.SharedResource + "dragsource.png";
-                dnd.StartDragAndDrop(sourceView, shadowView, dragData, OnSourceEventFunc);
-            }
-        }
-
         static void Main(string[] args)
         {
             var app = new Program();