Repository: microsoft/terminal
File: src/cascadia/TerminalControl/SearchBoxControl.cpp (C++)

Code before:
        }

        if (CloseAnimation().GetCurrentState() != Media::Animation::ClockState::Active)
        {
            CloseAnimation().Begin();
        }
    }

    // Method Description:
    // - Sets the search box control to its initial state and calls the initialized callback if it's set.
    void SearchBoxControl::_Initialize()
    {
        _UpdateSizeDependents();

        // Search box is in Visible visibility state by default. This is to make
        // sure DesiredSize() returns the correct size for the search box.
        // (DesiredSize() seems to report a size of 0,0 until the control is
        // visible for the first time, i.e not in Collapsed state).
        // Here, we set the search box to "Closed" state (and hence Collapsed
        // visibility) after we've updated the size-dependent properties.
        VisualStateManager::GoToState(*this, L"Closed", false);

        CloseAnimation().Completed([this](auto&&, auto&&) {
            CloseAnimation().Stop();
            VisualStateManager::GoToState(*this, L"Closed", false);
        });

        _initialized = true;
        if (_initializedCallback)
        {
            _initializedCallback();
            _initializedCallback = nullptr;
        }
    }

    bool SearchBoxControl::_AnimationEnabled()
    {
        const auto uiSettings = winrt::Windows::UI::ViewManagement::UISettings{};
        const auto isOsAnimationEnabled = uiSettings.AnimationsEnabled();
        const auto isAppAnimationEnabled = Media::Animation::Timeline::AllowDependentAnimations();
        return isOsAnimationEnabled && isAppAnimationEnabled;
    }

    // Method Description:
    // - Opens the search box taking a callback to be executed when it's opened.
    void SearchBoxControl::Open(std::function<void()> callback)
    {
        // defer opening the search box until we have initialized our size-dependent
        // properties so we don't animate to wrong values.
        if (!_initialized)
        {

Review comment (suggestion):
If it runs async, does it require us to use `get_weak()` here? (...in case that the callback is called after we've already destroyed ourselves accidentally.)

Code after:
        if (OpenAnimation().GetCurrentState() == Media::Animation::ClockState::Active)
        {
            OpenAnimation().Stop();
        }

        if (CloseAnimation().GetCurrentState() != Media::Animation::ClockState::Active)
        {
            CloseAnimation().Begin();
        }
    }

    // Method Description:
    // - Sets the search box control to its initial state and calls the initialized callback if it's set.
    void SearchBoxControl::_Initialize()
    {
        _UpdateSizeDependents();

        // Search box is in Visible visibility state by default. This is to make
        // sure DesiredSize() returns the correct size for the search box.
        // (DesiredSize() seems to report a size of 0,0 until the control is
        // visible for the first time, i.e not in Collapsed state).
        // Here, we set the search box to "Closed" state (and hence Collapsed
        // visibility) after we've updated the size-dependent properties.
        VisualStateManager::GoToState(*this, L"Closed", false);

        CloseAnimation().Completed([weakThis{ get_weak() }](auto&&, auto&&) {
            if (auto searchbox{ weakThis.get() })
            {
                searchbox->CloseAnimation().Stop();
                VisualStateManager::GoToState(*searchbox, L"Closed", false);
            }
        });

        _initialized = true;
        if (_initializedCallback)
        {
            _initializedCallback();
            _initializedCallback = nullptr;
        }
    }

    bool SearchBoxControl::_AnimationEnabled()
    {
        const auto uiSettings = winrt::Windows::UI::ViewManagement::UISettings{};
        const auto isOsAnimationEnabled = uiSettings.AnimationsEnabled();
        const auto isAppAnimationEnabled = Media::Animation::Timeline::AllowDependentAnimations();
        return isOsAnimationEnabled && isAppAnimationEnabled;
    }

    // Method Description:
    // - Opens the search box taking a callback to be executed when it's opened.