mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
Program plugin: simple refactoring
This commit is contained in:
@@ -27,8 +27,15 @@ namespace Wox.Plugin.Program
|
||||
{
|
||||
_settingsStorage = new PluginJsonStorage<Settings>();
|
||||
_settings = _settingsStorage.Load();
|
||||
_cacheStorage = new BinaryStorage<ProgramIndexCache>();
|
||||
_cache = _cacheStorage.Load();
|
||||
|
||||
Stopwatch.Debug("Preload programs", () =>
|
||||
{
|
||||
_cacheStorage = new BinaryStorage<ProgramIndexCache>();
|
||||
_cache = _cacheStorage.Load();
|
||||
_programs = _cache.Programs;
|
||||
});
|
||||
Log.Info($"Preload {_programs.Count} programs from cache");
|
||||
Stopwatch.Debug("Program Index", IndexPrograms);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
@@ -41,29 +48,35 @@ namespace Wox.Plugin.Program
|
||||
{
|
||||
var results = _programs.AsParallel()
|
||||
.Where(p => Score(p, query.Search) > 0)
|
||||
.Select(ScoreFilter)
|
||||
.OrderByDescending(p => p.Score)
|
||||
.Select(p => new Result
|
||||
{
|
||||
Title = p.Title,
|
||||
SubTitle = p.Path,
|
||||
IcoPath = p.IcoPath,
|
||||
Score = p.Score,
|
||||
ContextData = p,
|
||||
Action = e =>
|
||||
{
|
||||
var info = new ProcessStartInfo
|
||||
{
|
||||
FileName = p.Path,
|
||||
WorkingDirectory = p.Directory
|
||||
};
|
||||
var hide = StartProcess(info);
|
||||
return hide;
|
||||
}
|
||||
}).ToList();
|
||||
.Select(ResultFromProgram)
|
||||
.ToList();
|
||||
return results;
|
||||
}
|
||||
|
||||
public Result ResultFromProgram(Program p)
|
||||
{
|
||||
var result = new Result
|
||||
{
|
||||
Title = p.Title,
|
||||
SubTitle = p.Path,
|
||||
IcoPath = p.IcoPath,
|
||||
Score = p.Score,
|
||||
ContextData = p,
|
||||
Action = e =>
|
||||
{
|
||||
var info = new ProcessStartInfo
|
||||
{
|
||||
FileName = p.Path,
|
||||
WorkingDirectory = p.Directory
|
||||
};
|
||||
var hide = StartProcess(info);
|
||||
return hide;
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
private int Score(Program program, string query)
|
||||
{
|
||||
var score1 = StringMatcher.Score(program.Title, query);
|
||||
@@ -77,39 +90,26 @@ namespace Wox.Plugin.Program
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
_context = context;
|
||||
Stopwatch.Debug("Preload programs", () =>
|
||||
{
|
||||
_programs = _cache.Programs;
|
||||
});
|
||||
Log.Info($"Preload {_programs.Count} programs from cache");
|
||||
Stopwatch.Debug("Program Index", IndexPrograms);
|
||||
}
|
||||
|
||||
public static void IndexPrograms()
|
||||
{
|
||||
var sources = DefaultProgramSources();
|
||||
if (_settings.ProgramSources != null &&
|
||||
_settings.ProgramSources.Count(o => o.Enabled) > 0)
|
||||
{
|
||||
sources.AddRange(_settings.ProgramSources);
|
||||
}
|
||||
var sources = ProgramSources();
|
||||
|
||||
_programs = sources.AsParallel()
|
||||
.SelectMany(s => s.LoadPrograms())
|
||||
// filter duplicate program
|
||||
.GroupBy(x => new { ExecutePath = x.Path, ExecuteName = x.ExecutableName })
|
||||
.Select(g => g.First())
|
||||
.ToList();
|
||||
var programs = sources.AsParallel()
|
||||
.SelectMany(s => s.LoadPrograms())
|
||||
// filter duplicate program
|
||||
.GroupBy(x => new { ExecutePath = x.Path, ExecuteName = x.ExecutableName })
|
||||
.Select(g => g.First());
|
||||
programs = programs.Select(ScoreFilter);
|
||||
|
||||
_programs = programs.ToList();
|
||||
_cache.Programs = _programs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load program sources that wox always provide
|
||||
/// </summary>
|
||||
private static List<ProgramSource> DefaultProgramSources()
|
||||
private static List<ProgramSource> ProgramSources()
|
||||
{
|
||||
var list = new List<ProgramSource>
|
||||
var sources = new List<ProgramSource>
|
||||
{
|
||||
new CommonStartMenuProgramSource
|
||||
{
|
||||
@@ -127,33 +127,46 @@ namespace Wox.Plugin.Program
|
||||
Enabled = _settings.EnableRegistrySource,
|
||||
}
|
||||
};
|
||||
return list;
|
||||
|
||||
if (_settings.ProgramSources.Count(o => o.Enabled) > 0)
|
||||
{
|
||||
sources.AddRange(_settings.ProgramSources);
|
||||
}
|
||||
|
||||
return sources;
|
||||
}
|
||||
|
||||
private Program ScoreFilter(Program p)
|
||||
private static Program ScoreFilter(Program p)
|
||||
{
|
||||
p.Score += p.Source.BonusPoints;
|
||||
var start = new[] { "启动", "start" };
|
||||
var doc = new[] { "帮助", "help", "文档", "documentation" };
|
||||
var uninstall = new[] { "卸载", "uninstall" };
|
||||
|
||||
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start"))
|
||||
var contained = start.Any(s => p.Title.ToLower().Contains(s));
|
||||
if (contained)
|
||||
{
|
||||
p.Score += 10;
|
||||
|
||||
if (p.Title.Contains("帮助") || p.Title.ToLower().Contains("help") || p.Title.Contains("文档") || p.Title.ToLower().Contains("documentation"))
|
||||
}
|
||||
contained = doc.Any(d => p.Title.ToLower().Contains(d));
|
||||
if (contained)
|
||||
{
|
||||
p.Score -= 10;
|
||||
|
||||
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall"))
|
||||
}
|
||||
contained = uninstall.Any(u => p.Title.ToLower().Contains(u));
|
||||
if (contained)
|
||||
{
|
||||
p.Score -= 20;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
#region ISettingProvider Members
|
||||
|
||||
public Control CreateSettingPanel()
|
||||
{
|
||||
return new ProgramSetting(_context, _settings);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public string GetTranslatedPluginTitle()
|
||||
{
|
||||
return _context.API.GetTranslation("wox_plugin_program_plugin_name");
|
||||
|
||||
Reference in New Issue
Block a user