Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move focus back to text editor when Inheritance Margin is closed #53765

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ public InheritanceGlyphFactory(
var membersOnLine = inheritanceMarginTag.MembersOnLine;
Contract.ThrowIfTrue(membersOnLine.IsEmpty);

// ZoomLevel of textView is percentage based. (e.g. 20 -> 400 means 20% -> 400%)
// and the scaleFactor of CrispImage is 1 based. (e.g 1 means 100%)
var scaleFactor = _textView.ZoomLevel / 100;
return new MarginGlyph.InheritanceMargin(
_threadingContext,
_streamingFindUsagesPresenter,
_classificationTypeMap,
_classificationFormatMap,
_waitIndicator,
inheritanceMarginTag,
scaleFactor);
_textView);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.InheritanceMargin.MarginGlyph
{
Expand All @@ -25,6 +26,7 @@ internal partial class InheritanceMargin
private readonly IStreamingFindUsagesPresenter _streamingFindUsagesPresenter;
private readonly IWaitIndicator _waitIndicator;
private readonly Workspace _workspace;
private readonly IWpfTextView _textView;

public InheritanceMargin(
IThreadingContext threadingContext,
Expand All @@ -33,14 +35,18 @@ public InheritanceMargin(
IClassificationFormatMap classificationFormatMap,
IWaitIndicator waitIndicator,
InheritanceMarginTag tag,
double scaleFactor)
IWpfTextView textView)
{
_threadingContext = threadingContext;
_streamingFindUsagesPresenter = streamingFindUsagesPresenter;
_workspace = tag.Workspace;
_waitIndicator = waitIndicator;
_textView = textView;
InitializeComponent();

// ZoomLevel of textView is percentage based. (e.g. 20 -> 400 means 20% -> 400%)
// and the scaleFactor of CrispImage is 1 based. (e.g 1 means 100%)
var scaleFactor = textView.ZoomLevel;
var viewModel = InheritanceMarginViewModel.Create(classificationTypeMap, classificationFormatMap, tag, scaleFactor);
DataContext = viewModel;
ContextMenu.DataContext = viewModel;
Expand Down Expand Up @@ -99,6 +105,9 @@ private void InheritanceMargin_OnMouseLeave(object sender, MouseEventArgs e)
private void ContextMenu_OnClose(object sender, RoutedEventArgs e)
{
ResetBorderToInitialColor();
// Move the focus back to textView when the context menu is closed.
// It ensures the focus won't be left at the margin
ResetFocus();
}

private void ContextMenu_OnOpen(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -131,5 +140,17 @@ private void ResetBorderToInitialColor()
this.Background = Brushes.Transparent;
this.BorderBrush = Brushes.Transparent;
}

private void ResetFocus()
{
if (!_textView.HasAggregateFocus)
{
var visualElement = _textView.VisualElement;
if (visualElement.Focusable)
{
Keyboard.Focus(visualElement);
}
}
}
}
}