using System; using System.IO; using System.Windows.Data; using System.Windows.Media; using RainmeterStudio.Business; using RainmeterStudio.Core.Model; namespace RainmeterStudio.UI.Controller { /// /// Provides icons /// public static class IconProvider { /// /// Gets an icon by resource name /// /// Resource name /// The icon public static ImageSource GetIcon(string key) { return ResourceProvider.GetImage(key); } /// /// Gets an icon by reference /// /// The reference /// The icon public static ImageSource GetProjectItemIcon(Reference item) { // Resource name string key = "ProjectItem"; // Is a file? if (File.Exists(item.StoragePath)) { if (String.IsNullOrEmpty(Path.GetExtension(item.StoragePath))) key += "Unknown"; else key += "_" + Path.GetExtension(item.StoragePath).Substring(1); } // Not a file, try to figure out if a directory else if (item.Count > 0 || Directory.Exists(item.StoragePath)) { key += "Directory"; } // None else key += "None"; // Get icon var icon = GetIcon(key); if (icon == null) return GetIcon("ProjectItemUnknown"); return icon; } } /// /// Icon provider converter, for use in xaml /// public class IconProviderConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is Reference) { return IconProvider.GetProjectItemIcon((Reference)value); } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } } }