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

Update value tracking to fix a number of UI issues. #54838

Merged
merged 1 commit into from
Jul 15, 2021
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
19 changes: 17 additions & 2 deletions src/VisualStudio/Core/Def/ValueTracking/TreeItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -89,6 +90,16 @@ public TreeItemViewModel(
NotifyPropertyChanged(nameof(ShowGlyph));
}
};

TreeViewModel.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(TreeViewModel.HighlightBrush))
{
// If the highlight changes we need to recalculate the inlines so the
// highlighting is correct
NotifyPropertyChanged(nameof(Inlines));
}
};
}

public virtual void NavigateTo()
Expand Down Expand Up @@ -121,7 +132,7 @@ private ImmutableArray<Inline> CalculateInlines()
});

var spanStartPosition = TextSpan.Start - ClassifiedSpans[0].TextSpan.Start;
var spanEndPosition = TextSpan.End - ClassifiedSpans[0].TextSpan.End;
var highlightSpan = new TextSpan(spanStartPosition, TextSpan.Length);

return classifiedTexts.ToInlines(
TreeViewModel.ClassificationFormatMap,
Expand All @@ -130,7 +141,11 @@ private ImmutableArray<Inline> CalculateInlines()
{
if (TreeViewModel.HighlightBrush is not null)
{
if (position >= spanStartPosition && position <= spanEndPosition)
// Check the span start first because we always want to highlight a run that
// is at the start, even if the TextSpan length is 0. If it's not the start,
// highlighting should still happen if the run position is contained within
// the span.
if (position == highlightSpan.Start || highlightSpan.Contains(position))
{
run.SetValue(
TextElement.BackgroundProperty,
Expand Down
15 changes: 15 additions & 0 deletions src/VisualStudio/Core/Def/ValueTracking/ValueTrackingRoot.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<UserControl x:Class="Microsoft.VisualStudio.LanguageServices.ValueTracking.ValueTrackingRoot"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Microsoft.VisualStudio.LanguageServices.ValueTracking"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
Resources="{StaticResource {x:Static vsshell:VsResourceKeys.ThemedDialogDefaultStylesKey}}">
<Grid x:Name="RootGrid">
<TextBlock Text="{Binding EmptyText}" x:Name="EmptyTextMessage"/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Microsoft.VisualStudio.LanguageServices.ValueTracking
{
/// <summary>
/// Interaction logic for ValueTrackingRoot.xaml
/// </summary>
internal partial class ValueTrackingRoot : UserControl
{
public string EmptyText => ServicesVSResources.Select_an_appropriate_symbol_to_start_value_tracking;

public ValueTrackingRoot()
{
InitializeComponent();
}

public void SetChild(FrameworkElement? child)
{
RootGrid.Children.Clear();

if (child is null)
{
EmptyTextMessage.Visibility = Visibility.Visible;
}
else
{
EmptyTextMessage.Visibility = Visibility.Collapsed;
RootGrid.Children.Add(child);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using Roslyn.Utilities;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace Microsoft.VisualStudio.LanguageServices.ValueTracking
{
[Guid(Guids.ValueTrackingToolWindowIdString)]
internal class ValueTrackingToolWindow : ToolWindowPane
{
private readonly Grid _rootGrid = new();
private readonly ValueTrackingRoot _root = new();

public static ValueTrackingToolWindow? Instance { get; set; }

Expand All @@ -30,8 +33,7 @@ public ValueTrackingTreeViewModel? ViewModel
}

_viewModel = value;
_rootGrid.Children.Clear();
_rootGrid.Children.Add(new ValueTrackingTree(_viewModel));
_root.SetChild(new ValueTrackingTree(_viewModel));
}
}

Expand All @@ -44,20 +46,14 @@ public ValueTrackingTreeViewModel? ViewModel
public ValueTrackingToolWindow() : base(null)
{
Caption = ServicesVSResources.Value_Tracking;

_rootGrid.Children.Add(new TextBlock()
{
Text = ServicesVSResources.Select_an_appropriate_symbol_to_start_value_tracking
});

Content = _rootGrid;
Content = _root;
}

public ValueTrackingToolWindow(ValueTrackingTreeViewModel viewModel)
: base(null)
{
Caption = ServicesVSResources.Value_Tracking;
Content = _rootGrid;
Content = _root;
ViewModel = viewModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
Background="{DynamicResource {x:Static vs:ProgressBarColors.BackgroundBrushKey}}" />

<ScrollViewer Grid.Row="1" Grid.Column="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TreeView x:Name="ValueTrackingTreeView" ItemsSource="{Binding Roots}" BorderThickness="0" PreviewKeyDown="ValueTrackingTreeView_PreviewKeyDown" MouseDown="ValueTrackingTreeView_MouseClickPreview" PreviewMouseDoubleClick="ValueTrackingTreeView_MouseClickPreview">
<TreeView x:Name="ValueTrackingTreeView" ItemsSource="{Binding Roots}" SelectedItemChanged="ValueTrackingTreeView_SelectedItemChanged" BorderThickness="0" PreviewKeyDown="ValueTrackingTreeView_PreviewKeyDown" MouseDown="ValueTrackingTreeView_MouseClickPreview" PreviewMouseDoubleClick="ValueTrackingTreeView_MouseClickPreview">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsNodeExpanded, Mode=TwoWay}" />
Expand Down Expand Up @@ -89,10 +89,10 @@
</Grid.RowDefinitions>

<Label Content="File" Grid.Row="0" Grid.Column="0" x:Name="FileNameLabel" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding SelectedItemFile}" x:Name="FileName" AutomationProperties.LabeledBy="{Binding ElementName=FileNameLabel}" VerticalAlignment="Center" />
<Label Grid.Row="0" Grid.Column="1" Content="{Binding SelectedItemFile}" x:Name="FileName" AutomationProperties.LabeledBy="{Binding ElementName=FileNameLabel}" VerticalAlignment="Center" />

<Label Content="Line" Grid.Row="1" Grid.Column="0" x:Name="LineNumberLabel" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding SelectedItemLine}" x:Name="LineNumber" AutomationProperties.LabeledBy="{Binding ElementName=LineNumberLabel}" VerticalAlignment="Center" />
<Label Grid.Row="1" Grid.Column="1" Content="{Binding SelectedItemLine}" x:Name="LineNumber" AutomationProperties.LabeledBy="{Binding ElementName=LineNumberLabel}" VerticalAlignment="Center" />
</Grid>
</ScrollViewer>
</Grid>
Expand Down
35 changes: 13 additions & 22 deletions src/VisualStudio/Core/Def/ValueTracking/ValueTrackingTree.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Microsoft.VisualStudio.LanguageServices.ValueTracking
{
Expand All @@ -24,9 +12,11 @@ namespace Microsoft.VisualStudio.LanguageServices.ValueTracking
/// </summary>
internal partial class ValueTrackingTree : UserControl
{
private readonly ValueTrackingTreeViewModel _viewModel;

public ValueTrackingTree(ValueTrackingTreeViewModel viewModel)
{
DataContext = viewModel;
DataContext = _viewModel = viewModel;
InitializeComponent();
}

Expand All @@ -46,7 +36,7 @@ private void ValueTrackingTreeView_PreviewKeyDown(object sender, KeyEventArgs e)

// Local Functions

static bool TrySelectItem(TreeViewItemBase? node, bool navigate)
bool TrySelectItem(TreeViewItemBase? node, bool navigate)
{
if (node is null)
{
Expand All @@ -57,7 +47,7 @@ static bool TrySelectItem(TreeViewItemBase? node, bool navigate)
return true;
}

static bool TrySetExpanded(TreeViewItemBase? node, bool expanded)
bool TrySetExpanded(TreeViewItemBase? node, bool expanded)
{
if (node is null)
{
Expand All @@ -68,7 +58,7 @@ static bool TrySetExpanded(TreeViewItemBase? node, bool expanded)
return true;
}

static bool TryToggleExpanded(TreeViewItemBase? node)
bool TryToggleExpanded(TreeViewItemBase? node)
{
return TrySetExpanded(node, node is null ? false : !node.IsNodeExpanded);
}
Expand All @@ -83,14 +73,10 @@ private void ValueTrackingTreeView_MouseClickPreview(object sender, MouseButtonE
}
}

private static void SelectItem(TreeViewItemBase? item, bool navigate = false)
private void SelectItem(TreeViewItemBase? item, bool navigate = false)
{
if (item is null)
{
return;
}
_viewModel.SelectedItem = item;

item.IsNodeSelected = true;
if (navigate && item is TreeItemViewModel navigatableItem)
{
navigatableItem.NavigateTo();
Expand Down Expand Up @@ -124,5 +110,10 @@ private TreeViewItemBase GetPreviousItem()
var item = (TreeViewItemBase)ValueTrackingTreeView.SelectedItem;
return item.GetPreviousInTree();
}

private void ValueTrackingTreeView_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
{
_viewModel.SelectedItem = ValueTrackingTreeView.SelectedItem as TreeViewItemBase;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand All @@ -27,8 +28,8 @@ public Brush? HighlightBrush
public IEditorFormatMapService FormatMapService { get; }
public ObservableCollection<TreeItemViewModel> Roots { get; } = new();

private TreeItemViewModel? _selectedItem;
public TreeItemViewModel? SelectedItem
private TreeViewItemBase? _selectedItem;
public TreeViewItemBase? SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
Expand Down Expand Up @@ -62,7 +63,7 @@ public int LoadingCount
set => SetProperty(ref _loadingCount, value);
}

public bool ShowDetails => SelectedItem is not null;
public bool ShowDetails => SelectedItem is TreeItemViewModel;

public event PropertyChangedEventHandler? PropertyChanged;

Expand All @@ -72,20 +73,43 @@ public ValueTrackingTreeViewModel(IClassificationFormatMap classificationFormatM
ClassificationTypeMap = classificationTypeMap;
FormatMapService = formatMapService;

var properties = FormatMapService.GetEditorFormatMap("text")
.GetProperties(ReferenceHighlightTag.TagId);
var editorMap = FormatMapService.GetEditorFormatMap("text");
SetHighlightBrush(editorMap);

HighlightBrush = properties["Background"] as Brush;
editorMap.FormatMappingChanged += (s, e) =>
{
SetHighlightBrush(editorMap);
};

PropertyChanged += Self_PropertyChanged;
}

private void SetHighlightBrush(IEditorFormatMap editorMap)
{
var properties = editorMap.GetProperties(ReferenceHighlightTag.TagId);
HighlightBrush = properties["Background"] as Brush;
}

private void Self_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SelectedItem))
{
SelectedItemFile = SelectedItem?.FileName ?? "";
SelectedItemLine = SelectedItem?.LineNumber ?? 0;
if (SelectedItem is not null)
{
SelectedItem.IsNodeSelected = true;

if (SelectedItem is TreeItemViewModel itemWithInfo)
{
SelectedItemFile = itemWithInfo?.FileName ?? "";
SelectedItemLine = itemWithInfo?.LineNumber ?? 0;
}
else
{
SelectedItemFile = string.Empty;
SelectedItemLine = 0;
}
}

NotifyPropertyChanged(nameof(ShowDetails));
}

Expand Down