Skip to content

Commit a1beba1

Browse files
committed
设置自动锁定和密码框位置后实时生效
1 parent 9786fd4 commit a1beba1

File tree

9 files changed

+283
-296
lines changed

9 files changed

+283
-296
lines changed

src/ComputerLock/Interfaces/IGlobalLockService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ public interface IGlobalLockService : IDisposable
88
bool IsLocked { get; }
99
void Lock();
1010
void Unlock();
11+
12+
/// <summary>
13+
/// 更新自动锁定设置
14+
/// </summary>
15+
void UpdateAutoLockSettings();
1116
}

src/ComputerLock/Pages/Index.razor

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@
66
backdrop-filter: blur(10px);
77
}
88
</style>
9-
10-
<MudSwitch @bind-Value="@(AppSettings.LockOnStartup)"
11-
@bind-Value:after="SaveSettings"
12-
Label="@(Lang["LockOnStartup"])"
13-
Color="Color.Primary" />
14-
15-
<MudTooltip Text="@(Lang["DisableWindowsRemark"])" Placement="Placement.Bottom">
16-
<MudSwitch @bind-Value="@(AppSettings.IsDisableWindowsLock)"
9+
<div class="d-flex">
10+
<MudSwitch @bind-Value="@(AppSettings.LockOnStartup)"
1711
@bind-Value:after="SaveSettings"
18-
Label="@(Lang["DisableWindowsLock"])"
12+
Label="@(Lang["LockOnStartup"])"
1913
Color="Color.Primary" />
20-
</MudTooltip>
21-
22-
<MudSwitch @bind-Value="@(AppSettings.IsHideMouseCursor)"
23-
@bind-Value:after="SaveSettings"
24-
Label="@(Lang["HideMouseCursor"])"
25-
Color="Color.Primary" />
26-
27-
<MudSwitch @bind-Value="@(AppSettings.LockAnimation)"
28-
@bind-Value:after="SaveSettings"
29-
Label="@(Lang["LockAnimation"])"
30-
Color="Color.Primary" />
31-
14+
</div>
15+
<div class="d-flex">
16+
<MudTooltip Text="@(Lang["DisableWindowsRemark"])" Placement="Placement.Bottom">
17+
<MudSwitch @bind-Value="@(AppSettings.IsDisableWindowsLock)"
18+
@bind-Value:after="SaveSettings"
19+
Label="@(Lang["DisableWindowsLock"])"
20+
Color="Color.Primary" />
21+
</MudTooltip>
22+
</div>
23+
<div class="d-flex">
24+
<MudSwitch @bind-Value="@(AppSettings.IsHideMouseCursor)"
25+
@bind-Value:after="SaveSettings"
26+
Label="@(Lang["HideMouseCursor"])"
27+
Color="Color.Primary" />
28+
</div>
29+
<div class="d-flex">
30+
<MudSwitch @bind-Value="@(AppSettings.LockAnimation)"
31+
@bind-Value:after="SaveSettings"
32+
Label="@(Lang["LockAnimation"])"
33+
Color="Color.Primary" />
34+
</div>
3235
<MudNumericField T="int"
3336
Label="@(Lang["AutoLock"])"
3437
Value="@(AppSettings.AutoLockMinute)"
@@ -83,28 +86,30 @@
8386
</MudChip>
8487
}
8588
</div>
86-
87-
<MudSwitch @bind-Value="@(AppSettings.IsHidePasswordWindow)"
88-
@bind-Value:after="SaveSettings"
89-
Class="ml-6"
90-
Disabled="@(!AppSettings.EnablePasswordBox)"
91-
Label="@(Lang["HidePasswordWindow"])"
92-
Color="Color.Primary" />
93-
94-
<MudSwitch @bind-Value="@(_keyboardDownChecked)"
95-
@bind-Value:after="KeyboardDownChecked"
96-
Class="ml-6"
97-
Disabled="@(!AppSettings.EnablePasswordBox)"
98-
Label="@(Lang["KeyboardDownActivePwd"])"
99-
Color="Color.Primary" />
100-
101-
<MudSwitch @bind-Value="@(_mouseDownChecked)"
102-
@bind-Value:after="MouseDownChecked"
103-
Class="ml-6"
104-
Disabled="@(!AppSettings.EnablePasswordBox)"
105-
Label="@(Lang["MouseDownActivePwd"])"
106-
Color="Color.Primary" />
107-
89+
<div class="d-flex">
90+
<MudSwitch @bind-Value="@(AppSettings.IsHidePasswordWindow)"
91+
@bind-Value:after="SaveSettings"
92+
Class="ml-6"
93+
Disabled="@(!AppSettings.EnablePasswordBox)"
94+
Label="@(Lang["HidePasswordWindow"])"
95+
Color="Color.Primary" />
96+
</div>
97+
<div class="d-flex">
98+
<MudSwitch @bind-Value="@(_keyboardDownChecked)"
99+
@bind-Value:after="KeyboardDownChecked"
100+
Class="ml-6"
101+
Disabled="@(!AppSettings.EnablePasswordBox)"
102+
Label="@(Lang["KeyboardDownActivePwd"])"
103+
Color="Color.Primary" />
104+
</div>
105+
<div class="d-flex">
106+
<MudSwitch @bind-Value="@(_mouseDownChecked)"
107+
@bind-Value:after="MouseDownChecked"
108+
Class="ml-6"
109+
Disabled="@(!AppSettings.EnablePasswordBox)"
110+
Label="@(Lang["MouseDownActivePwd"])"
111+
Color="Color.Primary" />
112+
</div>
108113
<MudSelect T="ScreenLocationEnum"
109114
Label="@(Lang["PwdLocation"])"
110115
Variant="Variant.Outlined"
@@ -139,4 +144,4 @@
139144
Color="Color.Primary"></MudCheckBox>
140145
</MudPaper>
141146
}
142-
</MudPaper>
147+
</MudPaper>

