Skip to content

Commit

Permalink
Merge pull request #12931 from unoplatform/mergify/bp/legacy/4x/pr-12907
Browse files Browse the repository at this point in the history
fix: Update `WebView` history properties on JS-based navigation (backport #12907)
  • Loading branch information
MartinZikmund authored Jul 28, 2023
2 parents 481b665 + 5732239 commit afaf0a4
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Page
x:Class="UITests.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_NavigationProperties"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Microsoft_UI_Xaml_Controls.WebView2Tests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel Spacing="8">
<controls:WebView2 x:Name="TestWebView" Width="300" Height="300" />
<TextBlock>
<Run Text="Last history change:" />
<Run x:Name="LastHistoryChangeRun" Text="" />
</TextBlock>
<CheckBox x:Name="CanGoBackCheckBox" />
<CheckBox x:Name="CanGoForwardCheckBox" />
<Button Click="GoBack_Click" Content="Go back" />
<Button Click="GoForward_Click" Content="Go forward" />
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;
using Microsoft.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace UITests.Microsoft_UI_Xaml_Controls.WebView2Tests
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
[Uno.UI.Samples.Controls.Sample("WebView")]
public sealed partial class WebView2_NavigationProperties : Page
{
public WebView2_NavigationProperties()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
}

private async void OnLoaded(object sender, RoutedEventArgs e)
{
await TestWebView.EnsureCoreWebView2Async();
TestWebView.CoreWebView2.Navigate("https://nventive.com/en");
TestWebView.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
}

private void CoreWebView2_HistoryChanged(Microsoft.Web.WebView2.Core.CoreWebView2 sender, object args)
{
LastHistoryChangeRun.Text = DateTimeOffset.Now.TimeOfDay.ToString();
CanGoBackCheckBox.IsChecked = sender.CanGoBack;
CanGoForwardCheckBox.IsChecked = sender.CanGoForward;
}

private void GoForward_Click(object sender, RoutedEventArgs e)
{
TestWebView.GoForward();
}

private void GoBack_Click(object sender, RoutedEventArgs e)
{
TestWebView.GoBack();
}
}
}
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 @@ -3886,6 +3886,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_NavigationProperties.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_ChromeClient.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -8727,6 +8731,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_Basic.xaml.cs">
<DependentUpon>WebView2_Basic.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_NavigationProperties.xaml.cs">
<DependentUpon>WebView2_NavigationProperties.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_ChromeClient.xaml.cs">
<DependentUpon>WebView2_ChromeClient.xaml</DependentUpon>
</Compile>
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal CoreWebView2(IWebView owner)
_owner = owner;
}

internal IWebView Owner => _owner;

/// <summary>
/// Gets the CoreWebView2Settings object contains various modifiable
/// settings for the running WebView.
Expand Down
6 changes: 5 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/WebView/IWebView.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace Uno.UI.Xaml.Controls;
using Windows.UI.Core;

namespace Uno.UI.Xaml.Controls;

