mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[Awake]Keep settings after returning to passive mode (#31921)
* [Awake] Keep settings after set passive mode. * [Awake] Added new exceptions.
This commit is contained in:
@@ -37,10 +37,15 @@ namespace Awake.Core
|
|||||||
|
|
||||||
private static CancellationTokenSource _tokenSource;
|
private static CancellationTokenSource _tokenSource;
|
||||||
|
|
||||||
|
private static SettingsUtils? _moduleSettings;
|
||||||
|
|
||||||
|
private static SettingsUtils? ModuleSettings { get => _moduleSettings; set => _moduleSettings = value; }
|
||||||
|
|
||||||
static Manager()
|
static Manager()
|
||||||
{
|
{
|
||||||
_tokenSource = new CancellationTokenSource();
|
_tokenSource = new CancellationTokenSource();
|
||||||
_stateQueue = new BlockingCollection<ExecutionState>();
|
_stateQueue = new BlockingCollection<ExecutionState>();
|
||||||
|
ModuleSettings = new SettingsUtils();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void StartMonitor()
|
public static void StartMonitor()
|
||||||
@@ -294,17 +299,28 @@ namespace Awake.Core
|
|||||||
|
|
||||||
public static void SetPassiveKeepAwakeMode(string moduleName)
|
public static void SetPassiveKeepAwakeMode(string moduleName)
|
||||||
{
|
{
|
||||||
|
AwakeSettings currentSettings;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SettingsUtils settingsUtils = new SettingsUtils();
|
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
||||||
AwakeSettings settings = new AwakeSettings();
|
|
||||||
|
|
||||||
settings.Properties.Mode = AwakeMode.PASSIVE;
|
|
||||||
settingsUtils.SaveSettings(JsonSerializer.Serialize(settings), moduleName);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
string? errorString = $"Failed to reset Awake mode: {ex.Message}";
|
string? errorString = $"Failed to reset Awake mode GetSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
|
currentSettings = new AwakeSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
currentSettings.Properties.Mode = AwakeMode.PASSIVE;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
string? errorString = $"Failed to reset Awake mode SaveSettings: {ex.Message}";
|
||||||
Logger.LogError(errorString);
|
Logger.LogError(errorString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using System.Text.Json;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Awake.Core.Models;
|
using Awake.Core.Models;
|
||||||
|
using ManagedCommon;
|
||||||
using Microsoft.PowerToys.Settings.UI.Library;
|
using Microsoft.PowerToys.Settings.UI.Library;
|
||||||
|
|
||||||
namespace Awake.Core
|
namespace Awake.Core
|
||||||
@@ -83,14 +84,24 @@ namespace Awake.Core
|
|||||||
{
|
{
|
||||||
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
string? errorString = $"Failed GetSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
currentSettings = new AwakeSettings();
|
currentSettings = new AwakeSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSettings.Properties.KeepDisplayOn = !currentSettings.Properties.KeepDisplayOn;
|
currentSettings.Properties.KeepDisplayOn = !currentSettings.Properties.KeepDisplayOn;
|
||||||
|
|
||||||
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
try
|
||||||
|
{
|
||||||
|
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
string? errorString = $"Failed SaveSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void TimedKeepAwakeCommandHandler(string moduleName, int seconds)
|
private static void TimedKeepAwakeCommandHandler(string moduleName, int seconds)
|
||||||
@@ -103,8 +114,10 @@ namespace Awake.Core
|
|||||||
{
|
{
|
||||||
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
string? errorString = $"Failed GetSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
currentSettings = new AwakeSettings();
|
currentSettings = new AwakeSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +125,15 @@ namespace Awake.Core
|
|||||||
currentSettings.Properties.IntervalHours = (uint)timeSpan.Hours;
|
currentSettings.Properties.IntervalHours = (uint)timeSpan.Hours;
|
||||||
currentSettings.Properties.IntervalMinutes = (uint)timeSpan.Minutes;
|
currentSettings.Properties.IntervalMinutes = (uint)timeSpan.Minutes;
|
||||||
|
|
||||||
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
try
|
||||||
|
{
|
||||||
|
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
string? errorString = $"Failed SaveSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void PassiveKeepAwakeCommandHandler(string moduleName)
|
private static void PassiveKeepAwakeCommandHandler(string moduleName)
|
||||||
@@ -128,14 +149,24 @@ namespace Awake.Core
|
|||||||
{
|
{
|
||||||
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
currentSettings = ModuleSettings!.GetSettings<AwakeSettings>(moduleName);
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
string? errorString = $"Failed GetSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
currentSettings = new AwakeSettings();
|
currentSettings = new AwakeSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSettings.Properties.Mode = AwakeMode.INDEFINITE;
|
currentSettings.Properties.Mode = AwakeMode.INDEFINITE;
|
||||||
|
|
||||||
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
try
|
||||||
|
{
|
||||||
|
ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
string? errorString = $"Failed SaveSettings: {ex.Message}";
|
||||||
|
Logger.LogError(errorString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user