Advertisement
Guest User

ThemeIcon.cs

a guest
May 16th, 2015
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ReplaceIcon
  7. {
  8.     public class ThemeIcon
  9.     {
  10.         public string SizePattern { get; private set; }
  11.  
  12.         public List<IconSize> Sizes { get; private set; }
  13.  
  14.         public string this[string size]
  15.         {
  16.             get
  17.             {
  18.                 if (Sizes.All(s => s.Size != size))
  19.                     throw new Exception("Unsupported size");
  20.  
  21.                 return string.Format(SizePattern, size);
  22.             }
  23.         }
  24.  
  25.         public string this[IconSize size]
  26.         {
  27.             get
  28.             {
  29.                 if (!Sizes.Contains(size))
  30.                     throw new Exception("Unsupported size");
  31.  
  32.                 return string.Format(SizePattern, size.Size);
  33.             }
  34.         }
  35.  
  36.         public ThemeIcon(string path)
  37.         {
  38.             Sizes = GetSizes(path);
  39.             SizePattern = GetSizePattern(path);
  40.         }
  41.  
  42.         private string GetSizePattern(string path)
  43.         {
  44.             var parts = path.Split('/');
  45.  
  46.             if (Sizes.Any(s => s.Size == parts[parts.Length - 2]))
  47.             {
  48.                 parts[parts.Length - 2] = "{0}"; //     .../CaledoniaTheme/apps/{0}/firefox.png
  49.             }
  50.             else if (Sizes.Any(s => s.Size == parts[parts.Length - 3]))
  51.             {
  52.                 parts[parts.Length - 3] = "{0}"; //     .../CaledoniaTheme/{0}/apps/firefox.png
  53.             }
  54.  
  55.             string pattern = string.Join("/", parts);
  56.  
  57.             return pattern;
  58.         }
  59.  
  60.         private List<IconSize> GetSizes(string path)
  61.         {
  62.             var types = new [] { new Regex("[1-9]+[0-9]+"), new Regex("[1-9]+[0-9]+x[1-9]+[0-9]+") }; // ex.:    64    64x64
  63.  
  64.             var parts = path.Split('/');
  65.  
  66.             if (types[0].IsMatch(parts[parts.Length - 2]) || types[0].IsMatch(parts[parts.Length - 3]))
  67.                 return new List<IconSize>()
  68.                 {
  69.                     new IconSize("16", "16x16"),
  70.                     new IconSize("22", "22x22"),
  71.                     new IconSize("24", "24x24"),
  72.                     new IconSize("32", "32x32"),
  73.                     new IconSize("64", "64x64"),
  74.                     new IconSize("128", "128x128"),
  75.                     new IconSize("256", "256x256"),
  76.                     new IconSize("512", "512x512"),
  77.                     new IconSize("scalable")
  78.                 };
  79.  
  80.             if (types[1].IsMatch(parts[parts.Length - 2]) || types[1].IsMatch(parts[parts.Length - 3]))
  81.                 return new List<IconSize>()
  82.                 {
  83.                     new IconSize("16x16", "16"),
  84.                     new IconSize("22x22", "22"),
  85.                     new IconSize("24x24", "24"),
  86.                     new IconSize("32x32", "32"),
  87.                     new IconSize("64x64", "64"),
  88.                     new IconSize("128x128", "128"),
  89.                     new IconSize("256x256", "256"),
  90.                     new IconSize("512x512", "512"),
  91.                     new IconSize("scalable")
  92.                 };
  93.  
  94.             throw new Exception("Unknown size type");
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement