refactor: 重构 SleepManagement

This commit is contained in:
uye
2024-09-04 19:14:45 +08:00
parent 6b826adaea
commit 668d2e44c6
3 changed files with 35 additions and 20 deletions

View File

@@ -13,7 +13,6 @@
using System;
using System.Threading.Tasks;
using MaaWpfGui.Helper;
using MaaWpfGui.Utilities;
namespace MaaWpfGui.States
@@ -57,7 +56,7 @@ namespace MaaWpfGui.States
}
else
{
Instances.SettingsViewModel.SetupSleepManagement();
SleepManagement.BlockSleep();
}
OnIdleChanged(value);

View File

@@ -10,9 +10,11 @@
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
#nullable enable
using System;
using System.Runtime.InteropServices;
using Serilog;
namespace MaaWpfGui.Utilities
{
@@ -21,6 +23,21 @@ namespace MaaWpfGui.Utilities
[DllImport("kernel32.dll")]
private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
private static readonly ILogger _logger = Log.ForContext("SourceContext", "SleepManagement");
private static bool _allowBlockSleep = false;
private static bool _blockSleepWithScreenOn = true;
public static void SetBlockSleep(bool allowBlockSleep)
{
_allowBlockSleep = allowBlockSleep;
}
public static void SetBlockSleepWithScreenOn(bool blockSleepWithScreenOn)
{
_blockSleepWithScreenOn = blockSleepWithScreenOn;
}
[Flags]
private enum ExecutionState : uint
{
@@ -31,21 +48,29 @@ namespace MaaWpfGui.Utilities
public static void AllowSleep()
{
_logger.Information("Allowing system to sleep");
SetThreadExecutionState(ExecutionState.Continuous);
}
public static void BlockSleep(bool keepDisplayOn = true)
public static void BlockSleep(bool? allowBlockSleep = null, bool? blockSleepWithScreenOn = null)
{
SetThreadExecutionState(keepDisplayOn ?
ExecutionState.Continuous | ExecutionState.SystemRequired | ExecutionState.DisplayRequired :
ExecutionState.Continuous | ExecutionState.SystemRequired);
if (!(allowBlockSleep ?? _allowBlockSleep))
{
return;
}
bool keepDisplayOn = blockSleepWithScreenOn ?? _blockSleepWithScreenOn;
_logger.Information("Blocking system from sleeping");
ExecutionState state = ExecutionState.Continuous | ExecutionState.SystemRequired |
(keepDisplayOn ? ExecutionState.DisplayRequired : 0);
SetThreadExecutionState(state);
}
public static void ResetIdle(bool keepDisplayOn = true)
{
SetThreadExecutionState(keepDisplayOn ?
ExecutionState.SystemRequired | ExecutionState.DisplayRequired :
ExecutionState.SystemRequired);
ExecutionState state = ExecutionState.SystemRequired |
(keepDisplayOn ? ExecutionState.DisplayRequired : 0);
SetThreadExecutionState(state);
}
}
}

View File

@@ -1026,6 +1026,7 @@ namespace MaaWpfGui.ViewModels.UI
set
{
SetAndNotify(ref _blockSleep, value);
SleepManagement.SetBlockSleep(value);
ConfigurationHelper.SetValue(ConfigurationKeys.BlockSleep, value.ToString());
}
}
@@ -1038,6 +1039,7 @@ namespace MaaWpfGui.ViewModels.UI
set
{
SetAndNotify(ref _blockSleepWithScreenOn, value);
SleepManagement.SetBlockSleepWithScreenOn(value);
ConfigurationHelper.SetValue(ConfigurationKeys.BlockSleepWithScreenOn, value.ToString());
}
}
@@ -5482,16 +5484,5 @@ namespace MaaWpfGui.ViewModels.UI
{
HasAcknowledgedNightlyWarning = true;
}
public void SetupSleepManagement()
{
if (!BlockSleep)
{
return;
}
SleepManagement.BlockSleep(BlockSleepWithScreenOn);
_logger.Information("Blocking sleep.");
}
}
}