Repository: microsoft/terminal
File: src/cascadia/TerminalControl/XamlLights.cpp (C++)

Code before:
            CompositionLight(spotLight);
        }
    }

    // Method Description:
    // - This function is called when there are no more target UIElements on the screen
    // - Disposes of composition resources when no longer in use
    // Arguments:
    // - oldElement: unused
    void VisualBellLight::OnDisconnected(UIElement const& /* oldElement */)
    {
        if (CompositionLight())
        {
            CompositionLight(nullptr);
        }
    }

    winrt::hstring VisualBellLight::GetId()
    {
        return VisualBellLight::GetIdStatic();
    }

    void VisualBellLight::OnIsTargetChanged(DependencyObject const& d, DependencyPropertyChangedEventArgs const& e)
    {
        const auto& uielem{ d.try_as<UIElement>() };
        const auto& brush{ d.try_as<Brush>() };

        if (!uielem && !brush)
        {
            // terminate early
            return;
        }

        const auto isAdding = winrt::unbox_value<bool>(e.NewValue());
        const auto id = GetIdStatic();

        isAdding ? (uielem ? XamlLight::AddTargetElement(id, uielem) : XamlLight::AddTargetBrush(id, brush)) :
                   (uielem ? XamlLight::RemoveTargetElement(id, uielem) : XamlLight::RemoveTargetBrush(id, brush));
    }
}


Review comment (performance):
`try_as` doesn't returns `winrt::com_ptr` by value and this code only compiles, because you bind it as a const reference (since it's not possible to create references to temporary values normally).
The compiler puts the return value of `try_as` transparently on the stack and a reference into the `uielem`. In Release builds this is "imperfection" certainly optimized away, but it'd be better and more correct to write this instead:
```cpp
const auto uielem = d.try_as<UIElement>();
const auto brush = d.try_as<Brush>();
```

Code after:
            CompositionLight(spotLight);
        }
    }

    // Method Description:
    // - This function is called when there are no more target UIElements on the screen
    // - Disposes of composition resources when no longer in use
    // Arguments:
    // - oldElement: unused
    void VisualBellLight::OnDisconnected(UIElement const& /* oldElement */)
    {
        if (CompositionLight())
        {
            CompositionLight(nullptr);
        }
    }

    winrt::hstring VisualBellLight::GetId()
    {
        return VisualBellLight::GetIdStatic();
    }

    void VisualBellLight::OnIsTargetChanged(DependencyObject const& d, DependencyPropertyChangedEventArgs const& e)
    {
        const auto uielem{ d.try_as<UIElement>() };
        const auto brush{ d.try_as<Brush>() };

        if (!uielem && !brush)
        {
            // terminate early
            return;
        }

        const auto isAdding = winrt::unbox_value<bool>(e.NewValue());
        const auto id = GetIdStatic();

        if (isAdding)
        {
            if (uielem)
            {
                XamlLight::AddTargetElement(id, uielem);
            }
            else
            {
                XamlLight::AddTargetBrush(id, brush);
            }
        }
        else
        {
            if (uielem)
            {