[NUI] Check if event handler of the DisposedQueued object is valid
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ViewEvent.cs
index 685f31e..6645c13 100755 (executable)
@@ -42,8 +42,10 @@ namespace Tizen.NUI.BaseComponents
         private EventHandler<VisibilityChangedEventArgs> visibilityChangedEventHandler;
         private VisibilityChangedEventCallbackType visibilityChangedEventCallback;
         private EventHandler keyInputFocusGainedEventHandler;
+
         private KeyInputFocusGainedCallbackType keyInputFocusGainedCallback;
         private EventHandler keyInputFocusLostEventHandler;
+
         private KeyInputFocusLostCallbackType keyInputFocusLostCallback;
         private EventHandler onRelayoutEventHandler;
         private OnRelayoutEventCallbackType onRelayoutEventCallback;
@@ -80,10 +82,12 @@ namespace Tizen.NUI.BaseComponents
         private delegate void ResourcesLoadedCallbackType(IntPtr control);
         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
         private delegate void _backgroundResourceLoadedCallbackType(IntPtr view);
-        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
+
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         private delegate void KeyInputFocusGainedCallbackType(IntPtr control);
-        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         private delegate void KeyInputFocusLostCallbackType(IntPtr control);
+
         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
         private delegate void OnRelayoutEventCallbackType(IntPtr control);
         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
@@ -126,10 +130,14 @@ namespace Tizen.NUI.BaseComponents
                 if (keyInputFocusGainedEventHandler == null)
                 {
                     using KeyInputFocusSignal signal = new KeyInputFocusSignal(Interop.ViewSignal.KeyInputFocusGainedSignal(SwigCPtr), false);
-                    if (signal?.Empty() == false)
+
+                    if (keyInputFocusGainedCallback != null)
                     {
                         signal?.Disconnect(keyInputFocusGainedCallback);
-                        keyInputFocusGainedCallback = null;
+                        if (signal?.Empty() == true)
+                        {
+                            keyInputFocusGainedCallback = null;
+                        }
                     }
                 }
             }
@@ -159,10 +167,14 @@ namespace Tizen.NUI.BaseComponents
                 if (keyInputFocusLostEventHandler == null)
                 {
                     using KeyInputFocusSignal signal = new KeyInputFocusSignal(Interop.ViewSignal.KeyInputFocusLostSignal(SwigCPtr), false);
-                    if (signal?.Empty() == false)
+
+                    if (keyInputFocusLostCallback != null)
                     {
                         signal?.Disconnect(keyInputFocusLostCallback);
-                        keyInputFocusLostCallback = null;
+                        if (signal?.Empty() == true)
+                        {
+                            keyInputFocusLostCallback = null;
+                        }
                     }
                 }
             }
@@ -594,7 +606,7 @@ namespace Tizen.NUI.BaseComponents
                 }
                 backgroundResourceLoadedEventHandler += value;
             }
-            
+
             remove
             {
                 backgroundResourceLoadedEventHandler -= value;
@@ -708,94 +720,39 @@ namespace Tizen.NUI.BaseComponents
             return ret;
         }
 
-        private void OnSize2DChanged(int? width, int? height)
+        private void OnColorChanged(float r, float g, float b, float a)
         {
-            PropertyValue temp;
-            if (width != null)
-            {
-                temp = new Tizen.NUI.PropertyValue((float)width);
-                Tizen.NUI.Object.SetProperty(this.SwigCPtr, View.Property.SizeWidth, temp);
-                temp.Dispose();
-            }
-            if (height != null)
-            {
-                temp = new Tizen.NUI.PropertyValue((float)height);
-                Tizen.NUI.Object.SetProperty(this.SwigCPtr, View.Property.SizeHeight, temp);
-                temp.Dispose();
-            }
+            Color = new Color(r, g, b, a);
         }
 
-        private void OnMinimumSizeChanged(int? width, int? height)
+        private void OnMinimumSizeChanged(int width, int height)
         {
-            if (width != null && height != null)
-            {
-                MinimumSize = new Size2D((int)width, (int)height);
-            }
-            else if (width != null && height == null)
-            {
-                MinimumSize = new Size2D((int)width, (int)this.GetMinimumSize().Height);
-            }
-            else if (width == null && height != null)
-            {
-                MinimumSize = new Size2D((int)this.GetMinimumSize().Width, (int)height);
-            }
-            else
-            {
-                //both are null, do nothing.
-            }
+            MinimumSize = new Size2D(width, height);
         }
 
-        private void OnMaximumSizeChanged(int? width, int? height)
+        private void OnMaximumSizeChanged(int width, int height)
         {
-            if (width != null && height != null)
-            {
-                MaximumSize = new Size2D((int)width, (int)height);
-            }
-            else if (width != null && height == null)
-            {
-                MaximumSize = new Size2D((int)width, (int)this.GetMaximumSize().Height);
-            }
-            else if (width == null && height != null)
-            {
-                MaximumSize = new Size2D((int)this.GetMaximumSize().Width, (int)height);
-            }
-            else
-            {
-                //both are null, do nothing.
-            }
+            MaximumSize = new Size2D(width, height);
         }
 
         private void OnPosition2DChanged(int x, int y)
         {
-            Position2D = new Position2D(x, y);
+            SetPosition((float)x, (float)y, 0);
         }
 
