Skip to content

Commit

Permalink
Add property display names
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed Nov 7, 2019
1 parent 075bedc commit d71ef9a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
/CommandLineAssociationManager/bin/x64/Release
/AssociationManager/bin/x64/Release
/GongSolutions.Wpf.DragDrop/bin/Release/NET35
/CommandLine/x64/Debug
/CommandLineAssociationManager/bin/x64/Debug
/ContextMenuHandler/x64/Debug
/PropertyHandler/x64/Debug
/Setup/obj/x64/Debug
/x64/Debug
9 changes: 5 additions & 4 deletions AssociationManager/ProfilesView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ public void DragOver(IDropInfo dropInfo)
{
// Check property is not already there
var ti = dropInfo.Data as TreeItem;
string property = state.GetSystemPropertyName(ti);
var sp = ti.Item as SystemProperty;

if (property != null && !SelectedProfile.HasPropertyInFullDetails(property))
if (sp != null && !SelectedProfile.HasPropertyInFullDetails(sp.FullName))
{
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
dropInfo.Effects = DragDropEffects.Copy;
Expand Down Expand Up @@ -491,9 +491,10 @@ public void Drop(IDropInfo dropInfo)
// dropInfo.InsertPosition & ~RelativeInsertPosition.TargetItemCenter,
// dropInfo.TargetItem == null ? "null" : ((TreeItem)dropInfo.TargetItem).Name));

string property = state.GetSystemPropertyName((TreeItem)dropInfo.Data);
var ti = dropInfo.Data as TreeItem;
var sp = ti.Item as SystemProperty;

SelectedProfile.AddFullDetailsProperty(property, target, before);
SelectedProfile.AddFullDetailsProperty(sp.FullName, target, before);
IsDirty = true;
}
else if (Source == ProfileControls.FullDetails)
Expand Down
144 changes: 96 additions & 48 deletions AssociationManager/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Win32;
using System.Runtime.InteropServices;
using AssociationMessages;
using System.Diagnostics;

namespace FileMetadataAssociationManager
{
Expand Down Expand Up @@ -95,6 +96,7 @@ public void SortExtensions()
#else
public void PopulateSystemProperties()
{
List<SystemProperty> systemProperties = new List<SystemProperty>();
IPropertyDescriptionList propertyDescriptionList = null;
IPropertyDescription propertyDescription = null;
Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList);
Expand All @@ -107,37 +109,21 @@ public void PopulateSystemProperties()
uint count;
propertyDescriptionList.GetCount(out count);
guid = new Guid(ShellIIDGuid.IPropertyDescription);
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

for (uint i = 0; i < count; i++)
{
propertyDescriptionList.GetAt(i, ref guid, out propertyDescription);

string propName;
propertyDescription.GetCanonicalName(out propName);
IntPtr displayNamePtr;
string displayName = null;
propertyDescription.GetDisplayName(out displayNamePtr);
if (displayNamePtr != IntPtr.Zero)
displayName = Marshal.PtrToStringAuto(displayNamePtr);
SystemProperty sp = new SystemProperty {FullName = propName, DisplayName = displayName };

List<string> names = null;
string[] parts = propName.Split('.');
if (parts.Count() == 2)
{
// System.Foo
if (!dict.TryGetValue(parts[0], out names))
{
names = new List<string>();
dict.Add(parts[0], names);
}
names.Add(parts[1]);
}
else if (parts.Count() == 3)
{
// System.Bar.Baz
if (!dict.TryGetValue(parts[1], out names))
{
names = new List<string>();
dict.Add(parts[1], names);
}
names.Add(parts[2]);
}
systemProperties.Add(sp);

// If we ever need it:
// ShellPropertyDescription desc = new ShellPropertyDescription(propertyDescription);
Expand All @@ -149,22 +135,42 @@ public void PopulateSystemProperties()
}
}

// build tree
foreach (string cat in dict.Keys)
Dictionary<string, TreeItem> dict = new Dictionary<string, TreeItem>();
List<TreeItem> roots = new List<TreeItem>();

// Build tree based on property names
foreach (SystemProperty sp in systemProperties)
{
TreeItem main = new TreeItem(cat, PropType.Group);
foreach (string name in dict[cat])
main.AddChild(new TreeItem(name, PropType.Normal));
AddTreeItem(dict, roots, sp);
}

if (cat == "System")
AllProperties.Insert(0, main);
else if (cat == "PropGroup")
// Wire trees to tree controls, tweaking the structure as we go
TreeItem propGroup = null;
foreach (TreeItem root in roots)
{
if (root.Name == "System")
{
foreach (TreeItem ti in main.Children)
GroupProperties.Add(ti.Name);
AllProperties.Insert(0, root);

// Move property groups from root to their own list
propGroup = root.Children.Where(x => x.Name == "PropGroup").FirstOrDefault();
if (propGroup != null)
{
foreach (TreeItem ti in propGroup.Children)
GroupProperties.Add(ti.Name);
root.RemoveChild(propGroup);
}

// Make remaining children of System that are parents into roots
List<TreeItem> movers = new List<TreeItem>(root.Children.Where(x => x.Children.Count() > 0));
foreach (TreeItem ti in movers)
{
root.RemoveChild(ti);
AllProperties.Add(ti);
}
}
else
AllProperties.Add(main);
AllProperties.Add(root);
}
}
}
Expand All @@ -181,26 +187,62 @@ public void PopulateSystemProperties()
}
}

