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

Code before:
{
    // First, restore the window. SetWindowPlacement has a fun undocumented
    // piece of functionality where it will restore the window position
    // _without_ the animation, so use that instead of ShowWindow(SW_RESTORE).
    WINDOWPLACEMENT wpc;
    wpc.length = sizeof(WINDOWPLACEMENT);
    GetWindowPlacement(_window.get(), &wpc);
    wpc.showCmd = SW_RESTORE;
    SetWindowPlacement(_window.get(), &wpc);

    // Now that we're visible, animate the dropdown.
    _doSlideAnimation(dropdownDuration, true);
}

void IslandWindow::_slideUpWindow(const uint32_t dropdownDuration)
{
    // First, animate the window sliding up.
    _doSlideAnimation(dropdownDuration, false);

    // Then, use SetWindowPlacement to minimize without the animation.
    WINDOWPLACEMENT wpc;
    wpc.length = sizeof(WINDOWPLACEMENT);
    GetWindowPlacement(_window.get(), &wpc);
    wpc.showCmd = SW_MINIMIZE;
    SetWindowPlacement(_window.get(), &wpc);
}

// Method Description:
// - Force activate this window. This method will bring us to the foreground and
//   activate us. If the window is minimized, it will restore the window. If the
//   window is on another desktop, the OS will switch to that desktop.
// - If the window is minimized, and dropdownDuration is greater than 0, we'll
//   perform a "slide in" animation. We won't do this if the window is already
//   on the screen (since that seems silly).
// Arguments:
// - dropdownDuration: The duration to play the dropdown animation, in
//   milliseconds. If 0, we won't perform a dropdown animation.
// Return Value:
// - <none>
void IslandWindow::_globalActivateWindow(const uint32_t dropdownDuration)
{
    // From: https://stackoverflow.com/a/59659421
    // > The trick is to make windows ‘think’ that our process and the target
    // > window (hwnd) are related by attaching the threads (using
    // > AttachThreadInput API) and using an alternative API: BringWindowToTop.
    // If the window is minimized, then restore it. We don't want to do this
    // always though, because if you SW_RESTORE a maximized window, it will
    // restore-down the window.
    if (IsIconic(_window.get()))
    {
        if (dropdownDuration > 0)

Review comment (suggestion):
These two seem very similar. If you add a second param `const bool down` (like in `_doSlideAnimation`), you can cut down on this repeated code.

Code after:
{
    til::rectangle fullWindowSize{ GetWindowRect() };
    const double fullHeight = fullWindowSize.height<double>();

    const double animationDuration = dropdownDuration; // use floating-point math throughout
    const auto start = std::chrono::system_clock::now();

    // Do at most dropdownDuration frames. After that, just bail straight to the
    // final state.
    for (uint32_t i = 0; i < dropdownDuration; i++)
    {
        const auto end = std::chrono::system_clock::now();
        const auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        const double dt = ::base::saturated_cast<double>(millis.count());

        if (dt > animationDuration)
        {
            break;
        }

        // If going down, increase the height over time. If going up, decrease the height.
        const double currentHeight = ::base::saturated_cast<double>(
            down ? ((dt / animationDuration) * fullHeight) :
                   ((1.0 - (dt / animationDuration)) * fullHeight));

        wil::unique_hrgn rgn{ CreateRectRgn(0,
                                            0,
                                            fullWindowSize.width<int>(),
                                            ::base::saturated_cast<int>(currentHeight)) };

        SetWindowRgn(_interopWindowHandle, rgn.get(), true);

        // Go immediately into another frame. This prevents the window from
        // doing anything else (tearing our state). A Sleep() here will cause a
        // weird stutter, and causes the animation to not be as smooth.
    }

    // Reset the window.
    SetWindowRgn(_interopWindowHandle, nullptr, true);
}

void IslandWindow::_dropdownWindow(const uint32_t dropdownDuration)
{
    // First, restore the window. SetWindowPlacement has a fun undocumented
    // piece of functionality where it will restore the window position
    // _without_ the animation, so use that instead of ShowWindow(SW_RESTORE).
    WINDOWPLACEMENT wpc{};
    wpc.length = sizeof(WINDOWPLACEMENT);
    GetWindowPlacement(_window.get(), &wpc);
    wpc.showCmd = SW_RESTORE;
    SetWindowPlacement(_window.get(), &wpc);