[Awake]Reset Awake mode to "Keep using the selected power plan" (#31402)

* [Awake] Reset Awake mode to "Keep using the selected power plan"

* [Awake] Remove duplicated code.

* [Awake] Set passive even there is an issue in settings file.

* [Awake] Remove unnecessary code
This commit is contained in:
gokcekantarci
2024-02-27 17:35:33 +03:00
committed by GitHub
parent 81a6cde7a5
commit 4600bba7b6
3 changed files with 25 additions and 15 deletions

View File

@@ -10,11 +10,13 @@ using System.Globalization;
using System.IO;
using System.Reactive.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using Awake.Core.Models;
using Awake.Core.Native;
using Awake.Properties;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Telemetry;
using Microsoft.Win32;
@@ -153,6 +155,8 @@ namespace Awake.Core
{
Logger.LogInfo($"Completed expirable keep-awake.");
CancelExistingThread();
SetPassiveKeepAwakeMode(Constants.AppName);
},
_tokenSource.Token);
}
@@ -179,6 +183,8 @@ namespace Awake.Core
{
Logger.LogInfo($"Completed timed thread.");
CancelExistingThread();
SetPassiveKeepAwakeMode(Constants.AppName);
},
_tokenSource.Token);
}
@@ -285,5 +291,22 @@ namespace Awake.Core
};
return optionsList;
}
public static void SetPassiveKeepAwakeMode(string moduleName)
{
try
{
SettingsUtils settingsUtils = new SettingsUtils();
AwakeSettings settings = new AwakeSettings();
settings.Properties.Mode = AwakeMode.PASSIVE;
settingsUtils.SaveSettings(JsonSerializer.Serialize(settings), moduleName);
}
catch (Exception ex)
{
string? errorString = $"Failed to reset Awake mode: {ex.Message}";
Logger.LogError(errorString);
}
}
}
}

View File

@@ -117,20 +117,7 @@ namespace Awake.Core
private static void PassiveKeepAwakeCommandHandler(string moduleName)
{
AwakeSettings currentSettings;
try
{
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
}
catch (FileNotFoundException)
{
currentSettings = new AwakeSettings();
}
currentSettings.Properties.Mode = AwakeMode.PASSIVE;
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
Manager.SetPassiveKeepAwakeMode(moduleName);
}
private static void IndefiniteKeepAwakeCommandHandler(string moduleName)

View File

@@ -15,7 +15,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
KeepDisplayOn = false;
Mode = AwakeMode.PASSIVE;
IntervalHours = 0;
IntervalMinutes = 0;
IntervalMinutes = 1;
ExpirationDateTime = DateTimeOffset.Now;
CustomTrayTimes = new Dictionary<string, int>();
}