Skip to content

Commit

Permalink
Extracted DeclarationToIcon base converter, restored FindSymbol icons…
Browse files Browse the repository at this point in the history
…, tweaked search result formatting.
  • Loading branch information
retailcoder committed Apr 23, 2021
1 parent 6f06abc commit b924ce3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 77 deletions.
5 changes: 1 addition & 4 deletions Rubberduck.Core/UI/Command/ComCommands/FindSymbolCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Rubberduck.UI.Command.ComCommands
public class FindSymbolCommand : ComCommandBase
{
private readonly RubberduckParserState _state;
private readonly DeclarationIconCache _iconCache;
private readonly NavigateCommand _navigateCommand;

public FindSymbolCommand(
Expand All @@ -26,14 +25,12 @@ public class FindSymbolCommand : ComCommandBase
: base(vbeEvents)
{
_state = state;
_iconCache = iconCache;

_navigateCommand = new NavigateCommand(selectionService);
}

protected override void OnExecute(object parameter)
{
var viewModel = new FindSymbolViewModel(_state.AllUserDeclarations, _iconCache);
var viewModel = new FindSymbolViewModel(_state.AllUserDeclarations);
var view = new FindSymbolDialog(viewModel);
{
viewModel.Navigate += (sender, e) => { view.Hide(); };
Expand Down
120 changes: 82 additions & 38 deletions Rubberduck.Core/UI/Converters/CodeExplorerNodeToIconConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@

namespace Rubberduck.UI.Converters
{
public class CodeExplorerNodeToIconConverter : ImageSourceConverter, IMultiValueConverter
public class DeclarationToIconConverter : ImageSourceConverter
{
private static readonly ImageSource NullIcon = ToImageSource(CodeExplorerUI.status_offline);
private static readonly ImageSource ExceptionIcon = ToImageSource(CodeExplorerUI.exclamation);
private static readonly ImageSource ProjectIcon = ToImageSource(CodeExplorerUI.ObjectLibrary);
private static readonly ImageSource InterfaceIcon = ToImageSource(CodeExplorerUI.ObjectInterface);
private static readonly ImageSource PredeclaredIcon = ToImageSource(CodeExplorerUI.ObjectClassPredeclared);
private static readonly ImageSource NullIcon = ToImageSource(CodeExplorerUI.status_offline);
private static readonly ImageSource TestMethodIcon = ToImageSource(CodeExplorerUI.ObjectTestMethod);

private static readonly ImageSource OpenFolderIcon = ToImageSource(CodeExplorerUI.FolderOpen);
private static readonly ImageSource ClosedFolderIcon = ToImageSource(CodeExplorerUI.FolderClosed);

private static readonly ImageSource ReferenceFolderIcon = ToImageSource(CodeExplorerUI.ObjectAssembly);
private static readonly ImageSource ReferenceIcon = ToImageSource(CodeExplorerUI.Reference);
private static readonly ImageSource LockedReferenceIcon = ToImageSource(CodeExplorerUI.LockedReference);
private static readonly ImageSource BrokenReferenceIcon = ToImageSource(CodeExplorerUI.BrokenReference);
protected ImageSource NullIconSource => NullIcon;
protected ImageSource ExceptionIconSource => ExceptionIcon;

private static readonly IDictionary<DeclarationType, ImageSource> DeclarationIcons = new Dictionary<DeclarationType, ImageSource>
{
Expand Down Expand Up @@ -57,16 +51,87 @@ public class CodeExplorerNodeToIconConverter : ImageSourceConverter, IMultiValue
{ DeclarationType.PropertySet, ToImageSource(CodeExplorerUI.ObjectPropertySet)},
{ DeclarationType.UserDefinedType, ToImageSource(CodeExplorerUI.ObjectValueType)},
{ DeclarationType.UserDefinedTypeMember, ToImageSource(CodeExplorerUI.ObjectField)},
{ DeclarationType.Variable, ToImageSource(CodeExplorerUI.ObjectField)}
{ DeclarationType.Variable, ToImageSource(CodeExplorerUI.ObjectField)},
{ DeclarationType.Parameter, ToImageSource(CodeExplorerUI.ObjectField)},
};

public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as ICodeExplorerNode)?.Declaration is null)
if (value == null)
{
return NullIcon;
}

if (value is Declaration declaration)
{
if (declaration is ClassModuleDeclaration classModule)
{
if (classModule.QualifiedModuleName.ComponentType == VBEditor.SafeComWrappers.ComponentType.UserForm)
{
// a form has a predeclared ID, but we want it to have a UserForm icon:
return DeclarationIcons[DeclarationType.UserForm];
}
else
{
if (classModule.IsInterface || classModule.Annotations.Any(annotation => annotation.Annotation is InterfaceAnnotation))
{
return InterfaceIcon;
}
if (classModule.HasPredeclaredId)
{
return PredeclaredIcon;
}
return DeclarationIcons.ContainsKey(classModule.DeclarationType)
? DeclarationIcons[classModule.DeclarationType]
: NullIcon;

}
}
else
{
if (DeclarationIcons.ContainsKey(declaration.DeclarationType))
{
if (declaration.Annotations.Any(a => a.Annotation is TestMethodAnnotation))
{
return TestMethodIcon;
}
else
{
return DeclarationIcons[declaration.DeclarationType];
}
}
return NullIcon;
}
}
else
{
return null;
//throw new InvalidCastException($"Expected 'Declaration' value, but the type was '{value.GetType().Name}'");
}
}
}

public class CodeExplorerNodeToIconConverter : DeclarationToIconConverter, IMultiValueConverter
{
private static readonly ImageSource ProjectIcon = ToImageSource(CodeExplorerUI.ObjectLibrary);

private static readonly ImageSource OpenFolderIcon = ToImageSource(CodeExplorerUI.FolderOpen);
private static readonly ImageSource ClosedFolderIcon = ToImageSource(CodeExplorerUI.FolderClosed);

private static readonly ImageSource ReferenceFolderIcon = ToImageSource(CodeExplorerUI.ObjectAssembly);
private static readonly ImageSource ReferenceIcon = ToImageSource(CodeExplorerUI.Reference);

private static readonly ImageSource LockedReferenceIcon = ToImageSource(CodeExplorerUI.LockedReference);
private static readonly ImageSource BrokenReferenceIcon = ToImageSource(CodeExplorerUI.BrokenReference);


public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as ICodeExplorerNode)?.Declaration is null)
{
return NullIconSource;
}

switch (value)
{
case CodeExplorerProjectViewModel _:
Expand All @@ -82,34 +147,13 @@ public override object Convert(object value, Type targetType, object parameter,
case CodeExplorerCustomFolderViewModel folder:
return folder.IsExpanded ? OpenFolderIcon : ClosedFolderIcon;
case CodeExplorerComponentViewModel component:
if (component.IsPredeclared)
{
return PredeclaredIcon;
}

if (component.Declaration is null)
{
return ExceptionIcon;
}

if (component.Declaration is ClassModuleDeclaration classModule &&
(classModule.IsInterface || classModule.Annotations.Any(annotation => annotation.Annotation is InterfaceAnnotation)))
return base.Convert(component.Declaration, targetType, parameter, culture);
default:
if (value is ICodeExplorerNode node)
{
return InterfaceIcon;
return base.Convert(node.Declaration, targetType, parameter, culture);
}

var isUserForm = component.Declaration.QualifiedModuleName.ComponentType == VBEditor.SafeComWrappers.ComponentType.UserForm;
return DeclarationIcons.ContainsKey(component.Declaration.DeclarationType)
? DeclarationIcons[isUserForm ? DeclarationType.UserForm : component.Declaration.DeclarationType]
: ExceptionIcon;
default:
return value is ICodeExplorerNode node &&
node.Declaration != null &&
DeclarationIcons.ContainsKey(node.Declaration.DeclarationType)
? node.Declaration.Annotations.Any(a => a.Annotation is TestMethodAnnotation)
? TestMethodIcon
: DeclarationIcons[node.Declaration.DeclarationType]
: ExceptionIcon;
return ExceptionIconSource;
}
}

Expand Down
4 changes: 3 additions & 1 deletion Rubberduck.Core/UI/Converters/SearchResultToXamlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public object Convert(object value, Type targetType, object parameter, CultureIn

var highlightRun = new Run(escapedXml.Substring(highlight.StartColumn, highlight.EndColumn - highlight.StartColumn + 1))
{
Background = Brushes.Yellow
Background = Brushes.Yellow,
Foreground = Brushes.Black,
FontWeight = FontWeights.Bold
};
textBlock.Inlines.Add(highlightRun);

Expand Down
23 changes: 13 additions & 10 deletions Rubberduck.Core/UI/FindSymbol/FindSymbolControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Rubberduck.UI.FindSymbol"
xmlns:converters="clr-namespace:Rubberduck.UI.Converters"
mc:Ignorable="d"
d:DesignHeight="24" d:DesignWidth="270"
d:DataContext="{d:DesignInstance {x:Type local:FindSymbolViewModel}, IsDesignTimeCreatable=False}">
Expand All @@ -17,7 +18,7 @@
<UserControl.Resources>
<BitmapImage x:Key="ArrowImage" UriSource="pack://application:,,,/Rubberduck.Resources;component/Icons/Fugue/arrow.png" />
<local:SearchBoxMultiBindingConverter x:Key="SearchBoxTextConverter" />
<KeyBinding x:Key="EnterAndGo" Key="Return" Command="{Binding GoCommand}" />
<converters:DeclarationToIconConverter x:Key="IconConverter" />
</UserControl.Resources>

<Grid>
Expand All @@ -30,10 +31,13 @@
<ComboBox x:Name="searchComboBox"
IsEditable="True"
ItemsSource="{Binding MatchResults}"
SelectedItem="{Binding SelectedItem}"
IsTextSearchCaseSensitive="False"
IsTextSearchEnabled="True"
TextSearch.TextPath="IdentifierName"
VirtualizingPanel.IsVirtualizing="True">
TextSearch.TextPath="IdentifierName">
<ComboBox.Resources>
<KeyBinding x:Key="EnterGo" Key="Return" Command="{Binding GoCommand}" />
</ComboBox.Resources>
<ComboBox.Text>
<MultiBinding Converter="{StaticResource SearchBoxTextConverter}">
<Binding Path="SearchString" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged" />
Expand All @@ -44,16 +48,15 @@
<DataTemplate DataType="local:SearchResult">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="24" />
<ColumnDefinition MinWidth="168" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Height="16" Width="16" Margin="2" Source="{Binding Icon, Mode=OneTime}" />
<Image Grid.Column="0" Height="16" Width="16" Margin="2" HorizontalAlignment="Center"
Source="{Binding Declaration, Converter={StaticResource IconConverter}, Mode=OneTime}"
ToolTip="{Binding Declaration.DeclarationType}" />
<TextBlock Grid.Column="1" Text="{Binding IdentifierName}" FontWeight="Bold" MinWidth="160" VerticalAlignment="Center" />
<TextBlock Grid.Column="2" Text="{Binding Location}" VerticalAlignment="Center" />
<TextBlock Grid.Column="2" Text="{Binding Location}" VerticalAlignment="Center" HorizontalAlignment="Right" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
Expand Down
17 changes: 5 additions & 12 deletions Rubberduck.Core/UI/FindSymbol/FindSymbolControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,22 @@ public FindSymbolControl()

private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (ViewModel == null)
{
return;
}

ViewModel.Execute();
ViewModel?.Execute();
e.Handled = true;
}

private void CommandBinding_OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (ViewModel == null)
{
return;
}

e.CanExecute = ViewModel.CanExecute();
e.CanExecute = ViewModel?.CanExecute() ?? false;
e.Handled = true;
}

