mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
This PR contains a set of bug fixes and general improvements to [Awake](https://awake.den.dev/) and developer experience tooling for building the module. ### Awake Fixes - **#32544** - Fixed an issue where Awake settings became non-functional after the PC wakes from sleep. Added `WM_POWERBROADCAST` handling to detect system resume events (`PBT_APMRESUMEAUTOMATIC`, `PBT_APMRESUMESUSPEND`) and re-apply `SetThreadExecutionState` to restore the awake state. - **#36150** - Fixed an issue where Awake would not prevent sleep when AC power is connected. Added `PBT_APMPOWERSTATUSCHANGE` handling to re-apply `SetThreadExecutionState` when the power source changes (AC/battery transitions). - **#41674** - Fixed silent failure when `SetThreadExecutionState` fails. The monitor thread now handles the return value, logs an error, and reverts to passive mode with updated tray icon. - **#41738** - Fixed `--display-on` CLI flag default from `true` to `false` to align with documentation and PowerToys settings behavior. This is a breaking change for scripts relying on the undocumented default. - **#41918** - Fixed `WM_COMMAND` message processing flaw in `TrayHelper.WndProc` that incorrectly compared enum values against enum count. Added proper bounds checking for custom tray time entries. - **#44134** - Documented that `ES_DISPLAY_REQUIRED` (used when "Keep display on" is enabled) blocks Task Scheduler idle detection, preventing scheduled maintenance tasks like SSD TRIM. Workaround: disable "Keep display on" or manually run `Optimize-Volume -DriveLetter C -ReTrim`. - **#38770** - Fixed tray icon failing to appear after Windows updates. Increased retry attempts and delays for icon Add operations (10 attempts, up to ~15.5 seconds total) while keeping existing fast retry behavior for Update/Delete operations. - **#40501** - Fixed tray icon not disappearing when Awake is disabled. The `SetShellIcon` function was incorrectly requiring an icon for Delete operations, causing the `NIM_DELETE` message to never be sent. - Fixed an issue where toggling "Keep screen on" during an active timed session would disrupt the countdown timer. The display setting now updates directly without restarting the timer, preserving the exact remaining time. ### Performance Optimizations - Fixed O(n²) loop in `TrayHelper.CreateAwakeTimeSubMenu` by replacing `ElementAt(i)` with `foreach` iteration. - Fixed Observable subscription leak in `Manager.cs` by storing `IDisposable` and disposing in `CancelExistingThread()`. Also removed dead `_tokenSource` code that was no longer used. - Reduced allocations in `SingleThreadSynchronizationContext` by changing `Tuple<>` to `ValueTuple`. - Replaced dedicated exit event thread with `ThreadPool.RegisterWaitForSingleObject()` to reduce resource usage. ### Code Quality - Replaced `Console.WriteLine` with `Logger.LogError` in `TrayHelper.cs` for consistent logging. - Added proper error logging to silent exception catches in `AwakeService.cs`. - Removed dead `Math.Min(minutes, int.MaxValue)` code where `minutes` is already an `int`. - Extracted hardcoded tray icon ID to named constant `TrayIconId`. - Standardized null coalescing for `GetSettings<AwakeSettings>()` calls across all files. ### Debugging Experience Fixes - Fixed first-chance exceptions in `settings_window.cpp` during debugging. Added `HasKey()` check before accessing `hotkey_changed` property to prevent `hresult_error` exceptions when the property doesn't exist in module settings. - Fixed first-chance exceptions in FindMyMouse `parse_settings` during debugging. Refactored to extract the properties object once and added `HasKey()` checks before all `GetNamedObject()` calls. This prevents `winrt::hresult_error` exceptions when optional settings keys (like legacy `overlay_opacity`) don't exist, improving the debugging experience by eliminating spurious exception breaks. - Fixed LightSwitch.UITests build failures when building from a clean state. Added missing project references (`ManagedCommon`, `LightSwitchModuleInterface`) with `ReferenceOutputAssembly=false` to ensure proper build ordering, and added existence check for the native DLL copy operation. ### Developer Experience - Added `setup-dev-environment.ps1` script to automate development environment setup. - Added `clean-artifacts.ps1` script to resolve build errors from corrupted build state or missing image files. - Added build script that allows standalone command line build of the Awake module. - Added troubleshooting section to `doc/devdocs/development/debugging.md` with guidance on resolving common build errors.
154 lines
5.0 KiB
C#
154 lines
5.0 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Diagnostics;
|
|
using System.Text.Json;
|
|
using Common.UI;
|
|
using ManagedCommon;
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
using PowerToys.ModuleContracts;
|
|
|
|
namespace Awake.ModuleServices;
|
|
|
|
/// <summary>
|
|
/// Provides CLI-based Awake control for reuse across hosts.
|
|
/// </summary>
|
|
public sealed class AwakeService : ModuleServiceBase, IAwakeService
|
|
{
|
|
public static AwakeService Instance { get; } = new();
|
|
|
|
public override string Key => SettingsDeepLink.SettingsWindow.Awake.ToString();
|
|
|
|
protected override SettingsDeepLink.SettingsWindow SettingsWindow => SettingsDeepLink.SettingsWindow.Awake;
|
|
|
|
public override Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
// Default launch -> indefinite, honoring Awake's own settings for display behavior.
|
|
return SetIndefiniteAsync(cancellationToken);
|
|
}
|
|
|
|
public AwakeState GetCurrentState()
|
|
{
|
|
var isRunning = IsAwakeProcessRunning();
|
|
var settings = ReadSettings();
|
|
|
|
if (settings is null)
|
|
{
|
|
return new AwakeState(isRunning, AwakeStateMode.Passive, false, null, null);
|
|
}
|
|
|
|
var mode = settings.Properties.Mode switch
|
|
{
|
|
AwakeMode.PASSIVE => AwakeStateMode.Passive,
|
|
AwakeMode.INDEFINITE => AwakeStateMode.Indefinite,
|
|
AwakeMode.TIMED => AwakeStateMode.Timed,
|
|
AwakeMode.EXPIRABLE => AwakeStateMode.Expirable,
|
|
_ => AwakeStateMode.Passive,
|
|
};
|
|
|
|
TimeSpan? duration = null;
|
|
DateTimeOffset? expiration = null;
|
|
|
|
switch (mode)
|
|
{
|
|
case AwakeStateMode.Timed:
|
|
duration = TimeSpan.FromHours(settings.Properties.IntervalHours) + TimeSpan.FromMinutes(settings.Properties.IntervalMinutes);
|
|
break;
|
|
case AwakeStateMode.Expirable:
|
|
expiration = settings.Properties.ExpirationDateTime;
|
|
break;
|
|
}
|
|
|
|
return new AwakeState(isRunning, mode, settings.Properties.KeepDisplayOn, duration, expiration);
|
|
}
|
|
|
|
public Task<OperationResult> SetIndefiniteAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return UpdateSettingsAsync(
|
|
settings =>
|
|
{
|
|
settings.Properties.Mode = AwakeMode.INDEFINITE;
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<OperationResult> SetTimedAsync(int minutes, CancellationToken cancellationToken = default)
|
|
{
|
|
if (minutes <= 0)
|
|
{
|
|
return Task.FromResult(OperationResult.Fail("Minutes must be greater than zero."));
|
|
}
|
|
|
|
return UpdateSettingsAsync(
|
|
settings =>
|
|
{
|
|
settings.Properties.Mode = AwakeMode.TIMED;
|
|
settings.Properties.IntervalHours = (uint)(minutes / 60);
|
|
settings.Properties.IntervalMinutes = (uint)(minutes % 60);
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<OperationResult> SetOffAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return UpdateSettingsAsync(
|
|
settings =>
|
|
{
|
|
settings.Properties.Mode = AwakeMode.PASSIVE;
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
private static Task<OperationResult> UpdateSettingsAsync(Action<AwakeSettings> mutateSettings, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
var settingsUtils = SettingsUtils.Default;
|
|
var settings = settingsUtils.GetSettingsOrDefault<AwakeSettings>(AwakeSettings.ModuleName);
|
|
|
|
mutateSettings(settings);
|
|
|
|
settingsUtils.SaveSettings(JsonSerializer.Serialize(settings, AwakeServiceJsonContext.Default.AwakeSettings), AwakeSettings.ModuleName);
|
|
return Task.FromResult(OperationResult.Ok());
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
return Task.FromResult(OperationResult.Fail("Awake update was cancelled."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Task.FromResult(OperationResult.Fail($"Failed to update Awake settings: {ex.Message}"));
|
|
}
|
|
}
|
|
|
|
private static bool IsAwakeProcessRunning()
|
|
{
|
|
try
|
|
{
|
|
return Process.GetProcessesByName("PowerToys.Awake").Length > 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError($"Failed to check Awake process status: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static AwakeSettings? ReadSettings()
|
|
{
|
|
try
|
|
{
|
|
var settingsUtils = SettingsUtils.Default;
|
|
return settingsUtils.GetSettingsOrDefault<AwakeSettings>(AwakeSettings.ModuleName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError($"Failed to read Awake settings: {ex.Message}");
|
|
return null;
|
|
}
|
|
}
|
|
}
|