From 6fea4d9c5dc56bdbd1546b7d5a41adbddd7c367b Mon Sep 17 00:00:00 2001 From: Noraa Junker Date: Sun, 16 Nov 2025 23:21:52 +0100 Subject: [PATCH] Fix tray icon behaviour --- .../RunnerV2/Helpers/SettingsHelper.cs | 2 +- .../RunnerV2/Helpers/TrayIconManager.cs | 12 +++++ src/RunnerV2/RunnerV2/NativeMethods.cs | 6 +++ src/RunnerV2/RunnerV2/Runner.cs | 49 ++++++++++++------- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/RunnerV2/RunnerV2/Helpers/SettingsHelper.cs b/src/RunnerV2/RunnerV2/Helpers/SettingsHelper.cs index 51e57e45b0..239832d7a0 100644 --- a/src/RunnerV2/RunnerV2/Helpers/SettingsHelper.cs +++ b/src/RunnerV2/RunnerV2/Helpers/SettingsHelper.cs @@ -18,9 +18,9 @@ namespace RunnerV2.Helpers { internal static class SettingsHelper { + private static readonly SettingsUtils _settingsUtils = new(); private static Process? _process; private static TwoWayPipeMessageIPCManaged? _ipc; - private static SettingsUtils _settingsUtils = new(); public static void OpenSettingsWindow(bool showOobeWindow = false, bool showScoobeWindow = false, bool showFlyout = false, Point? flyoutPosition = null, string? additionalArguments = null) { diff --git a/src/RunnerV2/RunnerV2/Helpers/TrayIconManager.cs b/src/RunnerV2/RunnerV2/Helpers/TrayIconManager.cs index 9927453f5a..54f5d3a4a6 100644 --- a/src/RunnerV2/RunnerV2/Helpers/TrayIconManager.cs +++ b/src/RunnerV2/RunnerV2/Helpers/TrayIconManager.cs @@ -32,6 +32,18 @@ namespace RunnerV2.Helpers Shell_NotifyIcon(NIMADD, ref notifyicondata); } + internal static void StopTrayIcon() + { + NOTIFYICONDATA notifyicondata = new() + { + CbSize = (uint)Marshal.SizeOf(), + HWnd = Runner.RunnerHwnd, + UId = 1, + }; + + Shell_NotifyIcon(NIMDELETE, ref notifyicondata); + } + private enum TrayButton : uint { Settings = 1, diff --git a/src/RunnerV2/RunnerV2/NativeMethods.cs b/src/RunnerV2/RunnerV2/NativeMethods.cs index 34b4731345..71326724dc 100644 --- a/src/RunnerV2/RunnerV2/NativeMethods.cs +++ b/src/RunnerV2/RunnerV2/NativeMethods.cs @@ -63,6 +63,7 @@ namespace RunnerV2 internal static partial bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); internal const uint NIMADD = 0x00000000; + internal const uint NIMDELETE = 0x00000002; internal struct NOTIFYICONDATA { @@ -130,6 +131,8 @@ namespace RunnerV2 COMMAND = 0x0111, HOTKEY = 0x0312, ICON_NOTIFY = 0x0800, + WINDOWPOSCHANGING = 0x0046, + DESTROY = 0x0002, } [DllImport("user32.dll")] @@ -138,6 +141,9 @@ namespace RunnerV2 [LibraryImport("user32.dll", SetLastError = false)] internal static partial IntPtr DefWindowProcW(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); + [LibraryImport("user32.dll", SetLastError = true)] + internal static partial uint RegisterWindowMessageW([MarshalAs(UnmanagedType.LPWStr)] string lpString); + [LibraryImport("user32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] internal static partial nint CreateWindowExW( uint dwExStyle, diff --git a/src/RunnerV2/RunnerV2/Runner.cs b/src/RunnerV2/RunnerV2/Runner.cs index d0b7b48a7c..7a4f35fdf3 100644 --- a/src/RunnerV2/RunnerV2/Runner.cs +++ b/src/RunnerV2/RunnerV2/Runner.cs @@ -68,11 +68,14 @@ namespace RunnerV2 } afterInitializationAction(); + MessageLoop(); return true; } + private static readonly uint _taskbarCreatedMessage = RegisterWindowMessageW("TaskbarCreated"); + private static void MessageLoop() { while (true) @@ -82,20 +85,7 @@ namespace RunnerV2 TranslateMessage(ref msg); DispatchMessageW(ref msg); - switch (msg.Message) - { - case (uint)WindowMessages.HOTKEY: - HotkeyManager.ProcessHotkey(msg.WParam); - break; - case (uint)WindowMessages.ICON_NOTIFY: - TrayIconManager.ProcessTrayIconMessage(msg.LParam); - break; - case (uint)WindowMessages.COMMAND: - TrayIconManager.ProcessTrayMenuCommand(msg.WParam); - break; - default: - break; - } + HandleMessage(msg.HWnd, msg.Message, (nint)msg.WParam, (nint)msg.LParam); } } } @@ -172,7 +162,7 @@ namespace RunnerV2 HInstance = hInstance, LpszClassName = TrayWindowClassName, Style = CSHREDRAW | CSVREDRAW, - LpfnWndProc = TrayIconWindowProc, + LpfnWndProc = HandleMessage, HIcon = hIcon, HbrBackground = IntPtr.Zero, LpszMenuName = string.Empty, @@ -203,9 +193,34 @@ namespace RunnerV2 } } - private static IntPtr TrayIconWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) + private static IntPtr HandleMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { - TrayIconManager.ProcessTrayIconMessage(lParam.ToInt64()); + switch (msg) + { + case (uint)WindowMessages.HOTKEY: + HotkeyManager.ProcessHotkey((nuint)wParam); + break; + case (uint)WindowMessages.ICON_NOTIFY: + TrayIconManager.ProcessTrayIconMessage(lParam); + break; + case (uint)WindowMessages.COMMAND: + TrayIconManager.ProcessTrayMenuCommand((nuint)wParam); + break; + case (uint)WindowMessages.WINDOWPOSCHANGING: + TrayIconManager.StartTrayIcon(); + break; + case (uint)WindowMessages.DESTROY: + TrayIconManager.StopTrayIcon(); + break; + default: + if (msg == _taskbarCreatedMessage) + { + TrayIconManager.StartTrayIcon(); + } + + break; + } + return DefWindowProcW(hWnd, msg, wParam, lParam); } }