diff --git a/src/MaaWpfGui/States/RunningState.cs b/src/MaaWpfGui/States/RunningState.cs index d0993893d9..cf013825da 100644 --- a/src/MaaWpfGui/States/RunningState.cs +++ b/src/MaaWpfGui/States/RunningState.cs @@ -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); diff --git a/src/MaaWpfGui/Utilities/SleepManagement.cs b/src/MaaWpfGui/Utilities/SleepManagement.cs index 492a3d4195..d2c067daf6 100644 --- a/src/MaaWpfGui/Utilities/SleepManagement.cs +++ b/src/MaaWpfGui/Utilities/SleepManagement.cs @@ -10,9 +10,11 @@ // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY // +#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); } } } diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index 42eca77899..b51ca5ab1b 100755 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -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."); - } } }