Skip to content

Commit

Permalink
Merge pull request #20 from Daddoon/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Daddoon authored Sep 11, 2019
2 parents 3c040f3 + 5fc18e2 commit 6b11e81
Show file tree
Hide file tree
Showing 8 changed files with 2,057 additions and 1,934 deletions.
1,922 changes: 961 additions & 961 deletions Test.Forms.Android/Resources/Resource.designer.cs

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions Test.Forms/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ public MainPage()
{
//Test for basic file download with download manager with no auth
Source = "https://www.google.fr"

//Test for WASM support
//Source = "https://lupblazorpaint.z20.web.core.windows.net/"
};
geckoForms.HorizontalOptions = LayoutOptions.FillAndExpand;
geckoForms.VerticalOptions = LayoutOptions.FillAndExpand;

geckoForms.Navigating += GeckoForms_Navigating;

stackLayout.Children.Add(geckoForms);

Task.Run(async () =>
Expand All @@ -35,5 +34,17 @@ public MainPage()
});
});
}

private void GeckoForms_Navigating(object sender, WebNavigatingEventArgs e)
{
if (e.Url.Contains("google.fr"))
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
}
}
65 changes: 60 additions & 5 deletions Xam.Android.GeckoView.Forms.Android/Handlers/NavigationDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Org.Mozilla.Gecko;
using Org.Mozilla.Geckoview;
using Xam.Droid.GeckoView.Forms.Droid.Renderers;
using Xamarin.Forms;
using static Org.Mozilla.Geckoview.GeckoSession;

namespace Xam.Droid.GeckoView.Forms.Droid.Handlers
Expand All @@ -16,27 +17,81 @@ public NavigationDelegate(GeckoViewRenderer renderer)

public virtual void OnCanGoBack(GeckoSession session, bool canGoBack)
{

_renderer.UpdateCanGoBackForward(canGoBack, null);
}

public virtual void OnCanGoForward(GeckoSession session, bool canGoForward)
{

_renderer.UpdateCanGoBackForward(null, canGoForward);
}

public virtual GeckoResult OnLoadError(GeckoSession session, string uri, WebRequestError error)
{
return GeckoResult.FromValue(null);
NavigatedEvent(session, uri, true);
return null;
}

public virtual GeckoResult OnLoadRequest(GeckoSession session, avigationDelegateClassLoadRequest request)
{
return GeckoResult.FromValue(null);
//Can assume result precisely here compared to Xamarin.Forms API
var navEvent = WebNavigationEvent.NewPage;

if (request.IsRedirect)
{
navEvent = WebNavigationEvent.NewPage;
}
else if (request.Uri.Equals(request.TriggerUri, System.StringComparison.OrdinalIgnoreCase))
{
navEvent = WebNavigationEvent.Refresh;
}

WebViewSource source;
if (_renderer.Element != null && _renderer.Element.Source != null)
{
source = _renderer.Element.Source;
}
else
{
source = new UrlWebViewSource() { Url = request.Uri };
}

var args = new WebNavigatingEventArgs(navEvent, source, request.Uri);
_renderer.ForwardSendNavigating(args);

return args.Cancel ? GeckoResult.Deny : GeckoResult.Allow;
}

public virtual void OnLocationChange(GeckoSession session, string url)
private void NavigatedEvent(GeckoSession session, string url, bool isError)
{
WebViewSource source;
if (_renderer.Element != null && _renderer.Element.Source != null)
{
source = _renderer.Element.Source;
}
else
{
source = new UrlWebViewSource() { Url = url };
}

WebNavigationResult navigationResult;

if (isError)
{
navigationResult = WebNavigationResult.Failure;
}
else
{
navigationResult = WebNavigationResult.Success;
}

var args = new WebNavigatedEventArgs(WebNavigationEvent.NewPage, source, url, navigationResult);

_renderer.ForwardSendNavigated(args);
}

public virtual void OnLocationChange(GeckoSession session, string url)
{
NavigatedEvent(session, url, false);
}

public virtual GeckoResult OnNewSession(GeckoSession session, string uri)
Expand Down
55 changes: 53 additions & 2 deletions Xam.Android.GeckoView.Forms.Android/Renderers/GeckoViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ public GeckoViewRenderer(Context context) : base(context)
AutoPackage = false;
}

/// <summary>
/// Update the CanGoBack and CanGoForward. This must be called from a GeckoView Navigation delegate
/// If the boolean value is null, the specified variable will not be updated
/// </summary>
/// <param name="canGoBack"></param>
/// <param name="canGoForward"></param>
internal void UpdateCanGoBackForward(bool? canGoBack, bool? canGoForward)
{
if (Element == null)
{
return;
}

if (canGoBack != null)
{
((IWebViewController)Element).CanGoBack = (bool)canGoBack;
}

if (canGoForward != null)
{
((IWebViewController)Element).CanGoForward = (bool)canGoForward;
}
}

Org.Mozilla.Geckoview.GeckoView _view;

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -76,6 +100,7 @@ public virtual Tuple<GeckoSession, GeckoRuntime> CreateNewSession()
_session.Open(_runtime);
_session.ProgressDelegate = new ProgressDelegate(this);
_session.ContentDelegate = new ContentDelegate(this);
_session.NavigationDelegate = new NavigationDelegate(this);

return Tuple.Create(_session, _runtime);
}
Expand Down Expand Up @@ -148,12 +173,28 @@ protected virtual void OnReloadRequested(object sender, EventArgs e)

protected virtual void OnGoForwardRequested(object sender, EventArgs e)
{
_view?.Session.GoForward();
if (Element == null)
{
return;
}

if (Element.CanGoForward)
{
_view?.Session.GoForward();
}
}

protected virtual void OnGoBackRequested(object sender, EventArgs e)
{
_view?.Session.GoBack();
if (Element == null)
{
return;
}

if (Element.CanGoBack)
{
_view?.Session.GoBack();
}
}

protected virtual Task<string> OnEvaluateJavaScriptRequested(string script)
Expand All @@ -166,6 +207,16 @@ protected virtual void OnEvalRequested(object sender, EvalRequested e)
throw new NotImplementedException($"{nameof(OnEvalRequested)}: Javascript evaluation is not yet supported on GeckoView");
}

internal void ForwardSendNavigating(WebNavigatingEventArgs args)
{
Element.SendNavigating(args);
}

internal void ForwardSendNavigated(WebNavigatedEventArgs args)
{
Element.SendNavigated(args);
}

protected virtual void Load()
{
WebViewSource source = Element.Source;
Expand Down
Loading

0 comments on commit 6b11e81

Please sign in to comment.