Ensure keyboard events can be scoped to dialogs only.

This commit is contained in:
ahjephson
2024-08-17 15:14:11 +01:00
parent f3aec1cc0b
commit 1dba943927
9 changed files with 107 additions and 28 deletions

View File

@@ -1,11 +1,21 @@
using Lantean.QBTMudBlade.Models;
using Lantean.QBTMudBlade.Services;
using Microsoft.AspNetCore.Components;
using MudBlazor;
namespace Lantean.QBTMudBlade.Components.Dialogs
{
public partial class AddTorrentLinkDialog
public partial class AddTorrentLinkDialog : IAsyncDisposable
{
private bool _disposedValue;
private readonly KeyboardEvent _ctrlEnterKey = new KeyboardEvent("Enter")
{
CtrlKey = true,
};
[Inject]
protected IKeyboardService KeyboardService { get; set; } = default!;
[CascadingParameter]
public MudDialogInstance MudDialog { get; set; } = default!;
@@ -15,6 +25,19 @@ namespace Lantean.QBTMudBlade.Components.Dialogs
protected AddTorrentOptions TorrentOptions { get; set; } = default!;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await KeyboardService.RegisterKeypressEvent(_ctrlEnterKey, k =>
{
Submit();
return Task.CompletedTask;
});
await KeyboardService.Focus();
}
}
protected void Cancel()
{
MudDialog.Cancel();
@@ -31,19 +54,25 @@ namespace Lantean.QBTMudBlade.Components.Dialogs
MudDialog.Close(DialogResult.Ok(options));
}
protected override Task Submit(KeyboardEvent keyboardEvent)
protected virtual async ValueTask DisposeAsync(bool disposing)
{
Submit();
if (!_disposedValue)
{
if (disposing)
{
await KeyboardService.UnregisterKeypressEvent(_ctrlEnterKey);
await KeyboardService.UnFocus();
}
return Task.CompletedTask;
_disposedValue = true;
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
public async ValueTask DisposeAsync()
{
if (firstRender && UrlsTextField is not null)
{
await UrlsTextField.FocusAsync();
}
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
await DisposeAsync(disposing: true);
GC.SuppressFinalize(this);
}
}
}