Repository: microsoft/terminal
File: src/cascadia/Remoting/WindowManager.cpp (C++)

Code before:
                // Inform the monarch of the time we were last activated
                _monarch.HandleActivatePeasant(lastActivated);
            }
        }

        if (!_isKing)
        {
            return;
        }
        // Here, we're the king!
        //
        // This is where you should do any additional setup that might need to be
        // done when we become the king. This will be called both for the first
        // window, and when the current monarch dies.

        _monarch.WindowCreated({ get_weak(), &WindowManager::_WindowCreatedHandlers });
        _monarch.WindowClosed({ get_weak(), &WindowManager::_WindowClosedHandlers });
        _monarch.FindTargetWindowRequested({ this, &WindowManager::_raiseFindTargetWindowRequested });
        _monarch.ShowNotificationIconRequested([this](auto&&, auto&&) { _ShowNotificationIconRequestedHandlers(*this, nullptr); });
        _monarch.HideNotificationIconRequested([this](auto&&, auto&&) { _HideNotificationIconRequestedHandlers(*this, nullptr); });
        _monarch.QuitAllRequested({ get_weak(), &WindowManager::_QuitAllRequestedHandlers });

        _BecameMonarchHandlers(*this, nullptr);
    }

    bool WindowManager::_areWeTheKing()
    {
        const auto ourPID{ GetCurrentProcessId() };
        const auto kingPID{ _monarch.GetPID() };
        return (ourPID == kingPID);
    }

    Remoting::IPeasant WindowManager::_createOurPeasant(std::optional<uint64_t> givenID,
                                                        const winrt::hstring& givenName)
    {
        auto p = winrt::make_self<Remoting::implementation::Peasant>();
        if (givenID)
        {
            p->AssignID(givenID.value());
        }

        // If the name wasn't specified, this will be an empty string.
        p->WindowName(givenName);
        _peasant = *p;

        // Try to add us to the monarch. If that fails, try to find a monarch
        // again, until we find one (we will eventually find us)
        while (true)
        {
            try
            {

Review comment (suggestion):
Hopefully this originates the right thing for us. We could, if we are so inclined, use our own error constant as well.

Code after:
            // See MSFT:38540483, GH#12774 for details.
            if constexpr (Feature_IsolatedMonarchMode::IsEnabled())
            {
                // Fall back to having a in proc monarch. Were now isolated from
                // other windows. This is a pretty torn state, but at least we
                // didn't just explode.
                TraceLoggingWrite(g_hRemotingProvider,
                                  "WindowManager_NullMonarchIsolateMode",
                                  TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                                  TraceLoggingKeyword(TIL_KEYWORD_TRACE));

                _monarch = winrt::make<winrt::Microsoft::Terminal::Remoting::implementation::Monarch>();
            }
            else
            {
                // The monarch is null. We're hoping that we can find another,
                // hopefully us. We're gonna go back around the loop again and
                // see what happens. If this is really an infinite loop (where
                // the OS won't even give us back US as the monarch), then I
                // suppose we'll find out soon enough.
                TraceLoggingWrite(g_hRemotingProvider,
                                  "WindowManager_NullMonarchTryAgain",
                                  TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                                  TraceLoggingKeyword(TIL_KEYWORD_TRACE));

                winrt::hresult_error(E_UNEXPECTED, L"Did not expect the Monarch to ever be null");
            }
        }

        // We're pretty confident that we have a Monarch here.

        // Save the result of checking if we're the king. We want to avoid
        // unnecessary calls back and forth if we can.
        _isKing = _areWeTheKing();

        TraceLoggingWrite(g_hRemotingProvider,
                          "WindowManager_ConnectedToMonarch",
                          TraceLoggingUInt64(_monarch.GetPID(), "monarchPID", "The PID of the new Monarch"),
                          TraceLoggingBoolean(_isKing, "isKing", "true if we are the new monarch"),
                          TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                          TraceLoggingKeyword(TIL_KEYWORD_TRACE));

        if (_peasant)
        {
            if (const auto& lastActivated{ _peasant.GetLastActivatedArgs() })
            {
                // Inform the monarch of the time we were last activated
                _monarch.HandleActivatePeasant(lastActivated);
            }
        }