src/ComputerLock/Pages/Index.razor.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,13 @@ private void AutoLockChanged(int autoLockMinute)
130130
{
131131
AppSettings.AutoLockSecond = autoLockMinute * 60;
132132
SaveSettings();
133-
RestartTips();
133+
GlobalLockService.UpdateAutoLockSettings();
134134
}
135135

136136
private void PwdBoxLocationChanged(ScreenLocationEnum location)
137137
{
138138
AppSettings.PasswordInputLocation = location;
139139
SaveSettings();
140-
RestartTips();
141-
}
142-
private void RestartTips()
143-
{
144-
Snackbar.Configuration.NewestOnTop = true;
145-
Snackbar.Configuration.VisibleStateDuration = int.MaxValue;
146-
Snackbar.Configuration.ShowCloseIcon = true;
147-
Snackbar.Configuration.SnackbarVariant = Variant.Text;
148-
Snackbar.Add(Lang["Restarting"], Severity.Normal, config =>
149-
{
150-
config.Action = Lang["Restart"];
151-
config.HideIcon = true;
152-
config.ActionColor = MudBlazor.Color.Warning;
153-
config.ActionVariant = Variant.Outlined;
154-
config.OnClick = _ =>
155-
{
156-
Restart();
157-
return Task.CompletedTask;
158-
};
159-
});
160-
}
161-
162-
private void Restart()
163-
{
164-
WindowTitleBar.Restart();
165140
}
166141

167142
private void SaveSettings()

src/ComputerLock/Platforms/UserActivityMonitor.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace ComputerLock.Platforms;
66

7-
public class UserActivityMonitor : IDisposable
7+
public class UserActivityMonitor
88
{
9-
109
[DllImport("user32.dll")]
1110
private static extern bool GetLastInputInfo(ref LastInputInfo plii);
1211
struct LastInputInfo
@@ -17,35 +16,44 @@ struct LastInputInfo
1716

1817
private Timer? _timer;
1918
public EventHandler? OnIdle;
20-
2119
private int _autoLockMillisecond;
22-
private bool _isMonitoring = false;
23-
public void Init(int autoLockSecond)
20+
private readonly object _lock = new();
21+
22+
public void SetAutoLockSecond(int autoLockSecond)
2423
{
2524
_autoLockMillisecond = autoLockSecond * 1000;
26-
27-
_timer = new Timer();
28-
_timer.Interval = 1000;
29-
_timer.Elapsed += Timer_Elapsed;
30-
_timer.Start();
3125
}
3226

3327
public void StartMonitoring()
3428
{
35-
_isMonitoring = true;
29+
lock (_lock)
30+
{
31+
if (_timer == null)
32+
{
33+
_timer = new Timer();
34+
_timer.Interval = 1000;
35+
_timer.Elapsed += Timer_Elapsed;
36+
}
37+
_timer.Start();
38+
}
3639
}
3740

3841
public void StopMonitoring()
3942
{
40-
_isMonitoring = false;
41-
}
42-
private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
43-
{
44-
if (!_isMonitoring)
43+
lock (_lock)
4544
{
46-
return;
45+
if (_timer != null)
46+
{
47+
_timer.Elapsed -= Timer_Elapsed;
48+
_timer.Stop();
49+
_timer.Dispose();
50+
_timer = null;
51+
}
4752
}
53+
}
4854

55+
private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
56+
{
4957
var lastInputInfo = new LastInputInfo();
5058
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
5159

@@ -58,10 +66,4 @@ private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
5866
}
5967
}
6068
}
61-
62-
public void Dispose()
63-
{
64-
_timer?.Stop();
65-
_timer?.Dispose();
66-
}
6769
}

src/ComputerLock/Resources/Lang.Designer.cs

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ComputerLock/Resources/Lang.en.resx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<root>
33
<!--
44
Microsoft ResX Schema
@@ -168,9 +168,6 @@
168168
<data name="Center" xml:space="preserve">
169169
<value>Center</value>
170170
</data>
171-
<data name="Restarting" xml:space="preserve">
172-
<value>The setting is successful and will take effect after restarting.</value>
173-
</data>
174171
<data name="LockHotkey" xml:space="preserve">
175172
<value>Lock hotkey</value>
176173
</data>
@@ -258,9 +255,6 @@
258255
<data name="EnablePasswordBoxTips" xml:space="preserve">
259256
<value>Press ESC and enter password to unlock</value>
260257
</data>
261-
<data name="Restart" xml:space="preserve">
262-
<value>Restart</value>
263-
</data>
264258
<data name="LogFiles" xml:space="preserve">
265259
<value>Log Files</value>
266260
</data>

0 commit comments

Comments
 (0)