using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
public static class DisplayFusionFunction
{
// --- THEME & ASSETS ---
private static Color bgClr = Color.FromArgb(13, 17, 23);
private static Color fgClr = Color.FromArgb(255, 255, 255);
private static Color clr2 = Color.FromArgb(71, 71, 77);
private static Font fnt = new Font("Segoe UI", 8, FontStyle.Regular);
public static void Run(IntPtr windowHandle)
{
if (windowHandle == IntPtr.Zero) return;
// --- DATA EXTRACTION ---
string fullFilePath = BFS.Application.GetMainFileByWindow(windowHandle);
string windowClass = BFS.Window.GetClass(windowHandle);
string windowTitle = BFS.Window.GetText(windowHandle);
string processFileName = Path.GetFileName(fullFilePath);
string fullFolderPath = Path.GetDirectoryName(fullFilePath);
// --- INITIALIZATION ---
using (ContextMenuStrip menu = new ContextMenuStrip())
{
menu.ShowCheckMargin = false;
menu.ShowImageMargin = false;
menu.BackColor = bgClr;
menu.ForeColor = fgClr;
menu.Font = fnt;
menu.RenderMode = ToolStripRenderMode.System;
menu.KeyDown += (s, e) => { if (e.KeyCode == Keys.Escape) menu.Close(); };
// --- MAIN MENU POPULATION ---
// Separator at the beginning of the main menu
menu.Items.Add(new ToolStripSeparator());
string[] appsList = { "Window Spy", "KeyHistory", "RegJump" };
menu.Items.Add(CreateWindowPropertiesSubMenu("Window Properties", windowClass, windowTitle, processFileName, fullFolderPath, fullFilePath, appsList));
menu.Items.Add(CreateSubMenu("Apps", new string[] { "Pik_SnapShot", "Pik_TxtExtract", "Pik_ColorPicker", "Screen Ruler", "Screen Zoom", "PowerToys Quick Access" }));
menu.Items.Add(CreateSubMenu("Monitor", new string[] { "Mon_Clone", "Mon_Extend", "Load Monitor Profile: Portrait" }));
menu.Items.Add(CreateSubMenu("Windows", new string[] { "Defender Toggle", "Defender Exclusions", "Taskmanager", "System Properties" }));
menu.Items.Add(CreateSubMenu("Fusion", new string[] { "Minimize Window To System Tray", "Rollup Window to TitleBar", "Rollup Window to Icon", "Toggle Window Always on Top", "Toggle Window Transparency", "Save Window Positions", "Size and Move Window to Left Side of Monitor", "Size and Move Window to Right Side of Monitor", "Move Window to Top-Left Corner and Size 50%", "Move Window to Top-Right Corner and Size 50%", "Move Window to Bottom-Left Corner and Size 50%", "Move Window to Bottom-Right Corner and Size 50%", "Restart", "Task Kill", "Win_Super F4" }));
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Open DisplayFusion Settings");
menu.Items.Add("Open DisplayFusion Monitor Configuration");
menu.Items.Add(new ToolStripSeparator());
ToolStripMenuItem mainCancel = new ToolStripMenuItem("Cancel (Esc)");
mainCancel.ForeColor = clr2;
mainCancel.Font = fnt;
mainCancel.Click += (s, e) => menu.Close();
menu.Items.Add(mainCancel);
menu.Items.Add(new ToolStripSeparator());
// --- POSITIONING & SHOW ---
menu.CreateControl();
int mouseX = BFS.Input.GetMousePositionX();
int mouseY = BFS.Input.GetMousePositionY();
Rectangle monitorArea = BFS.Monitor.GetMonitorWorkAreaByMouseCursor();
int finalX = mouseX;
int finalY = mouseY;
if (mouseX + menu.Width > monitorArea.Right) finalX = mouseX - menu.Width;
if (mouseY + menu.Height > monitorArea.Bottom) finalY = mouseY - menu.Height;
finalX = Math.Max(finalX, monitorArea.Left);
finalY = Math.Max(finalY, monitorArea.Top);
menu.AutoClose = true;
menu.Opened += (s, e) =>
{
Point menuCenter = new Point(menu.Left + (menu.Width / 2), menu.Top + (menu.Height / 2));
BFS.Input.SetMousePosition(menuCenter.X, menuCenter.Y);
menu.Focus();
};
menu.Show(finalX, finalY);
BFS.Window.Focus(menu.Handle);
// --- AUTO-CLOSE LOOP ---
DateTime lastMouseOverTime = DateTime.Now;
while (menu.Visible)
{
bool mouseOverMenu = menu.DisplayRectangle.Contains(menu.PointToClient(Cursor.Position));
foreach (ToolStripItem item in menu.Items)
{
if (item is ToolStripMenuItem tsmi && tsmi.DropDown.Visible)
{
if (tsmi.DropDown.DisplayRectangle.Contains(tsmi.DropDown.PointToClient(Cursor.Position)))
{
mouseOverMenu = true;
break;
}
}
}
if (mouseOverMenu) lastMouseOverTime = DateTime.Now;
else if ((DateTime.Now - lastMouseOverTime).TotalMilliseconds > 700) menu.Close();
if (!menu.Focused && !menu.ContainsFocus)
{
IntPtr activeWindow = BFS.Window.GetFocusedWindow();
if (activeWindow != menu.Handle) menu.Close();
}
Application.DoEvents();
System.Threading.Thread.Sleep(20);
}
}
}
// --- HELPER: WINDOW PROPERTIES SUBMENU ---
private static ToolStripMenuItem CreateWindowPropertiesSubMenu(string text, string winClass, string winTitle, string procName, string folderPath, string filePath, string[] apps)
{
ToolStripMenuItem parent = new ToolStripMenuItem(text);
parent.BackColor = bgClr; parent.ForeColor = fgClr; parent.DropDown.BackColor = bgClr; parent.Font = fnt;
parent.DropDownDirection = ToolStripDropDownDirection.Left;
// Separator at the beginning of this submenu
parent.DropDownItems.Add(new ToolStripSeparator());
string[] labels = { "Window Class", "Window Title", "Process Filename", "Full Folder Path", "Full File Path" };
string[] values = { winClass, winTitle, procName, folderPath, filePath };
for (int i = 0; i < labels.Length; i++)
{
ToolStripMenuItem child = new ToolStripMenuItem(labels[i]);
child.BackColor = bgClr; child.ForeColor = fgClr; child.Font = fnt;
string val = values[i];
child.Click += (s, e) => { if (!string.IsNullOrEmpty(val)) Clipboard.SetText(val); };
parent.DropDownItems.Add(child);
}
parent.DropDownItems.Add(new ToolStripSeparator());
foreach (string appName in apps)
{
ToolStripMenuItem appChild = new ToolStripMenuItem(appName);
appChild.BackColor = bgClr; appChild.ForeColor = fgClr; appChild.Font = fnt;
appChild.Click += MenuItem_Click;
parent.DropDownItems.Add(appChild);
}
parent.DropDownItems.Add(new ToolStripSeparator());
ToolStripMenuItem subCancel = new ToolStripMenuItem("Cancel");
subCancel.ForeColor = clr2; subCancel.Font = fnt;
subCancel.Click += (s, e) => parent.DropDown.Close();
parent.DropDownItems.Add(subCancel);
return parent;
}
// --- HELPER: STANDARD SUBMENU GENERATOR ---
private static ToolStripMenuItem CreateSubMenu(string text, string[] subFunctions)
{
ToolStripMenuItem parent = new ToolStripMenuItem(text);
parent.BackColor = bgClr; parent.ForeColor = fgClr; parent.DropDown.BackColor = bgClr; parent.Font = fnt;
parent.DropDownDirection = ToolStripDropDownDirection.Left;
// Separator at the beginning of this submenu
parent.DropDownItems.Add(new ToolStripSeparator());
foreach (string func in subFunctions)
{
ToolStripMenuItem child = new ToolStripMenuItem(func);
child.BackColor = bgClr; child.ForeColor = fgClr; child.Font = fnt;
child.Click += MenuItem_Click;
parent.DropDownItems.Add(child);
}
parent.DropDownItems.Add(new ToolStripSeparator());
ToolStripMenuItem subCancel = new ToolStripMenuItem("Cancel");
subCancel.ForeColor = clr2; subCancel.Font = fnt;
subCancel.Click += (s, e) => parent.DropDown.Close();
parent.DropDownItems.Add(subCancel);
return parent;
}
private static void MenuItem_Click(object sender, EventArgs e)
{
ToolStripItem item = sender as ToolStripItem;
if (item != null) BFS.DisplayFusion.RunFunction(item.Text);
}
}