public string GetSystemPropertyName(TreeItem ti)
// Top level entry point for the algorithm that builds the property name tree from an unordered sequence
// of property names
private TreeItem AddTreeItem(Dictionary<string, TreeItem> dict, List<TreeItem> roots, SystemProperty sp)
{
Debug.Assert(sp.FullName.Contains('.')); // Because the algorithm assumes that this is the case
TreeItem ti = AddTreeItemInner(dict, roots, sp.FullName, sp.DisplayName);
ti.Item = sp;

return ti;
}

// Recurse backwards through each term in the property name, adding tree items as we go,
// until we join onto an existing part of the tree
private TreeItem AddTreeItemInner(Dictionary<string, TreeItem> dict, List<TreeItem> roots,
string name, string displayName = null)
{
if (ti == null || (PropType)ti.Item != PropType.Normal)
return null;
else
TreeItem ti, parent;
string parentName = FirstPartsOf(name);

if (parentName != null)
{
StringBuilder sb = new StringBuilder(ti.Name);
bool hasSystem = false;
for (TreeItem parent = ti.Parent; parent != null; parent = parent.Parent)
if (!dict.TryGetValue(parentName, out parent))
{
sb.Insert(0, ".");
sb.Insert(0, parent.Name);
if(parent.Name == "System")
hasSystem = true;
parent = AddTreeItemInner(dict, roots, parentName);
dict.Add(parentName, parent);
}
if (!hasSystem)
sb.Insert(0, "System.");

return sb.ToString();
if (displayName != null)
ti = new TreeItem(String.Format("{0} ({1})", LastPartOf(name), displayName));
else
ti = new TreeItem(LastPartOf(name));

parent.AddChild(ti);
}
else
{
if (!dict.TryGetValue(name, out ti))
{
ti = new TreeItem(name);
roots.Add(ti);
}
}

return ti;
}

private string FirstPartsOf(string name)
{
int index = name.LastIndexOf('.');
return index >= 0 ? name.Substring(0, index) : null;
}

private string LastPartOf(string name)
{
int index = name.LastIndexOf('.');
return index >= 0 ? name.Substring(index + 1) : name;
}
#endif

Expand Down Expand Up @@ -388,4 +430,10 @@ private DirectoryInfo ObtainDataDirectory()
return target;
}
}

public class SystemProperty
{
public string FullName { get; set; }
public string DisplayName { get; set; }
}
}

0 comments on commit d71ef9a

Please sign in to comment.