Skip to content

Commit

Permalink
Merge pull request #2360 from unoplatform/dev/djo/webview-android-fix
Browse files Browse the repository at this point in the history
[Android] Fix WebView clipping issue
  • Loading branch information
davidjohnoliver authored Dec 19, 2019
2 parents 9115ee0 + 37e25f3 commit bf4047e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\WebView\WebView_Animated_Opacity.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\WebView\WebView_ChromeClient.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3848,6 +3852,9 @@
<DependentUpon>UndefinedHeightListView.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\WebView\WebViewViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\WebView\WebView_Animated_Opacity.xaml.cs">
<DependentUpon>WebView_Animated_Opacity.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\WebView\WebView_NavigateToString2.xaml.cs">
<DependentUpon>WebView_NavigateToString2.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<UserControl x:Class="UITests.Shared.Windows_UI_Xaml_Controls.WebView.WebView_Animated_Opacity"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Shared.Windows_UI_Xaml_Controls.WebView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">


<Grid Background="Red">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Content="Some Text !" />

<ContentControl x:Name="test"
Grid.Row="1"
Width="100"
Height="100">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="LoadingStates">
<VisualState x:Name="Loading">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="PART_WebView"
Storyboard.TargetProperty="Opacity"
Duration="0:0:0.1"
To="1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<!-- Value -->
<WebView x:Name="PART_WebView"
Source="https://web.archive.org/web/20181010203846/https://www.bloomberg.com/view/articles/2018-10-09/google-is-big-and-google-was-small" />
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace UITests.Shared.Windows_UI_Xaml_Controls.WebView
{
[SampleControlInfo(description: "Opacity is animated on WebView. It shouldn't exceed bounds while page is loading (Android bug #2006)")]
public sealed partial class WebView_Animated_Opacity : UserControl
{
public WebView_Animated_Opacity()
{
this.InitializeComponent();

VisualStateManager.GoToState(test, "Loading", true);
}
}
}
35 changes: 20 additions & 15 deletions src/Uno.UI/UI/Xaml/Controls/WebView/WebView.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

namespace Windows.UI.Xaml.Controls
{
public partial class WebView : Control
public partial class WebView : Control, ICustomClippingElement
{
private Android.Webkit.WebView _webView;
private bool _wasLoadedFromString;
protected override void OnApplyTemplate()
private bool _wasLoadedFromString;

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

Expand Down Expand Up @@ -71,8 +71,8 @@ partial void NavigatePartial(Uri uri)
return;
}

_wasLoadedFromString = false;
if (uri.Scheme.Equals("local", StringComparison.OrdinalIgnoreCase))
_wasLoadedFromString = false;
if (uri.Scheme.Equals("local", StringComparison.OrdinalIgnoreCase))
{
var path = $"file:///android_asset/{uri.PathAndQuery}";
_webView.LoadUrl(path);
Expand Down Expand Up @@ -121,8 +121,8 @@ partial void NavigateWithHttpRequestMessagePartial(HttpRequestMessage requestMes
element => element.Value.JoinBy(", ")
);

_wasLoadedFromString = false;
_webView.LoadUrl(uri.AbsoluteUri, headers);
_wasLoadedFromString = false;
_webView.LoadUrl(uri.AbsoluteUri, headers);
}

partial void NavigateToStringPartial(string text)
Expand All @@ -132,8 +132,8 @@ partial void NavigateToStringPartial(string text)
return;
}

_wasLoadedFromString = true;
_webView.LoadData(text, "text/html; charset=utf-8", "utf-8");
_wasLoadedFromString = true;
_webView.LoadData(text, "text/html; charset=utf-8", "utf-8");
}

//This should be IAsyncOperation<string> instead of Task<string> but we use an extension method to enable the same signature in Win.
Expand Down Expand Up @@ -305,12 +305,12 @@ public override void OnPageFinished(Android.Webkit.WebView view, string url)
IsSuccess = _webViewSuccess,
WebErrorStatus = _webErrorStatus
};
if (!_webView._wasLoadedFromString && !string.IsNullOrEmpty(url))
{
args.Uri = new Uri(url);
}
if (!_webView._wasLoadedFromString && !string.IsNullOrEmpty(url))
{
args.Uri = new Uri(url);
}

_webView.NavigationCompleted?.Invoke(_webView, args);
_webView.NavigationCompleted?.Invoke(_webView, args);
base.OnPageFinished(view, url);
}

Expand Down Expand Up @@ -442,5 +442,10 @@ private async Task<T> StartActivity<T>(CancellationToken ct) where T : BaseActiv
return null;
}
}

bool ICustomClippingElement.AllowClippingToLayoutSlot => true;

// Force clipping, otherwise native WebView may exceed its bounds in some circumstances (eg when Xaml WebView is animated)
bool ICustomClippingElement.ForceClippingToLayoutSlot => true;
}
}

0 comments on commit bf4047e

Please sign in to comment.