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 <cursoragent@cursor.com>
This commit is contained in:
parent
713c563765
commit
fc8722e897
1 changed files with 52 additions and 1 deletions
|
|
@ -2,14 +2,65 @@
|
||||||
# IPC: stdin JSON commands, stdout JSON events
|
# IPC: stdin JSON commands, stdout JSON events
|
||||||
param([string]$IconPath, [string]$Tooltip)
|
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.Windows.Forms
|
||||||
Add-Type -AssemblyName System.Drawing
|
Add-Type -AssemblyName System.Drawing
|
||||||
|
|
||||||
$ErrorActionPreference = "Stop"
|
|
||||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||||
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
|
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
|
||||||
$OutputEncoding = [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 = New-Object System.Windows.Forms.NotifyIcon
|
||||||
$script:notifyIcon.Icon = New-Object System.Drawing.Icon($IconPath)
|
$script:notifyIcon.Icon = New-Object System.Drawing.Icon($IconPath)
|
||||||
$script:notifyIcon.Text = $Tooltip
|
$script:notifyIcon.Text = $Tooltip
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue