Skip to content

Commit 9e49ee1

Browse files
authored
Use KeyInterceptorService (#475)
* Use KeyInterceptorService * Fix
1 parent d55fec3 commit 9e49ee1

File tree

8 files changed

+52
-52
lines changed

8 files changed

+52
-52
lines changed

CodeBeam.MudBlazor.Extensions.Docs/CodeBeam.MudBlazor.Extensions.Docs.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<ItemGroup>
1616
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.*" />
17-
<PackageReference Include="MudBlazor" Version="7.3.0" />
17+
<PackageReference Include="MudBlazor" Version="7.12.1" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

CodeBeam.MudBlazor.Extensions.UnitTests/Mocks/MockKeyInterceptor.cs

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using MudBlazor.Services;
1+
using Microsoft.AspNetCore.Components.Web;
2+
using MudBlazor;
3+
using MudBlazor.Services;
24

35
namespace MudExtensions.UnitTests.Mocks
46
{
@@ -21,7 +23,7 @@ public MockKeyInterceptorServiceFactory()
2123
public IKeyInterceptor Create() => _interceptorService ?? new MockKeyInterceptorService();
2224
}
2325

24-
public class MockKeyInterceptorService : IKeyInterceptor
26+
public class MockKeyInterceptorService : IKeyInterceptor, IKeyInterceptorService
2527
{
2628
public void Dispose()
2729
{
@@ -45,5 +47,23 @@ public Task UpdateKey(KeyOptions option)
4547

4648
public event KeyboardEvent? KeyDown;
4749
public event KeyboardEvent? KeyUp;
50+
51+
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
52+
53+
public Task SubscribeAsync(IKeyInterceptorObserver observer, KeyInterceptorOptions options) => Task.CompletedTask;
54+
55+
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, IKeyDownObserver? keyDown = null, IKeyUpObserver? keyUp = null) => Task.CompletedTask;
56+
57+
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, Action<KeyboardEventArgs>? keyDown = null, Action<KeyboardEventArgs>? keyUp = null) => Task.CompletedTask;
58+
59+
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, Func<KeyboardEventArgs, Task>? keyDown = null, Func<KeyboardEventArgs, Task>? keyUp = null) => Task.CompletedTask;
60+
61+
public Task UpdateKeyAsync(IKeyInterceptorObserver observer, KeyOptions option) => Task.CompletedTask;
62+
63+
public Task UpdateKeyAsync(string elementId, KeyOptions option) => Task.CompletedTask;
64+
65+
public Task UnsubscribeAsync(IKeyInterceptorObserver observer) => Task.CompletedTask;
66+
67+
public Task UnsubscribeAsync(string elementId) => Task.CompletedTask;
4868
}
4969
}

CodeBeam.MudBlazor.Extensions/CodeBeam.MudBlazor.Extensions.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<ItemGroup>
3333
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
3434
<PackageReference Include="CsvHelper" Version="31.0.3" />
35-
<PackageReference Include="MudBlazor" Version="7.3.0" />
35+
<PackageReference Include="MudBlazor" Version="7.12.1" />
3636
<PackageReference Include="ZXing.Net" Version="0.16.9" />
3737
</ItemGroup>
3838

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@using MudExtensions.Services
55
@typeparam T
66
@inherits MudBaseInputExtended<T>
7-
@inject IKeyInterceptorFactory KeyInterceptorFactory
7+
@inject IKeyInterceptorService KeyInterceptorService
88
@inject IScrollManagerExtended ScrollManagerExtended
99

1010
<CascadingValue Name="Standalone" Value="false" IsFixed="true">

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs

+8-19
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ protected internal void SetSearchString(T value)
3636

