7 changed files with 74 additions and 9 deletions
@ -0,0 +1,24 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Browser.Interop; |
||||
|
using Avalonia.Interactivity; |
||||
|
using Avalonia.Platform; |
||||
|
|
||||
|
namespace Avalonia.Browser |
||||
|
{ |
||||
|
internal class BrowserSystemNavigationManager : ISystemNavigationManager |
||||
|
{ |
||||
|
public event EventHandler<RoutedEventArgs>? BackRequested; |
||||
|
|
||||
|
public BrowserSystemNavigationManager() |
||||
|
{ |
||||
|
NavigationHelper.AddBackHandler(() => |
||||
|
{ |
||||
|
var routedEventArgs = new RoutedEventArgs(); |
||||
|
|
||||
|
BackRequested?.Invoke(this, routedEventArgs); |
||||
|
|
||||
|
return routedEventArgs.Handled; |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System; |
||||
|
using System.Runtime.InteropServices.JavaScript; |
||||
|
|
||||
|
namespace Avalonia.Browser.Interop; |
||||
|
|
||||
|
internal static partial class NavigationHelper |
||||
|
{ |
||||
|
[JSImport("NavigationHelper.addBackHandler", AvaloniaModule.MainModuleName)] |
||||
|
public static partial void AddBackHandler([JSMarshalAs<JSType.Function<JSType.Boolean>>] Func<bool> backHandlerCallback); |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
export class NavigationHelper { |
||||
|
public static addBackHandler(backHandlerCallback: () => Boolean) { |
||||
|
history.pushState(null, "", window.location.href); |
||||
|
window.onpopstate = () => { |
||||
|
const handled = backHandlerCallback(); |
||||
|
|
||||
|
if (!handled) { |
||||
|
history.back(); |
||||
|
} else { |
||||
|
history.forward(); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue