From 078e62a6f4bc596ad4920655935c793933de3b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Sun, 21 Jan 2018 20:13:38 +0100 Subject: [PATCH] Added Iframe element, WebViewRenderer (basic implementation) and WebView sample --- Ooui.Forms/Exports.cs | 1 + Ooui.Forms/Renderers/WebViewRenderer.cs | 112 ++++++++++++++++++++++++ Ooui/Iframe.cs | 18 ++++ Samples/Program.cs | 1 + Samples/WebViewSample.cs | 41 +++++++++ 5 files changed, 173 insertions(+) create mode 100644 Ooui.Forms/Renderers/WebViewRenderer.cs create mode 100644 Ooui/Iframe.cs create mode 100644 Samples/WebViewSample.cs diff --git a/Ooui.Forms/Exports.cs b/Ooui.Forms/Exports.cs index 2f096f9..e9c192f 100644 --- a/Ooui.Forms/Exports.cs +++ b/Ooui.Forms/Exports.cs @@ -22,6 +22,7 @@ using Xamarin.Forms; [assembly: ExportRenderer(typeof(Slider), typeof(SliderRenderer))] [assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))] [assembly: ExportRenderer (typeof (TimePicker), typeof (TimePickerRenderer))] +[assembly: ExportRenderer(typeof(WebView), typeof(WebViewRenderer))] [assembly: ExportImageSourceHandler (typeof (FileImageSource), typeof (FileImageSourceHandler))] [assembly: ExportImageSourceHandler (typeof (StreamImageSource), typeof (StreamImagesourceHandler))] [assembly: ExportImageSourceHandler (typeof (UriImageSource), typeof (ImageLoaderSourceHandler))] diff --git a/Ooui.Forms/Renderers/WebViewRenderer.cs b/Ooui.Forms/Renderers/WebViewRenderer.cs new file mode 100644 index 0000000..381e905 --- /dev/null +++ b/Ooui.Forms/Renderers/WebViewRenderer.cs @@ -0,0 +1,112 @@ +using System; +using System.ComponentModel; +using Xamarin.Forms; +using Xamarin.Forms.Internals; + +namespace Ooui.Forms.Renderers +{ + public class WebViewRenderer : ViewRenderer, IWebViewDelegate + { + private bool _disposed; + private Iframe _iframe; + + void IWebViewDelegate.LoadHtml(string html, string baseUrl) + { + try + { + if (string.IsNullOrEmpty(html)) + { + if (Element.Source is HtmlWebViewSource urlWebViewSource) + { + html = urlWebViewSource.Html; + } + } + + if (_iframe != null) + { + _iframe.Src = html; + } + } + catch (Exception ex) + { + Log.Warning("WebView load string", $"WebView load string failed: {ex}"); + } + } + + void IWebViewDelegate.LoadUrl(string url) + { + try + { + if (string.IsNullOrEmpty(url)) + { + if (Element.Source is UrlWebViewSource urlWebViewSource) + { + url = urlWebViewSource.Url; + } + } + + if (_iframe != null) + { + _iframe.Src = url; + } + } + catch (Exception ex) + { + Log.Warning("WebView load url", $"WebView load url failed: {ex}"); + } + } + + public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) + { + var size = new Size(100, 100); + return new SizeRequest(size, size); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (e.NewElement != null) + { + if (Control == null) + { + var embed = new Div { ClassName = "embed-responsive" }; + _iframe = new Iframe(); + embed.AppendChild(_iframe); + + SetNativeControl(embed); + } + } + + Load(); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == WebView.SourceProperty.PropertyName) + Load(); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (disposing && !_disposed) + { + if (_iframe != null) + { + _iframe = null; + } + + _disposed = true; + } + } + + private void Load() + { + Element?.Source?.Load(this); + } + } +} diff --git a/Ooui/Iframe.cs b/Ooui/Iframe.cs new file mode 100644 index 0000000..587109e --- /dev/null +++ b/Ooui/Iframe.cs @@ -0,0 +1,18 @@ +namespace Ooui +{ + public class Iframe : Element + { + public Iframe() + : base("iframe") + { + + } + + string src = null; + public string Src + { + get => src; + set => SetProperty(ref src, value, "src"); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index 13921dc..4478211 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -41,6 +41,7 @@ namespace Samples new TipCalcSample().Publish(); new WeatherAppSample().Publish(); new XuzzleSample().Publish(); + new WebViewSample().Publish(); UI.Present ("/display-alert"); diff --git a/Samples/WebViewSample.cs b/Samples/WebViewSample.cs new file mode 100644 index 0000000..2c0593e --- /dev/null +++ b/Samples/WebViewSample.cs @@ -0,0 +1,41 @@ +using Ooui; +using Xamarin.Forms; + +namespace Samples +{ + public class WebViewSample : ISample + { + public string Title => "Xamarin.Forms WebView Sample"; + + public Ooui.Element CreateElement() + { + var panel = new StackLayout(); + + var titleLabel = new Xamarin.Forms.Label + { + Text = "WebView", + FontSize = 24, + FontAttributes = FontAttributes.Bold + }; + panel.Children.Add(titleLabel); + + WebView webview = new WebView + { + Source = "http://www.xamarin.com" + }; + panel.Children.Add(webview); + + var page = new ContentPage + { + Content = panel + }; + + return page.GetOouiElement(); + } + + public void Publish() + { + UI.Publish("/webview", CreateElement); + } + } +}