[Awake]Don't exit from tray icon unless standalone (#22232)

This commit is contained in:
Jaime Bernardo
2022-11-23 12:12:17 +00:00
committed by GitHub
parent 2074d32cb1
commit 97fdf033c8
2 changed files with 19 additions and 7 deletions

View File

@@ -82,23 +82,29 @@ namespace Awake.Core
}
}
internal static void SetTray(string text, AwakeSettings settings)
internal static void SetTray(string text, AwakeSettings settings, bool startedFromPowerToys)
{
SetTray(
text,
settings.Properties.KeepDisplayOn,
settings.Properties.Mode,
settings.Properties.TrayTimeShortcuts);
settings.Properties.TrayTimeShortcuts,
startedFromPowerToys);
}
public static void SetTray(string text, bool keepDisplayOn, AwakeMode mode, Dictionary<string, int> trayTimeShortcuts)
public static void SetTray(string text, bool keepDisplayOn, AwakeMode mode, Dictionary<string, int> trayTimeShortcuts, bool startedFromPowerToys)
{
TrayMenu = new DestroyMenuSafeHandle(PInvoke.CreatePopupMenu());
if (!TrayMenu.IsInvalid)
{
PInvoke.InsertMenu(TrayMenu, 0, MENU_ITEM_FLAGS.MF_BYPOSITION | MENU_ITEM_FLAGS.MF_STRING, (uint)TrayCommands.TC_EXIT, "Exit");
PInvoke.InsertMenu(TrayMenu, 0, MENU_ITEM_FLAGS.MF_BYPOSITION | MENU_ITEM_FLAGS.MF_SEPARATOR, 0, string.Empty);
if (!startedFromPowerToys)
{
// If Awake is started from PowerToys, the correct way to exit it is disabling it from Settings.
PInvoke.InsertMenu(TrayMenu, 0, MENU_ITEM_FLAGS.MF_BYPOSITION | MENU_ITEM_FLAGS.MF_STRING, (uint)TrayCommands.TC_EXIT, "Exit");
PInvoke.InsertMenu(TrayMenu, 0, MENU_ITEM_FLAGS.MF_BYPOSITION | MENU_ITEM_FLAGS.MF_SEPARATOR, 0, string.Empty);
}
PInvoke.InsertMenu(TrayMenu, 0, MENU_ITEM_FLAGS.MF_BYPOSITION | MENU_ITEM_FLAGS.MF_STRING | (keepDisplayOn ? MENU_ITEM_FLAGS.MF_CHECKED : MENU_ITEM_FLAGS.MF_UNCHECKED), (uint)TrayCommands.TC_DISPLAY_SETTING, "Keep screen on");
}

View File

@@ -44,6 +44,8 @@ namespace Awake
private static FileSystemWatcher? _watcher;
private static SettingsUtils? _settingsUtils;
private static bool _startedFromPowerToys;
public static Mutex LockMutex { get => _mutex; set => _mutex = value; }
private static Logger? _log;
@@ -188,6 +190,10 @@ namespace Awake
_log.Info("No PID specified. Allocating console...");
APIHelper.AllocateConsole();
}
else
{
_startedFromPowerToys = true;
}
_log.Info($"The value for --use-pt-config is: {usePtConfig}");
_log.Info($"The value for --display-on is: {displayOn}");
@@ -238,7 +244,7 @@ namespace Awake
.Select(e => e.EventArgs)
.Subscribe(HandleAwakeConfigChange);
TrayHelper.SetTray(InternalConstants.FullAppName, new AwakeSettings());
TrayHelper.SetTray(InternalConstants.FullAppName, new AwakeSettings(), _startedFromPowerToys);
// Initially the file might not be updated, so we need to start processing
// settings right away.
@@ -330,7 +336,7 @@ namespace Awake
}
}
TrayHelper.SetTray(InternalConstants.FullAppName, settings);
TrayHelper.SetTray(InternalConstants.FullAppName, settings, _startedFromPowerToys);
}
else
{