[NUI] Support scroll in WebView. (#2605)
authorJiyun Yang <ji.yang@samsung.com>
Tue, 2 Feb 2021 07:53:51 +0000 (16:53 +0900)
committerGitHub <noreply@github.com>
Tue, 2 Feb 2021 07:53:51 +0000 (16:53 +0900)
1) ScrollPosition, ScrollSize, ContentSize properties are added.
2) ScrollBy is added.
3) ScrollEdgeReached events are notified when scrolled to edge.
4) Support program argument in WebView constructor to control chromium scroll options.

Co-authored-by: huayongxu <49056704+huayongxu@users.noreply.github.com>
src/Tizen.NUI/src/internal/Interop/Interop.WebView.cs
src/Tizen.NUI/src/internal/WebView.cs
src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedEventArgs.cs [new file with mode: 0755]
src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedSignal.cs [new file with mode: 0755]

index f86aa52..a70b7a3 100755 (executable)
@@ -15,6 +15,9 @@ namespace Tizen.NUI
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_New_2")]
             public static extern global::System.IntPtr WebView_New_2(string jarg1, string jarg2);
 
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_New_3")]
+            public static extern global::System.IntPtr WebView_New_3(int jarg1, string[] jarg2);
+
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_new_WebView__SWIG_1")]
             public static extern global::System.IntPtr new_WebView__SWIG_1(global::System.Runtime.InteropServices.HandleRef jarg1);
 
@@ -51,6 +54,15 @@ namespace Tizen.NUI
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_Property_DEFAULT_FONT_SIZE_get")]
             public static extern int WebView_Property_DEFAULT_FONT_SIZE_get();
 
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_Property_SCROLL_POSITION_get")]
+            public static extern int ScrollPositionGet();
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_Property_SCROLL_SIZE_get")]
+            public static extern int ScrollSizeGet();
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_Property_CONTENT_SIZE_get")]
+            public static extern int ContentSizeGet();
+
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_LoadUrl")]
             public static extern void WebView_LoadUrl(global::System.Runtime.InteropServices.HandleRef jarg1, string jarg2);
 
@@ -69,6 +81,9 @@ namespace Tizen.NUI
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_Resume")]
             public static extern void WebView_Resume(global::System.Runtime.InteropServices.HandleRef jarg1);
 
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_ScrollBy")]
+            public static extern void ScrollBy(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2, int jarg3);
+
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebView_GoBack")]
             public static extern void WebView_GoBack(global::System.Runtime.InteropServices.HandleRef jarg1);
 
@@ -125,6 +140,18 @@ namespace Tizen.NUI
 
             [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebViewPageLoadErrorSignal_Disconnect")]
             public static extern void WebViewPageLoadErrorSignal_Disconnect(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_new_WebViewScrollEdgeReachedSignal_ScrollEdgeReached")]
+            public static extern global::System.IntPtr NewWebViewScrollEdgeReachedSignalScrollEdgeReached(global::System.Runtime.InteropServices.HandleRef jarg1);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_delete_WebViewScrollEdgeReachedSignal")]
+            public static extern void DeleteWebViewScrollEdgeReachedSignal(global::System.Runtime.InteropServices.HandleRef jarg1);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebViewScrollEdgeReachedSignal_Connect")]
+            public static extern void WebViewScrollEdgeReachedSignalConnect(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2);
+
+            [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_WebViewScrollEdgeReachedSignal_Disconnect")]
+            public static extern void WebViewScrollEdgeReachedSignalDisconnect(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2);
         }
     }
-}
\ No newline at end of file
+}
index f6a195f..83819a9 100755 (executable)
@@ -38,6 +38,9 @@ namespace Tizen.NUI
         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
         private delegate void WebViewPageLoadErrorCallbackDelegate(IntPtr data, string pageUrl, int errorCode);
 
+        [UnmanagedFunctionPointer(CallingConvention.StdCall)]
+        private delegate void WebViewScrollEdgeReachedCallbackDelegate(IntPtr data, int edge);
+
         private readonly WebViewPageLoadSignal pageLoadStartedSignal;
         private EventHandler<WebViewPageLoadEventArgs> pageLoadStartedEventHandler;
         private WebViewPageLoadCallbackDelegate pageLoadStartedCallback;
@@ -50,6 +53,10 @@ namespace Tizen.NUI
         private EventHandler<WebViewPageLoadErrorEventArgs> pageLoadErrorEventHandler;
         private WebViewPageLoadErrorCallbackDelegate pageLoadErrorCallback;
 
