Skip to content

Commit

Permalink
Refactor and fix Highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Jan 30, 2017
1 parent 2486461 commit 43bae59
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 75 deletions.
77 changes: 6 additions & 71 deletions CodeNav/CodeNav.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,73 +93,6 @@ private static Window GetWindow(IWpfTextViewHost textViewHost, _DTE dte)
return null;
}

private static void GetItemsToHighlight(List<string> list, CodeElement element)
{
list.Add(element.FullName);

var parent = element.Collection.Parent;
if (parent == null || parent is CodeElement == false) return;

GetItemsToHighlight(list, parent);
}

private static void UnHighlight(List<CodeItem> document, List<string> itemNames)
{
foreach (var name in itemNames)
{
var item = FindCodeItem(document, name);
if (item == null) return;

item.Foreground = new SolidColorBrush(Colors.Black);

if (item is CodeClassItem)
{
(item as CodeClassItem).BorderBrush = new SolidColorBrush(Colors.DarkGray);
}
}
}

private static void Highlight(List<CodeItem> document, List<string> itemNames)
{
foreach (var name in itemNames)
{
var item = FindCodeItem(document, name);
if (item == null) return;

item.Foreground = new SolidColorBrush(Colors.SteelBlue);

if (item is CodeClassItem)
{
(item as CodeClassItem).BorderBrush = new SolidColorBrush(Colors.SteelBlue);
}
}
}

private static CodeItem FindCodeItem(List<CodeItem> items, string itemFullName)
{
foreach (var item in items)
{
if (item.FullName.Equals(itemFullName))
{
return item;
}

if (item is CodeClassItem)
{
var classItem = (CodeClassItem)item;
if (classItem.Members.Any())
{
var found = FindCodeItem(classItem.Members, itemFullName);
if (found != null)
{
return found;
}
}
}
}
return null;
}

public void UpdateDocument(Window gotFocus = null)
{
// Do we have a text document in the activated window
Expand Down Expand Up @@ -263,6 +196,7 @@ private void VSColorTheme_ThemeChanged(ThemeChangedEventArgs e)
{
((GridSplitter)_codeNavGrid.Children[0]).Background =
ToBrush(EnvironmentColors.EnvironmentBackgroundColorKey);
HighlightHelper.SetForeground(CodeDocumentViewModel?.CodeDocument);
}

private void UpdateCurrentItem()
Expand All @@ -275,16 +209,16 @@ private void UpdateCurrentItem()

if (currentFunctionElement == null)
{
UnHighlight(CodeDocumentViewModel.CodeDocument, _highlightedItems);
HighlightHelper.UnHighlight(CodeDocumentViewModel.CodeDocument, _highlightedItems);
return;
}

UnHighlight(CodeDocumentViewModel.CodeDocument, _highlightedItems);
HighlightHelper.UnHighlight(CodeDocumentViewModel.CodeDocument, _highlightedItems);

_highlightedItems = new List<string>();
GetItemsToHighlight(_highlightedItems, currentFunctionElement);
HighlightHelper.GetItemsToHighlight(_highlightedItems, currentFunctionElement);

Highlight(CodeDocumentViewModel.CodeDocument, _highlightedItems);
HighlightHelper.Highlight(CodeDocumentViewModel.CodeDocument, _highlightedItems);
}

private void Splitter_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) =>
Expand Down Expand Up @@ -350,6 +284,7 @@ private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerComplet
_cache = (List<CodeItem>)e.Result;

VisibilityHelper.SetControlVisibility(_codeNavColumn, !CodeDocumentViewModel.CodeDocument.Any());
HighlightHelper.SetForeground(CodeDocumentViewModel?.CodeDocument);

