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

Code before:

    // Start with the stored window position.
    RECT rcRestore = _rcWindowBeforeFullscreen;

    // If the window DPI has changed, re-size the stored position by the change in DPI. This
    // ensures the window restores to the same logical size (even if to a monitor with a different
    // DPI/ scale factor).
    UINT dpiWindow = GetDpiForWindow(hWnd);
    rcRestore.right = rcRestore.left + MulDiv(rcRestore.right - rcRestore.left, dpiWindow, _dpiBeforeFullscreen);
    rcRestore.bottom = rcRestore.top + MulDiv(rcRestore.bottom - rcRestore.top, dpiWindow, _dpiBeforeFullscreen);

    // Offset the stored position by the difference in work area.
    OffsetRect(&rcRestore,
               rcWork.left - _rcWorkBeforeFullscreen.left,
               rcWork.top - _rcWorkBeforeFullscreen.top);

    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,

Review comment (suggestion):
Could width or height reasonably be an odd number where this won't work properly? Or will the offsetting below correct that to slide the whole thing 1px into place at the end then?

I think this is fine... but this is where my brain is tying itself into a knot.

Code after:

    // Start with the stored window position.
    RECT rcRestore = _rcWindowBeforeFullscreen;

    // If the window DPI has changed, re-size the stored position by the change in DPI. This
    // ensures the window restores to the same logical size (even if to a monitor with a different
    // DPI/ scale factor).
    UINT dpiWindow = GetDpiForWindow(hWnd);
    rcRestore.right = rcRestore.left + MulDiv(rcRestore.right - rcRestore.left, dpiWindow, _dpiBeforeFullscreen);
    rcRestore.bottom = rcRestore.top + MulDiv(rcRestore.bottom - rcRestore.top, dpiWindow, _dpiBeforeFullscreen);

    // Offset the stored position by the difference in work area.
    OffsetRect(&rcRestore,
               rcWork.left - _rcWorkBeforeFullscreen.left,
               rcWork.top - _rcWorkBeforeFullscreen.top);

    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);
    }