3737
internal string? _searchString { get; set; }
3838
private readonly string? multiSelectionText;
39-
private IKeyInterceptor? _keyInterceptor;
4039
static readonly KeyInterceptorOptions _keyInterceptorOptions = new()
4140
{
42-
//EnableLogging = true,
41+
EnableLogging = true,
4342
TargetClass = "mud-input-control",
4443
Keys =
4544
{
@@ -850,10 +849,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
850849

851850
if (firstRender)
852851
{
853-
_keyInterceptor = KeyInterceptorFactory.Create();
854-
await _keyInterceptor.Connect(_elementId, _keyInterceptorOptions);
855-
_keyInterceptor.KeyDown += HandleKeyDown;
856-
_keyInterceptor.KeyUp += HandleKeyUp;
852+
// TODO: Use Task for HandleKeyDown / HandleKeyDown
853+
await KeyInterceptorService.SubscribeAsync(_elementId, _keyInterceptorOptions, keyDown: HandleKeyDown, keyUp: HandleKeyUp);
857854
await UpdateDataVisualiserTextAsync();
858855
_firstRendered = true;
859856
StateHasChanged();
@@ -872,12 +869,10 @@ protected override void Dispose(bool disposing)
872869

873870
if (disposing)
874871
{
875-
if (_keyInterceptor != null)
872+
if (IsJSRuntimeAvailable)
876873
{
877-
_keyInterceptor.KeyDown -= HandleKeyDown;
878-
_keyInterceptor.KeyUp -= HandleKeyUp;
879-
_keyInterceptor.Dispose();
880-
_keyInterceptor = null;
874+
// TODO: Switch to IAsyncDisposable
875+
KeyInterceptorService.UnsubscribeAsync(_elementId).CatchAndLog();
881876
}
882877

883878
Items.Clear();
@@ -1167,10 +1162,7 @@ public async Task OpenMenu()
11671162
UpdateIcon();
11681163

11691164
// Disable escape propagation: if ComboBox menu is open, only the ComboBox popover should close and underlying components should not handle escape key.
1170-
if (_keyInterceptor != null)
1171-
{
1172-
await _keyInterceptor.UpdateKey(new() { Key = "Escape", StopDown = "Key+none" });
1173-
}
1165+
await KeyInterceptorService.UpdateKeyAsync(_elementId, new KeyOptions("Escape", stopDown: "key+none"));
11741166

11751167
_allSelected = GetAllSelectedState();
11761168

@@ -1211,10 +1203,7 @@ public async Task CloseMenu()
12111203
_searchString = null;
12121204

12131205
// Enable escape propagation: The ComboBox popover was closed, no underlying components are allowed to handle escape key.
1214-
if (_keyInterceptor != null)
1215-
{
1216-
await _keyInterceptor.UpdateKey(new() { Key = "Escape", StopDown = "none" });
1217-
}
1206+
await KeyInterceptorService.UpdateKeyAsync(_elementId, new KeyOptions("Escape", stopDown: "none"));
12181207

12191208
await OnClose.InvokeAsync();
12201209
}

CodeBeam.MudBlazor.Extensions/Components/ListExtended/MudListExtended.razor.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class MudListExtended<T> : MudComponentBase, IDisposable
1616
{
1717
#region Parameters, Fields, Injected Services
1818

19-
[Inject] IKeyInterceptorFactory? KeyInterceptorFactory { get; set; }
19+
[Inject] private IKeyInterceptorService KeyInterceptorService { get; set; } = null!;
2020
[Inject] IScrollManagerExtended? ScrollManagerExtended { get; set; }
2121

2222
// Fields used in more than one place (or protected and internal ones) are shown here.
@@ -757,9 +757,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
757757
if (firstRender)
758758
{
759759
_firstRendered = false;
760-
_keyInterceptor = KeyInterceptorFactory?.Create();
761760

762-
await _keyInterceptor.Connect(_elementId, new KeyInterceptorOptions()
761+
await KeyInterceptorService.SubscribeAsync(_elementId, new KeyInterceptorOptions
763762
{
764763
//EnableLogging = true,
765764
TargetClass = "mud-list-item-extended",
@@ -776,7 +775,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
776775
new KeyOptions { Key="A", PreventDown = "key+ctrl" }, // select all items instead of all page text
777776
new KeyOptions { Key="/./", SubscribeDown = true, SubscribeUp = true }, // for our users
778777
},
779-
});
778+
}, KeyObserver.KeyDownIgnore(), KeyObserver.KeyUpIgnore());
780779

781780
if (MudSelectExtended == null && MudAutocomplete == null)
782781
{

CodeBeam.MudBlazor.Extensions/Components/SelectExtended/MudSelectExtended.razor.cs

+9-13
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ public MudSelectExtended()
2525
IconSize = Size.Medium;
2626
}
2727

28-
[Inject] private IKeyInterceptorFactory? KeyInterceptorFactory { get; set; }
28+
[Inject] private IKeyInterceptorService KeyInterceptorService { get; set; } = null!;
2929

3030
private MudListExtended<T?>? _list;
3131
private bool _dense;
3232
private string? multiSelectionText;
33-
private IKeyInterceptor? _keyInterceptor;
3433
/// <summary>
3534
/// The collection of items within this select
3635
/// </summary>
@@ -776,8 +775,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
776775

777776
if (firstRender)
778777
{
779-
_keyInterceptor = KeyInterceptorFactory?.Create();
780-
await _keyInterceptor.Connect(_elementId, new KeyInterceptorOptions()
778+
// TODO: Make HandleKeyDown / HandleKeyUp async Task
779+
await KeyInterceptorService.SubscribeAsync(_elementId, new KeyInterceptorOptions()
781780
{
782781
//EnableLogging = true,
783782
TargetClass = "mud-input-control",
@@ -794,9 +793,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
794793
new KeyOptions { Key="A", PreventDown = "key+ctrl" }, // select all items instead of all page text
795794
new KeyOptions { Key="/./", SubscribeDown = true, SubscribeUp = true }, // for our users
796795
},
797-
});
798-
_keyInterceptor.KeyDown += HandleKeyDown;
799-
_keyInterceptor.KeyUp += HandleKeyUp;
796+
}, keyDown: HandleKeyDown, keyUp: HandleKeyUp);
800797
await UpdateTextPropertyAsync(false);
801798
_list?.ForceUpdateItems();
802799
SelectedListItem = Items.FirstOrDefault(x => x.Value != null && Value != null && x.Value.Equals(Value))?.ListItem;
@@ -824,12 +821,11 @@ protected override void Dispose(bool disposing)
824821

825822
if (disposing)
826823
{
827-
if (_keyInterceptor != null)
824+
if (IsJSRuntimeAvailable)
828825
{
829-
_keyInterceptor.KeyDown -= HandleKeyDown;
830-
_keyInterceptor.KeyUp -= HandleKeyUp;
826+
//TODO: Use IAsyncDisposable instead.
827+
KeyInterceptorService.UnsubscribeAsync(_elementId).CatchAndLog();
831828
}
832-
_keyInterceptor?.Dispose();
833829
}
834830
}
835831

@@ -1012,7 +1008,7 @@ public async Task OpenMenu()
10121008
StateHasChanged();
10131009

10141010
//disable escape propagation: if selectmenu is open, only the select popover should close and underlying components should not handle escape key
1015-
await _keyInterceptor.UpdateKey(new() { Key = "Escape", StopDown = "Key+none" });
1011+
await KeyInterceptorService.UpdateKeyAsync(_elementId, new() { Key = "Escape", StopDown = "Key+none" });
10161012
await OnOpen.InvokeAsync();
10171013
}
10181014

