Browse Source

add browser support

pull/9856/head
Emmanuel Hansen 4 years ago
parent
commit
ac206db098
  1. 5
      src/Avalonia.Base/Input/Platform/PlatformHotkeyConfiguration.cs
  2. 20
      src/Avalonia.Controls/TopLevel.cs
  3. 24
      src/Browser/Avalonia.Browser/BrowserSystemNavigationManager.cs
  4. 5
      src/Browser/Avalonia.Browser/BrowserTopLevelImpl.cs
  5. 10
      src/Browser/Avalonia.Browser/Interop/NavigationHelper.cs
  6. 5
      src/Browser/Avalonia.Browser/webapp/modules/avalonia.ts
  7. 14
      src/Browser/Avalonia.Browser/webapp/modules/avalonia/navigationHelper.ts

5
src/Avalonia.Base/Input/Platform/PlatformHotkeyConfiguration.cs

@ -81,6 +81,10 @@ namespace Avalonia.Input.Platform
{ {
new KeyGesture(Key.Apps) new KeyGesture(Key.Apps)
}; };
Back = new List<KeyGesture>
{
new KeyGesture(Key.Left, KeyModifiers.Alt)
};
} }
public KeyModifiers CommandModifiers { get; set; } public KeyModifiers CommandModifiers { get; set; }
@ -101,5 +105,6 @@ namespace Avalonia.Input.Platform
public List<KeyGesture> MoveCursorToTheStartOfDocumentWithSelection { get; set; } public List<KeyGesture> MoveCursorToTheStartOfDocumentWithSelection { get; set; }
public List<KeyGesture> MoveCursorToTheEndOfDocumentWithSelection { get; set; } public List<KeyGesture> MoveCursorToTheEndOfDocumentWithSelection { get; set; }
public List<KeyGesture> OpenContextMenu { get; set; } public List<KeyGesture> OpenContextMenu { get; set; }
public List<KeyGesture> Back { get; set; }
} }
} }

20
src/Avalonia.Controls/TopLevel.cs

@ -18,6 +18,8 @@ using Avalonia.Rendering;
using Avalonia.Styling; using Avalonia.Styling;
using Avalonia.Utilities; using Avalonia.Utilities;
using Avalonia.VisualTree; using Avalonia.VisualTree;
using Avalonia.Input.Platform;
using System.Linq;
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
@ -223,20 +225,24 @@ namespace Avalonia.Controls
}; };
} }
var backKeyGesture = new KeyGesture(Key.Left, KeyModifiers.Alt); _backGestureSubscription = _inputManager?.PreProcess.Subscribe(e =>
_backGestureSubscription = _inputManager?.PreProcess.OfType<RawInputEventArgs>().Subscribe(e =>
{ {
bool backRequested = false; bool backRequested = false;
if (e is RawKeyEventArgs rawKeyEventArgs && rawKeyEventArgs.Type == RawKeyEventType.KeyDown) if (e is RawKeyEventArgs rawKeyEventArgs && rawKeyEventArgs.Type == RawKeyEventType.KeyDown)
{ {
var keyEvent = new KeyEventArgs() var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>()?.Back;
if (keymap != null)
{ {
KeyModifiers = (KeyModifiers)rawKeyEventArgs.Modifiers, var keyEvent = new KeyEventArgs()
Key = rawKeyEventArgs.Key {
}; KeyModifiers = (KeyModifiers)rawKeyEventArgs.Modifiers,
Key = rawKeyEventArgs.Key
};
backRequested = backKeyGesture.Matches(keyEvent); backRequested = keymap.Any( key => key.Matches(keyEvent));
}
} }
else if(e is RawPointerEventArgs pointerEventArgs) else if(e is RawPointerEventArgs pointerEventArgs)
{ {

24
src/Browser/Avalonia.Browser/BrowserSystemNavigationManager.cs

@ -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;
});
}
}
}

5
src/Browser/Avalonia.Browser/BrowserTopLevelImpl.cs

@ -19,7 +19,8 @@ using Avalonia.Rendering.Composition;
namespace Avalonia.Browser namespace Avalonia.Browser
{ {
internal class BrowserTopLevelImpl : ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost, ITopLevelImplWithStorageProvider internal class BrowserTopLevelImpl : ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost, ITopLevelImplWithStorageProvider,
ITopLevelWithSystemNavigationManager
{ {
private Size _clientSize; private Size _clientSize;
private IInputRoot? _inputRoot; private IInputRoot? _inputRoot;
@ -234,5 +235,7 @@ namespace Avalonia.Browser
public INativeControlHostImpl? NativeControlHost { get; } public INativeControlHostImpl? NativeControlHost { get; }
public IStorageProvider StorageProvider { get; } = new BrowserStorageProvider(); public IStorageProvider StorageProvider { get; } = new BrowserStorageProvider();
public ISystemNavigationManager SystemNavigationManager { get; } = new BrowserSystemNavigationManager();
} }
} }

10
src/Browser/Avalonia.Browser/Interop/NavigationHelper.cs

@ -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);
}

5
src/Browser/Avalonia.Browser/webapp/modules/avalonia.ts

@ -5,6 +5,7 @@ import { AvaloniaDOM } from "./avalonia/dom";
import { Caniuse } from "./avalonia/caniuse"; import { Caniuse } from "./avalonia/caniuse";
import { StreamHelper } from "./avalonia/stream"; import { StreamHelper } from "./avalonia/stream";
import { NativeControlHost } from "./avalonia/nativeControlHost"; import { NativeControlHost } from "./avalonia/nativeControlHost";
import { NavigationHelper } from "./avalonia/navigationHelper";
async function registerAvaloniaModule(api: RuntimeAPI): Promise<void> { async function registerAvaloniaModule(api: RuntimeAPI): Promise<void> {
api.setModuleImports("avalonia", { api.setModuleImports("avalonia", {
@ -15,7 +16,8 @@ async function registerAvaloniaModule(api: RuntimeAPI): Promise<void> {
DpiWatcher, DpiWatcher,
AvaloniaDOM, AvaloniaDOM,
StreamHelper, StreamHelper,
NativeControlHost NativeControlHost,
NavigationHelper
}); });
} }
export { export {
@ -27,6 +29,7 @@ export {
AvaloniaDOM, AvaloniaDOM,
StreamHelper, StreamHelper,
NativeControlHost, NativeControlHost,
NavigationHelper,
registerAvaloniaModule registerAvaloniaModule
}; };

14
src/Browser/Avalonia.Browser/webapp/modules/avalonia/navigationHelper.ts

@ -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…
Cancel
Save