Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/IslandWindow.cpp (C++)

Code before:

    const til::size ncSize{ GetTotalNonClientExclusiveSize(dpiWindow) };

    RECT rcWorkAdjusted = rcWork;

    // GH#10199 - adjust the size of the "work" rect by the size of our borders.
    // We want to make sure the window is restored within the bounds of the
    // monitor we're on, but it's totally fine if the invisible borders are
    // outside the monitor.
    rcWorkAdjusted.left -= ncSize.width<long>() / 2;
    rcWorkAdjusted.right += ncSize.width<long>() / 2;
    rcWorkAdjusted.top -= ncSize.height<long>() / 2;
    rcWorkAdjusted.bottom += ncSize.height<long>() / 2;

    // Enforce that our position is entirely within the bounds of our work area.
    // Prefer the top-left be on-screen rather than bottom-right (right before left, bottom before top).
    if (rcRestore.right > rcWorkAdjusted.right)
    {
        OffsetRect(&rcRestore, rcWorkAdjusted.right - rcRestore.right, 0);
    }
    if (rcRestore.left < rcWorkAdjusted.left)
    {
        OffsetRect(&rcRestore, rcWorkAdjusted.left - rcRestore.left, 0);
    }
    if (rcRestore.bottom > rcWorkAdjusted.bottom)
    {
        OffsetRect(&rcRestore, 0, rcWorkAdjusted.bottom - rcRestore.bottom);
    }
    if (rcRestore.top < rcWorkAdjusted.top)
    {
        OffsetRect(&rcRestore, 0, rcWorkAdjusted.top - rcRestore.top);
    }

    // Show the window at the computed position.
    SetWindowPos(hWnd,
                 HWND_TOP,
                 rcRestore.left,
                 rcRestore.top,
                 rcRestore.right - rcRestore.left,
                 rcRestore.bottom - rcRestore.top,
                 SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}

// Method Description:
// - Controls setting us into or out of fullscreen mode. Largely taken from
//   Window::SetIsFullscreen in conhost.
// - When entering fullscreen mode, we'll save the current window size and
//   location, and expand to take the entire monitor size. When leaving, we'll
//   use that saved size to restore back to.
// Arguments:
// - fullscreenEnabled true if we should enable fullscreen mode, false to disable.

Review comment (bug):
This fails to work properly if `rcRestore` covers a larger area than `rcWorkAdjusted` doesn't it? Is that alright?

Code after:

    const til::size ncSize{ GetTotalNonClientExclusiveSize(dpiWindow) };

    RECT rcWorkAdjusted = rcWork;

    // GH#10199 - adjust the size of the "work" rect by the size of our borders.
    // We want to make sure the window is restored within the bounds of the
    // monitor we're on, but it's totally fine if the invisible borders are
    // outside the monitor.
    const auto halfWidth{ ncSize.width<long>() / 2 };
    const auto halfHeight{ ncSize.height<long>() / 2 };

    rcWorkAdjusted.left -= halfWidth;
    rcWorkAdjusted.right += halfWidth;
    rcWorkAdjusted.top -= halfHeight;
    rcWorkAdjusted.bottom += halfHeight;

    // Enforce that our position is entirely within the bounds of our work area.
    // Prefer the top-left be on-screen rather than bottom-right (right before left, bottom before top).
    if (rcRestore.right > rcWorkAdjusted.right)
    {
        OffsetRect(&rcRestore, rcWorkAdjusted.right - rcRestore.right, 0);
    }
    if (rcRestore.left < rcWorkAdjusted.left)
    {
        OffsetRect(&rcRestore, rcWorkAdjusted.left - rcRestore.left, 0);
    }
    if (rcRestore.bottom > rcWorkAdjusted.bottom)
    {
        OffsetRect(&rcRestore, 0, rcWorkAdjusted.bottom - rcRestore.bottom);
    }
    if (rcRestore.top < rcWorkAdjusted.top)
    {
        OffsetRect(&rcRestore, 0, rcWorkAdjusted.top - rcRestore.top);
    }

    // Show the window at the computed position.
    SetWindowPos(hWnd,
                 HWND_TOP,
                 rcRestore.left,
                 rcRestore.top,
                 rcRestore.right - rcRestore.left,
                 rcRestore.bottom - rcRestore.top,
                 SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}

// Method Description:
// - Controls setting us into or out of fullscreen mode. Largely taken from
//   Window::SetIsFullscreen in conhost.
// - When entering fullscreen mode, we'll save the current window size and
//   location, and expand to take the entire monitor size. When leaving, we'll