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

Code before:
        if (_pointerMiddleButtonPressed && !eventArgs.GetCurrentPoint(*this).Properties().IsMiddleButtonPressed())
        {
            _pointerMiddleButtonPressed = false;
            if (const auto tabViewItem{ sender.try_as<MUX::Controls::TabViewItem>() })
            {
                tabViewItem.ReleasePointerCapture(eventArgs.Pointer());
                auto tab = _GetTabByTabViewItem(tabViewItem);
                if (!_pointerMiddleButtonExited && tab)
                {
                    _HandleCloseTabRequested(tab);
                }
            }
            eventArgs.Handled(true);
        }
    }

    // Method Description:
    // - Tracking pointer state for tab remove
    // Arguments:
    // - sender: the control that originated this event (TabViewItem)
    // - eventArgs: the event's constituent arguments
    void TerminalPage::_OnPointerEntered(const IInspectable& sender, const Windows::UI::Xaml::Input::PointerRoutedEventArgs& eventArgs)
    {
        if (eventArgs.GetCurrentPoint(*this).Properties().IsMiddleButtonPressed())
        {
            if (const auto tabViewItem{ sender.try_as<MUX::Controls::TabViewItem>() })
            {
                _pointerMiddleButtonExited = false;
            }
            eventArgs.Handled(true);
        }
    }

    // Method Description:
    // - Tracking pointer state for tab remove
    // Arguments:
    // - sender: the control that originated this event (TabViewItem)
    // - eventArgs: the event's constituent arguments
    void TerminalPage::_OnPointerExited(const IInspectable& sender, const Windows::UI::Xaml::Input::PointerRoutedEventArgs& eventArgs)
    {
        if (eventArgs.GetCurrentPoint(*this).Properties().IsMiddleButtonPressed())
        {
            if (const auto tabViewItem{ sender.try_as<MUX::Controls::TabViewItem>() })
            {
                _pointerMiddleButtonExited = true;
            }
            eventArgs.Handled(true);
        }
    }

    void TerminalPage::_UpdatedSelectedTab(const winrt::TerminalApp::TabBase& tab)

Review comment (suggestion):
Checking this again is unnecessary, isn't it? These event handlers are already specific to the `TabViewItem` and won't be called by anything else. I believe the only reason the other methods call `try_as` is to safely get a reference to the `TabViewItem` instance, but here we don't need the instance at all.

Code after:
    // Method Description:
    // - Tracking pointer state for tab remove
    // Arguments:
    // - sender: the control that originated this event (TabViewItem)
    // - eventArgs: the event's constituent arguments
    void TerminalPage::_OnTabPointerEntered(const IInspectable& /*sender*/, const Windows::UI::Xaml::Input::PointerRoutedEventArgs& eventArgs)
    {
        if (eventArgs.GetCurrentPoint(nullptr).Properties().IsMiddleButtonPressed())
        {
            _tabPointerMiddleButtonExited = false;
            eventArgs.Handled(true);
        }
    }

    // Method Description:
    // - Tracking pointer state for tab remove
    // Arguments:
    // - sender: the control that originated this event (TabViewItem)
    // - eventArgs: the event's constituent arguments
    void TerminalPage::_OnTabPointerExited(const IInspectable& /*sender*/, const Windows::UI::Xaml::Input::PointerRoutedEventArgs& eventArgs)
    {
        if (eventArgs.GetCurrentPoint(nullptr).Properties().IsMiddleButtonPressed())
        {
            _tabPointerMiddleButtonExited = true;
            eventArgs.Handled(true);
        }
    }

    void TerminalPage::_UpdatedSelectedTab(const winrt::TerminalApp::TabBase& tab)
    {
        // Unfocus all the tabs.
        for (const auto& tab : _tabs)
        {
            tab.Focus(FocusState::Unfocused);
        }

        try
        {
            _tabContent.Children().Clear();
            _tabContent.Children().Append(tab.Content());

            // GH#7409: If the tab switcher is open, then we _don't_ want to
            // automatically focus the new tab here. The tab switcher wants
            // to be able to "preview" the selected tab as the user tabs
            // through the menu, but if we toss the focus to the control
            // here, then the user won't be able to navigate the ATS any
            // longer.
            //
            // When the tab switcher is eventually dismissed, the focus will
            // get tossed back to the focused terminal control, so we don't
            // need to worry about focus getting lost.