Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #9590 from mono/fix-hc-dark-sel-icon-loader
Browse files Browse the repository at this point in the history
[Ide] Dynamically add ~contrast~dark~sel resource mappings
  • Loading branch information
sevoku authored Jan 23, 2020
2 parents 96f5a04 + f5db883 commit 7642846
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions main/src/core/MonoDevelop.Ide/MonoDevelop.Ide/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,49 @@ public IEnumerable<string> GetAlternativeFiles (string fileName, string baseName
resourceList.Sort (); // sort resources by name
}

return resourceList.Where (r => r.StartsWith (baseName) && r.EndsWith (ext));
return GetFinalFilelist (resourceList, baseName, ext);
}

IEnumerable<string> GetFinalFilelist (IEnumerable<string> source, string baseName, string ext)
{
foreach (var name in source) {
if (name.StartsWith (baseName) && name.EndsWith (ext)) {
yield return name;

// to avoid duplicate resource entries in project files, add virtual file mappings for
// high contrast icons in selected state.
// note: we include the "." to ensure that we match "~dark~sel" exactly and not "~dark~sel~xyz".
int start = baseName.Length;
int length = name.Length - baseName.Length - ext.Length + 1;
if (name.IndexOf ("~dark~sel.", start, length, StringComparison.Ordinal) == start ||
name.IndexOf ("~dark~sel@2x.", start, length, StringComparison.Ordinal) == start)
{
var map = name.Replace ("~dark~sel", "~contrast~dark~sel");
if (virtualMappings == null) {
virtualMappings = new Dictionary<string, string> ();
}
if (!virtualMappings.ContainsKey (map)) {
virtualMappings.Add (map, name);
}
yield return map;
} else {
// remove existing mapping if a resource with the same name exists.
// note: we know that the source is sorted
virtualMappings?.Remove (name);
}
}
}
}

Dictionary<string, string> virtualMappings;

public Stream LoadImage (string fileName)
{
return addin.GetResource (fileName, true);
// load original resource if a mapping exists
if (virtualMappings != null && virtualMappings.TryGetValue (fileName, out var mapsTo))
return addin.GetResource (mapsTo, true);
else
return addin.GetResource (fileName, true);
}
}
}

0 comments on commit 7642846

Please sign in to comment.