using System;
using System.Collections.Generic;
// DisplayFusion Scripted Function, created with Anthropic Fable 5
// Toggles all windows of the currently focused application between
// Maximized and Restored:
// - If any window is not maximized, maximize all of them.
// - If all windows are maximized, restore all of them.
public static class DisplayFusionFunction
{
public static void Run(IntPtr windowHandle)
{
// Get the focused window; fall back to the handle DisplayFusion passes in
IntPtr focused = BFS.Window.GetFocusedWindow();
if (focused == IntPtr.Zero)
focused = windowHandle;
if (focused == IntPtr.Zero)
return;
// Identify the owning application by process ID
uint processId = BFS.Application.GetAppIDByWindow(focused);
if (processId == 0)
return;
// Collect all candidate windows belonging to this process,
// including minimized ones
List<IntPtr> windows = new List<IntPtr>();
foreach (IntPtr handle in BFS.Window.GetVisibleAndMinimizedWindowHandles())
{
if (BFS.Application.GetAppIDByWindow(handle) != processId)
continue;
// Skip tool windows and popups without a title
if (string.IsNullOrEmpty(BFS.Window.GetText(handle)))
continue;
windows.Add(handle);
}
if (windows.Count == 0)
return;
// Determine the target state: if every window is maximized, restore all;
// otherwise maximize all (minimized windows count as not maximized)
bool allMaximized = true;
foreach (IntPtr handle in windows)
{
if (!BFS.Window.IsMaximized(handle))
{
allMaximized = false;
break;
}
}
foreach (IntPtr handle in windows)
{
if (allMaximized)
{
BFS.Window.Restore(handle);
}
else
{
// Un-minimize first so Maximize lands reliably
if (BFS.Window.IsMinimized(handle))
BFS.Window.Restore(handle);
BFS.Window.Maximize(handle);
}
}
// Keep the originally focused window active
BFS.Window.Focus(focused);
}
}