refactor: 移除媒体检测逻辑,简化代码体积
This commit is contained in:
@@ -22,9 +22,8 @@ namespace TimerApp
|
||||
|
||||
// 状态检测缓存
|
||||
private int _checkTickCounter;
|
||||
private const int CheckIntervalTicks = 3; // 每3秒检查一次空闲/媒体状态
|
||||
private const int CheckIntervalTicks = 3; // 每3秒检查一次空闲状态
|
||||
private long _cachedIdleMs;
|
||||
private bool _cachedMediaPlaying;
|
||||
|
||||
// 配置 (默认值)
|
||||
public TimeSpan WorkDuration { get; set; } = TimeSpan.FromMinutes(20);
|
||||
@@ -47,7 +46,6 @@ namespace TimerApp
|
||||
|
||||
public ActivityMonitor()
|
||||
{
|
||||
SystemEvents.PowerModeChanged += OnPowerModeChanged;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
@@ -126,7 +124,7 @@ namespace TimerApp
|
||||
{
|
||||
// 在锁外执行耗时的系统检测
|
||||
long idleMs = 0;
|
||||
bool? newMediaPlaying = null;
|
||||
bool shouldUpdate = false;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -139,27 +137,25 @@ namespace TimerApp
|
||||
{
|
||||
_checkTickCounter = 0;
|
||||
// 需要更新,但在锁外进行
|
||||
newMediaPlaying = true;
|
||||
shouldUpdate = true;
|
||||
}
|
||||
|
||||
// 如果不需要更新,直接使用缓存值
|
||||
if (newMediaPlaying == null)
|
||||
if (!shouldUpdate)
|
||||
{
|
||||
idleMs = _cachedIdleMs;
|
||||
}
|
||||
}
|
||||
|
||||
// 在锁外执行实际的检测
|
||||
if (newMediaPlaying == true)
|
||||
if (shouldUpdate)
|
||||
{
|
||||
idleMs = NativeMethods.GetIdleTime();
|
||||
bool playing = NativeMethods.IsMediaPlaying();
|
||||
|
||||
// 更新缓存
|
||||
lock (_lock)
|
||||
{
|
||||
_cachedIdleMs = idleMs;
|
||||
_cachedMediaPlaying = playing;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +166,6 @@ namespace TimerApp
|
||||
|
||||
// 使用(可能是新更新的)缓存值
|
||||
idleMs = _cachedIdleMs;
|
||||
bool isMediaPlaying = _cachedMediaPlaying;
|
||||
TimeSpan idleTime = TimeSpan.FromMilliseconds(idleMs);
|
||||
|
||||
if (CurrentState == MonitorState.Resting)
|
||||
@@ -196,12 +191,13 @@ namespace TimerApp
|
||||
else
|
||||
{
|
||||
// 工作/空闲模式逻辑
|
||||
if (idleTime > IdleThreshold || isMediaPlaying)
|
||||
bool isUserInactive = idleTime > IdleThreshold;
|
||||
|
||||
if (isUserInactive)
|
||||
{
|
||||
// 用户离开了或正在播放视频
|
||||
// 用户确实离开了 -> 进入空闲状态
|
||||
if (CurrentState == MonitorState.Working)
|
||||
{
|
||||
// 如果空闲时间超过阈值,状态变为空闲
|
||||
ChangeState(MonitorState.Idle);
|
||||
}
|
||||
|
||||
@@ -211,24 +207,17 @@ namespace TimerApp
|
||||
{
|
||||
_accumulatedWorkTime = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
// 如果正在播放视频,不累加工作时间,但保持当前状态
|
||||
if (isMediaPlaying && CurrentState == MonitorState.Working)
|
||||
{
|
||||
TimeSpan remainingWork = WorkDuration - _accumulatedWorkTime;
|
||||
WorkProgressChanged?.Invoke(this, remainingWork);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 用户在活动且没有播放视频
|
||||
// 用户在活动
|
||||
if (CurrentState == MonitorState.Idle)
|
||||
{
|
||||
// 从空闲变为工作
|
||||
ChangeState(MonitorState.Working);
|
||||
}
|
||||
|
||||
// 累加工作时间
|
||||
// 正常工作:累加时间
|
||||
_accumulatedWorkTime += TimeSpan.FromSeconds(1);
|
||||
|
||||
// 检查是否达到工作时长
|
||||
@@ -277,13 +266,6 @@ namespace TimerApp
|
||||
_accumulatedWorkTime = TimeSpan.Zero;
|
||||
_isPaused = false;
|
||||
|
||||
// Ensure task is running
|
||||
if (_cts == null || _cts.IsCancellationRequested)
|
||||
{
|
||||
// Re-start if stopped (though Restart implies it's running)
|
||||
// Usually Start() calls ResetWork, so we just reset here
|
||||
}
|
||||
|
||||
// Force state to Working since user manually restarted
|
||||
ChangeState(MonitorState.Working);
|
||||
|
||||
@@ -322,18 +304,10 @@ namespace TimerApp
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
SystemEvents.PowerModeChanged -= OnPowerModeChanged;
|
||||
// SystemEvents.PowerModeChanged -= OnPowerModeChanged;
|
||||
Stop();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
|
||||
{
|
||||
if (e.Mode == PowerModes.Resume)
|
||||
{
|
||||
// 系统唤醒时,强制重置媒体播放检测状态,
|
||||
// 避免因检测线程挂起导致一直误报“正在播放”而无法进入工作状态。
|
||||
NativeMethods.InvalidateMediaCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user