Skip to content

Commit

Permalink
设置自动锁定和密码框位置后实时生效
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuLing-zhang committed Feb 13, 2025
1 parent 9786fd4 commit a1beba1
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 296 deletions.
5 changes: 5 additions & 0 deletions src/ComputerLock/Interfaces/IGlobalLockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public interface IGlobalLockService : IDisposable
bool IsLocked { get; }
void Lock();
void Unlock();

/// <summary>
/// 更新自动锁定设置
/// </summary>
void UpdateAutoLockSettings();
}
93 changes: 49 additions & 44 deletions src/ComputerLock/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@
backdrop-filter: blur(10px);
}
</style>

<MudSwitch @bind-Value="@(AppSettings.LockOnStartup)"
@bind-Value:after="SaveSettings"
Label="@(Lang["LockOnStartup"])"
Color="Color.Primary" />

<MudTooltip Text="@(Lang["DisableWindowsRemark"])" Placement="Placement.Bottom">
<MudSwitch @bind-Value="@(AppSettings.IsDisableWindowsLock)"
<div class="d-flex">
<MudSwitch @bind-Value="@(AppSettings.LockOnStartup)"
@bind-Value:after="SaveSettings"
Label="@(Lang["DisableWindowsLock"])"
Label="@(Lang["LockOnStartup"])"
Color="Color.Primary" />
</MudTooltip>

<MudSwitch @bind-Value="@(AppSettings.IsHideMouseCursor)"
@bind-Value:after="SaveSettings"
Label="@(Lang["HideMouseCursor"])"
Color="Color.Primary" />

<MudSwitch @bind-Value="@(AppSettings.LockAnimation)"
@bind-Value:after="SaveSettings"
Label="@(Lang["LockAnimation"])"
Color="Color.Primary" />

</div>
<div class="d-flex">
<MudTooltip Text="@(Lang["DisableWindowsRemark"])" Placement="Placement.Bottom">
<MudSwitch @bind-Value="@(AppSettings.IsDisableWindowsLock)"
@bind-Value:after="SaveSettings"
Label="@(Lang["DisableWindowsLock"])"
Color="Color.Primary" />
</MudTooltip>
</div>
<div class="d-flex">
<MudSwitch @bind-Value="@(AppSettings.IsHideMouseCursor)"
@bind-Value:after="SaveSettings"
Label="@(Lang["HideMouseCursor"])"
Color="Color.Primary" />
</div>
<div class="d-flex">
<MudSwitch @bind-Value="@(AppSettings.LockAnimation)"
@bind-Value:after="SaveSettings"
Label="@(Lang["LockAnimation"])"
Color="Color.Primary" />
</div>
<MudNumericField T="int"
Label="@(Lang["AutoLock"])"
Value="@(AppSettings.AutoLockMinute)"
Expand Down Expand Up @@ -83,28 +86,30 @@
</MudChip>
}
</div>

<MudSwitch @bind-Value="@(AppSettings.IsHidePasswordWindow)"
@bind-Value:after="SaveSettings"
Class="ml-6"
Disabled="@(!AppSettings.EnablePasswordBox)"
Label="@(Lang["HidePasswordWindow"])"
Color="Color.Primary" />

<MudSwitch @bind-Value="@(_keyboardDownChecked)"
@bind-Value:after="KeyboardDownChecked"
Class="ml-6"
Disabled="@(!AppSettings.EnablePasswordBox)"
Label="@(Lang["KeyboardDownActivePwd"])"
Color="Color.Primary" />

<MudSwitch @bind-Value="@(_mouseDownChecked)"
@bind-Value:after="MouseDownChecked"
Class="ml-6"
Disabled="@(!AppSettings.EnablePasswordBox)"
Label="@(Lang["MouseDownActivePwd"])"
Color="Color.Primary" />

