From fc8722e89702047d6abd9dc2c4a60e76a103dd14 Mon Sep 17 00:00:00 2001 From: Emirhan Date: Mon, 29 Jun 2026 15:09:16 +0700 Subject: [PATCH] fix(tray): make Windows context menu DPI-aware Set process DPI awareness (Per-Monitor V2 with fallbacks) and enable WinForms visual styles before creating the tray NotifyIcon, so the Windows context menu renders sharply on DPI-scaled displays. Closes #2161 Co-authored-by: Cursor --- cli/src/cli/tray/tray.ps1 | 53 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/cli/src/cli/tray/tray.ps1 b/cli/src/cli/tray/tray.ps1 index 30562e31..bc55f393 100644 --- a/cli/src/cli/tray/tray.ps1 +++ b/cli/src/cli/tray/tray.ps1 @@ -2,14 +2,65 @@ # IPC: stdin JSON commands, stdout JSON events param([string]$IconPath, [string]$Tooltip) +$ErrorActionPreference = "Stop" + +Add-Type @" +using System; +using System.Runtime.InteropServices; + +public static class WinDpiAwareness { + public static IntPtr PerMonitorAwareV2 { get { return new IntPtr(-4); } } + public static IntPtr PerMonitorAware { get { return new IntPtr(-3); } } + + [DllImport("user32.dll")] + public static extern bool SetProcessDpiAwarenessContext(IntPtr value); + + [DllImport("user32.dll")] + public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr value); + + [DllImport("shcore.dll")] + public static extern int SetProcessDpiAwareness(int value); + + [DllImport("user32.dll")] + public static extern bool SetProcessDPIAware(); +} +"@ + +function Enable-HighDpiAwareness { + $contexts = @( + [WinDpiAwareness]::PerMonitorAwareV2, + [WinDpiAwareness]::PerMonitorAware + ) + + foreach ($context in $contexts) { + try { + if ([WinDpiAwareness]::SetProcessDpiAwarenessContext($context)) { break } + } catch {} + } + + try { [WinDpiAwareness]::SetProcessDpiAwareness(2) | Out-Null } catch {} + try { [WinDpiAwareness]::SetProcessDPIAware() | Out-Null } catch {} + + foreach ($context in $contexts) { + try { + $previous = [WinDpiAwareness]::SetThreadDpiAwarenessContext($context) + if ($previous -ne [IntPtr]::Zero) { break } + } catch {} + } +} + +Enable-HighDpiAwareness + Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing -$ErrorActionPreference = "Stop" [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 [Console]::InputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 +[System.Windows.Forms.Application]::EnableVisualStyles() +[System.Windows.Forms.Application]::SetCompatibleTextRenderingDefault($false) + $script:notifyIcon = New-Object System.Windows.Forms.NotifyIcon $script:notifyIcon.Icon = New-Object System.Drawing.Icon($IconPath) $script:notifyIcon.Text = $Tooltip