internal interface IWebView
{
bool SwitchSourceBeforeNavigating { get; }

bool IsLoaded { get; }

CoreDispatcher Dispatcher { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using System.Globalization;
using Android.Graphics;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Microsoft.Web.WebView2.Core;
using Windows.UI.Xaml.Controls;
using Windows.Web;

namespace Uno.UI.Xaml.Controls;
Expand All @@ -26,6 +24,16 @@ internal InternalClient(CoreWebView2 coreWebView, NativeWebViewWrapper webViewWr
_nativeWebViewWrapper = webViewWrapper;
}

public override void DoUpdateVisitedHistory(Android.Webkit.WebView view, string url, bool isReload)
{
base.DoUpdateVisitedHistory(view, url, isReload);

_coreWebView.DocumentTitle = view.Title;

_nativeWebViewWrapper.RefreshHistoryProperties();
_coreWebView.RaiseHistoryChanged();
}

#pragma warning disable CS0672 // Member overrides obsolete member
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
#pragma warning restore CS0672 // Member overrides obsolete member
Expand Down Expand Up @@ -62,9 +70,6 @@ public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
_coreWebView.DocumentTitle = view.Title;

_nativeWebViewWrapper.RefreshHistoryProperties();
_coreWebView.RaiseHistoryChanged();

var uri = !_nativeWebViewWrapper._wasLoadedFromString && !string.IsNullOrEmpty(url) ? new Uri(url) : null;

_coreWebView.RaiseNavigationCompleted(uri, true, 200, CoreWebView2WebErrorStatus.Unknown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
using System.Threading.Tasks;
using Uno.Extensions;
using Uno.Foundation.Logging;
using Windows.Web;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.Resources;
using Uno.UI.Xaml.Controls;
using System.Net.Http;
using Microsoft.Web.WebView2.Core;
using Uno.UI.Extensions;
using Windows.Foundation;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Net;
using Windows.UI.Core;

#if !__MACOS__ && !__MACCATALYST__ // catalyst https://github.com/xamarin/xamarin-macios/issues/13935
Expand Down Expand Up @@ -49,6 +45,8 @@ public partial class UnoWKWebView : WKWebView, INativeWebView, IWKScriptMessageH
private readonly string OkString;
private readonly string CancelString;

private bool _isHistoryChangeQueued;

public UnoWKWebView() : base(CGRect.Empty, new WebKit.WKWebViewConfiguration())
{
var resourceLoader = ResourceLoader.GetForCurrentView();
Expand Down Expand Up @@ -423,10 +421,26 @@ private void RaiseNavigationStarting(object navigationData, out bool cancel)
private void RaiseNavigationCompleted(Uri uri, bool isSuccess, int httpStatusCode, CoreWebView2WebErrorStatus errorStatus)
{
_coreWebView.SetHistoryProperties(CanGoBack, CanGoForward);
_coreWebView.RaiseHistoryChanged();
QueueHistoryChange();

_coreWebView.RaiseNavigationCompleted(uri, isSuccess, httpStatusCode, errorStatus);
}

private void QueueHistoryChange()
{
if (!_isHistoryChangeQueued)
{
_isHistoryChangeQueued = true;
_ = _coreWebView.Owner.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, RaiseQueuedHistoryChange);
}
}

private void RaiseQueuedHistoryChange()
{
_coreWebView.RaiseHistoryChanged();
_isHistoryChangeQueued = false;
}

#if __IOS__
private void ParseUriAndLauchMailto(Uri mailtoUri)
{
Expand Down Expand Up @@ -572,6 +586,24 @@ internal void OnError(WKWebView webView, WKNavigation navigation, NSError error)
_isCancelling = false;
}

public override void DidChangeValue(string forKey)
{
base.DidChangeValue(forKey);

if (forKey.Equals(nameof(Title), StringComparison.OrdinalIgnoreCase))
{
_coreWebView.DocumentTitle = Title;
}
else if (
forKey.Equals(nameof(Url), StringComparison.OrdinalIgnoreCase) ||
forKey.Equals(nameof(CanGoBack), StringComparison.OrdinalIgnoreCase) ||
forKey.Equals(nameof(CanGoForward), StringComparison.OrdinalIgnoreCase))
{
_coreWebView.SetHistoryProperties(CanGoBack, CanGoForward);
QueueHistoryChange();
}
}

public override bool CanGoBack => base.CanGoBack && GetNearestValidHistoryItem(direction: -1) != null;

public override bool CanGoForward => base.CanGoForward && GetNearestValidHistoryItem(direction: 1) != null;
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/WebView/WebView1/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using Windows.UI.Core;

namespace Windows.UI.Xaml.Controls;

Expand Down Expand Up @@ -42,6 +43,8 @@ public WebView()

bool IWebView.SwitchSourceBeforeNavigating => true;

CoreDispatcher IWebView.Dispatcher => Dispatcher;

protected override void OnApplyTemplate() => CoreWebView2.OnOwnerApplyTemplate();

public void Navigate(global::System.Uri source) => CoreWebView2.Navigate(source.ToString());
Expand Down
8 changes: 5 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/WebView/WebView2/WebView2.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#nullable enable

using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using System;
using System.Threading.Tasks;
using Uno;
using Microsoft.Web.WebView2.Core;
using Uno.UI.Xaml.Controls;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;

namespace Microsoft.UI.Xaml.Controls;

Expand Down Expand Up @@ -44,6 +44,8 @@ public WebView2()

bool IWebView.SwitchSourceBeforeNavigating => false; // WebView2 switches source only when navigation completes.

CoreDispatcher IWebView.Dispatcher => Dispatcher;

protected override void OnApplyTemplate() => CoreWebView2.OnOwnerApplyTemplate();

private void WebView2_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) =>
Expand Down

0 comments on commit afaf0a4

Please sign in to comment.