@@ -21,6 +21,7 @@ internal class GlobalLockService : IGlobalLockService
21
21
private readonly IWindowsMessageBox _messageBox ;
22
22
private readonly IStringLocalizer < Lang > _lang ;
23
23
public bool IsLocked { get ; private set ; }
24
+ private bool _isWindowsLocked ;
24
25
private CancellationTokenSource ? _cts ;
25
26
public GlobalLockService ( ILogger logger , AppSettings appSettings , UserActivityMonitor activityMonitor , HotkeyHook hotkeyHook , TaskManagerHook taskManagerHook , MouseHook mouseHook , SystemKeyHook systemKeyHook , IServiceProvider serviceProvider , IWindowsMessageBox messageBox , IStringLocalizer < Lang > lang )
26
27
{
@@ -41,8 +42,10 @@ public GlobalLockService(ILogger logger, AppSettings appSettings, UserActivityMo
41
42
_lang = lang ;
42
43
43
44
InitActivityMonitor ( ) ;
44
- }
45
45
46
+ _logger . Write ( "空闲自动锁定 -> 准备监控系统会话状态" ) ;
47
+ SystemEvents . SessionSwitch += SystemEvents_SessionSwitch ;
48
+ }
46
49
47
50
/// <summary>
48
51
/// 初始化空闲检测
@@ -55,34 +58,68 @@ private void InitActivityMonitor()
55
58
Lock ( ) ;
56
59
} ;
57
60
58
- _logger . Write ( "空闲自动锁定 -> 准备监控系统会话状态" ) ;
59
- SystemEvents . SessionSwitch += ( _ , e ) =>
61
+ AutoLockStart ( ) ;
62
+ }
63
+
64
+ /// <summary>
65
+ /// Windows 事件监控
66
+ /// </summary>
67
+ private void SystemEvents_SessionSwitch ( object sender , SessionSwitchEventArgs e )
68
+ {
69
+ if ( e . Reason == SessionSwitchReason . SessionLock )
60
70
{
61
- // 如果已经手动锁定,不处理系统锁定事件
62
- if ( IsLocked )
63
- {
64
- return ;
65
- }
66
- if ( e . Reason == SessionSwitchReason . SessionLock )
71
+ _isWindowsLocked = true ;
72
+ WindowsLock ( ) ;
73
+ }
74
+ else if ( e . Reason == SessionSwitchReason . SessionUnlock )
75
+ {
76
+ _isWindowsLocked = false ;
77
+ WindowsUnlock ( ) ;
78
+ }
79
+ }
80
+
81
+ /// <summary>
82
+ /// Windows 操作系统锁定
83
+ /// </summary>
84
+ private void WindowsLock ( )
85
+ {
86
+ if ( ! _appSettings . IsUnlockWhenWindowsLock )
87
+ {
88
+ if ( ! IsLocked )
67
89
{
68
- // Windows 操作系统锁定
69
- _logger . Write ( "空闲自动锁定 -> Windows系统锁定,暂停空闲检测" ) ;
90
+ _logger . Write ( "系统 -> Windows 系统锁定,暂停空闲检测" ) ;
70
91
_activityMonitor . StopMonitoring ( ) ;
71
92
}
72
- else if ( e . Reason == SessionSwitchReason . SessionUnlock )
93
+ }
94
+ else
95
+ {
96
+ _logger . Write ( "系统 -> Windows 系统锁定,程序解锁" ) ;
97
+ Unlock ( ) ;
98
+ }
99
+ }
100
+
101
+ /// <summary>
102
+ /// Windows 操作系统解锁
103
+ /// </summary>
104
+ private void WindowsUnlock ( )
105
+ {
106
+ if ( ! _appSettings . IsUnlockWhenWindowsLock )
107
+ {
108
+ if ( ! IsLocked )
73
109
{
74
- // Windows 操作系统解锁
75
- _logger . Write ( $ "空闲自动锁定 -> Windows系统解锁,恢复空闲检测,{ _appSettings . AutoLockSecond } 秒") ;
76
- _activityMonitor . SetAutoLockSecond ( _appSettings . AutoLockSecond ) ;
77
- _activityMonitor . StartMonitoring ( ) ;
110
+ _logger . Write ( $ "系统 -> Windows 系统解锁") ;
111
+ AutoLockStart ( ) ;
78
112
}
79
- } ;
80
-
81
- AutoLockStart ( ) ;
113
+ }
82
114
}
83
115
84
116
private void AutoLockStart ( )
85
117
{
118
+ if ( _isWindowsLocked && _appSettings . IsUnlockWhenWindowsLock )
119
+ {
120
+ _logger . Write ( $ "系统 -> Windows 锁定状态,不启用空闲检测") ;
121
+ }
122
+
86
123
if ( _appSettings . AutoLockSecond > 0 )
87
124
{
88
125
_logger . Write ( $ "系统 -> 启动空闲检测,{ _appSettings . AutoLockSecond } 秒") ;
@@ -93,6 +130,12 @@ private void AutoLockStart()
93
130
94
131
public void Lock ( )
95
132
{
133
+ if ( _isWindowsLocked && _appSettings . IsUnlockWhenWindowsLock )
134
+ {
135
+ _logger . Write ( $ "系统 -> Windows 锁定状态禁止程序锁定") ;
136
+ return ;
137
+ }
138
+
96
139
if ( ! CheckLockConfig ( out var message ) )
97
140
{
98
141
_messageBox . Show ( message ) ;
@@ -214,6 +257,8 @@ public void Unlock()
214
257
215
258
public void UpdateAutoLockSettings ( )
216
259
{
260
+ _logger . Write ( "系统 -> 更新自动锁定设置" ) ;
261
+ _logger . Write ( "系统 -> 停止空闲检测" ) ;
217
262
_activityMonitor . StopMonitoring ( ) ;
218
263
AutoLockStart ( ) ;
219
264
}
@@ -254,5 +299,6 @@ private void SystemUnlock()
254
299
public void Dispose ( )
255
300
{
256
301
_hotkeyHook . Dispose ( ) ;
302
+ SystemEvents . SessionSwitch -= SystemEvents_SessionSwitch ;
257
303
}
258
304
}
0 commit comments