Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Wpf.Ui/Controls/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,8 @@ or PInvoke.WM_NCLBUTTONUP

foreach (TitleBarButton button in _buttons)
{
if (!button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
// Check if button is null to avoid potential NullReferenceException if OnApplyTemplate hasn't been called yet, e.g. when TitleBar has Visibility == Collapsed.
if (button == null || !button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
{

Copilot AI Mar 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_buttons is declared as TitleBarButton[] but is initialized with null elements until OnApplyTemplate() runs. Adding button == null here fixes the immediate NRE, but it leaves nullability inconsistent and still assumes _buttons contains no nulls in the inner loop (e.g., anotherButton.IsHovered). Consider making _buttons a TitleBarButton?[] (or using an _isTemplateApplied flag / Array.Empty<TitleBarButton>() until templated) and iterating only over non-null buttons to keep the hook logic robust and aligned with nullable reference types.

Copilot uses AI. Check for mistakes.
Comment on lines +679 to 681

Copilot AI Mar 6, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression test for the reported scenario (TitleBar Visibility=Collapsed + mouse hover over the window) to ensure the window message hook no longer throws. There are existing FlaUI integration tests for TitleBar behavior, so this case should be covered to prevent reintroducing the NRE.

Copilot uses AI. Check for mistakes.
continue;
}
Expand Down
Loading