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

Code before:
    //   this), so instead they go in their own special helper struct.
    // * they all need to be manually revoked in _revokeWindowCallbacks.

    // Register the 'X' button of the window for a warning experience of multiple
    // tabs opened, this is consistent with Alt+F4 closing
    _windowCallbacks.WindowCloseButtonClicked = _window->WindowCloseButtonClicked([this]() {
        _windowLogic.CloseWindow();
    });
    // If the user requests a close in another way handle the same as if the 'X'
    // was clicked.
    _revokers.CloseRequested = _windowLogic.CloseRequested(winrt::auto_revoke, { this, &AppHost::_CloseRequested });

    // Add an event handler to plumb clicks in the titlebar area down to the
    // application layer.
    _windowCallbacks.DragRegionClicked = _window->DragRegionClicked([this]() { _windowLogic.TitlebarClicked(); });

    _windowCallbacks.WindowVisibilityChanged = _window->WindowVisibilityChanged([this](bool showOrHide) { _windowLogic.WindowVisibilityChanged(showOrHide); });

    _windowCallbacks.UpdateSettingsRequested = _window->UpdateSettingsRequested({ this, &AppHost::_requestUpdateSettings });

    _revokers.Initialized = _windowLogic.Initialized(winrt::auto_revoke, { this, &AppHost::_WindowInitializedHandler });
    _revokers.RequestedThemeChanged = _windowLogic.RequestedThemeChanged(winrt::auto_revoke, { this, &AppHost::_UpdateTheme });
    _revokers.FullscreenChanged = _windowLogic.FullscreenChanged(winrt::auto_revoke, { this, &AppHost::_FullscreenChanged });
    _revokers.FocusModeChanged = _windowLogic.FocusModeChanged(winrt::auto_revoke, { this, &AppHost::_FocusModeChanged });
    _revokers.AlwaysOnTopChanged = _windowLogic.AlwaysOnTopChanged(winrt::auto_revoke, { this, &AppHost::_AlwaysOnTopChanged });
    _revokers.ShowTabsFullscreenChanged = _windowLogic.ShowTabsFullscreenChanged(winrt::auto_revoke, { this, &AppHost::_ShowTabsFullscreenChanged });
    _revokers.RaiseVisualBell = _windowLogic.RaiseVisualBell(winrt::auto_revoke, { this, &AppHost::_RaiseVisualBell });
    _revokers.SystemMenuChangeRequested = _windowLogic.SystemMenuChangeRequested(winrt::auto_revoke, { this, &AppHost::_SystemMenuChangeRequested });
    _revokers.ChangeMaximizeRequested = _windowLogic.ChangeMaximizeRequested(winrt::auto_revoke, { this, &AppHost::_ChangeMaximizeRequested });
    _revokers.RequestLaunchPosition = _windowLogic.RequestLaunchPosition(winrt::auto_revoke, { this, &AppHost::_HandleRequestLaunchPosition });

    _windowCallbacks.MaximizeChanged = _window->MaximizeChanged([this](bool newMaximize) {
        if (_windowLogic)
        {
            _windowLogic.Maximized(newMaximize);
        }
    });

    _windowCallbacks.AutomaticShutdownRequested = _window->AutomaticShutdownRequested([this]() {
        // This is the WM_ENDSESSION handler.
        // The event is raised when the user is logged out, because the system is rebooting, etc.
        // Due to the design of WM_ENDSESSION, returning from the message indicates to the OS that it's fine to
        // terminate us at any time. Luckily Windows has never heavily relied on message passing or asynchronous
        // eventing in any of its UI frameworks. It also was clearly impossible to use WaitForMultipleObjects with
        // bWaitAll=TRUE and a timeout to wait for all applications to exit cleanly.
        // As such we attempt to synchronously shut down the app here. Otherwise, it could just call _quit().

        const auto state = ApplicationState::SharedInstance();

        state.PersistedWindowLayouts(nullptr);


Review comment (suggestion):
qq: rather than adding another specific event for this, could we just use the existing `_requestUpdateSettings` handler to update this value when the settings change? I'm not sure the value would ever change outside of a settings change. If that works, you can probably get rid of a lot of plumbing ☺️ 

Code after:
    if (_windowLogic)
    {
        _windowLogic.DismissDialog();
        _windowLogic = nullptr;
    }
}

int64_t AppHost::GetLastActivatedTime() const noexcept
{
    return _lastActivatedTime.QuadPart;
}

// Lazily gets the virtual desktop ID for this window.
winrt::Windows::Foundation::IAsyncOperation<winrt::guid> AppHost::GetVirtualDesktopId()
{
    static constexpr winrt::guid null_guid{};

    if (_virtualDesktopId != null_guid)
    {
        co_return _virtualDesktopId;
    }

    const auto dispatcher = _windowLogic.GetRoot().Dispatcher();
    const auto hwnd = _window->GetHandle();
    const auto weakThis = weak_from_this();
    const auto desktopManager = getDesktopManager();

    if (!hwnd || !desktopManager)
    {
        co_return null_guid;
    }

    // The amazing IVirtualDesktopManager API is cross-process COM into explorer.exe,
    // so we can't call it on the UI thread (= slow & reentrant = bugs/freezes).
    // Fun fact: GetWindowDesktopId() is O(n) over all HWNDs. :)
    co_await winrt::resume_background();

    GUID id;
    if (FAILED_LOG(desktopManager->GetWindowDesktopId(hwnd, &id)))
    {
        co_return null_guid;
    }

    co_await wil::resume_foreground(dispatcher);

    const auto strongThis = weakThis.lock();
    if (!strongThis)
    {
        co_return null_guid;
    }