Skip to content
Merged
Changes from all 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
11 changes: 6 additions & 5 deletions src/Wpf.Ui/Controls/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public event TypedEventHandler<TitleBar, RoutedEventArgs> HelpClicked
/// </summary>
public Action<TitleBar, System.Windows.Window>? MinimizeActionOverride { get; set; }

private readonly TitleBarButton[] _buttons = new TitleBarButton[4];
private readonly TitleBarButton?[] _buttons = new TitleBarButton[4];
private readonly TextBlock _titleBlock;
private System.Windows.Window _currentWindow = null!;

Expand Down Expand Up @@ -674,17 +674,18 @@ or PInvoke.WM_NCLBUTTONUP
return IntPtr.Zero;
}

foreach (TitleBarButton button in _buttons)
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 is null || !button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
{
Comment on lines +679 to 681
Copy link

Copilot AI Mar 6, 2026

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;
}

// Fix for when sometimes, button hover backgrounds aren't cleared correctly, causing multiple buttons to appear as if hovered.
foreach (TitleBarButton anotherButton in _buttons)
foreach (TitleBarButton? anotherButton in _buttons)
{
if (anotherButton == button)
if (anotherButton is null || anotherButton == button)
{
continue;
}
Expand Down
Loading