From 7f76b3970135e125604b20e56d352d5363930cf6 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Sat, 30 Nov 2024 11:45:01 -0800 Subject: [PATCH] fix exception in menuitem extension (#156) * fix exception in menuitem extension --- .../Templates/Controls/Helpers/MenuExtensions.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs b/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs index decb5eec..28f0713b 100644 --- a/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs +++ b/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/MenuExtensions.cs @@ -38,13 +38,17 @@ static MenuExtensions() var focusedControl = (Control)AvaloniaLocator.Current.GetRequiredService()! .GetFocusedElement(); - var menuItems = visual.GetLogicalAncestors().OfType(); - var focusedTree = focusedControl!.GetLogicalAncestors(); + if (focusedControl != null) + { + var focusedTree = focusedControl.GetLogicalAncestors(); + var menuItems = visual.GetLogicalAncestors().OfType(); - foreach (MenuItem menuItem in menuItems.Where(item => !focusedTree.Contains(item)) - .ToArray()) - menuItem.Close(); + foreach (MenuItem menuItem in menuItems + .Where(item => !focusedTree.Contains(item)) + .ToArray()) + menuItem.Close(); + } }); })); visual.SetValue(DisposablesProperty, new[] { disposable });