From 9fd6a13b3d1494fd251e1136771ce22f142b6702 Mon Sep 17 00:00:00 2001 From: chungryeol lim Date: Thu, 9 Mar 2017 17:35:07 +0900 Subject: [PATCH] Add WebViewRenderer - Depend On : https://review.tizen.org/gerrit/#/c/115231/ (This will be added after WebView Merge) Change-Id: I1fdc0c4420ba7e7a6b48b6ee0ce60d59e31e3691 Signed-off-by: chungryeol lim --- .../Renderers/WebViewRenderer.cs | 177 +++++++++++++++++++++ .../Xamarin.Forms.Platform.Tizen.csproj | 1 + .../Xamarin.Forms.Platform.Tizen.project.json | 3 +- packaging/xamarin-forms-tizen.spec | 1 + 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 Xamarin.Forms.Platform.Tizen/Renderers/WebViewRenderer.cs diff --git a/Xamarin.Forms.Platform.Tizen/Renderers/WebViewRenderer.cs b/Xamarin.Forms.Platform.Tizen/Renderers/WebViewRenderer.cs new file mode 100644 index 0000000..56579fb --- /dev/null +++ b/Xamarin.Forms.Platform.Tizen/Renderers/WebViewRenderer.cs @@ -0,0 +1,177 @@ +using System; +using System.ComponentModel; +using Xamarin.Forms.Internals; +using TChromium = Tizen.WebView.Chromium; +using TWebView = Tizen.WebView.WebView; + +namespace Xamarin.Forms.Platform.Tizen +{ + public class WebViewRenderer : VisualElementRenderer, IWebViewDelegate + { + bool _updating; + WebNavigationEvent _eventState; + TWebView _control = null; + + public void LoadHtml(string html, string baseUrl) + { + _control.LoadHtml(html, baseUrl); + } + + public void LoadUrl(string url) + { + if (!string.IsNullOrEmpty(url)) + { + _control.LoadUrl(url); + } + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + if (Element != null) + { + if (_control != null) + { + _control.StopLoading(); + _control.LoadStarted -= OnLoadStarted; + _control.LoadFinished -= OnLoadFinished; + _control.LoadError -= OnLoadError; + } + Element.EvalRequested -= OnEvalRequested; + Element.GoBackRequested -= OnGoBackRequested; + Element.GoForwardRequested -= OnGoForwardRequested; + } + } + base.Dispose(disposing); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (_control == null) + { + TChromium.Initialize(); + Forms.Context.Terminated += (sender, arg) => TChromium.Shutdown(); + _control = new TWebView(Forms.Context.MainWindow); + _control.LoadStarted += OnLoadStarted; + _control.LoadFinished += OnLoadFinished; + _control.LoadError += OnLoadError; + SetNativeControl(_control); + } + + if (e.OldElement != null) + { + e.OldElement.EvalRequested -= OnEvalRequested; + e.OldElement.GoBackRequested -= OnGoBackRequested; + e.OldElement.GoForwardRequested -= OnGoForwardRequested; + } + + if (e.NewElement != null) + { + e.NewElement.EvalRequested += OnEvalRequested; + e.NewElement.GoForwardRequested += OnGoForwardRequested; + e.NewElement.GoBackRequested += OnGoBackRequested; + Load(); + } + base.OnElementChanged(e); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == WebView.SourceProperty.PropertyName) + Load(); + + base.OnElementPropertyChanged(sender, e); + } + + void OnLoadError(object sender, global::Tizen.WebView.SmartCallbackLoadErrorArgs e) + { + string url = e.Url; + if (!string.IsNullOrEmpty(url)) + SendNavigated(new UrlWebViewSource { Url = url }, _eventState, WebNavigationResult.Failure); + } + + void OnLoadStarted(object sender, EventArgs e) + { + string url = _control.Url; + if (!string.IsNullOrEmpty(url)) + { + var args = new WebNavigatingEventArgs(_eventState, new UrlWebViewSource { Url = url }, url); + Element.SendNavigating(args); + + if (args.Cancel) + { + _eventState = WebNavigationEvent.NewPage; + } + } + } + + void OnLoadFinished(object sender, EventArgs e) + { + string url = _control.Url; + if (!string.IsNullOrEmpty(url)) + SendNavigated(new UrlWebViewSource { Url = url }, _eventState, WebNavigationResult.Success); + + _control.SetFocus(true); + UpdateCanGoBackForward(); + } + + void Load() + { + if (_updating) + return; + + if (Element.Source != null) + { + Element.Source.Load(this); + } + + UpdateCanGoBackForward(); + } + + void OnEvalRequested(object sender, EvalRequested eventArg) + { + _control.Eval(eventArg.Script); + } + + void OnGoBackRequested(object sender, EventArgs eventArgs) + { + if (_control.CanGoBack()) + { + _eventState = WebNavigationEvent.Back; + _control.GoBack(); + } + + UpdateCanGoBackForward(); + } + + void OnGoForwardRequested(object sender, EventArgs eventArgs) + { + if (_control.CanGoForward()) + { + _eventState = WebNavigationEvent.Forward; + _control.GoForward(); + } + + UpdateCanGoBackForward(); + } + + void SendNavigated(UrlWebViewSource source, WebNavigationEvent evnt, WebNavigationResult result) + { + _updating = true; + ((IElementController)Element).SetValueFromRenderer(WebView.SourceProperty, source); + _updating = false; + + Element.SendNavigated(new WebNavigatedEventArgs(evnt, source, source.Url, result)); + + UpdateCanGoBackForward(); + _eventState = WebNavigationEvent.NewPage; + } + + void UpdateCanGoBackForward() + { + Element.CanGoBack = _control.CanGoBack(); + Element.CanGoForward = _control.CanGoForward(); + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.csproj b/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.csproj index 1d50c33..85ad0a4 100755 --- a/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.csproj +++ b/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.csproj @@ -133,6 +133,7 @@ + diff --git a/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.project.json b/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.project.json index b77e8cb..154c271 100644 --- a/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.project.json +++ b/Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.project.json @@ -4,7 +4,8 @@ "NETStandard.Library": "1.6.0", "System.Runtime.Serialization.Xml": "4.1.1", "Tizen.Applications": "1.1.0", - "Tizen.System.Information": "1.0.1" + "Tizen.System.Information": "1.0.1", + "Tizen.WebView": "1.0.0" }, "frameworks": { "netstandard1.6": { diff --git a/packaging/xamarin-forms-tizen.spec b/packaging/xamarin-forms-tizen.spec index fea2a20..39ce5b8 100644 --- a/packaging/xamarin-forms-tizen.spec +++ b/packaging/xamarin-forms-tizen.spec @@ -30,6 +30,7 @@ BuildRequires: csapi-information-nuget BuildRequires: csapi-location-nuget BuildRequires: csapi-maps-nuget BuildRequires: elm-sharp-nuget +BuildRequires: csapi-webview-nuget %description Allows one to use portable controls subsets that are mapped to native -- 2.7.4