Implement hiding the icon

This commit is contained in:
Noraa Junker
2026-02-03 00:54:42 +01:00
parent 5a3ccb3b60
commit 8bacded9c4
3 changed files with 58 additions and 22 deletions

View File

@@ -168,19 +168,6 @@ namespace RunnerV2.Helpers
}
}
CentralizedKeyboardHookManager.RemoveAllHooksFromModule("QuickAccess");
if (SettingsUtils.Default.GetSettings<GeneralSettings>().EnableQuickAccess)
{
CentralizedKeyboardHookManager.AddKeyboardHook("QuickAccess", SettingsUtils.Default.GetSettings<GeneralSettings>().QuickAccessShortcut, QuickAccessHelper.Show);
}
else
{
CentralizedKeyboardHookManager.RemoveAllHooksFromModule("QuickAccess");
QuickAccessHelper.Stop();
}
TrayIconManager.UpdateTrayIcon();
break;
case "powertoys":
foreach (var powertoysSettingsPart in property.Value.EnumerateObject())

View File

@@ -17,6 +17,8 @@ namespace RunnerV2.Helpers
{
internal static partial class TrayIconManager
{
private static bool _trayIconVisible;
private static nint GetTrayIcon()
{
if (SettingsUtils.Default.GetSettings<GeneralSettings>().ShowThemeAdaptiveTrayIcon)
@@ -31,19 +33,31 @@ namespace RunnerV2.Helpers
public static void UpdateTrayIcon()
{
if (!_trayIconVisible)
{
return;
}
NOTIFYICONDATA notifyicondata = new()
{
CbSize = (uint)Marshal.SizeOf<NOTIFYICONDATA>(),
HWnd = Runner.RunnerHwnd,
UId = 1,
HIcon = GetTrayIcon(),
UFlags = 0x0000001 | 0x0000002,
UFlags = 0x0000001 | 0x00000002 | 0x4,
UCallbackMessage = (uint)WindowMessages.ICON_NOTIFY,
SzTip = "PowerToys Runner",
};
Shell_NotifyIcon(0x1, ref notifyicondata);
}
internal static void StartTrayIcon()
{
if (_trayIconVisible)
{
return;
}
NOTIFYICONDATA notifyicondata = new()
{
CbSize = (uint)Marshal.SizeOf<NOTIFYICONDATA>(),
@@ -57,16 +71,17 @@ namespace RunnerV2.Helpers
ChangeWindowMessageFilterEx(Runner.RunnerHwnd, 0x0111, 0x0001, IntPtr.Zero);
new ThemeListener().ThemeChanged += (_) =>
{
PostMessageW(Runner.RunnerHwnd, 0x0800, IntPtr.Zero, 0x9000);
};
Shell_NotifyIcon(NIMADD, ref notifyicondata);
_trayIconVisible = true;
}
internal static void StopTrayIcon()
{
if (!_trayIconVisible)
{
return;
}
NOTIFYICONDATA notifyicondata = new()
{
CbSize = (uint)Marshal.SizeOf<NOTIFYICONDATA>(),
@@ -75,6 +90,7 @@ namespace RunnerV2.Helpers
};
Shell_NotifyIcon(NIMDELETE, ref notifyicondata);
_trayIconVisible = false;
}
internal enum TrayButton : uint
@@ -99,6 +115,10 @@ namespace RunnerV2.Helpers
AppendMenuW(_trayIconMenu, 0u, new UIntPtr((uint)TrayButton.ReportBug), "Report a Bug");
AppendMenuW(_trayIconMenu, 0x00000800u, UIntPtr.Zero, string.Empty); // separator
AppendMenuW(_trayIconMenu, 0u, new UIntPtr((uint)TrayButton.Close), "Close");
new ThemeListener().ThemeChanged += (_) =>
{
PostMessageW(Runner.RunnerHwnd, 0x0800, IntPtr.Zero, 0x9000);
};
}
internal static void ProcessTrayIconMessage(long lParam)

View File

@@ -81,7 +81,10 @@ namespace RunnerV2
Logger.LogInfo("Runner started");
InitializeTrayWindow();
TrayIconManager.StartTrayIcon();
if (SettingsUtils.Default.GetSettings<GeneralSettings>().ShowSysTrayIcon)
{
TrayIconManager.StartTrayIcon();
}
if (SettingsUtils.Default.GetSettings<GeneralSettings>().EnableQuickAccess)
{
@@ -295,7 +298,11 @@ namespace RunnerV2
TrayIconManager.ProcessTrayMenuCommand((nuint)wParam);
break;
case (uint)WindowMessages.WINDOWPOSCHANGING:
TrayIconManager.StartTrayIcon();
if (SettingsUtils.Default.GetSettings<GeneralSettings>().ShowSysTrayIcon)
{
TrayIconManager.StartTrayIcon();
}
break;
case (uint)WindowMessages.DESTROY:
Close();
@@ -306,9 +313,31 @@ namespace RunnerV2
ToggleModuleStateBasedOnEnabledProperty(module);
}
CentralizedKeyboardHookManager.RemoveAllHooksFromModule("QuickAccess");
if (SettingsUtils.Default.GetSettings<GeneralSettings>().EnableQuickAccess)
{
CentralizedKeyboardHookManager.AddKeyboardHook("QuickAccess", SettingsUtils.Default.GetSettings<GeneralSettings>().QuickAccessShortcut, QuickAccessHelper.Show);
}
else
{
CentralizedKeyboardHookManager.RemoveAllHooksFromModule("QuickAccess");
QuickAccessHelper.Stop();
}
TrayIconManager.UpdateTrayIcon();
if (SettingsUtils.Default.GetSettings<GeneralSettings>().ShowSysTrayIcon)
{
TrayIconManager.StartTrayIcon();
}
else
{
TrayIconManager.StopTrayIcon();
}
break;
default:
if (msg == _taskbarCreatedMessage)
if (msg == _taskbarCreatedMessage && SettingsUtils.Default.GetSettings<GeneralSettings>().ShowSysTrayIcon)
{
TrayIconManager.StartTrayIcon();
}