From 00ad336ff592aeb7dc33c5eb0f02f242c1a7b23d Mon Sep 17 00:00:00 2001 From: Den Delimarsky Date: Wed, 27 Nov 2024 14:25:38 -0800 Subject: [PATCH] Fixes #35848 --- src/modules/awake/Awake/Core/Manager.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/modules/awake/Awake/Core/Manager.cs b/src/modules/awake/Awake/Core/Manager.cs index f84b3516301f..dbbc707145da 100644 --- a/src/modules/awake/Awake/Core/Manager.cs +++ b/src/modules/awake/Awake/Core/Manager.cs @@ -10,6 +10,7 @@ using System.Globalization; using System.IO; using System.Reactive.Linq; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; @@ -233,9 +234,9 @@ internal static void SetExpirableKeepAwake(DateTimeOffset expireAt, bool keepDis } } - internal static void SetTimedKeepAwake(uint seconds, bool keepDisplayOn = true) + internal static void SetTimedKeepAwake(uint seconds, bool keepDisplayOn = true, [CallerMemberName] string callerName = "") { - Logger.LogInfo($"Timed keep-awake. Expected runtime: {seconds} seconds with display on setting set to {keepDisplayOn}."); + Logger.LogInfo($"Timed keep-awake invoked by {callerName}. Expected runtime: {seconds} seconds with display on setting set to {keepDisplayOn}."); PowerToysTelemetry.Log.WriteEvent(new Telemetry.AwakeTimedKeepAwakeEvent()); @@ -262,7 +263,7 @@ internal static void SetTimedKeepAwake(uint seconds, bool keepDisplayOn = true) }, () => { - Console.WriteLine("Completed timed thread."); + Logger.LogInfo("Completed timed thread."); CancelExistingThread(); if (IsUsingPowerToysConfig) @@ -285,15 +286,19 @@ internal static void SetTimedKeepAwake(uint seconds, bool keepDisplayOn = true) { AwakeSettings currentSettings = ModuleSettings!.GetSettings(Constants.AppName) ?? new AwakeSettings(); TimeSpan timeSpan = TimeSpan.FromSeconds(seconds); + + uint totalHours = (uint)timeSpan.TotalHours; + uint remainingMinutes = (uint)(timeSpan.TotalMinutes % 60); + bool settingsChanged = currentSettings.Properties.Mode != AwakeMode.TIMED || - currentSettings.Properties.IntervalHours != (uint)timeSpan.Hours || - currentSettings.Properties.IntervalMinutes != (uint)timeSpan.Minutes; + currentSettings.Properties.IntervalHours != totalHours || + currentSettings.Properties.IntervalMinutes != remainingMinutes; if (settingsChanged) { currentSettings.Properties.Mode = AwakeMode.TIMED; - currentSettings.Properties.IntervalHours = (uint)timeSpan.Hours; - currentSettings.Properties.IntervalMinutes = (uint)timeSpan.Minutes; + currentSettings.Properties.IntervalHours = totalHours; + currentSettings.Properties.IntervalMinutes = remainingMinutes; ModuleSettings!.SaveSettings(JsonSerializer.Serialize(currentSettings), Constants.AppName); } }