<div class="d-flex">
<MudSwitch @bind-Value="@(AppSettings.IsHidePasswordWindow)"
@bind-Value:after="SaveSettings"
Class="ml-6"
Disabled="@(!AppSettings.EnablePasswordBox)"
Label="@(Lang["HidePasswordWindow"])"
Color="Color.Primary" />
</div>
<div class="d-flex">
<MudSwitch @bind-Value="@(_keyboardDownChecked)"
@bind-Value:after="KeyboardDownChecked"
Class="ml-6"
Disabled="@(!AppSettings.EnablePasswordBox)"
Label="@(Lang["KeyboardDownActivePwd"])"
Color="Color.Primary" />
</div>
<div class="d-flex">
<MudSwitch @bind-Value="@(_mouseDownChecked)"
@bind-Value:after="MouseDownChecked"
Class="ml-6"
Disabled="@(!AppSettings.EnablePasswordBox)"
Label="@(Lang["MouseDownActivePwd"])"
Color="Color.Primary" />
</div>
<MudSelect T="ScreenLocationEnum"
Label="@(Lang["PwdLocation"])"
Variant="Variant.Outlined"
Expand Down Expand Up @@ -139,4 +144,4 @@
Color="Color.Primary"></MudCheckBox>
</MudPaper>
}
</MudPaper>
</MudPaper>
27 changes: 1 addition & 26 deletions src/ComputerLock/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,13 @@ private void AutoLockChanged(int autoLockMinute)
{
AppSettings.AutoLockSecond = autoLockMinute * 60;
SaveSettings();
RestartTips();
GlobalLockService.UpdateAutoLockSettings();
}

private void PwdBoxLocationChanged(ScreenLocationEnum location)
{
AppSettings.PasswordInputLocation = location;
SaveSettings();
RestartTips();
}
private void RestartTips()
{
Snackbar.Configuration.NewestOnTop = true;
Snackbar.Configuration.VisibleStateDuration = int.MaxValue;
Snackbar.Configuration.ShowCloseIcon = true;
Snackbar.Configuration.SnackbarVariant = Variant.Text;
Snackbar.Add(Lang["Restarting"], Severity.Normal, config =>
{
config.Action = Lang["Restart"];
config.HideIcon = true;
config.ActionColor = MudBlazor.Color.Warning;
config.ActionVariant = Variant.Outlined;
config.OnClick = _ =>
{
Restart();
return Task.CompletedTask;
};
});
}

private void Restart()
{
WindowTitleBar.Restart();
}

private void SaveSettings()
Expand Down
48 changes: 25 additions & 23 deletions src/ComputerLock/Platforms/UserActivityMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace ComputerLock.Platforms;

public class UserActivityMonitor : IDisposable
public class UserActivityMonitor
{

[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LastInputInfo plii);
struct LastInputInfo
Expand All @@ -17,35 +16,44 @@ struct LastInputInfo

private Timer? _timer;
public EventHandler? OnIdle;

private int _autoLockMillisecond;
private bool _isMonitoring = false;
public void Init(int autoLockSecond)
private readonly object _lock = new();

public void SetAutoLockSecond(int autoLockSecond)
{
_autoLockMillisecond = autoLockSecond * 1000;

_timer = new Timer();
_timer.Interval = 1000;
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}

public void StartMonitoring()
{
_isMonitoring = true;
lock (_lock)
{
if (_timer == null)
{
_timer = new Timer();
_timer.Interval = 1000;
_timer.Elapsed += Timer_Elapsed;
}
_timer.Start();
}
}

public void StopMonitoring()
{
_isMonitoring = false;
}
private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
{
if (!_isMonitoring)
lock (_lock)
{
return;
if (_timer != null)
{
_timer.Elapsed -= Timer_Elapsed;
_timer.Stop();
_timer.Dispose();
_timer = null;
}
}
}

private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
{
var lastInputInfo = new LastInputInfo();
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);

Expand All @@ -58,10 +66,4 @@ private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
}
}
}

public void Dispose()
{
_timer?.Stop();
_timer?.Dispose();
}
}
18 changes: 0 additions & 18 deletions src/ComputerLock/Resources/Lang.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions src/ComputerLock/Resources/Lang.en.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -168,9 +168,6 @@
<data name="Center" xml:space="preserve">
<value>Center</value>
</data>
<data name="Restarting" xml:space="preserve">
<value>The setting is successful and will take effect after restarting.</value>
</data>
<data name="LockHotkey" xml:space="preserve">
<value>Lock hotkey</value>
</data>
Expand Down Expand Up @@ -258,9 +255,6 @@
<data name="EnablePasswordBoxTips" xml:space="preserve">
<value>Press ESC and enter password to unlock</value>
</data>
<data name="Restart" xml:space="preserve">
<value>Restart</value>
</data>
<data name="LogFiles" xml:space="preserve">
<value>Log Files</value>
</data>
Expand Down
Loading

0 comments on commit a1beba1

Please sign in to comment.