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

Code before:
            auto tabViewItem = tab->GetTabViewItem();
            tabViewItem.Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [this, lastFocusedProfile, tabViewItem]() {
                // _GetIconFromProfile has to run on the main thread
                const auto* const matchingProfile = _settings->FindProfile(lastFocusedProfile);
                if (matchingProfile)
                {
                    tabViewItem.Icon(App::_GetIconFromProfile(*matchingProfile));
                }
            });
        }
    }

    // Method Description:
    // - Get the title of the currently focused terminal control, and set it's
    //   tab's text to that text. If this tab is the focused tab, then also
    //   bubble this title to any listeners of our TitleChanged event.
    // Arguments:
    // - tab: the Tab to update the title for.
    void App::_UpdateTitle(std::shared_ptr<Tab> tab)
    {
        auto newTabTitle = tab->GetFocusedTitle();
        const auto lastFocusedProfileOpt = tab->GetFocusedProfile();
        const auto lastFocusedProfile = lastFocusedProfileOpt.value();
        const auto* const matchingProfile = _settings->FindProfile(lastFocusedProfile);

        auto tabTitle = matchingProfile->GetTabTitle();

        // TODO #608: If the settings don't want the terminal's text in the
        // tab, then display something else.

        if (tabTitle.empty())
        {
            tab->SetTabText(newTabTitle);
            if (_settings->GlobalSettings().GetShowTitleInTitlebar() &&
                tab->IsFocused())
            {
                _titleChangeHandlers(newTabTitle);
            }
        }
        else
        {
            tab->SetTabText(winrt::to_hstring(tabTitle.data()));
            if (_settings->GlobalSettings().GetShowTitleInTitlebar() &&
                tab->IsFocused())
            {
                _titleChangeHandlers(tabTitle);
            }
        }
    }

    // Method Description:

Review comment (refactor):
Can't this just be...

```suggestion
        if (!tableTitle.empty())
        {
             tabTitle = winrt::to_hstring(matchingProfile->GetTabTitle());
        }
```

and then you can de-duplicate the contents of the if/else below since they're both exactly the same except for this parameter?

Code after:
            _ApplyTheme(_settings->GlobalSettings().GetRequestedTheme());

            // repopulate the new tab button's flyout with entries for each
            // profile, which might have changed
            _CreateNewTabFlyout();
        });
    }

    // Method Description:
    // - Get the icon of the currently focused terminal control, and set its
    //   tab's icon to that icon.
    // Arguments:
    // - tab: the Tab to update the title for.
    void App::_UpdateTabIcon(std::shared_ptr<Tab> tab)
    {
        const auto lastFocusedProfileOpt = tab->GetFocusedProfile();
        if (lastFocusedProfileOpt.has_value())
        {
            const auto lastFocusedProfile = lastFocusedProfileOpt.value();

            auto tabViewItem = tab->GetTabViewItem();
            tabViewItem.Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [this, lastFocusedProfile, tabViewItem]() {
                // _GetIconFromProfile has to run on the main thread
                const auto* const matchingProfile = _settings->FindProfile(lastFocusedProfile);
                if (matchingProfile)
                {
                    tabViewItem.Icon(App::_GetIconFromProfile(*matchingProfile));
                }
            });
        }
    }

    // Method Description:
    // - Get the title of the currently focused terminal control, and set it's
    //   tab's text to that text. If this tab is the focused tab, then also
    //   bubble this title to any listeners of our TitleChanged event.
    // Arguments:
    // - tab: the Tab to update the title for.
    void App::_UpdateTitle(std::shared_ptr<Tab> tab)
    {
        auto newTabTitle = tab->GetFocusedTitle();
        const auto lastFocusedProfile = tab->GetFocusedProfile().value();
        const auto* const matchingProfile = _settings->FindProfile(lastFocusedProfile);

        const auto tabTitle = matchingProfile->GetTabTitle();

        // Checks if tab title has been set in the profile settings and
        // updates accordingly.

        const auto newActualTitle = tabTitle.empty() ? newTabTitle : tabTitle;