Files
AX-Copilot-Codex/.decompiledproj/AxCopilot/Views/TrayContextMenu.cs

131 lines
3.6 KiB
C#

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AxCopilot.Views;
internal static class TrayContextMenu
{
internal const string GlyphOpen = "\ue7c5";
internal const string GlyphSettings = "\ue713";
internal const string GlyphReload = "\ue72c";
internal const string GlyphFolder = "\ue838";
internal const string GlyphInfo = "\ue946";
internal const string GlyphStats = "\ue9d9";
internal const string GlyphChat = "\ue8bd";
internal const string GlyphGuide = "\ue736";
internal const string GlyphAutoRun = "\ue82f";
internal const string GlyphExit = "\ue711";
private static readonly Bitmap DummyImage = new Bitmap(1, 1);
private static float GetDpiScale()
{
try
{
using Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
return Math.Max(1f, graphics.DpiX / 96f);
}
catch
{
return 1f;
}
}
private static int Dp(int logicalPx, float dpiScale)
{
return Math.Max(1, (int)Math.Round((float)logicalPx / dpiScale));
}
public static Padding DpiPadding(int left, int top, int right, int bottom)
{
float dpiScale = GetDpiScale();
return new Padding(Dp(left, dpiScale), Dp(top, dpiScale), Dp(right, dpiScale), Dp(bottom, dpiScale));
}
public static ToolStripMenuItem MakeItem(string text, string glyph, EventHandler onClick)
{
float dpiScale = GetDpiScale();
ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem(text)
{
Tag = glyph,
Image = DummyImage,
Padding = new Padding(Dp(4, dpiScale), Dp(10, dpiScale), Dp(16, dpiScale), Dp(10, dpiScale))
};
toolStripMenuItem.Click += onClick;
return toolStripMenuItem;
}
public static ContextMenuStrip CreateMenu()
{
float dpiScale = GetDpiScale();
ContextMenuStrip menu = new ContextMenuStrip();
menu.Renderer = new ModernTrayRenderer();
menu.Font = new Font("Segoe UI", 10f, GraphicsUnit.Point);
menu.ShowImageMargin = true;
menu.ImageScalingSize = new Size(Dp(52, dpiScale), Dp(32, dpiScale));
menu.MinimumSize = new Size(Dp(280, dpiScale), 0);
menu.Opened += delegate
{
ApplyRoundedCorners(menu);
};
return menu;
}
private static void ApplyRoundedCorners(ContextMenuStrip menu)
{
try
{
nint num = CreateRoundRectRgn(0, 0, menu.Width, menu.Height, 16, 16);
if (num != IntPtr.Zero && SetWindowRgn(menu.Handle, num, bRedraw: true) == 0)
{
DeleteObject(num);
}
}
catch
{
}
}
[DllImport("gdi32.dll")]
private static extern nint CreateRoundRectRgn(int x1, int y1, int x2, int y2, int cx, int cy);
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(nint hObject);
[DllImport("user32.dll")]
private static extern int SetWindowRgn(nint hWnd, nint hRgn, bool bRedraw);
public static void ApplySpacing(ContextMenuStrip menu, int top = 10, int bottom = 10, int left = 0, int right = 0)
{
if (menu.Items.Count == 0)
{
return;
}
float dpiScale = GetDpiScale();
int top2 = Dp(top, dpiScale);
int bottom2 = Dp(bottom, dpiScale);
int num = Dp(left, dpiScale);
int num2 = Dp(right, dpiScale);
foreach (ToolStripItem item in menu.Items)
{
Padding margin = item.Margin;
item.Margin = new Padding(margin.Left + num, margin.Top, margin.Right + num2, margin.Bottom);
}
ToolStripItem toolStripItem2 = menu.Items[0];
toolStripItem2.Margin = new Padding(toolStripItem2.Margin.Left, top2, toolStripItem2.Margin.Right, toolStripItem2.Margin.Bottom);
ToolStripItem toolStripItem3 = menu.Items[menu.Items.Count - 1];
toolStripItem3.Margin = new Padding(toolStripItem3.Margin.Left, toolStripItem3.Margin.Top, toolStripItem3.Margin.Right, bottom2);
}
}