Skip to content

Commit

Permalink
fix(WebView): Update iOS navigation properties on JS-based history ch…
Browse files Browse the repository at this point in the history
…anges
  • Loading branch information
MartinZikmund committed Jul 28, 2023
1 parent fac11f6 commit 11f2de9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
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 @@ -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 11f2de9

Please sign in to comment.