private void FindSymbolControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
searchComboBox.Focus();
}

// doing this navigates on arrow-up/down, which isn't expected
//private void searchComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) => ViewModel?.Execute();
}
}
14 changes: 5 additions & 9 deletions Rubberduck.Core/UI/FindSymbol/FindSymbolViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public class FindSymbolViewModel : INotifyPropertyChanged
DeclarationType.Project
};

public FindSymbolViewModel(IEnumerable<Declaration> declarations, DeclarationIconCache cache)
public FindSymbolViewModel(IEnumerable<Declaration> declarations)
{
_declarations = declarations.Where(declaration => !ExcludedTypes.Contains(declaration.DeclarationType)).ToList();
_cache = cache;

Search(string.Empty);
}
Expand Down Expand Up @@ -50,7 +49,6 @@ public void OnNavigate()
}

private readonly IEnumerable<Declaration> _declarations;
private readonly DeclarationIconCache _cache;

private void Search(string value)
{
Expand All @@ -70,8 +68,8 @@ private IEnumerable<SearchResult> GetSearchResultCollectionOfString(string value
var results = _declarations
.Where(declaration => string.IsNullOrEmpty(value) || declaration.IdentifierName.ToLowerInvariant().Contains(lower))
.OrderBy(declaration => declaration.IdentifierName)
.Take(80)
.Select(declaration => new SearchResult(declaration, _cache[declaration]));
.Take(30)
.Select(declaration => new SearchResult(declaration));

return results;
}
Expand All @@ -85,6 +83,8 @@ public string SearchString
if (_searchString != value)
{
_searchString = value;
OnPropertyChanged();

Search(value);
}
}
Expand All @@ -102,10 +102,6 @@ public SearchResult SelectedItem
_searchString = value?.IdentifierName;
OnPropertyChanged();
}
if (_selectedItem != null)
{
Execute();
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions Rubberduck.Core/UI/FindSymbol/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ namespace Rubberduck.UI.FindSymbol
{
public class SearchResult
{
public SearchResult(Declaration declaration, BitmapImage icon)
public SearchResult(Declaration declaration)
{
Declaration = declaration;
Icon = icon;
}

public Declaration Declaration { get; }

public string IdentifierName => Declaration.IdentifierName;
public string Location => Declaration.Scope;

public ImageSource Icon { get; }
}

public class SearchBoxMultiBindingConverter : IMultiValueConverter
Expand Down

0 comments on commit b924ce3

Please sign in to comment.