From 2c3df0ac0e80f9169fcde6bfcfaf76f5f63ba313 Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Tue, 12 Feb 2019 14:10:33 +0100 Subject: [PATCH] [Enhancement] Android: WebView - Control over Zoom controls (#3607) fixes #1661 * Started implementation of zoom support on Android WebView * Implemented fully working zoom and zoom controls for Android WebView * Corrected merge error * Removed redundant API checks * Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs Co-Authored-By: jfversluis * Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs Co-Authored-By: jfversluis * Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs Co-Authored-By: jfversluis * Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs Co-Authored-By: jfversluis * Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs Co-Authored-By: jfversluis * Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs Co-Authored-By: jfversluis --- .../CoreGalleryPages/WebViewCoreGalleryPage.cs | 24 +++++++++++ Xamarin.Forms.Core.UnitTests/WebViewUnitTests.cs | 24 +++++++++++ .../AndroidSpecific/WebView.cs | 46 ++++++++++++++++++++++ Xamarin.Forms.CustomAttributes/TestAttributes.cs | 4 +- .../Renderers/WebViewRenderer.cs | 20 ++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) diff --git a/Xamarin.Forms.Controls/CoreGalleryPages/WebViewCoreGalleryPage.cs b/Xamarin.Forms.Controls/CoreGalleryPages/WebViewCoreGalleryPage.cs index 8370266..10b70d9 100644 --- a/Xamarin.Forms.Controls/CoreGalleryPages/WebViewCoreGalleryPage.cs +++ b/Xamarin.Forms.Controls/CoreGalleryPages/WebViewCoreGalleryPage.cs @@ -63,6 +63,7 @@ namespace Xamarin.Forms.Controls ); // NOTE: Currently the ability to programmatically enable/disable mixed content only exists on Android + // NOTE: Currently the ability to programmatically enable/disable zoom only exists on Android if (Device.RuntimePlatform == Device.Android) { var mixedContentTestPage = "https://mixed-content-test.appspot.com/"; @@ -81,13 +82,36 @@ namespace Xamarin.Forms.Controls Url = mixedContentTestPage }; + var enableZoomControlsWebView = new WebView() { HeightRequest = 200 }; + enableZoomControlsWebView.On().SetEnableZoomControls(true); + enableZoomControlsWebView.On().SetDisplayZoomControls(false); + enableZoomControlsWebView.Source = new UrlWebViewSource + { + Url = "https://www.xamarin.com" + }; + + var displayZoomControlsWebView = new WebView() { HeightRequest = 200 }; + displayZoomControlsWebView.On().SetEnableZoomControls(true); + displayZoomControlsWebView.On().SetDisplayZoomControls(true); + displayZoomControlsWebView.Source = new UrlWebViewSource + { + Url = "https://www.xamarin.com" + }; + var mixedContentDisallowedContainer = new ViewContainer(Test.WebView.MixedContentDisallowed, mixedContentDisallowedWebView); var mixedContentAllowedContainer = new ViewContainer(Test.WebView.MixedContentAllowed, mixedContentAllowedWebView); + var enableZoomControlsContainer = new ViewContainer(Test.WebView.EnableZoomControls, + enableZoomControlsWebView); + var displayZoomControlsWebViewContainer = new ViewContainer(Test.WebView.DisplayZoomControls, + displayZoomControlsWebView); + Add(mixedContentDisallowedContainer); Add(mixedContentAllowedContainer); + Add(enableZoomControlsContainer); + Add(displayZoomControlsWebViewContainer); } diff --git a/Xamarin.Forms.Core.UnitTests/WebViewUnitTests.cs b/Xamarin.Forms.Core.UnitTests/WebViewUnitTests.cs index ed12f02..c021320 100644 --- a/Xamarin.Forms.Core.UnitTests/WebViewUnitTests.cs +++ b/Xamarin.Forms.Core.UnitTests/WebViewUnitTests.cs @@ -106,6 +106,30 @@ namespace Xamarin.Forms.Core.UnitTests } [Test] + public void TestEnableZoomControls() + { + var defaultWebView = new WebView(); + + var enableZoomControlsWebView = new WebView(); + enableZoomControlsWebView.On().SetEnableZoomControls(true); + + Assert.AreEqual(defaultWebView.On().EnableZoomControls(), false); + Assert.AreEqual(enableZoomControlsWebView.On().EnableZoomControls(), true); + } + + [Test] + public void TestDisplayZoomControls() + { + var defaultWebView = new WebView(); + + var displayZoomControlsWebView = new WebView(); + displayZoomControlsWebView.On().SetDisplayZoomControls(false); + + Assert.AreEqual(defaultWebView.On().DisplayZoomControls(), true); + Assert.AreEqual(displayZoomControlsWebView.On().DisplayZoomControls(), false); + } + + [Test] public void TestWindowsSetAllowJavaScriptAlertsFlag() { var defaultWebView = new WebView(); diff --git a/Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs b/Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs index 8c96d37..b8e5da8 100644 --- a/Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs +++ b/Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs @@ -34,5 +34,51 @@ namespace Xamarin.Forms.PlatformConfiguration.AndroidSpecific SetMixedContentMode(config.Element, value); return config; } + + public static readonly BindableProperty EnableZoomControlProperty = BindableProperty.Create("EnableZoomControls", typeof(bool), typeof(FormsElement), false); + + public static bool GetEnableZoomControls(FormsElement element) + { + return (bool)element.GetValue(EnableZoomControlProperty); + } + + public static void SetEnableZoomControls(FormsElement element, bool value) + { + element.SetValue(EnableZoomControlProperty, value); + } + + public static bool EnableZoomControls(this IPlatformElementConfiguration config) + { + return GetEnableZoomControls(config.Element); + } + + public static IPlatformElementConfiguration SetEnableZoomControls(this IPlatformElementConfiguration config, bool value) + { + SetEnableZoomControls(config.Element, value); + return config; + } + + public static readonly BindableProperty DisplayZoomControlsProperty = BindableProperty.Create("DisplayZoomControls", typeof(bool), typeof(FormsElement), true); + + public static bool GetDisplayZoomControls(FormsElement element) + { + return (bool)element.GetValue(DisplayZoomControlsProperty); + } + + public static void SetDisplayZoomControls(FormsElement element, bool value) + { + element.SetValue(DisplayZoomControlsProperty, value); + } + + public static bool DisplayZoomControls(this IPlatformElementConfiguration config) + { + return GetDisplayZoomControls(config.Element); + } + + public static IPlatformElementConfiguration SetDisplayZoomControls(this IPlatformElementConfiguration config, bool value) + { + SetDisplayZoomControls(config.Element, value); + return config; + } } } diff --git a/Xamarin.Forms.CustomAttributes/TestAttributes.cs b/Xamarin.Forms.CustomAttributes/TestAttributes.cs index dc37df9..aff1749 100644 --- a/Xamarin.Forms.CustomAttributes/TestAttributes.cs +++ b/Xamarin.Forms.CustomAttributes/TestAttributes.cs @@ -736,7 +736,9 @@ namespace Xamarin.Forms.CustomAttributes MixedContentDisallowed, MixedContentAllowed, JavaScriptAlert, - EvaluateJavaScript + EvaluateJavaScript, + EnableZoomControls, + DisplayZoomControls } public enum UrlWebViewSource diff --git a/Xamarin.Forms.Platform.Android/Renderers/WebViewRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/WebViewRenderer.cs index 51cd095..e9fb6c2 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/WebViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/WebViewRenderer.cs @@ -129,6 +129,8 @@ namespace Xamarin.Forms.Platform.Android newElementController.ReloadRequested += OnReloadRequested; UpdateMixedContentMode(); + UpdateEnableZoomControls(); + UpdateDisplayZoomControls(); } Load(); @@ -146,6 +148,12 @@ namespace Xamarin.Forms.Platform.Android case "MixedContentMode": UpdateMixedContentMode(); break; + case "EnableZoomControls": + UpdateEnableZoomControls(); + break; + case "DisplayZoomControls": + UpdateDisplayZoomControls(); + break; } } @@ -210,6 +218,18 @@ namespace Xamarin.Forms.Platform.Android } } + void UpdateEnableZoomControls() + { + var value = Element.OnThisPlatform().EnableZoomControls(); + Control.Settings.SetSupportZoom(value); + Control.Settings.BuiltInZoomControls = value; + } + + void UpdateDisplayZoomControls() + { + Control.Settings.DisplayZoomControls = Element.OnThisPlatform().DisplayZoomControls(); + } + class JavascriptResult : Java.Lang.Object, IValueCallback { TaskCompletionSource source; -- 2.7.4