Skip to content

Commit

Permalink
Memory footprint improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey M committed Aug 29, 2020
1 parent 6f04022 commit 701475c
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 22 deletions.
20 changes: 20 additions & 0 deletions DPackRx.Tests/Features/CodeBrowserViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public void OnInitialize(string search, CodeModelFilterFlags flags, int expected
Assert.That(viewModel.FilteredMembers.Count, Is.EqualTo(expectedCount));
Assert.That(viewModel.Search, Is.EqualTo(search));
Assert.That(viewModel.Filter, Is.EqualTo(flags));
Assert.That(viewModel.SameType, Is.True);
Assert.That(viewModel.FileName, Is.EqualTo("test"));
Assert.That(viewModel.Title, Is.Not.Null.And.Not.Empty);
Assert.That(viewModel.Title, Contains.Substring(" - test"));
_fileProcessorMock.Verify(p => p.GetMembers(ProcessorFlags.IncludeFileCodeModel, It.IsAny<CodeModelFilterFlags>()));
_optionsServiceMock.Verify(o => o.GetStringOption(viewModel.Feature, "File", string.Empty));
_optionsServiceMock.Verify(o => o.GetStringOption(viewModel.Feature, "Search", string.Empty));
Expand All @@ -145,6 +149,22 @@ public void OnInitialize(string search, CodeModelFilterFlags flags, int expected
_searchMatchServiceMock.Verify(s => s.MatchItems(search, It.IsAny<IEnumerable<IMatchItem>>()), Times.Once);
}

[Test]
public void OnInitialize_NotSameType()
{
_members.Clear();
_members.AddRange(new[]
{
new MemberCodeModel { Name = "Test1", FullName = "Test1", ElementKind = Kind.Class, Rank = 0, Matched = false },
new MemberCodeModel { Name = "Test2", FullName = "Test2", ElementKind = Kind.Class, Rank = 0, Matched = false },
});
var viewModel = GetViewModel();

viewModel.OnInitialize(CodeModelFilterFlags.All);

Assert.That(viewModel.SameType, Is.False);
}

[Test]
public void OnInitialize_ResetSearch()
{
Expand Down
29 changes: 21 additions & 8 deletions DPackRx/Features/CodeBrowser/CodeBrowserControl.xaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<UserControl x:Class="DPackRx.Features.CodeBrowser.CodeBrowserControl"
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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:DPackRx.Features.CodeBrowser"
xmlns:ui="clr-namespace:DPackRx.UI"
xmlns:converters="clr-namespace:DPackRx.UI.Converters"
xmlns:behaviors="clr-namespace:DPackRx.UI.Behaviors"
mc:Ignorable="d"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600"
behaviors:UserControlFocusOnLoad.Enabled="True">

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Must reference resource via pack syntax due to host being VS -->
<ResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/FeatureDictionary.xaml" />
<ui:SharedResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/FeatureDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ResourceDictionary>
</UserControl.Resources>

<DockPanel LastChildFill="True">
Expand Down Expand Up @@ -104,8 +105,20 @@
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ParentFullName, Mode=OneTime}"
ToolTip="{Binding Path=XmlDoc}" />
<TextBlock ToolTip="{Binding Path=XmlDoc}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.SameType, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="True">
<Setter Property="Text" Value="{Binding Name, Mode=OneTime}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=DataContext.SameType, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="False">
<Setter Property="Text" Value="{Binding ParentFullName, Mode=OneTime}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Expand Down
29 changes: 29 additions & 0 deletions DPackRx/Features/CodeBrowser/CodeBrowserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Data;
Expand Down Expand Up @@ -35,7 +36,9 @@ public class CodeBrowserViewModel : FeatureViewModelBase
private readonly IShellImageService _shellImageService;
private readonly ObservableCollection<MemberCodeModel> _sourceMembers;
private readonly CollectionViewSource _members;
private string _title;
private string _search = string.Empty;
private bool _sameType;

#endregion

Expand Down Expand Up @@ -79,6 +82,7 @@ public override void OnInitialize(object argument)
throw new ArgumentException("Invalid initialization argument", nameof(argument));

this.FileName = _optionsService.GetStringOption(this.Feature, "File", this.FileName);
this.Title = $"USysWare Code Browser - {Path.GetFileName(this.FileName)}";
_search = _optionsService.GetStringOption(this.Feature, "Search", _search);
this.Filter = (CodeModelFilterFlags)argument;
var filter = (CodeModelFilterFlags)_optionsService.GetIntOption(this.Feature, "Filter", (int)this.Filter);
Expand Down Expand Up @@ -138,6 +142,16 @@ public override void OnClose(bool apply)

#region Properties

public string Title
{
get { return _title; }
set
{
_title = value;
RaisePropertyChanged(nameof(this.Title));
}
}

/// <summary>
/// Search text.
/// </summary>
Expand Down Expand Up @@ -167,6 +181,19 @@ public ICollectionView Members
get { return _members?.View; }
}

