rainmeter-studio/RainmeterStudio/UI/Controller/IconProvider.cs

85 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media;
using RainmeterStudio.Business;
using RainmeterStudio.Core.Model;
namespace RainmeterStudio.UI.Controller
{
/// <summary>
/// Provides icons
/// </summary>
public static class IconProvider
{
/// <summary>
/// Gets an icon by resource name
/// </summary>
/// <param name="key">Resource name</param>
/// <returns>The icon</returns>
public static ImageSource GetIcon(string key)
{
return ResourceProvider.GetImage(key);
}
/// <summary>
/// Gets an icon by reference
/// </summary>
/// <param name="item">The reference</param>
/// <returns>The icon</returns>
public static ImageSource GetProjectItemIcon(Reference item)
{
// Resource name
string key = "ProjectItem";
2014-08-31 11:41:24 +00:00
// Is a file?
if (item.TargetKind == ReferenceTargetKind.File || item.TargetKind == ReferenceTargetKind.Project)
2014-08-31 11:41:24 +00:00
{
2014-09-12 16:22:21 +00:00
var extension = Path.GetExtension(item.StoragePath);
if (String.IsNullOrEmpty(extension))
2014-08-31 11:41:24 +00:00
key += "Unknown";
else
2014-09-12 16:22:21 +00:00
key += "_" + extension.Substring(1);
2014-08-31 11:41:24 +00:00
}
2014-08-31 11:41:24 +00:00
// Not a file, try to figure out if a directory
else if (item.TargetKind == ReferenceTargetKind.Directory)
2014-08-31 11:41:24 +00:00
{
key += "Directory";
}
2014-08-31 11:41:24 +00:00
// None
else key += "None";
// Get icon
var icon = GetIcon(key);
if (icon == null)
return GetIcon("ProjectItemUnknown");
return icon;
}
}
/// <summary>
/// Icon provider converter, for use in xaml
/// </summary>
public class IconProviderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
2014-08-30 07:24:01 +00:00
if (value is Reference)
{
2014-08-30 07:24:01 +00:00
return IconProvider.GetProjectItemIcon((Reference)value);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
}