[Awake]Fix DAISY build issues (#34054)

This PR addresses some post-merge issues caught by @davidegiacometti,
including:

1. Separator in the context menu shown when not running from inside
PowerToys.
2. "Keep display on" setting not persisting across switches between
modes.
3. Awake not launching in standalone mode.

Additionally:

1. Exits are now properly handled in **timed** and **expirable**
keep-awake modes when running standalone. This ensures that Awake exists
after completion and doesn't switch to an in-actionable passive mode.
2. Tray tooltips now cover how much time is left on the timer.
3. Fixes #29354
4. Avoids a nasty memory leak because of re-instantiating of `Icon`
objects for every tray update.
5. Adds DPI awareness to the context menu (#16123)
This commit is contained in:
Den Delimarsky
2024-07-30 08:08:37 -07:00
committed by GitHub
parent 5d77874382
commit e8ad4fa804
8 changed files with 255 additions and 113 deletions

View File

@@ -26,8 +26,6 @@ namespace Awake
{
internal sealed class Program
{
private static readonly ManualResetEvent _exitSignal = new(false);
private static Mutex? _mutex;
private static FileSystemWatcher? _watcher;
private static SettingsUtils? _settingsUtils;
@@ -47,6 +45,8 @@ namespace Awake
internal static readonly string[] AliasesPidOption = ["--pid", "-p"];
internal static readonly string[] AliasesExpireAtOption = ["--expire-at", "-e"];
private static readonly Icon _defaultAwakeIcon = new(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets/Awake/awake.ico"));
private static int Main(string[] args)
{
_settingsUtils = new SettingsUtils();
@@ -54,15 +54,17 @@ namespace Awake
Logger.InitializeLogger(Path.Combine("\\", Core.Constants.AppName, "Logs"));
AppDomain.CurrentDomain.UnhandledException += AwakeUnhandledExceptionCatcher;
if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredAwakeEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
{
Exit("PowerToys.Awake tried to start with a group policy setting that disables the tool. Please contact your system administrator.", 1, _exitSignal, true);
Exit("PowerToys.Awake tried to start with a group policy setting that disables the tool. Please contact your system administrator.", 1);
return 0;
}
if (!instantiated)
{
Exit(Core.Constants.AppName + " is already running! Exiting the application.", 1, _exitSignal, true);
Exit(Core.Constants.AppName + " is already running! Exiting the application.", 1);
}
Logger.LogInfo($"Launching {Core.Constants.AppName}...");
@@ -129,18 +131,26 @@ namespace Awake
return rootCommand.InvokeAsync(args).Result;
}
private static void AwakeUnhandledExceptionCatcher(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception exception)
{
Logger.LogError(exception.ToString());
Logger.LogError(exception.StackTrace);
}
}
private static bool ExitHandler(ControlType ctrlType)
{
Logger.LogInfo($"Exited through handler with control type: {ctrlType}");
Exit(Resources.AWAKE_EXIT_MESSAGE, Environment.ExitCode, _exitSignal);
Exit(Resources.AWAKE_EXIT_MESSAGE, Environment.ExitCode);
return false;
}
private static void Exit(string message, int exitCode, ManualResetEvent exitSignal, bool force = false)
private static void Exit(string message, int exitCode)
{
Logger.LogInfo(message);
Manager.CompleteExit(exitCode, exitSignal, force);
Manager.CompleteExit(exitCode);
}
private static void HandleCommandLineArguments(bool usePtConfig, bool displayOn, uint timeLimit, int pid, string expireAt)
@@ -169,6 +179,15 @@ namespace Awake
// Start the monitor thread that will be used to track the current state.
Manager.StartMonitor();
TrayHelper.InitializeTray(Core.Constants.FullAppName, _defaultAwakeIcon);
var eventHandle = new EventWaitHandle(false, EventResetMode.ManualReset, interop.Constants.AwakeExitEvent());
new Thread(() =>
{
WaitHandle.WaitAny([eventHandle]);
Exit(Resources.AWAKE_EXIT_SIGNAL_MESSAGE, 0);
}).Start();
if (usePtConfig)
{
// Configuration file is used, therefore we disregard any other command-line parameter
@@ -177,17 +196,6 @@ namespace Awake
try
{
var eventHandle = new EventWaitHandle(false, EventResetMode.ManualReset, interop.Constants.AwakeExitEvent());
new Thread(() =>
{
if (WaitHandle.WaitAny([_exitSignal, eventHandle]) == 1)
{
Exit(Resources.AWAKE_EXIT_SIGNAL_MESSAGE, 0, _exitSignal, true);
}
}).Start();
TrayHelper.InitializeTray(Core.Constants.FullAppName, new Icon(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets/Awake/awake.ico")), _exitSignal);
string? settingsPath = _settingsUtils!.GetSettingsFilePath(Core.Constants.AppName);
Logger.LogInfo($"Reading configuration file: {settingsPath}");
@@ -245,11 +253,9 @@ namespace Awake
RunnerHelper.WaitForPowerToysRunner(pid, () =>
{
Logger.LogInfo($"Triggered PID-based exit handler for PID {pid}.");
Exit(Resources.AWAKE_EXIT_BINDING_HOOK_MESSAGE, 0, _exitSignal, true);
Exit(Resources.AWAKE_EXIT_BINDING_HOOK_MESSAGE, 0);
});
}
_exitSignal.WaitOne();
}
private static void ScaffoldConfiguration(string settingsPath)