stopwatch.Stop();
LogHelper.Log($"RunWorkerCompleted in {stopwatch.ElapsedMilliseconds} ms");
Expand Down
1 change: 1 addition & 0 deletions CodeNav/CodeNav.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="FilterToolWindow.Designer.cs">
<DependentUpon>FilterToolWindow.cs</DependentUpon>
</Compile>
<Compile Include="Helpers\HighlightHelper.cs" />
<Compile Include="Helpers\LogHelper.cs" />
<Compile Include="Helpers\SortHelper.cs" />
<Compile Include="Helpers\VisibilityHelper.cs" />
Expand Down
4 changes: 2 additions & 2 deletions CodeNav/CodeViewUserControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
Path=ActualWidth, Converter={StaticResource WidthMinusMarginConverter}, ConverterParameter=15}" ToolTip="{Binding Tooltip}">
<Frame Source="{Binding IconPath}" Margin="0,0,3,0" />
<TextBlock>
<Run Text="{Binding Name}" FontSize="11" Foreground="{DynamicResource {x:Static vs_shell:EnvironmentColors.ToolWindowTextBrushKey}}" Tag="{Binding Name}" />
<Run Text="{Binding Name}" FontSize="11" Foreground="{Binding Foreground}" Tag="{Binding Name}" />
<Run Text="{Binding Type}" FontSize="10" Foreground="DarkGray" />
<Run Text="{Binding Parameters}" FontSize="10" Foreground="DarkGray" />
</TextBlock>
Expand All @@ -92,7 +92,7 @@
<StackPanel Orientation="Horizontal">
<Frame Source="{Binding IconPath}" Margin="0,0,3,0" />
<TextBlock>
<Run Text="{Binding Name}" FontSize="11" Foreground="{DynamicResource {x:Static vs_shell:EnvironmentColors.ToolWindowTextBrushKey}}" />
<Run Text="{Binding Name}" FontSize="11" Foreground="{Binding Foreground}" />
<Run Text="{Binding Parameters}" FontSize="10" Foreground="DarkGray" />
</TextBlock>
</StackPanel>
Expand Down
108 changes: 108 additions & 0 deletions CodeNav/Helpers/HighlightHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using CodeNav.Models;
using EnvDTE;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using Color = System.Windows.Media.Color;

namespace CodeNav.Helpers
{
public static class HighlightHelper
{
public static void UnHighlight(List<CodeItem> document, List<string> itemNames)
{
foreach (var name in itemNames)
{
var item = FindCodeItem(document, name);
if (item == null) return;

item.Foreground = ToBrush(EnvironmentColors.ToolWindowTextColorKey);

if (item is CodeClassItem)
{
(item as CodeClassItem).BorderBrush = new SolidColorBrush(Colors.DarkGray);
}
}
}

public static void Highlight(List<CodeItem> document, List<string> itemNames)
{
foreach (var name in itemNames)
{
var item = FindCodeItem(document, name);
if (item == null) return;

item.Foreground = new SolidColorBrush(Colors.SteelBlue);

if (item is CodeClassItem)
{
(item as CodeClassItem).BorderBrush = new SolidColorBrush(Colors.SteelBlue);
}
}
}

public static void GetItemsToHighlight(List<string> list, CodeElement element)
{
list.Add(element.FullName);

var parent = element.Collection.Parent;
if (parent == null || parent is CodeElement == false) return;

GetItemsToHighlight(list, parent);
}

public static void SetForeground(IEnumerable<CodeItem> items)
{
foreach (var item in items)
{
item.Foreground = ToBrush(EnvironmentColors.ToolWindowTextColorKey);

if (item is CodeClassItem)
{
var classItem = (CodeClassItem) item;
if (classItem.Members.Any())
{
SetForeground(classItem.Members);
}
}
}
}

private static SolidColorBrush ToBrush(ThemeResourceKey key)
{
var color = VSColorTheme.GetThemedColor(key);
return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
}

private static CodeItem FindCodeItem(List<CodeItem> items, string itemFullName)
{
foreach (var item in items)
{
if (item.FullName.Equals(itemFullName))
{
return item;
}

if (item is CodeClassItem)
{
var classItem = (CodeClassItem)item;
if (classItem.Members.Any())
{
var found = FindCodeItem(classItem.Members, itemFullName);
if (found != null)
{
return found;
}
}
}
}
return null;
}
}
}
4 changes: 2 additions & 2 deletions CodeNav/Models/CodeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Visibility IsVisible
set
{
_visibility = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsVisible"));
NotifyOfPropertyChange();
}
}
#endregion
Expand All @@ -45,7 +45,7 @@ public SolidColorBrush Foreground
set
{
_foreground = value;
OnPropertyChanged(new PropertyChangedEventArgs("Foreground"));
NotifyOfPropertyChange();
}
}
#endregion
Expand Down

0 comments on commit 43bae59

Please sign in to comment.