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

Code before:
    adjustedHeight = islandHeight + nonClientSize.height<long>();

    til::size dimensions{ Utils::ClampToShortMax(adjustedWidth, 1),
                          Utils::ClampToShortMax(adjustedHeight, 1) };

    // Find nearest monitor for the position that we've actually settled on
    HMONITOR hMonNearest = MonitorFromRect(&proposedRect, MONITOR_DEFAULTTONEAREST);
    MONITORINFO nearestMonitorInfo;
    nearestMonitorInfo.cbSize = sizeof(MONITORINFO);
    // Get monitor dimensions:
    GetMonitorInfo(hMonNearest, &nearestMonitorInfo);
    const til::size desktopDimensions{ gsl::narrow<short>(nearestMonitorInfo.rcWork.right - nearestMonitorInfo.rcWork.left),
                                       gsl::narrow<short>(nearestMonitorInfo.rcWork.bottom - nearestMonitorInfo.rcWork.top) };

    til::point origin{ ::base::saturated_cast<ptrdiff_t>(proposedRect.left),
                       ::base::saturated_cast<ptrdiff_t>(proposedRect.top) };

    if (_logic.IsQuakeWindow())
    {
        // If we just use rcWork by itself, we'll fail to account for the invisible
        // space reserved for the resize handles. So retrieve that size here.
        const til::size ncSize{ _window->GetTotalNonClientExclusiveSize(dpix) };
        const til::size availableSpace = desktopDimensions + nonClientSize;

        origin = til::point{
            ::base::saturated_cast<ptrdiff_t>(nearestMonitorInfo.rcWork.left - (nonClientSize.width() / 2)),
            ::base::saturated_cast<ptrdiff_t>(nearestMonitorInfo.rcWork.top)
        };
        dimensions = til::size{
            availableSpace.width(),
            availableSpace.height() / 2
        };
        launchMode = LaunchMode::FocusMode;
    }
    else if (centerOnLaunch)
    {
        // Move our proposed location into the center of that specific monitor.
        origin = til::point{
            ::base::saturated_cast<ptrdiff_t>(nearestMonitorInfo.rcWork.left + ((desktopDimensions.width() / 2) - (dimensions.width() / 2))),
            ::base::saturated_cast<ptrdiff_t>(nearestMonitorInfo.rcWork.top + ((desktopDimensions.height() / 2) - (dimensions.height() / 2)))
        };
    }

    const til::rectangle newRect{ origin, dimensions };
    bool succeeded = SetWindowPos(hwnd,
                                  nullptr,
                                  newRect.left<int>(),
                                  newRect.top<int>(),
                                  newRect.width<int>(),
                                  newRect.height<int>(),
                                  SWP_NOACTIVATE | SWP_NOZORDER);

Review comment (bug):
I feel like this is weird. We're doing the math unprotected from over/underflow first then casting the result into `ptrdiff_t`.

Shouldn't we be doing a saturated operation on these things or casting them into the math wrappers first and then let it do the saturated subtract? 

The same goes for some of the other saturated casts below. Though I'm not totally clear on what the source types are here that requires us to do the cast to begin with.

Code after:
    adjustedHeight = islandHeight + nonClientSize.height<long>();

    til::size dimensions{ Utils::ClampToShortMax(adjustedWidth, 1),
                          Utils::ClampToShortMax(adjustedHeight, 1) };

    // Find nearest monitor for the position that we've actually settled on
    HMONITOR hMonNearest = MonitorFromRect(&proposedRect, MONITOR_DEFAULTTONEAREST);
    MONITORINFO nearestMonitorInfo;
    nearestMonitorInfo.cbSize = sizeof(MONITORINFO);
    // Get monitor dimensions:
    GetMonitorInfo(hMonNearest, &nearestMonitorInfo);
    const til::size desktopDimensions{ gsl::narrow<short>(nearestMonitorInfo.rcWork.right - nearestMonitorInfo.rcWork.left),
                                       gsl::narrow<short>(nearestMonitorInfo.rcWork.bottom - nearestMonitorInfo.rcWork.top) };

    til::point origin{ (proposedRect.left),
                       (proposedRect.top) };

    if (_logic.IsQuakeWindow())
    {
        // If we just use rcWork by itself, we'll fail to account for the invisible
        // space reserved for the resize handles. So retrieve that size here.
        const til::size ncSize{ _window->GetTotalNonClientExclusiveSize(dpix) };
        const til::size availableSpace = desktopDimensions + nonClientSize;

        origin = til::point{
            ::base::ClampSub<long>(nearestMonitorInfo.rcWork.left, (nonClientSize.width() / 2)),
            (nearestMonitorInfo.rcWork.top)
        };
        dimensions = til::size{
            availableSpace.width(),
            availableSpace.height() / 2
        };
        launchMode = LaunchMode::FocusMode;
    }
    else if (centerOnLaunch)
    {
        // Move our proposed location into the center of that specific monitor.
        origin = til::point{
            (nearestMonitorInfo.rcWork.left + ((desktopDimensions.width() / 2) - (dimensions.width() / 2))),
            (nearestMonitorInfo.rcWork.top + ((desktopDimensions.height() / 2) - (dimensions.height() / 2)))
        };
    }

    const til::rectangle newRect{ origin, dimensions };
    bool succeeded = SetWindowPos(hwnd,
                                  nullptr,
                                  newRect.left<int>(),
                                  newRect.top<int>(),
                                  newRect.width<int>(),
                                  newRect.height<int>(),
                                  SWP_NOACTIVATE | SWP_NOZORDER);