Files
PowerToys/Plugins/Wox.Plugin.ControlPanel/Main.cs

128 lines
4.4 KiB
C#
Raw Normal View History

2014-07-17 17:14:58 +02:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2014-07-18 14:09:52 +08:00
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
2014-07-18 14:09:52 +08:00
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
2014-07-17 17:14:58 +02:00
namespace Wox.Plugin.ControlPanel
2014-07-17 17:14:58 +02:00
{
public class Main : IPlugin, IPluginI18n, ISettingProvider, ISavable
2014-07-17 17:14:58 +02:00
{
private readonly Settings _settings = new Settings {ShouldUsePinYin = false};
private readonly PluginJsonStorage<Settings> _settingsStorage;
2014-07-17 17:14:58 +02:00
private PluginInitContext context;
private List<ControlPanelItem> controlPanelItems = new List<ControlPanelItem>();
2014-07-17 17:14:58 +02:00
private string iconFolder;
private string fileType;
2014-07-17 17:14:58 +02:00
public Main()
{
_settingsStorage = new PluginJsonStorage<Settings>();
_settings = _settingsStorage.Load();
}
2016-04-22 22:42:26 +01:00
public void Init(PluginInitContext context)
2014-07-17 17:14:58 +02:00
{
this.context = context;
2014-07-18 14:09:52 +08:00
controlPanelItems = ControlPanelList.Create(48);
iconFolder = Path.Combine(context.CurrentPluginMetadata.PluginDirectory, @"Images\ControlPanelIcons\");
fileType = ".bmp";
2014-07-17 17:14:58 +02:00
2014-07-18 14:09:52 +08:00
if (!Directory.Exists(iconFolder))
{
Directory.CreateDirectory(iconFolder);
}
2014-07-17 17:14:58 +02:00
foreach (ControlPanelItem item in controlPanelItems)
{
2014-07-19 14:31:19 +02:00
if (!File.Exists(iconFolder + item.GUID + fileType) && item.Icon != null)
2014-07-17 17:14:58 +02:00
{
2014-07-19 14:31:19 +02:00
item.Icon.ToBitmap().Save(iconFolder + item.GUID + fileType);
2014-07-17 17:14:58 +02:00
}
}
}
public List<Result> Query(Query query)
2014-07-17 17:14:58 +02:00
{
List<Result> results = new List<Result>();
foreach (var item in controlPanelItems)
{
item.Score = Score(item, query.Search, _settings.ShouldUsePinYin);
if (item.Score > 0)
{
2016-04-22 22:42:26 +01:00
var result = new Result
2014-07-17 17:43:29 +02:00
{
Title = item.LocalizedString,
SubTitle = item.InfoTip,
2014-07-18 14:09:52 +08:00
Score = item.Score,
2016-04-22 22:42:26 +01:00
IcoPath = Path.Combine(context.CurrentPluginMetadata.PluginDirectory,
2017-01-12 20:50:17 +00:00
@"Images\\ControlPanelIcons\\" + item.GUID + fileType),
Action = e =>
2017-01-12 20:50:17 +00:00
{
try
{
2017-01-12 20:50:17 +00:00
Process.Start(item.ExecutablePath);
2014-07-17 17:14:58 +02:00
}
2017-01-12 20:50:17 +00:00
catch (Exception)
{
//Silently Fail for now.. todo
}
return true;
}
2016-04-22 22:42:26 +01:00
};
results.Add(result);
2014-07-17 17:14:58 +02:00
}
}
2014-07-17 17:47:58 +02:00
List<Result> panelItems = results.OrderByDescending(o => o.Score).Take(5).ToList();
return panelItems;
2014-07-18 14:09:52 +08:00
}
private int Score(ControlPanelItem item, string query, bool shouldUsePinYin)
2014-07-18 14:09:52 +08:00
{
2017-01-12 20:50:12 +00:00
var scores = new List<int> {0};
2019-09-30 21:11:34 +10:00
if (!string.IsNullOrEmpty(item.LocalizedString))
{
var score1 = StringMatcher.FuzzySearch(query, item.LocalizedString).ScoreAfterSearchPrecisionFilter();
var score2 = shouldUsePinYin ? StringMatcher.ScoreForPinyin(item.LocalizedString, query) : 0;
2017-01-12 20:50:12 +00:00
scores.Add(score1);
scores.Add(score2);
}
2017-01-12 20:50:12 +00:00
if (!string.IsNullOrEmpty(item.InfoTip))
{
var score1 = StringMatcher.FuzzySearch(query, item.InfoTip).ScoreAfterSearchPrecisionFilter();
var score2 = shouldUsePinYin ? StringMatcher.ScoreForPinyin(item.InfoTip, query) : 0;
2017-01-12 20:50:12 +00:00
scores.Add(score1);
scores.Add(score2);
}
return scores.Max();
2014-07-17 17:14:58 +02: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");
}
public Control CreateSettingPanel()
{
return new ControlPanelSettingsView(new ControlPanelSettingsViewModel(_settings));
}
public void Save()
{
_settingsStorage.Save();
}
2014-07-17 17:14:58 +02:00
}
2017-01-12 20:50:17 +00:00
}