PowerToys/Plugins/Wox.Plugin.Color/Main.cs

121 lines
3.5 KiB
C#
Raw Normal View History

2014-06-04 18:15:38 +08:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
2014-06-04 23:22:34 +08:00
using System.Windows;
2014-06-04 18:15:38 +08:00
namespace Wox.Plugin.Color
2014-06-04 18:15:38 +08:00
{
2015-02-07 21:27:48 +08:00
public sealed class ColorsPlugin : IPlugin, IPluginI18n
2014-06-04 18:15:38 +08:00
{
2014-06-30 21:31:13 +08:00
private string DIR_PATH = Path.Combine(Path.GetTempPath(), @"Plugins\Colors\");
2015-02-07 21:27:48 +08:00
private PluginInitContext context;
2014-06-04 18:15:38 +08:00
private const int IMG_SIZE = 32;
private DirectoryInfo ColorsDirectory { get; set; }
public ColorsPlugin()
{
2014-06-04 23:22:34 +08:00
if (!Directory.Exists(DIR_PATH))
2014-06-04 18:15:38 +08:00
{
ColorsDirectory = Directory.CreateDirectory(DIR_PATH);
}
else
{
ColorsDirectory = new DirectoryInfo(DIR_PATH);
}
}
public List<Result> Query(Query query)
2014-06-04 18:15:38 +08:00
{
var raw = query.Search;
2014-06-04 23:22:34 +08:00
if (!IsAvailable(raw)) return new List<Result>(0);
2014-06-04 18:15:38 +08:00
try
{
var cached = Find(raw);
2014-06-04 23:22:34 +08:00
if (cached.Length == 0)
2014-06-04 18:15:38 +08:00
{
var path = CreateImage(raw);
2014-06-04 23:22:34 +08:00
return new List<Result>
{
new Result
{
Title = raw,
IcoPath = path,
Action = _ =>
{
Clipboard.SetText(raw);
return true;
}
}
};
2014-06-04 18:15:38 +08:00
}
2014-06-04 23:22:34 +08:00
return cached.Select(x => new Result
{
Title = raw,
IcoPath = x.FullName,
Action = _ =>
{
Clipboard.SetText(raw);
return true;
}
}).ToList();
2014-06-04 18:15:38 +08:00
}
catch (Exception exception)
{
// todo: log
return new List<Result>(0);
}
}
private bool IsAvailable(string query)
{
// todo: rgb, names
var length = query.Length - 1; // minus `#` sign
return query.StartsWith("#") && (length == 3 || length == 6);
}
public FileInfo[] Find(string name)
{
var file = string.Format("{0}.png", name.Substring(1));
return ColorsDirectory.GetFiles(file, SearchOption.TopDirectoryOnly);
}
2014-06-30 21:31:13 +08:00
private string CreateImage(string name)
2014-06-04 18:15:38 +08:00
{
using (var bitmap = new Bitmap(IMG_SIZE, IMG_SIZE))
using (var graphics = Graphics.FromImage(bitmap))
{
var color = ColorTranslator.FromHtml(name);
graphics.Clear(color);
var path = CreateFileName(name);
bitmap.Save(path, ImageFormat.Png);
return path;
}
}
private string CreateFileName(string name)
{
return string.Format("{0}{1}.png", ColorsDirectory.FullName, name.Substring(1));
}
public void Init(PluginInitContext context)
2014-06-04 18:15:38 +08:00
{
2015-02-07 21:27:48 +08:00
this.context = context;
}
public string GetTranslatedPluginTitle()
{
return context.API.GetTranslation("wox_plugin_color_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return context.API.GetTranslation("wox_plugin_color_plugin_description");
2014-06-04 18:15:38 +08:00
}
}
}