From 62bf2e475cf493b0c1c0766d0bb7e1e51a655e78 Mon Sep 17 00:00:00 2001 From: huayongxu <49056704+huayongxu@users.noreply.github.com> Date: Fri, 22 Jan 2021 18:24:11 +0800 Subject: [PATCH] [NUI] Support scroll in WebView. (#2549) 1) ScrollPosition, ScrollSize, ContentSize properties are added. 2) ScrollBy is added. 3) ScrollEdgeReached events are notified when scrolled to edge. --- .../src/internal/Interop/Interop.WebView.cs | 26 +++- src/Tizen.NUI/src/internal/WebView.cs | 139 ++++++++++++++++++++- .../internal/WebViewScrollEdgeReachedEventArgs.cs | 59 +++++++++ .../src/internal/WebViewScrollEdgeReachedSignal.cs | 55 ++++++++ 4 files changed, 276 insertions(+), 3 deletions(-) create mode 100755 src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedEventArgs.cs create mode 100755 src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedSignal.cs diff --git a/src/Tizen.NUI/src/internal/Interop/Interop.WebView.cs b/src/Tizen.NUI/src/internal/Interop/Interop.WebView.cs index f86aa52..ae9bebe 100755 --- a/src/Tizen.NUI/src/internal/Interop/Interop.WebView.cs +++ b/src/Tizen.NUI/src/internal/Interop/Interop.WebView.cs @@ -51,6 +51,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 +78,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 +137,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 +} diff --git a/src/Tizen.NUI/src/internal/WebView.cs b/src/Tizen.NUI/src/internal/WebView.cs index c35d501..75852b2 100755 --- a/src/Tizen.NUI/src/internal/WebView.cs +++ b/src/Tizen.NUI/src/internal/WebView.cs @@ -30,13 +30,15 @@ namespace Tizen.NUI [EditorBrowsable(EditorBrowsableState.Never)] public class WebView : View { - [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void WebViewPageLoadCallbackDelegate(IntPtr data, string pageUrl); [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 pageLoadStartedEventHandler; private WebViewPageLoadCallbackDelegate pageLoadStartedCallback; @@ -49,12 +51,16 @@ namespace Tizen.NUI private EventHandler pageLoadErrorEventHandler; private WebViewPageLoadErrorCallbackDelegate pageLoadErrorCallback; + private readonly WebViewScrollEdgeReachedSignal scrollEdgeReachedSignal; + private EventHandler scrollEdgeReachedEventHandler; + private WebViewScrollEdgeReachedCallbackDelegate scrollEdgeReachedCallback; + internal WebView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.WebView.WebView_SWIGUpcast(cPtr), cMemoryOwn) { - 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) @@ -148,6 +154,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(); @@ -158,6 +170,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) => @@ -304,6 +319,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; + }); + /// /// Creates an uninitialized WebView. /// @@ -473,6 +521,55 @@ namespace Tizen.NUI } /// + /// The postion of scroll. + /// + [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(); + } + } + } + + /// + /// The size of scroll, read-only. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public Size ScrollSize + { + get + { + Vector2 sv = (Vector2)GetValue(ScrollSizeProperty); + return new Size(sv.Width, sv.Height); + } + } + + /// + /// The size of content, read-only. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public Size ContentSize + { + get + { + Vector2 sv = (Vector2)GetValue(ContentSizeProperty); + return new Size(sv.Width, sv.Height); + } + } + + /// /// Event for the PageLoadStarted signal which can be used to subscribe or unsubscribe the event handler.
/// This signal is emitted when page loading has started.
///
@@ -551,6 +648,32 @@ namespace Tizen.NUI } /// + /// Event for the ScrollEdgeReached signal which can be used to subscribe or unsubscribe the event handler.
+ /// This signal is emitted when web view is scrolled to edge.
+ ///
+ [EditorBrowsable(EditorBrowsableState.Never)] + public event EventHandler ScrollEdgeReached + { + add + { + if (scrollEdgeReachedEventHandler == null) + { + scrollEdgeReachedCallback = OnScrollEdgeReached; + scrollEdgeReachedSignal.Connect(scrollEdgeReachedCallback); + } + scrollEdgeReachedEventHandler += value; + } + remove + { + scrollEdgeReachedEventHandler -= value; + if (scrollEdgeReachedEventHandler == null && scrollEdgeReachedCallback != null) + { + scrollEdgeReachedSignal.Disconnect(scrollEdgeReachedCallback); + } + } + } + + /// /// Loads a html. /// The path of Web /// @@ -613,6 +736,18 @@ namespace Tizen.NUI } /// + /// Scroll web view by deltaX and detlaY. + /// The deltaX of scroll + /// The deltaY of scroll + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public void ScrollBy(int deltaX, int deltaY) + { + Interop.WebView.ScrollBy(SwigCPtr, deltaX, deltaY); + if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + } + + /// /// Goes to the back /// [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 index 0000000..8cf0464 --- /dev/null +++ b/src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedEventArgs.cs @@ -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 +{ + /// + /// Event arguments that passed via the WebView.ScrollEdgeReached. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public class WebViewScrollEdgeReachedEventArgs : EventArgs + { + /// + /// The enumeration for edge. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public enum Edge + { + [EditorBrowsable(EditorBrowsableState.Never)] + Left, + + [EditorBrowsable(EditorBrowsableState.Never)] + Right, + + [EditorBrowsable(EditorBrowsableState.Never)] + Top, + + [EditorBrowsable(EditorBrowsableState.Never)] + Bottom, + } + + /// + /// The edge, e.g. left, right, etc. + /// + [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 index 0000000..d04c921 --- /dev/null +++ b/src/Tizen.NUI/src/internal/WebViewScrollEdgeReachedSignal.cs @@ -0,0 +1,55 @@ +/* + * 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 + { + public WebViewScrollEdgeReachedSignal(global::System.IntPtr cPtr) : base(cPtr, true) + { + } + + 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(func); + { + Interop.WebView.WebViewScrollEdgeReachedSignalDisconnect(SwigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip)); + if (NDalicPINVOKE.SWIGPendingException.Pending) + { + throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + } + } + } + + protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr) + { + Interop.WebView.DeleteWebViewScrollEdgeReachedSignal(swigCPtr); + } + } +} -- 2.7.4