@@ -21,6 +21,7 @@ internal class GlobalLockService : IGlobalLockService
2121 private readonly IWindowsMessageBox _messageBox ;
2222 private readonly IStringLocalizer < Lang > _lang ;
2323 public bool IsLocked { get ; private set ; }
24+ private bool _isWindowsLocked ;
2425 private CancellationTokenSource ? _cts ;
2526 public GlobalLockService ( ILogger logger , AppSettings appSettings , UserActivityMonitor activityMonitor , HotkeyHook hotkeyHook , TaskManagerHook taskManagerHook , MouseHook mouseHook , SystemKeyHook systemKeyHook , IServiceProvider serviceProvider , IWindowsMessageBox messageBox , IStringLocalizer < Lang > lang )
2627 {
@@ -41,8 +42,10 @@ public GlobalLockService(ILogger logger, AppSettings appSettings, UserActivityMo
4142 _lang = lang ;
4243
4344 InitActivityMonitor ( ) ;
44- }
4545
46+ _logger . Write ( "空闲自动锁定 -> 准备监控系统会话状态" ) ;
47+ SystemEvents . SessionSwitch += SystemEvents_SessionSwitch ;
48+ }
4649
4750 /// <summary>
4851 /// 初始化空闲检测
@@ -55,34 +58,68 @@ private void InitActivityMonitor()
5558 Lock ( ) ;
5659 } ;
5760
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 )
6070 {
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 )
6789 {
68- // Windows 操作系统锁定
69- _logger . Write ( "空闲自动锁定 -> Windows系统锁定,暂停空闲检测" ) ;
90+ _logger . Write ( "系统 -> Windows 系统锁定,暂停空闲检测" ) ;
7091 _activityMonitor . StopMonitoring ( ) ;
7192 }
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 )
73109 {
74- // Windows 操作系统解锁
75- _logger . Write ( $ "空闲自动锁定 -> Windows系统解锁,恢复空闲检测,{ _appSettings . AutoLockSecond } 秒") ;
76- _activityMonitor . SetAutoLockSecond ( _appSettings . AutoLockSecond ) ;
77- _activityMonitor . StartMonitoring ( ) ;
110+ _logger . Write ( $ "系统 -> Windows 系统解锁") ;
111+ AutoLockStart ( ) ;
78112 }
79- } ;
80-
81- AutoLockStart ( ) ;
113+ }
82114 }
83115
84116 private void AutoLockStart ( )
85117 {
118+ if ( _isWindowsLocked && _appSettings . IsUnlockWhenWindowsLock )
119+ {
120+ _logger . Write ( $ "系统 -> Windows 锁定状态,不启用空闲检测") ;
121+ }
122+
86123 if ( _appSettings . AutoLockSecond > 0 )
87124 {
88125 _logger . Write ( $ "系统 -> 启动空闲检测,{ _appSettings . AutoLockSecond } 秒") ;
@@ -93,6 +130,12 @@ private void AutoLockStart()
93130
94131 public void Lock ( )
95132 {
133+ if ( _isWindowsLocked && _appSettings . IsUnlockWhenWindowsLock )
134+ {
135+ _logger . Write ( $ "系统 -> Windows 锁定状态禁止程序锁定") ;
136+ return ;
137+ }
138+
96139 if ( ! CheckLockConfig ( out var message ) )
97140 {
98141 _messageBox . Show ( message ) ;
@@ -214,6 +257,8 @@ public void Unlock()
214257
215258 public void UpdateAutoLockSettings ( )
216259 {
260+ _logger . Write ( "系统 -> 更新自动锁定设置" ) ;
261+ _logger . Write ( "系统 -> 停止空闲检测" ) ;
217262 _activityMonitor . StopMonitoring ( ) ;
218263 AutoLockStart ( ) ;
219264 }
@@ -254,5 +299,6 @@ private void SystemUnlock()
254299 public void Dispose ( )
255300 {
256301 _hotkeyHook . Dispose ( ) ;
302+ SystemEvents . SessionSwitch -= SystemEvents_SessionSwitch ;
257303 }
258304}
0 commit comments