2014-07-17 23:14:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2014-07-18 14:09:52 +08:00
|
|
|
|
using System.Linq;
|
2017-01-13 04:49:13 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-07-18 14:09:52 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-07-17 23:14:58 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
namespace Wox.Plugin.ControlPanel
|
2014-07-17 23:14:58 +08:00
|
|
|
|
{
|
2016-05-07 10:55:09 +08:00
|
|
|
|
public class Main : IPlugin, IPluginI18n
|
2014-07-17 23:14:58 +08:00
|
|
|
|
{
|
|
|
|
|
private PluginInitContext context;
|
2014-12-15 18:29:16 +08:00
|
|
|
|
private List<ControlPanelItem> controlPanelItems = new List<ControlPanelItem>();
|
2014-07-17 23:14:58 +08:00
|
|
|
|
private string iconFolder;
|
2014-07-18 01:37:51 +08:00
|
|
|
|
private string fileType;
|
2014-07-17 23:14:58 +08:00
|
|
|
|
|
2016-04-23 05:42:26 +08:00
|
|
|
|
public void Init(PluginInitContext context)
|
2014-07-17 23:14:58 +08:00
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
2014-07-18 14:09:52 +08:00
|
|
|
|
controlPanelItems = ControlPanelList.Create(48);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
iconFolder = Path.Combine(context.CurrentPluginMetadata.PluginDirectory, @"Images\ControlPanelIcons\");
|
2014-07-18 01:37:51 +08:00
|
|
|
|
fileType = ".bmp";
|
2014-07-17 23:14:58 +08:00
|
|
|
|
|
2014-07-18 14:09:52 +08:00
|
|
|
|
if (!Directory.Exists(iconFolder))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(iconFolder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 23:14:58 +08:00
|
|
|
|
foreach (ControlPanelItem item in controlPanelItems)
|
|
|
|
|
{
|
2014-07-19 20:31:19 +08:00
|
|
|
|
if (!File.Exists(iconFolder + item.GUID + fileType) && item.Icon != null)
|
2014-07-17 23:14:58 +08:00
|
|
|
|
{
|
2014-07-19 20:31:19 +08:00
|
|
|
|
item.Icon.ToBitmap().Save(iconFolder + item.GUID + fileType);
|
2014-07-17 23:14:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-13 04:49:13 +08:00
|
|
|
|
|
|
|
|
|
var characters = controlPanelItems.Select(i => i.LocalizedString)
|
|
|
|
|
.Concat(controlPanelItems.Select(i => i.InfoTip));
|
|
|
|
|
|
|
|
|
|
Parallel.ForEach(characters, c =>
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(c) && Alphabet.ContainsChinese(c))
|
|
|
|
|
{
|
|
|
|
|
Alphabet.PinyinComination(c);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-07-17 23:14:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
public List<Result> Query(Query query)
|
2014-07-17 23:14:58 +08:00
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in controlPanelItems)
|
|
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
|
item.Score = Score(item, query.Search);
|
|
|
|
|
if (item.Score > 0)
|
2014-07-18 02:05:09 +08:00
|
|
|
|
{
|
2016-04-23 05:42:26 +08:00
|
|
|
|
var result = new Result
|
2014-07-17 23:43:29 +08:00
|
|
|
|
{
|
2014-07-18 02:05:09 +08:00
|
|
|
|
Title = item.LocalizedString,
|
|
|
|
|
SubTitle = item.InfoTip,
|
2014-07-18 14:09:52 +08:00
|
|
|
|
Score = item.Score,
|
2016-04-23 05:42:26 +08:00
|
|
|
|
IcoPath = Path.Combine(context.CurrentPluginMetadata.PluginDirectory,
|
|
|
|
|
@"Images\\ControlPanelIcons\\" + item.GUID + fileType),
|
2014-07-18 02:05:09 +08:00
|
|
|
|
Action = e =>
|
|
|
|
|
{
|
2016-04-23 05:42:26 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process.Start(item.ExecutablePath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
|
//Silently Fail for now.. todo
|
|
|
|
|
}
|
2016-04-23 05:42:26 +08:00
|
|
|
|
return true;
|
2014-07-17 23:14:58 +08:00
|
|
|
|
}
|
2016-04-23 05:42:26 +08:00
|
|
|
|
};
|
|
|
|
|
results.Add(result);
|
2014-07-17 23:14:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-17 23:47:58 +08:00
|
|
|
|
|
2014-07-21 19:45:47 +08:00
|
|
|
|
List<Result> panelItems = results.OrderByDescending(o => o.Score).Take(5).ToList();
|
|
|
|
|
return panelItems;
|
2014-07-18 14:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
|
private int Score(ControlPanelItem item, string query)
|
2014-07-18 14:09:52 +08:00
|
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
|
var scores = new List<int>();
|
|
|
|
|
if (item.LocalizedString != null)
|
|
|
|
|
{
|
|
|
|
|
var score1 = StringMatcher.Score(item.LocalizedString, query);
|
|
|
|
|
var socre2 = StringMatcher.ScoreForPinyin(item.LocalizedString, query);
|
|
|
|
|
scores.Add(Math.Max(score1, socre2));
|
|
|
|
|
}
|
|
|
|
|
if (item.InfoTip != null)
|
|
|
|
|
{
|
|
|
|
|
// todo should we add pinyin score for infotip also?
|
|
|
|
|
var score = StringMatcher.Score(item.InfoTip, query);
|
|
|
|
|
scores.Add(score);
|
|
|
|
|
}
|
|
|
|
|
return scores.Max();
|
2014-07-17 23:14:58 +08:00
|
|
|
|
}
|
2015-02-07 21:27:48 +08:00
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
{
|
|
|
|
|
return context.API.GetTranslation("wox_plugin_controlpanel_plugin_name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
{
|
|
|
|
|
return context.API.GetTranslation("wox_plugin_controlpanel_plugin_description");
|
|
|
|
|
}
|
2014-07-17 23:14:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|