Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?
Save up to 50% on our desktop apps during our big Summer Sale!Save up to 50% on our desktop apps during our big Summer Sale, including DisplayFusion, ClipboardFusion, FileSeek, LogFusion, TrayStatus, and VoiceBot!Save up to 50% on our desktop apps during our big Summer Sale!

Toggle Maximize/Restore for Focused App windows

Description
Toggles all windows belonging to the same application as the currently focused window between Maximized and Restored, acting as a group rather than one window at a time.
If any window in the group is not currently maximized (including minimized windows), all windows are maximized. If every window is already maximized, all are restored instead. Minimized windows are un-minimized before being maximized, so they land reliably in the maximized state. The originally focused window stays active throughout.
Language
C#.net
Minimum Version
Created By
Yirg
Contributors
-
Date Created
3d ago
Date Last Modified
3d ago

Scripted Function (Macro) Code

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);
    }
}