Skip to content

Commit

Permalink
Merge pull request #53765 from Cosifne/dev/shech/InheritanceMarginFix…
Browse files Browse the repository at this point in the history
…Focus

Move focus back to text editor when Inheritance Margin is closed
  • Loading branch information
Cosifne authored Jun 17, 2021
2 parents d26255c + d4f8555 commit 49af982
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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);
}
}
}
}
}

0 comments on commit 49af982

Please sign in to comment.