Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/SettingContainer.cpp (C++)

Code before:
                                    panelControl.Focus(FocusState::Programmatic);
                                    return;
                                }
                            }
                        }
                        // if we get here, we didn't find something to reasonably focus to.
                    }
                });

                // apply name (automation property)
                Automation::AutomationProperties::SetName(child, RS_(L"SettingContainer_OverrideMessageBaseLayer"));
            }
        }

        _UpdateOverrideSystem();

        if (const auto& child{ GetTemplateChild(L"Expander") })
        {
            if (const auto& expander{ child.try_as<Microsoft::UI::Xaml::Controls::Expander>() })
            {
                // apply header text as name (automation property)
                if (const auto& header{ Header() })
                {
                    if (const auto headerText{ header.try_as<hstring>() })
                    {
                        Automation::AutomationProperties::SetName(expander, *headerText);
                    }
                }

                // apply help text as tooltip and full description (automation property)
                if (const auto& helpText{ HelpText() }; !helpText.empty())
                {
                    Controls::ToolTipService::SetToolTip(expander, box_value(helpText));
                    Automation::AutomationProperties::SetFullDescription(expander, helpText);
                }
            }
        }
        else if (const auto& content{ Content() })
        {
            if (const auto& obj{ content.try_as<DependencyObject>() })
            {
                // apply header text as name (automation property)
                if (const auto& header{ Header() })
                {
                    if (const auto headerText{ header.try_as<hstring>() })
                    {
                        Automation::AutomationProperties::SetName(obj, *headerText);
                    }
                }

                // apply help text as tooltip and full description (automation property)

Review comment (refactor):
i actually think, by Carlos' suggestion, you can simplify even more. After all, the only difference between lines 134,141 and 155,162 is nothing. There's no difference...! so you can simplify the code significantly

Code after:
                                    panelControl.Focus(FocusState::Programmatic);
                                    return;
                                }
                            }
                        }
                        // if we get here, we didn't find something to reasonably focus to.
                    }
                });

                // apply name (automation property)
                Automation::AutomationProperties::SetName(child, RS_(L"SettingContainer_OverrideMessageBaseLayer"));
            }
        }

        _UpdateOverrideSystem();

        // Get the correct base to apply automation properties to
        DependencyObject base{ nullptr };
        if (const auto& child{ GetTemplateChild(L"Expander") })
        {
            if (const auto& expander{ child.try_as<Microsoft::UI::Xaml::Controls::Expander>() })
            {
                base = child;
            }
        }
        else if (const auto& content{ Content() })
        {
            if (const auto& obj{ content.try_as<DependencyObject>() })
            {
                base = obj;
            }
        }

        if (base)
        {
            // apply header as name (automation property)
            if (const auto& header{ Header() })
            {
                if (const auto headerText{ header.try_as<hstring>() })
                {
                    Automation::AutomationProperties::SetName(base, *headerText);
                }
            }

            // apply help text as tooltip and full description (automation property)
            if (const auto& helpText{ HelpText() }; !helpText.empty())
            {
                Controls::ToolTipService::SetToolTip(base, box_value(helpText));
                Automation::AutomationProperties::SetFullDescription(base, helpText);
            }
        }