Skip to content

Commit

Permalink
Fix focus when active tab page is changed via the Window menu
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Oct 5, 2024
1 parent 5149e4e commit d989085
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 7 additions & 2 deletions ILSpy/Commands/ShowPane.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ICSharpCode.ILSpy.Docking;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.ViewModels;

namespace ICSharpCode.ILSpy.Commands
Expand Down Expand Up @@ -30,7 +29,13 @@ public TabPageCommand(TabPageModel model)

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;
}
}
}
3 changes: 2 additions & 1 deletion ILSpy/Util/MenuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ static Control CreateMenuItem(TabPageModel pane, DockWorkspace dock)
menuItem.SetBinding(MenuItem.IsCheckedProperty, new Binding(nameof(dock.ActiveTabPage)) {
Source = dock,
ConverterParameter = pane,
Converter = BinaryOperationConverter.Equality
Converter = BinaryOperationConverter.Equality,
Mode = BindingMode.OneWay
});

return menuItem;
Expand Down
17 changes: 17 additions & 0 deletions ILSpy/ViewModels/TabPageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,6 +100,19 @@ public static void ShowTextView(this TabPageModel tabPage, Action<DecompilerText
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<FrameworkElement>()
.FirstOrDefault(item => item.Focusable);

focusable?.Focus();
}
}

public interface IHaveState
Expand Down

0 comments on commit d989085

Please sign in to comment.