/// <summary>
/// Whether all code model members are from the same type declaration.
/// </summary>
public bool SameType
{
get { return _sameType; }
set
{
_sameType = value;
RaisePropertyChanged(nameof(this.SameType));
}
}

/// <summary>
/// Filtered members.
/// </summary>
Expand Down Expand Up @@ -227,6 +254,8 @@ private void ApplyMembers()
var model = _fileProcessor.GetMembers(flags, this.Filter);
var members = model.Members;
var fileName = model.FileName;
this.SameType = model.Members.Count(
m => (m.ElementKind == Kind.Class) || (m.ElementKind == Kind.Interface) || (m.ElementKind == Kind.Struct) || (m.ElementKind == Kind.Enum)) <= 1;

// Reset search on new file
if (!string.IsNullOrEmpty(this.FileName) && !string.IsNullOrEmpty(fileName) && !fileName.Equals(this.FileName, StringComparison.OrdinalIgnoreCase))
Expand Down
6 changes: 3 additions & 3 deletions DPackRx/Features/CodeBrowser/CodeBrowserWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<platformUI:DialogWindow x:Class="DPackRx.Features.CodeBrowser.CodeBrowserWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DPackRx.Features.CodeBrowser"
xmlns:behaviors="clr-namespace:DPackRx.UI.Behaviors"
xmlns:ui="clr-namespace:DPackRx.UI"
xmlns:platformUI="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.15.0"
mc:Ignorable="d"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:CodeBrowserViewModel, IsDesignTimeCreatable=True}"
Icon="Images/CodeBrowser.ico"
Height="400" Width="600" MinHeight="200" MinWidth="400"
Title="USysWare Code Browser" WindowStartupLocation="CenterOwner" ResizeMode="CanResizeWithGrip" ShowInTaskbar="False"
Title="{Binding Title}" WindowStartupLocation="CenterOwner" ResizeMode="CanResizeWithGrip" ShowInTaskbar="False"
behaviors:WindowCloseOnEsc.Enabled="True"
behaviors:WindowClose.DialogResult="{Binding CloseWindow}">

Expand Down
13 changes: 7 additions & 6 deletions DPackRx/Features/FileBrowser/FileBrowserControl.xaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<UserControl x:Class="DPackRx.Features.FileBrowser.FileBrowserControl"
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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:DPackRx.Features.FileBrowser"
xmlns:ui="clr-namespace:DPackRx.UI"
xmlns:converters="clr-namespace:DPackRx.UI.Converters"
xmlns:behaviors="clr-namespace:DPackRx.UI.Behaviors"
mc:Ignorable="d"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600"
behaviors:UserControlFocusOnLoad.Enabled="True">

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Must reference resource via pack syntax due to host being VS -->
<ResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/FeatureDictionary.xaml" />
<ui:SharedResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/FeatureDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ResourceDictionary>
</UserControl.Resources>

<DockPanel LastChildFill="True">
Expand Down
9 changes: 5 additions & 4 deletions DPackRx/Options/OptionsFileBrowserControl.xaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<UserControl x:Class="DPackRx.Options.OptionsFileBrowserControl"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DPackRx.Options"
xmlns:ui="clr-namespace:DPackRx.UI"
xmlns:behaviors="clr-namespace:DPackRx.UI.Behaviors"
mc:Ignorable="d"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="400">

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Must reference resource via pack syntax due to host being VS -->
<ResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/OptionsDictionary.xaml" />
<ui:SharedResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/OptionsDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Expand Down
3 changes: 2 additions & 1 deletion DPackRx/Options/OptionsGeneralControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DPackRx.Options"
xmlns:ui="clr-namespace:DPackRx.UI"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="400">

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Must reference resource via pack syntax due to host being VS -->
<ResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/OptionsDictionary.xaml" />
<ui:SharedResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/OptionsDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Expand Down
7 changes: 7 additions & 0 deletions DPackRx/Properties/DesignTimeResources.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- Design-time only resource -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/FeatureDictionary.xaml" />
<ResourceDictionary Source="pack://application:,,,/DPackRx;component/UI/Styles/OptionsDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
50 changes: 50 additions & 0 deletions DPackRx/UI/SharedResourceDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Windows;

namespace DPackRx.UI
{
/// <summary>
/// Provides cached hash table / dictionary implementation that contains WPF resources used by components and other elements of a WPF application.
/// </summary>
public class SharedResourceDictionary : ResourceDictionary
{
#region Fields

private Uri _sourceUri;
private static readonly Dictionary<Uri, ResourceDictionary> _sharedDictionaries = new Dictionary<Uri, ResourceDictionary>(10);

#endregion

#region Properties

/// <summary>
/// Uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get { return _sourceUri; }
set
{
_sourceUri = value;

if (value != null)
{
if (_sharedDictionaries.ContainsKey(value))
{
this.MergedDictionaries.Add(_sharedDictionaries[value]);
}
else
{
// Load the dictionary by setting the source of the base class
base.Source = value;

_sharedDictionaries.Add(value, this);
}
}
}
}

#endregion
}
}

0 comments on commit 701475c

Please sign in to comment.