+        private readonly WebViewScrollEdgeReachedSignal scrollEdgeReachedSignal;
+        private EventHandler<WebViewScrollEdgeReachedEventArgs> scrollEdgeReachedEventHandler;
+        private WebViewScrollEdgeReachedCallbackDelegate scrollEdgeReachedCallback;
+
         internal WebView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.WebView.WebView_SWIGUpcast(cPtr), cMemoryOwn)
         {
             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
@@ -57,6 +64,7 @@ namespace Tizen.NUI
             pageLoadStartedSignal = new WebViewPageLoadSignal(Interop.WebView.new_WebViewPageLoadSignal_PageLoadStarted(swigCPtr));
             pageLoadFinishedSignal = new WebViewPageLoadSignal(Interop.WebView.new_WebViewPageLoadSignal_PageLoadFinished(swigCPtr));
             pageLoadErrorSignal = new WebViewPageLoadErrorSignal(Interop.WebView.new_WebViewPageLoadErrorSignal_PageLoadError(swigCPtr));
+            scrollEdgeReachedSignal = new WebViewScrollEdgeReachedSignal(Interop.WebView.NewWebViewScrollEdgeReachedSignalScrollEdgeReached(swigCPtr));
         }
 
         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(WebView obj)
@@ -156,6 +164,12 @@ namespace Tizen.NUI
             }
         }
 
+        private void OnScrollEdgeReached(IntPtr data, int edge)
+        {
+            WebViewScrollEdgeReachedEventArgs arg = new WebViewScrollEdgeReachedEventArgs((WebViewScrollEdgeReachedEventArgs.Edge)edge);
+            scrollEdgeReachedEventHandler?.Invoke(this, arg);
+        }
+
         internal static new class Property
         {
             internal static readonly int URL = Interop.WebView.WebView_Property_URL_get();
@@ -166,6 +180,9 @@ namespace Tizen.NUI
             internal static readonly int LOAD_IMAGES_AUTOMATICALLY = Interop.WebView.WebView_Property_LOAD_IMAGES_AUTOMATICALLY_get();
             internal static readonly int DEFAULT_TEXT_ENCODING_NAME = Interop.WebView.WebView_Property_DEFAULT_TEXT_ENCODING_NAME_get();
             internal static readonly int DEFAULT_FONT_SIZE = Interop.WebView.WebView_Property_DEFAULT_FONT_SIZE_get();
+            internal static readonly int ScrollPosition = Interop.WebView.ScrollPositionGet();
+            internal static readonly int ScrollSize = Interop.WebView.ScrollSizeGet();
+            internal static readonly int ContentSize = Interop.WebView.ContentSizeGet();
         }
 
         private static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(WebView), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
@@ -312,6 +329,39 @@ namespace Tizen.NUI
             return temp;
         });
 
+
+        private static readonly BindableProperty ScrollPositionProperty = BindableProperty.Create(nameof(ScrollPosition), typeof(Vector2), typeof(WebView), null, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var webview = (WebView)bindable;
+            if (newValue != null)
+            {
+                Tizen.NUI.Object.SetProperty(webview.swigCPtr, WebView.Property.ScrollPosition, new Tizen.NUI.PropertyValue((Vector2)newValue));
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var webview = (WebView)bindable;
+            Vector2 temp = new Vector2(0.0f, 0.0f);
+            Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.ScrollPosition).Get(temp);
+            return temp;
+        });
+
+        private static readonly BindableProperty ScrollSizeProperty = BindableProperty.Create(nameof(ScrollSize), typeof(Vector2), typeof(WebView), null, defaultValueCreator: (bindable) =>
+        {
+            var webview = (WebView)bindable;
+            Vector2 temp = new Vector2(0.0f, 0.0f);
+            Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.ScrollSize).Get(temp);
+            return temp;
+        });
+
+        private static readonly BindableProperty ContentSizeProperty = BindableProperty.Create(nameof(ContentSize), typeof(Vector2), typeof(WebView), null, defaultValueCreator: (bindable) =>
+        {
+            var webview = (WebView)bindable;
+            Vector2 temp = new Vector2(0.0f, 0.0f);
+            Tizen.NUI.Object.GetProperty(webview.swigCPtr, WebView.Property.ContentSize).Get(temp);
+            return temp;
+        });
+
         /// <summary>
         /// Creates an uninitialized WebView.
         /// </summary>
@@ -334,6 +384,16 @@ namespace Tizen.NUI
         }
 
         /// <summary>
+        /// Creates a WebView.
+        /// <param name="args">Argument array. The first value of array must be program's name.</param>
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public WebView(string[] args) : this(args == null ? Interop.WebView.WebView_New() : Interop.WebView.WebView_New_3(args.Length, args), true)
+        {
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+        }
+
+        /// <summary>
         /// Copy constructor.
         /// <param name="webView">WebView to copy. The copied WebView will point at the same implementation</param>
         /// </summary>
@@ -481,6 +541,55 @@ namespace Tizen.NUI
         }
 
         /// <summary>
