Skip to content

Commit c8fbe60

Browse files
committed
快捷键设置模块提取为组件
1 parent 5c7306e commit c8fbe60

File tree

4 files changed

+76
-33
lines changed

4 files changed

+76
-33
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<MudPaper Class="d-flex align-center" Elevation="0">
2+
<MudText Typo="Typo.button">@(Title)</MudText>
3+
<MudButton Class="ml-3"
4+
Size="Size.Small"
5+
Color="Color.Primary"
6+
OnClick="SetShortcutKey">
7+
@(_lockHotkeyDisplay)
8+
</MudButton>
9+
@if (Hotkey.IsNotEmpty())
10+
{
11+
<MudTooltip Text="@(Lang["DeleteShortcutKeys"])" Delay="600">
12+
<MudIconButton Icon="@Icons.Material.Filled.Delete"
13+
Size="Size.Small"
14+
OnClick="ClearShortcutKey" />
15+
</MudTooltip>
16+
}
17+
</MudPaper>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace ComputerLock.Components;
2+
public partial class HotkeyInput
3+
{
4+
private string _lockHotkeyDisplay => HotkeyTools.StringKeyToDisplay(Hotkey) ?? "";
5+
6+
[Parameter]
7+
public string Title { get; set; } = null!;
8+
9+
[Parameter]
10+
public string Hotkey { get; set; } = null!;
11+
12+
[Parameter]
13+
public EventCallback<string> OnHotkeySet { get; set; }
14+
15+
[Parameter]
16+
public EventCallback OnHotkeyClear { get; set; }
17+
18+
19+
[Inject]
20+
private IStringLocalizer<Lang> Lang { get; set; } = null!;
21+
22+
[Inject]
23+
private IDialogService Dialog { get; set; } = null!;
24+
25+
[Inject]
26+
private HotkeyTools HotkeyTools { get; set; } = null!;
27+
28+
29+
private async Task SetShortcutKey()
30+
{
31+
var noHeader = new DialogOptions()
32+
{
33+
NoHeader = true,
34+
BackgroundClass = "dialog-blurry",
35+
CloseOnEscapeKey = false,
36+
};
37+
var dialog = await Dialog.ShowAsync<HotkeySetting>("", noHeader);
38+
var result = await dialog.Result;
39+
if (result!.Canceled)
40+
{
41+
return;
42+
}
43+
44+
await OnHotkeySet.InvokeAsync(result.Data!.ToString());
45+
}
46+
47+
private async Task ClearShortcutKey()
48+
{
49+
await OnHotkeyClear.InvokeAsync();
50+
}
51+
}

src/ComputerLock/Pages/Index.razor

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,10 @@ else
4545
ValueChanged="AutoLockChanged">
4646
</MudNumericField>
4747

48-
<MudPaper Class="d-flex align-center" Elevation="0">
49-
<MudText Typo="Typo.button">@(Lang["LockHotkey"])</MudText>
50-
<MudButton Class="ml-3"
51-
Size="Size.Small"
52-
Color="Color.Primary"
53-
OnClick="SetShortcutKey">
54-
@(AppSettings.LockHotkeyDisplay)
55-
</MudButton>
56-
@if (AppSettings.ShortcutKeyForLock.IsNotEmpty())
57-
{
58-
<MudTooltip Text="@(Lang["DeleteShortcutKeys"])" Delay="600">
59-
<MudIconButton Icon="@Icons.Material.Filled.Delete"
60-
Size="Size.Small"
61-
OnClick="ClearShortcutKey" />
62-
</MudTooltip>
63-
}
64-
</MudPaper>
48+
<HotkeyInput Title="@(Lang["LockHotkey"])"
49+
Hotkey="@(AppSettings.ShortcutKeyForLock)"
50+
OnHotkeySet="SetHotkey"
51+
OnHotkeyClear="ClearHotkey" />
6552

6653
<MudToggleGroup @bind-Value="@(AppSettings.ScreenUnlockMethod)"
6754
@bind-Value:after="SaveSettings"

src/ComputerLock/Pages/Index.razor.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,14 @@ private void SaveSettings()
150150
{
151151
AppSettingsProvider.SaveSettings(AppSettings);
152152
}
153-
private async Task SetShortcutKey()
153+
private Task SetHotkey(string hotkey)
154154
{
155-
var noHeader = new DialogOptions()
156-
{
157-
NoHeader = true,
158-
BackgroundClass = "dialog-blurry",
159-
CloseOnEscapeKey = false,
160-
};
161-
var dialog = await Dialog.ShowAsync<HotkeySetting>("", noHeader);
162-
var result = await dialog.Result;
163-
if (result!.Canceled)
164-
{
165-
return;
166-
}
167-
168-
AppSettings.ShortcutKeyForLock = result.Data!.ToString()!;
155+
AppSettings.ShortcutKeyForLock = hotkey;
169156
SaveSettings();
170157
RegisterHotkey();
158+
return Task.CompletedTask;
171159
}
172-
private Task ClearShortcutKey()
160+
private Task ClearHotkey()
173161
{
174162
AppSettings.ShortcutKeyForLock = "";
175163
SaveSettings();

0 commit comments

Comments
 (0)