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

Code before:
                _UpdateMRUTab(tab);
            }

            tab.TabViewItem().StartBringIntoView();

            // Raise an event that our title changed
            if (_settings.GlobalSettings().ShowTitleInTitlebar())
            {
                _TitleChangedHandlers(*this, tab.Title());
            }

            auto tab_impl = _GetTerminalTabImpl(tab);
            if (tab_impl)
            {
                auto profile = tab_impl->GetFocusedProfile();
                _UpdateBackground(profile);
            }
        }
        CATCH_LOG();
    }

    void TerminalPage::_UpdateBackground(const winrt::Microsoft::Terminal::Settings::Model::Profile& profile)
    {
        if (profile)
        {
            auto controlSettings = TerminalSettings::CreateWithProfile(_settings, profile, *_bindings);
            if (_settings.GlobalSettings().BgImageForWindow())
            {
                _SetBackgroundImage(controlSettings.DefaultSettings());
            }
        }
    }

    // Method Description:
    // - Responds to the TabView control's Selection Changed event (to move a
    //      new terminal control into focus) when not in in the middle of a tab rearrangement.
    // Arguments:
    // - sender: the control that originated this event
    // - eventArgs: the event's constituent arguments
    void TerminalPage::_OnTabSelectionChanged(const IInspectable& sender, const WUX::Controls::SelectionChangedEventArgs& /*eventArgs*/)
    {
        if (!_rearranging && !_removing)
        {
            auto tabView = sender.as<MUX::Controls::TabView>();
            auto selectedIndex = tabView.SelectedIndex();
            if (selectedIndex >= 0 && selectedIndex < gsl::narrow_cast<int32_t>(_tabs.Size()))
            {
                const auto tab{ _tabs.GetAt(selectedIndex) };
                _UpdatedSelectedTab(tab);
            }
        }

Review comment (suggestion):
may be able to skip building the TerminalSettings here and just go straight for `profile.DefaultAppearance()`, and get `BackgroundImagePath()` out of that

Code after:
                _UpdateMRUTab(tab);
            }

            tab.TabViewItem().StartBringIntoView();

            // Raise an event that our title changed
            if (_settings.GlobalSettings().ShowTitleInTitlebar())
            {
                _TitleChangedHandlers(*this, tab.Title());
            }

            auto tab_impl = _GetTerminalTabImpl(tab);
            if (tab_impl)
            {
                auto profile = tab_impl->GetFocusedProfile();
                _UpdateBackground(profile);
            }
        }
        CATCH_LOG();
    }

    void TerminalPage::_UpdateBackground(const winrt::Microsoft::Terminal::Settings::Model::Profile& profile)
    {
        if (profile && _settings.GlobalSettings().UseBackgroundImageForWindow())
        {
            _SetBackgroundImage(profile.DefaultAppearance());
        }
    }

    // Method Description:
    // - Responds to the TabView control's Selection Changed event (to move a
    //      new terminal control into focus) when not in in the middle of a tab rearrangement.
    // Arguments:
    // - sender: the control that originated this event
    // - eventArgs: the event's constituent arguments
    void TerminalPage::_OnTabSelectionChanged(const IInspectable& sender, const WUX::Controls::SelectionChangedEventArgs& /*eventArgs*/)
    {
        if (!_rearranging && !_removing)
        {
            auto tabView = sender.as<MUX::Controls::TabView>();
            auto selectedIndex = tabView.SelectedIndex();
            if (selectedIndex >= 0 && selectedIndex < gsl::narrow_cast<int32_t>(_tabs.Size()))
            {
                const auto tab{ _tabs.GetAt(selectedIndex) };
                _UpdatedSelectedTab(tab);
            }
        }
    }

    // Method Description:
    // - Updates all tabs with their current index in _tabs.