Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ClickLightWin/Interop/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public static partial bool SetWindowPos(
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool DestroyIcon(nint hIcon);

// ---- Foreground activation -----------------------------------------------

// A tray context menu shown by hand never gets foreground activation, so a
// click outside it fails to dismiss it. Forcing our process to the foreground
// first restores the normal click-away close (the classic Win32 tray fix).
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool SetForegroundWindow(nint hWnd);

// ---- Global hotkeys ------------------------------------------------------

public const int WM_HOTKEY = 0x0312;
Expand Down
9 changes: 7 additions & 2 deletions src/ClickLightWin/Tray/TrayIcon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Drawing;
using System.Windows.Forms; // NotifyIcon, ContextMenuStrip
using ClickLightWin.Interop;

namespace ClickLightWin.Tray;

Expand Down Expand Up @@ -83,8 +84,12 @@ public TrayIcon(Settings settings, LaunchAtLoginController launchAtLogin,
// screen, so this stays correct for other taskbar positions too.
_icon.MouseUp += (_, e) =>
{
if (e.Button == MouseButtons.Right)
menu.Show(Cursor.Position, ToolStripDropDownDirection.AboveLeft);
if (e.Button != MouseButtons.Right) return;
// Give our process the foreground first; without it a hand-shown tray
// menu won't dismiss when you click elsewhere (you'd be stuck picking an
// item to close it). See NativeMethods.SetForegroundWindow.
NativeMethods.SetForegroundWindow(menu.Handle);
menu.Show(Cursor.Position, ToolStripDropDownDirection.AboveLeft);
};
}

Expand Down
Loading