-        private void OnSizeChanged(float? width, float? height, float? depth)
+        private void OnPositionChanged(float x, float y, float z)
         {
-            PropertyValue temp;
-            if (width != null)
-            {
-                temp = new Tizen.NUI.PropertyValue((float)width);
-                Tizen.NUI.Object.SetProperty(this.SwigCPtr, View.Property.SizeWidth, temp);
-                temp.Dispose();
-            }
-            if (height != null)
-            {
-                temp = new Tizen.NUI.PropertyValue((float)height);
-                Tizen.NUI.Object.SetProperty(this.SwigCPtr, View.Property.SizeHeight, temp);
-                temp.Dispose();
-            }
-            if (depth != null)
-            {
-                temp = new Tizen.NUI.PropertyValue((float)depth);
-                Tizen.NUI.Object.SetProperty(this.SwigCPtr, View.Property.SizeDepth, temp);
-                temp.Dispose();
-            }
+            SetPosition(x, y, z);
         }
 
-        private void OnPositionChanged(float x, float y, float z)
+        private void OnSize2DChanged(int width, int height)
         {
-            Position = new Position(x, y, z);
+            SetSize((float)width, (float)height, 0);
+        }
+
+        private void OnSizeChanged(float width, float height, float depth)
+        {
+            SetSize(width, height, depth);
         }
 
         private void OnParentOriginChanged(float x, float y, float z)
@@ -825,18 +782,74 @@ namespace Tizen.NUI.BaseComponents
 
         private void OnKeyInputFocusGained(IntPtr view)
         {
-            if (keyInputFocusGainedEventHandler != null)
+            if (IsNativeHandleInvalid())
             {
-                keyInputFocusGainedEventHandler(this, null);
+                if (this.Disposed)
+                {
+                    if (keyInputFocusGainedEventHandler != null)
+                    {
+                        var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
+                        var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
+                        var me = this.GetType().FullName;
+
+                        throw new ObjectDisposedException(nameof(SwigCPtr), $"Error! NUI's native dali object is already disposed. " +
+                            $"OR the native dali object handle of NUI becomes null! \n" +
+                            $" process:{process} thread:{thread}, isDisposed:{this.Disposed}, isDisposeQueued:{this.IsDisposeQueued}, me:{me}\n");
+                    }
+                }
+                else
+                {
+                    if (this.IsDisposeQueued)
+                    {
+                        var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
+                        var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
+                        var me = this.GetType().FullName;
+
+                        //in this case, the View object is ready to be disposed waiting on DisposeQueue, so event callback should not be invoked!
+                        Tizen.Log.Error("NUI", "in this case, the View object is ready to be disposed waiting on DisposeQueue, so event callback should not be invoked! just return here! \n" +
+                            $"process:{process} thread:{thread}, isDisposed:{this.Disposed}, isDisposeQueued:{this.IsDisposeQueued}, me:{me}\n");
+                        return;
+                    }
+                }
             }
+
+            keyInputFocusGainedEventHandler?.Invoke(this, null);
         }
 
         private void OnKeyInputFocusLost(IntPtr view)
         {
-            if (keyInputFocusLostEventHandler != null)
+            if (IsNativeHandleInvalid())
             {
-                keyInputFocusLostEventHandler(this, null);
+                if (this.Disposed)
+                {
+                    if (keyInputFocusLostEventHandler != null)
+                    {
+                        var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
+                        var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
+                        var me = this.GetType().FullName;
+
+                        throw new ObjectDisposedException(nameof(SwigCPtr), $"Error! NUI's native dali object is already disposed. " +
+                            $"OR the native dali object handle of NUI becomes null! \n" +
+                            $" process:{process} thread:{thread}, isDisposed:{this.Disposed}, isDisposeQueued:{this.IsDisposeQueued}, me:{me}\n");
+                    }
+                }
+                else
+                {
+                    if (this.IsDisposeQueued)
+                    {
+                        var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
+                        var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
+                        var me = this.GetType().FullName;
+
+                        //in this case, the View object is ready to be disposed waiting on DisposeQueue, so event callback should not be invoked!
+                        Tizen.Log.Error("NUI", "in this case, the View object is ready to be disposed waiting on DisposeQueue, so event callback should not be invoked! just return here! \n" +
+                            $"process:{process} thread:{thread}, isDisposed:{this.Disposed}, isDisposeQueued:{this.IsDisposeQueued}, me:{me}\n");
+                        return;
+                    }
+                }
             }
+
+            keyInputFocusLostEventHandler?.Invoke(this, null);
         }
 
         private bool OnKeyEvent(IntPtr view, IntPtr keyEvent)