From d989085a5881ce85bd36b20e971c429e2b91f142 Mon Sep 17 00:00:00 2001 From: tom-englert Date: Sat, 5 Oct 2024 19:43:46 +0200 Subject: [PATCH] Fix focus when active tab page is changed via the Window menu --- ILSpy/Commands/ShowPane.cs | 9 +++++++-- ILSpy/Util/MenuService.cs | 3 ++- ILSpy/ViewModels/TabPageModel.cs | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ILSpy/Commands/ShowPane.cs b/ILSpy/Commands/ShowPane.cs index 372081e0b..1c6694bd9 100644 --- a/ILSpy/Commands/ShowPane.cs +++ b/ILSpy/Commands/ShowPane.cs @@ -1,5 +1,4 @@ using ICSharpCode.ILSpy.Docking; -using ICSharpCode.ILSpy.Properties; using ICSharpCode.ILSpy.ViewModels; namespace ICSharpCode.ILSpy.Commands @@ -30,7 +29,13 @@ namespace ICSharpCode.ILSpy.Commands public override void Execute(object parameter) { - DockWorkspace.Instance.ActiveTabPage = model; + var workspace = DockWorkspace.Instance; + + // ensure the tab control is focused before setting the active tab page, else the tab will not be focused + workspace.ActiveTabPage?.Focus(); + // reset first, else clicking on the already active tab will not focus the tab and the menu checkmark will not be updated + workspace.ActiveTabPage = null; + workspace.ActiveTabPage = model; } } } \ No newline at end of file diff --git a/ILSpy/Util/MenuService.cs b/ILSpy/Util/MenuService.cs index a42e8c35a..6b1d2e29d 100644 --- a/ILSpy/Util/MenuService.cs +++ b/ILSpy/Util/MenuService.cs @@ -207,7 +207,8 @@ namespace ICSharpCode.ILSpy.Util menuItem.SetBinding(MenuItem.IsCheckedProperty, new Binding(nameof(dock.ActiveTabPage)) { Source = dock, ConverterParameter = pane, - Converter = BinaryOperationConverter.Equality + Converter = BinaryOperationConverter.Equality, + Mode = BindingMode.OneWay }); return menuItem; diff --git a/ILSpy/ViewModels/TabPageModel.cs b/ILSpy/ViewModels/TabPageModel.cs index abcaa4666..691d7d28c 100644 --- a/ILSpy/ViewModels/TabPageModel.cs +++ b/ILSpy/ViewModels/TabPageModel.cs @@ -17,10 +17,14 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.Linq; using System.Threading.Tasks; +using System.Windows; using ICSharpCode.ILSpy.TextView; +using TomsToolbox.Wpf; + namespace ICSharpCode.ILSpy.ViewModels { public class TabPageModel : PaneModel @@ -96,6 +100,19 @@ namespace ICSharpCode.ILSpy.ViewModels tabPage.Title = Properties.Resources.Decompiling; action(textView); } + + public static void Focus(this TabPageModel tabPage) + { + if (tabPage.Content is not FrameworkElement content) + return; + + var focusable = content + .VisualDescendantsAndSelf() + .OfType() + .FirstOrDefault(item => item.Focusable); + + focusable?.Focus(); + } } public interface IHaveState