Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
    // - Send an event (which will be caught by AppHost) to change the show window state of the entire hosting window
    // Arguments:
    // - sender (not used)
    // - args: the arguments specifying how to set the display status to ShowWindow for our window handle
    void TerminalPage::_ShowWindowChangedHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::ShowWindowArgs args)
    {
        ShowWindowChanged.raise(*this, args);
    }

    Windows::Foundation::IAsyncOperation<IVectorView<MatchResult>> TerminalPage::_FindPackagesInCatalogAsync(PackageCatalog catalog, PackageMatchField field, PackageFieldMatchOption matchOption, hstring query)
    {
        FindPackagesOptions findPackagesOptions = WindowsPackageManagerFactory::Instance().CreateFindPackagesOptions();
        PackageMatchFilter filter = WindowsPackageManagerFactory::Instance().CreatePackageMatchFilter();
        filter.Field(field);
        filter.Option(matchOption);
        filter.Value(query);
        findPackagesOptions.Filters().Append(filter);
        findPackagesOptions.ResultLimit(20);
        FindPackagesResult findPackagesResult{ co_await catalog.FindPackagesAsync(findPackagesOptions) };

        co_return findPackagesResult.Matches();
    }

    Windows::Foundation::IAsyncOperation<IVectorView<MatchResult>> TerminalPage::_FindPackageAsync(hstring query)
    {
        co_await winrt::resume_background();

        PackageManager packageManager = WindowsPackageManagerFactory::Instance().CreatePackageManager();
        PackageCatalogReference catalogRef{
            packageManager.GetPredefinedPackageCatalog(PredefinedPackageCatalog::OpenWindowsCatalog)
        };
        ConnectResult connectResult = catalogRef.Connect();
        if (connectResult.Status() != ConnectResultStatus::Ok)
        {
            co_return nullptr;
        }
        PackageCatalog catalog = connectResult.PackageCatalog();

        auto pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Command, PackageFieldMatchOption::StartsWithCaseInsensitive, query);
        if (pkgList.Size() > 0)
        {
            co_return pkgList;
        }
        pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Name, PackageFieldMatchOption::ContainsCaseInsensitive, query);
        if (pkgList.Size() > 0)
        {
            co_return pkgList;
        }
        pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Moniker, PackageFieldMatchOption::ContainsCaseInsensitive, query);
        co_return pkgList;
    }

Review comment (suggestion):
Considering that `_SearchMissingCommandHandler` already asserts that it's on a background thread in the beginning, this may be superfluous.

Code after:
    // - Send an event (which will be caught by AppHost) to change the show window state of the entire hosting window
    // Arguments:
    // - sender (not used)
    // - args: the arguments specifying how to set the display status to ShowWindow for our window handle
    void TerminalPage::_ShowWindowChangedHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::ShowWindowArgs args)
    {
        ShowWindowChanged.raise(*this, args);
    }

    Windows::Foundation::IAsyncOperation<IVectorView<MatchResult>> TerminalPage::_FindPackageAsync(hstring query)
    {
        const PackageManager packageManager = WindowsPackageManagerFactory::CreatePackageManager();
        PackageCatalogReference catalogRef{
            packageManager.GetPredefinedPackageCatalog(PredefinedPackageCatalog::OpenWindowsCatalog)
        };
        catalogRef.PackageCatalogBackgroundUpdateInterval(std::chrono::hours(24));

        ConnectResult connectResult{ nullptr };
        for (int retries = 0;;)
        {
            connectResult = catalogRef.Connect();
            if (connectResult.Status() == ConnectResultStatus::Ok)
            {
                break;
            }

            if (++retries == 3)
            {
                co_return nullptr;
            }
        }

        PackageCatalog catalog = connectResult.PackageCatalog();
        // clang-format off
        static constexpr std::array<WinGetSearchParams, 3> searches{ {
            { .Field = PackageMatchField::Command, .MatchOption = PackageFieldMatchOption::StartsWithCaseInsensitive },
            { .Field = PackageMatchField::Name, .MatchOption = PackageFieldMatchOption::ContainsCaseInsensitive },
            { .Field = PackageMatchField::Moniker, .MatchOption = PackageFieldMatchOption::ContainsCaseInsensitive } } };
        // clang-format on

        PackageMatchFilter filter = WindowsPackageManagerFactory::CreatePackageMatchFilter();
        filter.Value(query);

        FindPackagesOptions options = WindowsPackageManagerFactory::CreateFindPackagesOptions();
        options.Filters().Append(filter);
        options.ResultLimit(20);

        IVectorView<MatchResult> pkgList;
        for (const auto& search : searches)
        {
            filter.Field(search.Field);