Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awake Updates - PROMETHEAN_09082024 #34717

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
22 changes: 15 additions & 7 deletions doc/planning/awake.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ The build ID can be found in `Core\Constants.cs` in the `BuildId` variable - it

The build ID moniker is made up of two components - a reference to a [Halo](https://en.wikipedia.org/wiki/Halo_(franchise)) character, and the date when the work on the specific build started in the format of `MMDDYYYY`.

| Build ID | Build Date |
|:-------------------------------------------------------------------|:----------------|
| [`VISEGRADRELAY_08152024`](#VISEGRADRELAY_08152024-august-15-2024) | August 15, 2024 |
| [`DAISY023_04102024`](#DAISY023_04102024-april-10-2024) | April 10, 2024 |
| [`ATRIOX_04132023`](#ATRIOX_04132023-april-13-2023) | April 13, 2023 |
| [`LIBRARIAN_03202022`](#librarian_03202022-march-20-2022) | March 20, 2022 |
| `ARBITER_01312022` | January 31, 2022 |
| Build ID | Build Date |
|:-------------------------------------------------------------------|:------------------|
| [`PROMETHEAN_09082024`](#PROMETHEAN_09082024-september-8-2024) | September 8, 2024 |
| [`VISEGRADRELAY_08152024`](#VISEGRADRELAY_08152024-august-15-2024) | August 15, 2024 |
| [`DAISY023_04102024`](#DAISY023_04102024-april-10-2024) | April 10, 2024 |
| [`ATRIOX_04132023`](#ATRIOX_04132023-april-13-2023) | April 13, 2023 |
| [`LIBRARIAN_03202022`](#librarian_03202022-march-20-2022) | March 20, 2022 |
| `ARBITER_01312022` | January 31, 2022 |

### `PROMETHEAN_09082024` (September 8, 2024)

>[!NOTE]
>See pull request: [Awake - `PROMETHEAN_09082024`](https://github.com/microsoft/PowerToys/pull/34717)

- Updating the initialization logic to make sure that settings are respected for proper group policy and single-instance detection.

### `VISEGRADRELAY_08152024` (August 15, 2024)

Expand Down
2 changes: 1 addition & 1 deletion src/modules/awake/Awake/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ internal static class Constants
// Format of the build ID is: CODENAME_MMDDYYYY, where MMDDYYYY
// is representative of the date when the last change was made before
// the pull request is issued.
internal const string BuildId = "VISEGRADRELAY_08152024";
internal const string BuildId = "PROMETHEAN_09082024";
}
}
153 changes: 75 additions & 78 deletions src/modules/awake/Awake/Core/TrayHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
Expand All @@ -28,13 +29,11 @@ namespace Awake.Core
internal static class TrayHelper
{
private static NotifyIconData _notifyIconData;

private static IntPtr _trayMenu;
private static IntPtr _hiddenWindowHandle;

private static IntPtr TrayMenu { get => _trayMenu; set => _trayMenu = value; }

private static IntPtr _hiddenWindowHandle;

internal static IntPtr HiddenWindowHandle { get => _hiddenWindowHandle; private set => _hiddenWindowHandle = value; }

static TrayHelper()
Expand All @@ -45,42 +44,35 @@ static TrayHelper()

private static void ShowContextMenu(IntPtr hWnd)
{
if (TrayMenu != IntPtr.Zero)
if (TrayMenu == IntPtr.Zero)
{
Bridge.SetForegroundWindow(hWnd);

// Get the handle to the context menu associated with the tray icon
IntPtr hMenu = TrayMenu;
Logger.LogError("Tried to create a context menu while the TrayMenu object is a null pointer. Normal when used in standalone mode.");
return;
}

// Get the current cursor position
Bridge.GetCursorPos(out Models.Point cursorPos);
Bridge.SetForegroundWindow(hWnd);

Bridge.ScreenToClient(hWnd, ref cursorPos);
// Get cursor position and convert it to client coordinates
Bridge.GetCursorPos(out Models.Point cursorPos);
Bridge.ScreenToClient(hWnd, ref cursorPos);

MenuInfo menuInfo = new()
{
CbSize = (uint)Marshal.SizeOf(typeof(MenuInfo)),
FMask = Native.Constants.MIM_STYLE,
DwStyle = Native.Constants.MNS_AUTO_DISMISS,
};
Bridge.SetMenuInfo(hMenu, ref menuInfo);

// Display the context menu at the cursor position
Bridge.TrackPopupMenuEx(
hMenu,
Native.Constants.TPM_LEFT_ALIGN | Native.Constants.TPM_BOTTOMALIGN | Native.Constants.TPM_LEFT_BUTTON,
cursorPos.X,
cursorPos.Y,
hWnd,
IntPtr.Zero);
}
else
// Set menu information
var menuInfo = new MenuInfo
{
// Tray menu was not initialized. Log the issue.
// This is normal when operating in "standalone mode" - that is, detached
// from the PowerToys configuration file.
Logger.LogError("Tried to create a context menu while the TrayMenu object is a null pointer. Normal when used in standalone mode.");
}
CbSize = (uint)Marshal.SizeOf<MenuInfo>(),
FMask = Native.Constants.MIM_STYLE,
DwStyle = Native.Constants.MNS_AUTO_DISMISS,
};
Bridge.SetMenuInfo(TrayMenu, ref menuInfo);

// Display the context menu at the cursor position
Bridge.TrackPopupMenuEx(
TrayMenu,
Native.Constants.TPM_LEFT_ALIGN | Native.Constants.TPM_BOTTOMALIGN | Native.Constants.TPM_LEFT_BUTTON,
cursorPos.X,
cursorPos.Y,
hWnd,
IntPtr.Zero);
}

public static void InitializeTray(Icon icon, string text)
Expand Down Expand Up @@ -152,58 +144,63 @@ public static void InitializeTray(Icon icon, string text)

internal static void SetShellIcon(IntPtr hWnd, string text, Icon? icon, TrayIconAction action = TrayIconAction.Add)
{
Logger.LogInfo($"Setting the shell icon.\nText: {text}\nAction: {action}");

int message = Native.Constants.NIM_ADD;

switch (action)
if (hWnd != IntPtr.Zero && icon != null)
{
case TrayIconAction.Update:
message = Native.Constants.NIM_MODIFY;
break;
case TrayIconAction.Delete:
message = Native.Constants.NIM_DELETE;
break;
case TrayIconAction.Add:
default:
break;
}
int message = Native.Constants.NIM_ADD;

if (action == TrayIconAction.Add || action == TrayIconAction.Update)
{
Logger.LogInfo($"Adding or updating tray icon. HIcon handle is {icon?.Handle}\nHWnd: {hWnd}");
switch (action)
{
case TrayIconAction.Update:
message = Native.Constants.NIM_MODIFY;
break;
case TrayIconAction.Delete:
message = Native.Constants.NIM_DELETE;
break;
case TrayIconAction.Add:
default:
break;
}

_notifyIconData = new NotifyIconData
if (action == TrayIconAction.Add || action == TrayIconAction.Update)
{
CbSize = Marshal.SizeOf(typeof(NotifyIconData)),
HWnd = hWnd,
UId = 1000,
UFlags = Native.Constants.NIF_ICON | Native.Constants.NIF_TIP | Native.Constants.NIF_MESSAGE,
UCallbackMessage = (int)Native.Constants.WM_USER,
HIcon = icon?.Handle ?? IntPtr.Zero,
SzTip = text,
};
}
else if (action == TrayIconAction.Delete)
{
_notifyIconData = new NotifyIconData
_notifyIconData = new NotifyIconData
{
CbSize = Marshal.SizeOf(typeof(NotifyIconData)),
HWnd = hWnd,
UId = 1000,
UFlags = Native.Constants.NIF_ICON | Native.Constants.NIF_TIP | Native.Constants.NIF_MESSAGE,
UCallbackMessage = (int)Native.Constants.WM_USER,
HIcon = icon?.Handle ?? IntPtr.Zero,
SzTip = text,
};
}
else if (action == TrayIconAction.Delete)
{
CbSize = Marshal.SizeOf(typeof(NotifyIconData)),
HWnd = hWnd,
UId = 1000,
UFlags = 0,
};
}
_notifyIconData = new NotifyIconData
{
CbSize = Marshal.SizeOf(typeof(NotifyIconData)),
HWnd = hWnd,
UId = 1000,
UFlags = 0,
};
}

if (!Bridge.Shell_NotifyIcon(message, ref _notifyIconData))
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to change tray icon. Action: {action} and error code: {errorCode}");
}
if (!Bridge.Shell_NotifyIcon(message, ref _notifyIconData))
{
int errorCode = Marshal.GetLastWin32Error();
Logger.LogInfo($"Could not set the shell icon. Action: {action} and error code: {errorCode}. HIcon handle is {icon?.Handle} and HWnd is {hWnd}");

throw new Win32Exception(errorCode, $"Failed to change tray icon. Action: {action} and error code: {errorCode}");
}

if (action == TrayIconAction.Delete)
if (action == TrayIconAction.Delete)
{
_notifyIconData = default;
}
}
else
{
_notifyIconData = default;
Logger.LogInfo($"Cannot set the shell icon - parent window handle is zero or icon is not available. Text: {text} Action: {action}");
}
}

Expand Down
Loading
Loading