Browse Source

Merge pull request #8912 from AvaloniaUI/fixes/wasm-tab-key-handling

WASM: fix tab key handling
pull/8914/head
Max Katz 4 years ago
committed by GitHub
parent
commit
6fb4be9f11
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/Web/Avalonia.Web.Blazor/AvaloniaView.razor
  2. 13
      src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs
  3. 4
      src/Web/Avalonia.Web.Blazor/Interop/InputHelperInterop.cs
  4. 13
      src/Web/Avalonia.Web.Blazor/webapp/modules/Avalonia/InputHelper.ts

8
src/Web/Avalonia.Web.Blazor/AvaloniaView.razor

@ -1,7 +1,11 @@
<div id="container" class="avalonia-container" tabindex="0" oncontextmenu="return false;" <div id="container" @ref="_containerElement"
class="avalonia-container"
tabindex="0" oncontextmenu="return false;"
@onwheel="OnWheel" @onwheel="OnWheel"
@onkeydown="OnKeyDown" @onkeydown="OnKeyDown"
@onkeydown:preventDefault="true"
@onkeyup="OnKeyUp" @onkeyup="OnKeyUp"
@onkeyup:preventDefault="true"
@onpointerdown="OnPointerDown" @onpointerdown="OnPointerDown"
@onpointerup="OnPointerUp" @onpointerup="OnPointerUp"
@onpointermove="OnPointerMove" @onpointermove="OnPointerMove"
@ -16,6 +20,8 @@
onpaste="return false;" onpaste="return false;"
oncopy="return false;" oncopy="return false;"
oncut="return false;" oncut="return false;"
@onkeydown:preventDefault="true"
@onkeyup:preventDefault="true"
autocapitalize="none"/> autocapitalize="none"/>
</div> </div>

13
src/Web/Avalonia.Web.Blazor/AvaloniaView.razor.cs

@ -32,10 +32,12 @@ namespace Avalonia.Web.Blazor
private AvaloniaModule? _avaloniaModule = null; private AvaloniaModule? _avaloniaModule = null;
private InputHelperInterop? _inputHelper = null; private InputHelperInterop? _inputHelper = null;
private InputHelperInterop? _canvasHelper = null; private InputHelperInterop? _canvasHelper = null;
private InputHelperInterop? _containerHelper = null;
private NativeControlHostInterop? _nativeControlHost = null; private NativeControlHostInterop? _nativeControlHost = null;
private StorageProviderInterop? _storageProvider = null; private StorageProviderInterop? _storageProvider = null;
private ElementReference _htmlCanvas; private ElementReference _htmlCanvas;
private ElementReference _inputElement; private ElementReference _inputElement;
private ElementReference _containerElement;
private ElementReference _nativeControlsContainer; private ElementReference _nativeControlsContainer;
private double _dpi = 1; private double _dpi = 1;
private SKSize _canvasSize = new (100, 100); private SKSize _canvasSize = new (100, 100);
@ -247,8 +249,9 @@ namespace Avalonia.Web.Blazor
_inputHelper = new InputHelperInterop(_avaloniaModule, _inputElement); _inputHelper = new InputHelperInterop(_avaloniaModule, _inputElement);
_canvasHelper = new InputHelperInterop(_avaloniaModule, _htmlCanvas); _canvasHelper = new InputHelperInterop(_avaloniaModule, _htmlCanvas);
_containerHelper = new InputHelperInterop(_avaloniaModule, _containerElement);
_inputHelper.Hide(); HideIme();
_canvasHelper.SetCursor("default"); _canvasHelper.SetCursor("default");
_topLevelImpl.SetCssCursor = x => _topLevelImpl.SetCssCursor = x =>
{ {
@ -387,6 +390,12 @@ namespace Avalonia.Web.Blazor
} }
} }
private void HideIme()
{
_inputHelper?.Hide();
_containerHelper?.Focus();
}
public void SetClient(ITextInputMethodClient? client) public void SetClient(ITextInputMethodClient? client)
{ {
if (_inputHelper is null) if (_inputHelper is null)
@ -407,7 +416,7 @@ namespace Avalonia.Web.Blazor
else else
{ {
_inputElementFocused = false; _inputElementFocused = false;
_inputHelper.Hide(); HideIme();
} }
} }

4
src/Web/Avalonia.Web.Blazor/Interop/InputHelperInterop.cs

@ -13,10 +13,10 @@ namespace Avalonia.Web.Blazor.Interop
private readonly AvaloniaModule _module; private readonly AvaloniaModule _module;
private readonly ElementReference _inputElement; private readonly ElementReference _inputElement;
public InputHelperInterop(AvaloniaModule module, ElementReference element) public InputHelperInterop(AvaloniaModule module, ElementReference inputElement)
{ {
_module = module; _module = module;
_inputElement = element; _inputElement = inputElement;
} }
public void Clear() => _module.Invoke(ClearSymbol, _inputElement); public void Clear() => _module.Invoke(ClearSymbol, _inputElement);

13
src/Web/Avalonia.Web.Blazor/webapp/modules/Avalonia/InputHelper.ts

@ -3,9 +3,18 @@
inputElement.value = ""; inputElement.value = "";
} }
public static focus(inputElement: HTMLInputElement) {
public static isInputElement( element : HTMLInputElement | HTMLElement ) : element is HTMLInputElement {
return ( element as HTMLInputElement).setSelectionRange !== undefined;
}
public static focus(inputElement: HTMLElement) {
inputElement.focus(); inputElement.focus();
inputElement.setSelectionRange(0, 0);
if(this.isInputElement(inputElement))
{
(inputElement as HTMLInputElement).setSelectionRange(0,0);
}
} }
public static setCursor(inputElement: HTMLInputElement, kind: string) { public static setCursor(inputElement: HTMLInputElement, kind: string) {

Loading…
Cancel
Save