Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[Enhancement] Android: WebView - Control over Zoom controls (#3607) f…
Browse files Browse the repository at this point in the history
…ixes #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 <github@geraldversluis.nl>

* Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs

Co-Authored-By: jfversluis <github@geraldversluis.nl>

* Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs

Co-Authored-By: jfversluis <github@geraldversluis.nl>

* Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs

Co-Authored-By: jfversluis <github@geraldversluis.nl>

* Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs

Co-Authored-By: jfversluis <github@geraldversluis.nl>

* Update Xamarin.Forms.Core/PlatformConfiguration/AndroidSpecific/WebView.cs

Co-Authored-By: jfversluis <github@geraldversluis.nl>
  • Loading branch information
jfversluis authored and rmarinho committed Feb 12, 2019
1 parent 920e2dd commit 2c3df0a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Xamarin.Forms.Controls/CoreGalleryPages/WebViewCoreGalleryPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected override void Build (StackLayout stackLayout)
);

// 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/";
Expand All @@ -81,13 +82,36 @@ protected override void Build (StackLayout stackLayout)
Url = mixedContentTestPage
};

var enableZoomControlsWebView = new WebView() { HeightRequest = 200 };
enableZoomControlsWebView.On<Android>().SetEnableZoomControls(true);
enableZoomControlsWebView.On<Android>().SetDisplayZoomControls(false);
enableZoomControlsWebView.Source = new UrlWebViewSource
{
Url = "https://www.xamarin.com"
};

var displayZoomControlsWebView = new WebView() { HeightRequest = 200 };
displayZoomControlsWebView.On<Android>().SetEnableZoomControls(true);
displayZoomControlsWebView.On<Android>().SetDisplayZoomControls(true);
displayZoomControlsWebView.Source = new UrlWebViewSource
{
Url = "https://www.xamarin.com"
};

var mixedContentDisallowedContainer = new ViewContainer<WebView>(Test.WebView.MixedContentDisallowed,
mixedContentDisallowedWebView);
var mixedContentAllowedContainer = new ViewContainer<WebView>(Test.WebView.MixedContentAllowed,
mixedContentAllowedWebView);

var enableZoomControlsContainer = new ViewContainer<WebView>(Test.WebView.EnableZoomControls,
enableZoomControlsWebView);
var displayZoomControlsWebViewContainer = new ViewContainer<WebView>(Test.WebView.DisplayZoomControls,
displayZoomControlsWebView);

Add(mixedContentDisallowedContainer);
Add(mixedContentAllowedContainer);
Add(enableZoomControlsContainer);
Add(displayZoomControlsWebViewContainer);
}


Expand Down
24 changes: 24 additions & 0 deletions Xamarin.Forms.Core.UnitTests/WebViewUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ public void TestAndroidMixedContent()
Assert.AreEqual(mixedContentWebView.On<Android>().MixedContentMode(), MixedContentHandling.AlwaysAllow);
}

[Test]
public void TestEnableZoomControls()
{
var defaultWebView = new WebView();

var enableZoomControlsWebView = new WebView();
enableZoomControlsWebView.On<Android>().SetEnableZoomControls(true);

Assert.AreEqual(defaultWebView.On<Android>().EnableZoomControls(), false);
Assert.AreEqual(enableZoomControlsWebView.On<Android>().EnableZoomControls(), true);
}

[Test]
public void TestDisplayZoomControls()
{
var defaultWebView = new WebView();

var displayZoomControlsWebView = new WebView();
displayZoomControlsWebView.On<Android>().SetDisplayZoomControls(false);

Assert.AreEqual(defaultWebView.On<Android>().DisplayZoomControls(), true);
Assert.AreEqual(displayZoomControlsWebView.On<Android>().DisplayZoomControls(), false);
}

[Test]
public void TestWindowsSetAllowJavaScriptAlertsFlag()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,51 @@ public static IPlatformElementConfiguration<Android, FormsElement> SetMixedConte
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<Android, FormsElement> config)
{
return GetEnableZoomControls(config.Element);
}

public static IPlatformElementConfiguration<Android, FormsElement> SetEnableZoomControls(this IPlatformElementConfiguration<Android, FormsElement> 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<Android, FormsElement> config)
{
return GetDisplayZoomControls(config.Element);
}

public static IPlatformElementConfiguration<Android, FormsElement> SetDisplayZoomControls(this IPlatformElementConfiguration<Android, FormsElement> config, bool value)
{
SetDisplayZoomControls(config.Element, value);
return config;
}
}
}
4 changes: 3 additions & 1 deletion Xamarin.Forms.CustomAttributes/TestAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ public enum WebView
MixedContentDisallowed,
MixedContentAllowed,
JavaScriptAlert,
EvaluateJavaScript
EvaluateJavaScript,
EnableZoomControls,
DisplayZoomControls
}

public enum UrlWebViewSource
Expand Down
20 changes: 20 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/WebViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
newElementController.ReloadRequested += OnReloadRequested;

UpdateMixedContentMode();
UpdateEnableZoomControls();
UpdateDisplayZoomControls();
}

Load();
Expand All @@ -146,6 +148,12 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
case "MixedContentMode":
UpdateMixedContentMode();
break;
case "EnableZoomControls":
UpdateEnableZoomControls();
break;
case "DisplayZoomControls":
UpdateDisplayZoomControls();
break;
}
}

Expand Down Expand Up @@ -210,6 +218,18 @@ void UpdateMixedContentMode()
}
}

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<string> source;
Expand Down

0 comments on commit 2c3df0a

Please sign in to comment.