+        /// The postion of scroll.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Position ScrollPosition
+        {
+            get
+            {
+                Vector2 pv = (Vector2)GetValue(ScrollPositionProperty);
+                return new Position(pv.X, pv.Y);
+            }
+            set
+            {
+                if (value != null)
+                {
+                    Position pv = value;
+                    Vector2 vpv = new Vector2(pv.X, pv.Y);
+                    SetValue(ScrollPositionProperty, vpv);
+                    NotifyPropertyChanged();
+                }
+            }
+        }
+
+        /// <summary>
+        /// The size of scroll, read-only.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Size ScrollSize
+        {
+            get
+            {
+                Vector2 sv = (Vector2)GetValue(ScrollSizeProperty);
+                return new Size(sv.Width, sv.Height);
+            }
+        }
+
+        /// <summary>
+        /// The size of content, read-only.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Size ContentSize
+        {
+            get
+            {
+                Vector2 sv = (Vector2)GetValue(ContentSizeProperty);
+                return new Size(sv.Width, sv.Height);
+            }
+        }
+
+        /// <summary>
         /// Event for the PageLoadStarted signal which can be used to subscribe or unsubscribe the event handler.<br />
         /// This signal is emitted when page loading has started.<br />
         /// </summary>
@@ -559,6 +668,32 @@ namespace Tizen.NUI
         }
 
         /// <summary>
+        /// Event for the ScrollEdgeReached signal which can be used to subscribe or unsubscribe the event handler.<br />
+        /// This signal is emitted when web view is scrolled to edge.<br />
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public event EventHandler<WebViewScrollEdgeReachedEventArgs> ScrollEdgeReached
+        {
+            add
+            {
+                if (scrollEdgeReachedEventHandler == null)
+                {
+                    scrollEdgeReachedCallback = OnScrollEdgeReached;
+                    scrollEdgeReachedSignal.Connect(scrollEdgeReachedCallback);
+                }
+                scrollEdgeReachedEventHandler += value;
+            }
+            remove
+            {
+                scrollEdgeReachedEventHandler -= value;
+                if (scrollEdgeReachedEventHandler == null && scrollEdgeReachedCallback != null)
+                {
+                    scrollEdgeReachedSignal.Disconnect(scrollEdgeReachedCallback);
+                }
+            }
+        }
+
+        /// <summary>
         /// Loads a html.
         /// <param name="url">The path of Web</param>
         /// </summary>
@@ -621,6 +756,18 @@ namespace Tizen.NUI
         }
 
         /// <summary>
+        /// Scroll web view by deltaX and detlaY.
+        /// <param name="deltaX">The deltaX of scroll</param>
+        /// <param name="deltaY">The deltaY of scroll</param>
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public void ScrollBy(int deltaX, int deltaY)
+        {
+            Interop.WebView.ScrollBy(swigCPtr, deltaX, deltaY);
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+        }
+
+        /// <summary>
         /// Goes to the back
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
diff --git a/src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedEventArgs.cs b/src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedEventArgs.cs
new file mode 100755 (executable)
index 0000000..8cf0464
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
+using System.ComponentModel;
+
+namespace Tizen.NUI
+{
+    /// <summary>
+    /// Event arguments that passed via the WebView.ScrollEdgeReached.
+    /// </summary>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class WebViewScrollEdgeReachedEventArgs : EventArgs
+    {
+        /// <summary>
+        /// The enumeration for edge.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public enum Edge
+        {
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            Left,
+
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            Right,
+
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            Top,
+
+            [EditorBrowsable(EditorBrowsableState.Never)]
+            Bottom,
+        }
+
+        /// <summary>
+        /// The edge, e.g. left, right, etc.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Edge ScrollEdge { get; }
+
+        internal WebViewScrollEdgeReachedEventArgs(Edge e)
+        {
+            ScrollEdge = e;
+        }
+    }
+}
diff --git a/src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedSignal.cs b/src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedSignal.cs
new file mode 100755 (executable)
index 0000000..079963a
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+namespace Tizen.NUI
+{
+    internal class WebViewScrollEdgeReachedSignal : Disposable
+    {
+        private global::System.Runtime.InteropServices.HandleRef swigCPtr;
+
+        public WebViewScrollEdgeReachedSignal(global::System.IntPtr cPtr)
+        {
+            swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
+        }
+
+        public void Connect(System.Delegate func)
+        {
+            System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(func);
+            {
+                Interop.WebView.WebViewScrollEdgeReachedSignalConnect(swigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip));
+                if (NDalicPINVOKE.SWIGPendingException.Pending)
+                {
+                    throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+                }
+            }
+        }
+
+        public void Disconnect(System.Delegate func)
+        {
+            System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(func);
+            {
+                Interop.WebView.WebViewScrollEdgeReachedSignalDisconnect(swigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip));
+                if (NDalicPINVOKE.SWIGPendingException.Pending)
+                {
+                    throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+                }
+            }
+        }
+
+        protected override void Dispose(DisposeTypes type)
+        {
+            if (disposed)
+            {
+                return;
+            }
+
+            //Release your own unmanaged resources here.
+            //You should not access any managed member here except static instance.
+            //because the execution order of Finalizes is non-deterministic.
+
+            if (swigCPtr.Handle != global::System.IntPtr.Zero)
+            {
+                Interop.WebView.DeleteWebViewScrollEdgeReachedSignal(swigCPtr);
+                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
+            }
+
+            base.Dispose(type);
+        }
+    }
+}