Extract all converters into separate folder

This commit is contained in:
Yaroslav Zyubanov 2014-05-24 14:06:09 +06:00
parent d748e34d7e
commit 9eefd8839c
6 changed files with 116 additions and 99 deletions

View File

@ -0,0 +1,30 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace Wox.Converters
{
public abstract class ConvertorBase<T> : MarkupExtension, IValueConverter where T : class, new()
{
private static T converter;
/// <summary>
/// Must be implemented in inheritor.
/// </summary>
public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
/// <summary>
/// Override if needed.
/// </summary>
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return converter ?? (converter = new T());
}
}
}

View File

@ -3,23 +3,37 @@ using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Wox
namespace Wox.Converters
{
public class ImagePathConverter : IMultiValueConverter
{
private static Dictionary<string, object> imageCache = new Dictionary<string, object>();
private static List<string> imageExts = new List<string>() { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
private static List<string> selfExts = new List<string>() {
".exe", ".lnk",
".ani", ".cur",
".sln", ".appref-ms"
private static readonly Dictionary<string, object> imageCache = new Dictionary<string, object>();
private static readonly List<string> imageExts = new List<string>
{
".png",
".jpg",
".jpeg",
".gif",
".bmp",
".tiff",
".ico"
};
private static readonly List<string> selfExts = new List<string>
{
".exe",
".lnk",
".ani",
".cur",
".sln",
".appref-ms"
};
private static ImageSource GetIcon(string fileName)
@ -29,7 +43,8 @@ namespace Wox
if (icon != null)
{
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle,
new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
}
return null;
@ -37,13 +52,13 @@ namespace Wox
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
object img = null;
object img;
if (values[0] == null) return null;
string path = values[0].ToString();
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
return new System.Windows.Media.Imaging.BitmapImage(new Uri(path));
return new BitmapImage(new Uri(path));
}
string pluginDirectory = values[1].ToString();
@ -98,7 +113,7 @@ namespace Wox
}
// http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx
public static System.Drawing.Icon GetFileIcon(string name)
public static Icon GetFileIcon(string name)
{
SHFILEINFO shfi = new SHFILEINFO();
uint flags = SHGFI_SYSICONINDEX;
@ -106,13 +121,13 @@ namespace Wox
IntPtr himl = SHGetFileInfo(name,
FILE_ATTRIBUTE_NORMAL,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint) Marshal.SizeOf(shfi),
flags);
if (himl != IntPtr.Zero)
{
IntPtr hIcon = ImageList_GetIcon(himl, shfi.iIcon, ILD_NORMAL);
System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(hIcon).Clone();
var icon = (Icon) Icon.FromHandle(hIcon).Clone();
DestroyIcon(hIcon);
return icon;
}
@ -124,12 +139,12 @@ namespace Wox
private static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
private const int MAX_PATH = 256;
[StructLayout(LayoutKind.Sequential)]
private struct SHITEMID
{
public ushort cb;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] abID;
[MarshalAs(UnmanagedType.LPArray)] public byte[] abID;
}
[StructLayout(LayoutKind.Sequential)]
@ -144,8 +159,7 @@ namespace Wox
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpszTitle;
[MarshalAs(UnmanagedType.LPTStr)] public string lpszTitle;
public uint ulFlags;
public IntPtr lpfn;
public int lParam;
@ -174,10 +188,8 @@ namespace Wox
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
public string szTypeName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)] public string szTypeName;
};
private const uint SHGFI_ICON = 0x000000100; // get icon

View File

@ -1,19 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Markup;
using System.Globalization;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox
namespace Wox.Converters
{
public class OpacityModeConverter : MarkupExtension, IValueConverter
public class OpacityModeConverter : ConvertorBase<OpacityModeConverter>
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is OpacityMode)) return value.ToString();
var mode = (OpacityMode) value;
switch (mode)
{
@ -37,13 +32,6 @@ namespace Wox
return value.ToString();
}
public object ConvertBack(
object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;

View File

@ -0,0 +1,18 @@
using System;
using System.Globalization;
namespace Wox.Converters
{
public class StringEmptyConverter : ConvertorBase<StringEmptyConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty((string)value) ? parameter : value;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace Wox
namespace Wox.Converters
{
public class StringNullOrEmptyToVisibilityConverter : MarkupExtension, IValueConverter
public class StringNullOrEmptyToVisibilityConverter : ConvertorBase<StringNullOrEmptyToVisibilityConverter>
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty(value as string) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}

View File

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Markup;
namespace Wox
{
public class StringEmptyConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.IsNullOrEmpty((string)value) ? parameter : value;
}
public object ConvertBack(
object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}