@@ -1034,7 +1030,7 @@ public async Task CloseMenu()
10341030
//}
10351031

10361032
//enable escape propagation: the select popover was closed, now underlying components are allowed to handle escape key
1037-
await _keyInterceptor.UpdateKey(new() { Key = "Escape", StopDown = "none" });
1033+
await KeyInterceptorService.UpdateKeyAsync(_elementId, new() { Key = "Escape", StopDown = "none" });
10381034

10391035
await OnClose.InvokeAsync();
10401036
}

CodeBeam.MudBlazor.Extensions/Components/SwitchM3/MudSwitchM3.razor.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public partial class MudSwitchM3<T> : MudBooleanInput<T>
6464
.AddStyle("color", "var(--mud-palette-background)", !string.IsNullOrEmpty(ThumbOffIcon))
6565
.Build();
6666

67-
private IKeyInterceptor? _keyInterceptor;
68-
[Inject] private IKeyInterceptorFactory? KeyInterceptorFactory { get; set; }
67+
[Inject] private IKeyInterceptorService KeyInterceptorService { get; set; } = null!;
6968

7069
/// <summary>
7170
/// The color of the component. It supports the theme colors.
@@ -163,9 +162,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
163162
{
164163
if (firstRender)
165164
{
166-
_keyInterceptor = KeyInterceptorFactory?.Create();
167-
168-
await _keyInterceptor.Connect(_elementId, new KeyInterceptorOptions()
165+
// TODO: Make HandleKeyDown async Task
166+
await KeyInterceptorService.SubscribeAsync(_elementId, new KeyInterceptorOptions()
169167
{
170168
//EnableLogging = true,
171169
TargetClass = "mud-switch-base-m3",
@@ -174,9 +172,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
174172
new KeyOptions { Key="ArrowDown", PreventDown = "key+none" }, // prevent scrolling page, instead decrement
175173
new KeyOptions { Key=" ", PreventDown = "key+none", PreventUp = "key+none" },
176174
},
177-
});
178-
179-
_keyInterceptor.KeyDown += HandleKeyDown;
175+
}, keyDown: HandleKeyDown);
180176
}
181177
await base.OnAfterRenderAsync(firstRender);
182178
}
@@ -191,10 +187,10 @@ protected override void Dispose(bool disposing)
191187

192188
if (disposing)
193189
{
194-
if(_keyInterceptor != null)
190+
if (IsJSRuntimeAvailable)
195191
{
196-
_keyInterceptor.KeyDown -= HandleKeyDown;
197-
_keyInterceptor.Dispose();
192+
//TODO: Use IAsyncDisposable
193+
KeyInterceptorService.UnsubscribeAsync(_elementId).CatchAndLog();
198194
}
199195
}
200196
}

0 commit comments

Comments
 (0)