Merge branch 'dev'

This commit is contained in:
Jeremy Wu 2019-12-24 20:05:38 +11:00
commit 276278db41
43 changed files with 997 additions and 348 deletions

View File

@ -27,7 +27,6 @@ namespace Wox.Plugin.ControlPanel
Directory.CreateDirectory(iconFolder);
}
foreach (ControlPanelItem item in controlPanelItems)
{
if (!File.Exists(iconFolder + item.GUID + fileType) && item.Icon != null)
@ -43,7 +42,10 @@ namespace Wox.Plugin.ControlPanel
foreach (var item in controlPanelItems)
{
item.Score = Score(item, query.Search);
var titleMatch = StringMatcher.FuzzySearch(query.Search, item.LocalizedString);
var subTitleMatch = StringMatcher.FuzzySearch(query.Search, item.InfoTip);
item.Score = Math.Max(titleMatch.Score, subTitleMatch.Score);
if (item.Score > 0)
{
var result = new Result
@ -66,6 +68,16 @@ namespace Wox.Plugin.ControlPanel
return true;
}
};
if (item.Score == titleMatch.Score)
{
result.TitleHighlightData = titleMatch.MatchData;
}
else
{
result.SubTitleHighlightData = subTitleMatch.MatchData;
}
results.Add(result);
}
}
@ -74,26 +86,6 @@ namespace Wox.Plugin.ControlPanel
return panelItems;
}
private int Score(ControlPanelItem item, string query)
{
var scores = new List<int> {0};
if (!string.IsNullOrEmpty(item.LocalizedString))
{
var score1 = StringMatcher.FuzzySearch(query, item.LocalizedString).ScoreAfterSearchPrecisionFilter();
var score2 = StringMatcher.ScoreForPinyin(item.LocalizedString, query);
scores.Add(score1);
scores.Add(score2);
}
if (!string.IsNullOrEmpty(item.InfoTip))
{
var score1 = StringMatcher.FuzzySearch(query, item.InfoTip).ScoreAfterSearchPrecisionFilter();
var score2 = StringMatcher.ScoreForPinyin(item.InfoTip, query);
scores.Add(score1);
scores.Add(score2);
}
return scores.Max();
}
public string GetTranslatedPluginTitle()
{
return context.API.GetTranslation("wox_plugin_controlpanel_plugin_name");

View File

@ -55,6 +55,7 @@ namespace Wox.Plugin.Everything
r.Title = Path.GetFileName(path);
r.SubTitle = path;
r.IcoPath = path;
r.TitleHighlightData = StringMatcher.FuzzySearch(keyword, Path.GetFileName(path)).MatchData;
r.Action = c =>
{
bool hide;
@ -78,6 +79,7 @@ namespace Wox.Plugin.Everything
return hide;
};
r.ContextData = s;
r.SubTitleHighlightData = StringMatcher.FuzzySearch(keyword, path).MatchData;
results.Add(r);
}
}

View File

@ -0,0 +1,218 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Image;
using Wox.Plugin.SharedCommands;
namespace Wox.Plugin.Folder
{
internal class ContextMenuLoader : IContextMenu
{
private readonly PluginInitContext _context;
public ContextMenuLoader(PluginInitContext context)
{
_context = context;
}
public List<Result> LoadContextMenus(Result selectedResult)
{
var contextMenus = new List<Result>();
if (selectedResult.ContextData is SearchResult record)
{
if (record.Type == ResultType.File)
{
contextMenus.Add(CreateOpenWithEditorResult(record));
contextMenus.Add(CreateOpenContainingFolderResult(record));
}
var icoPath = (record.Type == ResultType.File) ? Main.FileImagePath : Main.FolderImagePath;
var fileOrFolder = (record.Type == ResultType.File) ? "file" : "folder";
contextMenus.Add(new Result
{
Title = "Copy path",
SubTitle = $"Copy the current {fileOrFolder} path to clipboard",
Action = (context) =>
{
try
{
Clipboard.SetText(record.FullPath);
return true;
}
catch (Exception e)
{
var message = "Fail to set text in clipboard";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
},
IcoPath = Main.CopyImagePath
});
contextMenus.Add(new Result
{
Title = $"Copy {fileOrFolder}",
SubTitle = $"Copy the {fileOrFolder} to clipboard",
Action = (context) =>
{
try
{
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
return true;
}
catch (Exception e)
{
var message = $"Fail to set {fileOrFolder} in clipboard";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
},
IcoPath = icoPath
});
if (record.Type == ResultType.File || record.Type == ResultType.Folder)
contextMenus.Add(new Result
{
Title = $"Delete {fileOrFolder}",
SubTitle = $"Delete the selected {fileOrFolder}",
Action = (context) =>
{
try
{
if (record.Type == ResultType.File)
File.Delete(record.FullPath);
else
Directory.Delete(record.FullPath);
}
catch(Exception e)
{
var message = $"Fail to delete {fileOrFolder} at {record.FullPath}";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
return true;
},
IcoPath = Main.DeleteFileFolderImagePath
});
if (record.Type == ResultType.File && CanRunAsDifferentUser(record.FullPath))
contextMenus.Add(new Result
{
Title = "Run as different user",
Action = (context) =>
{
try
{
Task.Run(()=> ShellCommand.RunAsDifferentUser(record.FullPath.SetProcessStartInfo()));
}
catch (FileNotFoundException e)
{
var name = "Plugin: Folder";
var message = $"File not found: {e.Message}";
_context.API.ShowMsg(name, message);
}
return true;
},
IcoPath = "Images/user.png"
});
}
return contextMenus;
}
private Result CreateOpenContainingFolderResult(SearchResult record)
{
return new Result
{
Title = "Open containing folder",
Action = _ =>
{
try
{
Process.Start("explorer.exe", $" /select,\"{record.FullPath}\"");
}
catch(Exception e)
{
var message = $"Fail to open file at {record.FullPath}";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
return true;
},
IcoPath = Main.FolderImagePath
};
}
private Result CreateOpenWithEditorResult(SearchResult record)
{
string editorPath = "notepad.exe"; // TODO add the ability to create a custom editor
var name = "Open With Editor: " + Path.GetFileNameWithoutExtension(editorPath);
return new Result
{
Title = name,
Action = _ =>
{
try
{
Process.Start(editorPath, record.FullPath);
return true;
}
catch (Exception e)
{
var message = $"Fail to editor for file at {record.FullPath}";
LogException(message, e);
_context.API.ShowMsg(message);
return false;
}
},
IcoPath = editorPath
};
}
public void LogException(string message, Exception e)
{
Log.Exception($"|Wox.Plugin.Folder.ContextMenu|{message}", e);
}
private bool CanRunAsDifferentUser(string path)
{
switch(Path.GetExtension(path))
{
case ".exe":
case ".bat":
return true;
default:
return false;
}
}
}
public class SearchResult
{
public string FullPath { get; set; }
public ResultType Type { get; set; }
}
public enum ResultType
{
Volume,
Folder,
File
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 290 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@ -1,27 +1,33 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
namespace Wox.Plugin.Folder
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IContextMenu
{
public const string FolderImagePath = "Images\\folder.png";
public const string FileImagePath = "Images\\file.png";
public const string DeleteFileFolderImagePath = "Images\\deletefilefolder.png";
public const string CopyImagePath = "Images\\copy.png";
private static List<string> _driverNames;
private PluginInitContext _context;
private readonly Settings _settings;
private readonly PluginJsonStorage<Settings> _storage;
private IContextMenu _contextMenuLoader;
public Main()
{
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
InitialDriverList();
}
public void Save()
@ -37,54 +43,21 @@ namespace Wox.Plugin.Folder
public void Init(PluginInitContext context)
{
_context = context;
_contextMenuLoader = new ContextMenuLoader(context);
InitialDriverList();
}
public List<Result> Query(Query query)
{
var results = GetUserFolderResults(query);
string search = query.Search.ToLower();
List<FolderLink> userFolderLinks = _settings.FolderLinks.Where(
x => x.Nickname.StartsWith(search, StringComparison.OrdinalIgnoreCase)).ToList();
List<Result> results =
userFolderLinks.Select(
item => new Result()
{
Title = item.Nickname,
IcoPath = item.Path,
SubTitle = "Ctrl + Enter to open the directory",
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(item.Path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + item.Path);
return false;
}
}
_context.API.ChangeQuery($"{query.ActionKeyword} {item.Path}{(item.Path.EndsWith("\\") ? "" : "\\")}");
return false;
},
ContextData = item,
}).ToList();
if (_driverNames != null && !_driverNames.Any(search.StartsWith))
if (!IsDriveOrSharedFolder(search))
return results;
//if (!input.EndsWith("\\"))
//{
// //"c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive"
// input = input + "\\";
//}
results.AddRange(QueryInternal_Directory_Exists(query));
// todo temp hack for scores
// todo why was this hack here?
foreach (var result in results)
{
result.Score += 10;
@ -92,12 +65,77 @@ namespace Wox.Plugin.Folder
return results;
}
private static bool IsDriveOrSharedFolder(string search)
{
if (search.StartsWith(@"\\"))
{ // share folder
return true;
}
if (_driverNames != null && _driverNames.Any(search.StartsWith))
{ // normal drive letter
return true;
}
if (_driverNames == null && search.Length > 2 && char.IsLetter(search[0]) && search[1] == ':')
{ // when we don't have the drive letters we can try...
return true; // we don't know so let's give it the possibility
}
return false;
}
private Result CreateFolderResult(string title, string path, Query query)
{
return new Result
{
Title = title,
IcoPath = path,
SubTitle = "Ctrl + Enter to open the directory",
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData,
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + path);
return false;
}
}
string changeTo = path.EndsWith("\\") ? path : path + "\\";
_context.API.ChangeQuery(string.IsNullOrEmpty(query.ActionKeyword) ?
changeTo :
query.ActionKeyword + " " + changeTo);
return false;
},
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = path }
};
}
private List<Result> GetUserFolderResults(Query query)
{
string search = query.Search.ToLower();
var userFolderLinks = _settings.FolderLinks.Where(
x => x.Nickname.StartsWith(search, StringComparison.OrdinalIgnoreCase));
var results = userFolderLinks.Select(item =>
CreateFolderResult(item.Nickname, item.Path, query)).ToList();
return results;
}
private void InitialDriverList()
{
if (_driverNames == null)
{
_driverNames = new List<string>();
DriveInfo[] allDrives = DriveInfo.GetDrives();
var allDrives = DriveInfo.GetDrives();
foreach (DriveInfo driver in allDrives)
{
_driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
@ -105,117 +143,135 @@ namespace Wox.Plugin.Folder
}
}
private static readonly char[] _specialSearchChars = new char[]
{
'?', '*', '>'
};
private List<Result> QueryInternal_Directory_Exists(Query query)
{
var search = query.Search.ToLower();
var search = query.Search;
var results = new List<Result>();
var hasSpecial = search.IndexOfAny(_specialSearchChars) >= 0;
string incompleteName = "";
if (!Directory.Exists(search + "\\"))
if (hasSpecial || !Directory.Exists(search + "\\"))
{
//if the last component of the path is incomplete,
//then make auto complete for it.
// if folder doesn't exist, we want to take the last part and use it afterwards to help the user
// find the right folder.
int index = search.LastIndexOf('\\');
if (index > 0 && index < (search.Length - 1))
{
incompleteName = search.Substring(index + 1);
incompleteName = incompleteName.ToLower();
incompleteName = search.Substring(index + 1).ToLower();
search = search.Substring(0, index + 1);
if (!Directory.Exists(search))
{
return results;
}
}
else
{
return results;
}
}
else
{
// folder exist, add \ at the end of doesn't exist
if (!search.EndsWith("\\"))
{
search += "\\";
}
}
string firstResult = "Open current directory";
results.Add(CreateOpenCurrentFolderResult(incompleteName, search));
var searchOption = SearchOption.TopDirectoryOnly;
incompleteName += "*";
// give the ability to search all folder when starting with >
if (incompleteName.StartsWith(">"))
{
searchOption = SearchOption.AllDirectories;
incompleteName = incompleteName.Substring(1);
}
try
{
// search folder and add results
var directoryInfo = new DirectoryInfo(search);
var fileSystemInfos = directoryInfo.GetFileSystemInfos(incompleteName, searchOption);
foreach (var fileSystemInfo in fileSystemInfos)
{
if ((fileSystemInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
var result =
fileSystemInfo is DirectoryInfo
? CreateFolderResult(fileSystemInfo.Name, fileSystemInfo.FullName, query)
: CreateFileResult(fileSystemInfo.FullName, query);
results.Add(result);
}
}
catch (Exception e)
{
if (e is UnauthorizedAccessException || e is ArgumentException)
{
results.Add(new Result { Title = e.Message, Score = 501 });
return results;
}
throw;
}
return results;
}
private static Result CreateFileResult(string filePath, Query query)
{
var result = new Result
{
Title = Path.GetFileName(filePath),
IcoPath = filePath,
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, Path.GetFileName(filePath)).MatchData,
Action = c =>
{
try
{
Process.Start(filePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + filePath);
}
return true;
},
ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath}
};
return result;
}
private static Result CreateOpenCurrentFolderResult(string incompleteName, string search)
{
var firstResult = "Open current directory";
if (incompleteName.Length > 0)
firstResult = "Open " + search;
results.Add(new Result
var folderName = search.TrimEnd('\\').Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).Last();
return new Result
{
Title = firstResult,
SubTitle = $"Use > to search files and subfolders within {folderName}, " +
$"* to search for file extensions in {folderName} or both >* to combine the search",
IcoPath = search,
Score = 10000,
Score = 500,
Action = c =>
{
Process.Start(search);
return true;
}
});
//Add children directories
DirectoryInfo[] dirs = new DirectoryInfo(search).GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
if (incompleteName.Length != 0 && !dir.Name.ToLower().StartsWith(incompleteName))
continue;
DirectoryInfo dirCopy = dir;
var result = new Result
{
Title = dir.Name,
IcoPath = dir.FullName,
SubTitle = "Ctrl + Enter to open the directory",
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(dirCopy.FullName);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + dirCopy.FullName);
return false;
}
}
_context.API.ChangeQuery($"{query.ActionKeyword} {dirCopy.FullName}\\");
return false;
}
};
results.Add(result);
}
//Add children files
FileInfo[] files = new DirectoryInfo(search).GetFiles();
foreach (FileInfo file in files)
{
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
if (incompleteName.Length != 0 && !file.Name.ToLower().StartsWith(incompleteName))
continue;
string filePath = file.FullName;
var result = new Result
{
Title = Path.GetFileName(filePath),
IcoPath = filePath,
Action = c =>
{
try
{
Process.Start(filePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + filePath);
}
return true;
}
};
results.Add(result);
}
return results;
};
}
public string GetTranslatedPluginTitle()
@ -227,5 +283,10 @@ namespace Wox.Plugin.Folder
{
return _context.API.GetTranslation("wox_plugin_folder_plugin_description");
}
public List<Result> LoadContextMenus(Result selectedResult)
{
return _contextMenuLoader.LoadContextMenus(selectedResult);
}
}
}

View File

@ -58,6 +58,7 @@
<Compile Include="..\..\SolutionAssemblyInfo.cs">
<Link>Properties\SolutionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="ContextMenuLoader.cs" />
<Compile Include="FolderLink.cs" />
<Compile Include="Main.cs" />
<Compile Include="FolderPluginSettings.xaml.cs">
@ -80,6 +81,15 @@
<None Include="Images\copy.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Images\deletefilefolder.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Images\file.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="Images\user.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Languages\en.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

View File

@ -8,6 +8,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
@ -142,6 +143,8 @@ namespace Wox.Plugin.PluginManagement
Title = r.name,
SubTitle = r.description,
IcoPath = "Images\\plugin.png",
TitleHighlightData = StringMatcher.FuzzySearch(query.SecondSearch, r.name).MatchData,
SubTitleHighlightData = StringMatcher.FuzzySearch(query.SecondSearch, r.description).MatchData,
Action = c =>
{
MessageBoxResult result = MessageBox.Show("Are you sure you wish to install the \'" + r.name + "\' plugin",
@ -191,6 +194,8 @@ namespace Wox.Plugin.PluginManagement
Title = plugin.Name,
SubTitle = plugin.Description,
IcoPath = plugin.IcoPath,
TitleHighlightData = StringMatcher.FuzzySearch(query.SecondSearch, plugin.Name).MatchData,
SubTitleHighlightData = StringMatcher.FuzzySearch(query.SecondSearch, plugin.Description).MatchData,
Action = e =>
{
UnInstallPlugin(plugin);

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@ -31,6 +31,7 @@
<system:String x:Key="wox_plugin_program_update_file_suffixes">Successfully updated file suffixes</system:String>
<system:String x:Key="wox_plugin_program_suffixes_cannot_empty">File suffixes can't be empty</system:String>
<system:String x:Key="wox_plugin_program_run_as_different_user">Run As Different User</system:String>
<system:String x:Key="wox_plugin_program_run_as_administrator">Run As Administrator</system:String>
<system:String x:Key="wox_plugin_program_open_containing_folder">Open containing folder</system:String>
<system:String x:Key="wox_plugin_program_disable_program">Disable this program from displaying</system:String>

View File

@ -24,7 +24,7 @@ namespace Wox.Plugin.Program
private static PluginInitContext _context;
private static BinaryStorage<Win32[]> _win32Storage;
private static BinaryStorage<UWP.Application[]> _uwpStorage;
private static BinaryStorage<UWP.Application[]> _uwpStorage;
private readonly PluginJsonStorage<Settings> _settingsStorage;
public Main()
@ -41,7 +41,7 @@ namespace Wox.Plugin.Program
});
Log.Info($"|Wox.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Length}>");
Log.Info($"|Wox.Plugin.Program.Main|Number of preload uwps <{_uwps.Length}>");
var a = Task.Run(() =>
{
if (IsStartupIndexProgramsRequired || !_win32s.Any())
@ -68,19 +68,25 @@ namespace Wox.Plugin.Program
public List<Result> Query(Query query)
{
Win32[] win32;
UWP.Application[] uwps;
lock (IndexLock)
{
var results1 = _win32s.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var results2 = _uwps.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var result = results1.Concat(results2).Where(r => r.Score > 0).ToList();
return result;
{ // just take the reference inside the lock to eliminate query time issues.
win32 = _win32s;
uwps = _uwps;
}
var results1 = win32.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var results2 = uwps.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var result = results1.Concat(results2).Where(r => r != null && r.Score > 0).ToList();
return result;
}
public void Init(PluginInitContext context)
@ -111,14 +117,14 @@ namespace Wox.Plugin.Program
public static void IndexPrograms()
{
var t1 = Task.Run(()=>IndexWin32Programs());
var t1 = Task.Run(() => IndexWin32Programs());
var t2 = Task.Run(()=>IndexUWPPrograms());
var t2 = Task.Run(() => IndexUWPPrograms());
Task.WaitAll(t1, t2);
_settings.LastIndexTime = DateTime.Today;
}
}
public Control CreateSettingPanel()
{
@ -141,7 +147,7 @@ namespace Wox.Plugin.Program
var program = selectedResult.ContextData as IProgram;
if (program != null)
{
menuOptions = program.ContextMenus(_context.API);
menuOptions = program.ContextMenus(_context.API);
}
menuOptions.Add(
@ -151,7 +157,7 @@ namespace Wox.Plugin.Program
Action = c =>
{
DisableProgram(program);
_context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_program_disable_dlgtitle_success"),
_context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_program_disable_dlgtitle_success"),
_context.API.GetTranslation("wox_plugin_program_disable_dlgtitle_success_message"));
return false;
},
@ -185,22 +191,19 @@ namespace Wox.Plugin.Program
);
}
public static bool StartProcess(ProcessStartInfo info)
public static void StartProcess(Func<ProcessStartInfo, Process> runProcess, ProcessStartInfo info)
{
bool hide;
try
{
Process.Start(info);
hide = true;
runProcess(info);
}
catch (Exception)
{
var name = "Plugin: Program";
var message = $"Can't start: {info.FileName}";
var message = $"Unable to start: {info.FileName}";
_context.API.ShowMsg(name, message, string.Empty);
hide = false;
}
return hide;
}
public void ReloadData()

View File

@ -35,7 +35,6 @@ namespace Wox.Plugin.Program.Programs
public UWP(Package package)
{
Location = package.InstalledLocation.Path;
Name = package.Id.Name;
FullName = package.Id.FullName;
@ -266,21 +265,25 @@ namespace Wox.Plugin.Program.Programs
private int Score(string query)
{
var score1 = StringMatcher.FuzzySearch(query, DisplayName).ScoreAfterSearchPrecisionFilter();
var score2 = StringMatcher.ScoreForPinyin(DisplayName, query);
var score3 = StringMatcher.FuzzySearch(query, Description).ScoreAfterSearchPrecisionFilter();
var score4 = StringMatcher.ScoreForPinyin(Description, query);
var score = new[] { score1, score2, score3, score4 }.Max();
var displayNameMatch = StringMatcher.FuzzySearch(query, DisplayName);
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
var score = new[] { displayNameMatch.Score, descriptionMatch.Score }.Max();
return score;
}
public Result Result(string query, IPublicAPI api)
{
var score = Score(query);
if (score <= 0)
{ // no need to create result if score is 0
return null;
}
var result = new Result
{
SubTitle = Package.Location,
Icon = Logo,
Score = Score(query),
Score = score,
ContextData = this,
Action = e =>
{
@ -293,14 +296,18 @@ namespace Wox.Plugin.Program.Programs
Description.Substring(0, DisplayName.Length) == DisplayName)
{
result.Title = Description;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Description).MatchData;
}
else if (!string.IsNullOrEmpty(Description))
{
result.Title = $"{DisplayName}: {Description}";
var title = $"{DisplayName}: {Description}";
result.Title = title;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, title).MatchData;
}
else
{
result.Title = DisplayName;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, DisplayName).MatchData;
}
return result;
}
@ -312,11 +319,14 @@ namespace Wox.Plugin.Program.Programs
new Result
{
Title = api.GetTranslation("wox_plugin_program_open_containing_folder"),
Action = _ =>
{
var hide = Main.StartProcess(new ProcessStartInfo(Package.Location));
return hide;
Main.StartProcess(Process.Start, new ProcessStartInfo(Package.Location));
return true;
},
IcoPath = "Images/folder.png"
}
};

View File

@ -6,10 +6,12 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using Shell;
using Wox.Infrastructure;
using Wox.Plugin.Program.Logger;
using Wox.Plugin.SharedCommands;
namespace Wox.Plugin.Program.Programs
{
@ -33,23 +35,27 @@ namespace Wox.Plugin.Program.Programs
private int Score(string query)
{
var score1 = StringMatcher.FuzzySearch(query, Name).ScoreAfterSearchPrecisionFilter();
var score2 = StringMatcher.ScoreForPinyin(Name, query);
var score3 = StringMatcher.FuzzySearch(query, Description).ScoreAfterSearchPrecisionFilter();
var score4 = StringMatcher.ScoreForPinyin(Description, query);
var score5 = StringMatcher.FuzzySearch(query, ExecutableName).ScoreAfterSearchPrecisionFilter();
var score = new[] { score1, score2, score3, score4, score5 }.Max();
var nameMatch = StringMatcher.FuzzySearch(query, Name);
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
var executableNameMatch = StringMatcher.FuzzySearch(query, ExecutableName);
var score = new[] { nameMatch.Score, descriptionMatch.Score, executableNameMatch.Score }.Max();
return score;
}
public Result Result(string query, IPublicAPI api)
{
var score = Score(query);
if (score <= 0)
{ // no need to create result if this is zero
return null;
}
var result = new Result
{
SubTitle = FullPath,
IcoPath = IcoPath,
Score = Score(query),
Score = score,
ContextData = this,
Action = e =>
{
@ -58,8 +64,10 @@ namespace Wox.Plugin.Program.Programs
FileName = FullPath,
WorkingDirectory = ParentDirectory
};
var hide = Main.StartProcess(info);
return hide;
Main.StartProcess(Process.Start, info);
return true;
}
};
@ -67,14 +75,18 @@ namespace Wox.Plugin.Program.Programs
Description.Substring(0, Name.Length) == Name)
{
result.Title = Description;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Description).MatchData;
}
else if (!string.IsNullOrEmpty(Description))
{
result.Title = $"{Name}: {Description}";
var title = $"{Name}: {Description}";
result.Title = title;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, title).MatchData;
}
else
{
result.Title = Name;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
}
return result;
@ -85,6 +97,19 @@ namespace Wox.Plugin.Program.Programs
{
var contextMenus = new List<Result>
{
new Result
{
Title = api.GetTranslation("wox_plugin_program_run_as_different_user"),
Action = _ =>
{
var info = FullPath.SetProcessStartInfo(ParentDirectory);
Task.Run(() => Main.StartProcess(ShellCommand.RunAsDifferentUser, info));
return true;
},
IcoPath = "Images/user.png"
},
new Result
{
Title = api.GetTranslation("wox_plugin_program_run_as_administrator"),
@ -96,8 +121,10 @@ namespace Wox.Plugin.Program.Programs
WorkingDirectory = ParentDirectory,
Verb = "runas"
};
var hide = Main.StartProcess(info);
return hide;
Task.Run(() => Main.StartProcess(Process.Start, info));
return true;
},
IcoPath = "Images/cmd.png"
},
@ -106,8 +133,9 @@ namespace Wox.Plugin.Program.Programs
Title = api.GetTranslation("wox_plugin_program_open_containing_folder"),
Action = _ =>
{
var hide = Main.StartProcess(new ProcessStartInfo(ParentDirectory));
return hide;
Main.StartProcess(Process.Start, new ProcessStartInfo(ParentDirectory));
return true;
},
IcoPath = "Images/folder.png"
}
@ -193,7 +221,7 @@ namespace Wox.Plugin.Program.Programs
catch (COMException e)
{
// C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception
ProgramLogger.LogException($"|Win32|LnkProgram|{path}"+
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
"|Error caused likely due to trying to get the description of the program", e);
program.Valid = false;

View File

@ -112,6 +112,9 @@
<None Include="Images\disable.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Images\user.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Languages\en.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@ -4,6 +4,8 @@
<system:String x:Key="wox_plugin_cmd_relace_winr">Replace Win+R</system:String>
<system:String x:Key="wox_plugin_cmd_leave_cmd_open">Do not close Command Prompt after command execution</system:String>
<system:String x:Key="wox_plugin_cmd_always_run_as_administrator">Always run as administrator</system:String>
<system:String x:Key="wox_plugin_cmd_run_as_different_user">Run as different user</system:String>
<system:String x:Key="wox_plugin_cmd_plugin_name">Shell</system:String>
<system:String x:Key="wox_plugin_cmd_plugin_description">Allows to execute system commands from Wox. Commands should start with ></system:String>
<system:String x:Key="wox_plugin_cmd_cmd_has_been_executed_times">this command has been executed {0} times</system:String>

View File

@ -1,14 +1,17 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WindowsInput;
using WindowsInput.Native;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin.SharedCommands;
using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control;
using Keys = System.Windows.Forms.Keys;
@ -83,7 +86,7 @@ namespace Wox.Plugin.Shell
IcoPath = Image,
Action = c =>
{
Execute(m);
Execute(Process.Start, PrepareProcessStartInfo(m));
return true;
}
}));
@ -116,7 +119,7 @@ namespace Wox.Plugin.Shell
IcoPath = Image,
Action = c =>
{
Execute(m.Key);
Execute(Process.Start, PrepareProcessStartInfo(m.Key));
return true;
}
};
@ -135,7 +138,7 @@ namespace Wox.Plugin.Shell
IcoPath = Image,
Action = c =>
{
Execute(cmd);
Execute(Process.Start, PrepareProcessStartInfo(cmd));
return true;
}
};
@ -153,27 +156,26 @@ namespace Wox.Plugin.Shell
IcoPath = Image,
Action = c =>
{
Execute(m.Key);
Execute(Process.Start, PrepareProcessStartInfo(m.Key));
return true;
}
}).Take(5);
return history.ToList();
}
private void Execute(string command, bool runAsAdministrator = false)
private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
{
command = command.Trim();
command = Environment.ExpandEnvironmentVariables(command);
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";
ProcessStartInfo info;
if (_settings.Shell == Shell.Cmd)
{
var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
info = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = arguments,
};
info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
}
else if (_settings.Shell == Shell.Powershell)
{
@ -186,11 +188,8 @@ namespace Wox.Plugin.Shell
{
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
}
info = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = arguments
};
info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
}
else if (_settings.Shell == Shell.RunCommand)
{
@ -200,41 +199,48 @@ namespace Wox.Plugin.Shell
var filename = parts[0];
if (ExistInPath(filename))
{
var arguemtns = parts[1];
info = new ProcessStartInfo
{
FileName = filename,
Arguments = arguemtns
};
var arguments = parts[1];
info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
}
else
{
info = new ProcessStartInfo(command);
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
}
}
else
{
info = new ProcessStartInfo(command);
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
}
}
else
{
return;
throw new NotImplementedException();
}
info.UseShellExecute = true;
info.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
info.Verb = runAsAdministrator ? "runas" : "";
_settings.AddCmdHistory(command);
return info;
}
private void Execute(Func<ProcessStartInfo, Process> startProcess,ProcessStartInfo info)
{
try
{
Process.Start(info);
_settings.AddCmdHistory(command);
startProcess(info);
}
catch (FileNotFoundException e)
{
MessageBox.Show($"Command not found: {e.Message}");
var name = "Plugin: Shell";
var message = $"Command not found: {e.Message}";
_context.API.ShowMsg(name, message);
}
catch(Win32Exception e)
{
var name = "Plugin: Shell";
var message = $"Error running the command: {e.Message}";
_context.API.ShowMsg(name, message);
}
}
@ -316,19 +322,31 @@ namespace Wox.Plugin.Shell
public List<Result> LoadContextMenus(Result selectedResult)
{
return new List<Result>
var resultlist = new List<Result>
{
new Result
{
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"),
Action = c =>
{
Execute(selectedResult.Title, true);
return true;
},
IcoPath = Image
}
};
new Result
{
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_different_user"),
Action = c =>
{
Task.Run(() =>Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title)));
return true;
},
IcoPath = "Images/user.png"
},
new Result
{
Title = _context.API.GetTranslation("wox_plugin_cmd_run_as_administrator"),
Action = c =>
{
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
return true;
},
IcoPath = Image
}
};
return resultlist;
}
}
}

View File

@ -7,6 +7,8 @@ namespace Wox.Plugin.Shell
public Shell Shell { get; set; } = Shell.Cmd;
public bool ReplaceWinR { get; set; } = true;
public bool LeaveShellOpen { get; set; }
public bool RunAsAdministrator { get; set; } = true;
public Dictionary<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName)

View File

@ -12,10 +12,12 @@
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" x:Name="ReplaceWinR" Content="{DynamicResource wox_plugin_cmd_relace_winr}" Margin="10" HorizontalAlignment="Left"/>
<CheckBox Grid.Row="1" x:Name="LeaveShellOpen" Content="{DynamicResource wox_plugin_cmd_leave_cmd_open}" Margin="10" HorizontalAlignment="Left"/>
<ComboBox Grid.Row="2" x:Name="ShellComboBox" Margin="10" HorizontalAlignment="Left" >
<CheckBox Grid.Row="2" x:Name="AlwaysRunAsAdministrator" Content="{DynamicResource wox_plugin_cmd_always_run_as_administrator}" Margin="10" HorizontalAlignment="Left"/>
<ComboBox Grid.Row="3" x:Name="ShellComboBox" Margin="10" HorizontalAlignment="Left" >
<ComboBoxItem>CMD</ComboBoxItem>
<ComboBoxItem>PowerShell</ComboBoxItem>
<ComboBoxItem>RunCommand</ComboBoxItem>

View File

@ -17,6 +17,7 @@ namespace Wox.Plugin.Shell
{
ReplaceWinR.IsChecked = _settings.ReplaceWinR;
LeaveShellOpen.IsChecked = _settings.LeaveShellOpen;
AlwaysRunAsAdministrator.IsChecked = _settings.RunAsAdministrator;
LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand;
LeaveShellOpen.Checked += (o, e) =>
@ -29,6 +30,16 @@ namespace Wox.Plugin.Shell
_settings.LeaveShellOpen = false;
};
AlwaysRunAsAdministrator.Checked += (o, e) =>
{
_settings.RunAsAdministrator = true;
};
AlwaysRunAsAdministrator.Unchecked += (o, e) =>
{
_settings.RunAsAdministrator = false;
};
ReplaceWinR.Checked += (o, e) =>
{
_settings.ReplaceWinR = true;

View File

@ -70,6 +70,9 @@
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="Images\user.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="Languages\en.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

View File

@ -56,12 +56,21 @@ namespace Wox.Plugin.Sys
var results = new List<Result>();
foreach (var c in commands)
{
var titleScore = StringMatcher.FuzzySearch(query.Search, c.Title).ScoreAfterSearchPrecisionFilter();
var subTitleScore = StringMatcher.FuzzySearch(query.Search, c.SubTitle).ScoreAfterSearchPrecisionFilter();
var score = Math.Max(titleScore, subTitleScore);
var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
var subTitleMatch = StringMatcher.FuzzySearch(query.Search, c.SubTitle);
var score = Math.Max(titleMatch.Score, subTitleMatch.Score);
if (score > 0)
{
c.Score = score;
if (score == titleMatch.Score)
{
c.TitleHighlightData = titleMatch.MatchData;
}
else
{
c.SubTitleHighlightData = subTitleMatch.MatchData;
}
results.Add(c);
}
}

View File

@ -172,13 +172,13 @@ namespace Wox.Core.Plugin
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
{
var results = new List<Result>();
try
{
List<Result> results = null;
var metadata = pair.Metadata;
var milliseconds = Stopwatch.Debug($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}", () =>
{
results = pair.Plugin.Query(query) ?? results;
results = pair.Plugin.Query(query) ?? new List<Result>();
UpdatePluginMetadata(results, metadata, query);
});
metadata.QueryCount += 1;

View File

@ -16,16 +16,22 @@ namespace Wox.Infrastructure
private static ConcurrentDictionary<string, string[][]> PinyinCache;
private static BinaryStorage<ConcurrentDictionary<string, string[][]>> _pinyinStorage;
private static Settings _settings;
public static void Initialize(Settings settings)
{
_settings = settings;
InitializePinyinHelpers();
}
private static void InitializePinyinHelpers()
{
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
{
_pinyinStorage = new BinaryStorage<ConcurrentDictionary<string, string[][]>>("Pinyin");
PinyinCache = _pinyinStorage.TryLoad(new ConcurrentDictionary<string, string[][]>());
// force pinyin library static constructor initialize
PinyinHelper.toHanyuPinyinStringArray('T', Format);
});
@ -34,6 +40,10 @@ namespace Wox.Infrastructure
public static void Save()
{
if (!_settings.ShouldUsePinyin)
{
return;
}
_pinyinStorage.Save(PinyinCache);
}
@ -68,39 +78,36 @@ namespace Wox.Infrastructure
/// </summmary>
public static string[][] PinyinComination(string characters)
{
if (_settings.ShouldUsePinyin && !string.IsNullOrEmpty(characters))
if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters))
{
if (!PinyinCache.ContainsKey(characters))
{
return Empty2DStringArray;
}
var allPinyins = new List<string[]>();
foreach (var c in characters)
if (!PinyinCache.ContainsKey(characters))
{
var allPinyins = new List<string[]>();
foreach (var c in characters)
{
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
if (pinyins != null)
{
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
if (pinyins != null)
{
var r = pinyins.Distinct().ToArray();
allPinyins.Add(r);
}
else
{
var r = new[] { c.ToString() };
allPinyins.Add(r);
}
var r = pinyins.Distinct().ToArray();
allPinyins.Add(r);
}
else
{
var r = new[] { c.ToString() };
allPinyins.Add(r);
}
}
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
PinyinCache[characters] = combination;
return combination;
}
else
{
return PinyinCache[characters];
}
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
PinyinCache[characters] = combination;
return combination;
}
else
{
return Empty2DStringArray;
return PinyinCache[characters];
}
}
@ -142,7 +149,5 @@ namespace Wox.Infrastructure
).ToArray();
return combination;
}
}
}

View File

@ -87,7 +87,7 @@ namespace Wox.Infrastructure.Logger
do
{
logger.Error($"Exception fulle name:\n <{e.GetType().FullName}>");
logger.Error($"Exception full name:\n <{e.GetType().FullName}>");
logger.Error($"Exception message:\n <{e.Message}>");
logger.Error($"Exception stack trace:\n <{e.StackTrace}>");
logger.Error($"Exception source:\n <{e.Source}>");

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Wox.Infrastructure.Storage
{
class WoxJsonStorage<T> : JsonStrorage<T> where T : new()
public class WoxJsonStorage<T> : JsonStrorage<T> where T : new()
{
public WoxJsonStorage()
{
@ -18,4 +18,4 @@ namespace Wox.Infrastructure.Storage
FilePath = Path.Combine(directoryPath, $"{filename}{FileSuffix}");
}
}
}
}

View File

@ -1,16 +1,19 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
using static Wox.Infrastructure.StringMatcher;
namespace Wox.Infrastructure
namespace Wox.Infrastructure
{
public static class StringMatcher
{
public static MatchOption DefaultMatchOption = new MatchOption();
public static string UserSettingSearchPrecision { get; set; }
public static bool ShouldUsePinyin { get; set; }
[Obsolete("This method is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")]
public static int Score(string source, string target)
@ -54,6 +57,9 @@ namespace Wox.Infrastructure
var firstMatchIndex = -1;
var lastMatchIndex = 0;
char ch;
var indexList = new List<int>();
for (var idx = 0; idx < len; idx++)
{
ch = stringToCompare[idx];
@ -63,6 +69,7 @@ namespace Wox.Infrastructure
firstMatchIndex = idx;
lastMatchIndex = idx + 1;
indexList.Add(idx);
sb.Append(opt.Prefix + ch + opt.Suffix);
patternIdx += 1;
}
@ -82,27 +89,38 @@ namespace Wox.Infrastructure
// return rendered string if we have a match for every char
if (patternIdx == pattern.Length)
{
return new MatchResult
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex);
var pinyinScore = ScoreForPinyin(stringToCompare, query);
var result = new MatchResult
{
Success = true,
Value = sb.ToString(),
Score = CalScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex)
MatchData = indexList,
RawScore = Math.Max(score, pinyinScore)
};
return result;
}
return new MatchResult { Success = false };
}
private static int CalScore(string query, string stringToCompare, int firstIndex, int matchLen)
private static int CalculateSearchScore(string query, string stringToCompare, int firstIndex, int matchLen)
{
//a match found near the beginning of a string is scored more than a match found near the end
//a match is scored more if the characters in the patterns are closer to each other, while the score is lower if they are more spread out
// A match found near the beginning of a string is scored more than a match found near the end
// A match is scored more if the characters in the patterns are closer to each other,
// while the score is lower if they are more spread out
var score = 100 * (query.Length + 1) / ((1 + firstIndex) + (matchLen + 1));
//a match with less characters assigning more weights
// A match with less characters assigning more weights
if (stringToCompare.Length - query.Length < 5)
{
score += 20;
}
else if (stringToCompare.Length - query.Length < 10)
{
score += 10;
}
return score;
}
@ -114,21 +132,13 @@ namespace Wox.Infrastructure
None = 0
}
public static bool IsSearchPrecisionScoreMet(this MatchResult matchResult)
{
var precisionScore = (SearchPrecisionScore)Enum.Parse(typeof(SearchPrecisionScore),
UserSettingSearchPrecision ?? SearchPrecisionScore.Regular.ToString());
return matchResult.Score >= (int)precisionScore;
}
public static int ScoreAfterSearchPrecisionFilter(this MatchResult matchResult)
{
return matchResult.IsSearchPrecisionScoreMet() ? matchResult.Score : 0;
}
public static int ScoreForPinyin(string source, string target)
{
if (!ShouldUsePinyin)
{
return 0;
}
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
{
if (Alphabet.ContainsChinese(source))
@ -158,12 +168,48 @@ namespace Wox.Infrastructure
public class MatchResult
{
public bool Success { get; set; }
public int Score { get; set; }
/// <summary>
/// highlight string
/// The final score of the match result with all search precision filters applied.
/// </summary>
public string Value { get; set; }
public int Score { get; private set; }
/// <summary>
/// The raw calculated search score without any search precision filtering applied.
/// </summary>
private int _rawScore;
public int RawScore
{
get { return _rawScore; }
set
{
_rawScore = value;
Score = ApplySearchPrecisionFilter(_rawScore);
}
}
/// <summary>
/// Matched data to highlight.
/// </summary>
public List<int> MatchData { get; set; }
public bool IsSearchPrecisionScoreMet()
{
return IsSearchPrecisionScoreMet(Score);
}
private bool IsSearchPrecisionScoreMet(int score)
{
var precisionScore = (SearchPrecisionScore)Enum.Parse(
typeof(SearchPrecisionScore),
UserSettingSearchPrecision ?? SearchPrecisionScore.Regular.ToString());
return score >= (int)precisionScore;
}
private int ApplySearchPrecisionFilter(int score)
{
return IsSearchPrecisionScoreMet(score) ? score : 0;
}
}
public class MatchOption

View File

@ -24,7 +24,17 @@ namespace Wox.Infrastructure.UserSettings
/// <summary>
/// when false Alphabet static service will always return empty results
/// </summary>
public bool ShouldUsePinyin { get; set; } = true;
private bool _shouldUsePinyin = true;
public bool ShouldUsePinyin
{
get { return _shouldUsePinyin; }
set
{
_shouldUsePinyin = value;
StringMatcher.ShouldUsePinyin = value;
}
}
private string _querySearchPrecision { get; set; } = StringMatcher.SearchPrecisionScore.Regular.ToString();
public string QuerySearchPrecision

View File

@ -42,6 +42,16 @@ namespace Wox.Plugin
public int Score { get; set; }
/// <summary>
/// A list of indexes for the characters to be highlighted in Title
/// </summary>
public IList<int> TitleHighlightData { get; set; }
/// <summary>
/// A list of indexes for the characters to be highlighted in SubTitle
/// </summary>
public IList<int> SubTitleHighlightData { get; set; }
/// <summary>
/// Only resulsts that originQuery match with curren query will be displayed in the panel
/// </summary>
@ -69,7 +79,9 @@ namespace Wox.Plugin
var equality = string.Equals(r?.Title, Title) &&
string.Equals(r?.SubTitle, SubTitle) &&
string.Equals(r?.IcoPath, IcoPath);
string.Equals(r?.IcoPath, IcoPath) &&
TitleHighlightData == r.TitleHighlightData &&
SubTitleHighlightData == r.SubTitleHighlightData;
return equality;
}
@ -108,7 +120,7 @@ namespace Wox.Plugin
public object ContextData { get; set; }
/// <summary>
/// Plugin ID that generate this result
/// Plugin ID that generated this result
/// </summary>
public string PluginID { get; internal set; }
}

View File

@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Wox.Plugin.SharedCommands
{
public static class ShellCommand
{
public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")] static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll")] static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hwnd);
private static bool containsSecurityWindow;
public static Process RunAsDifferentUser(ProcessStartInfo processStartInfo)
{
processStartInfo.Verb = "RunAsUser";
var process = Process.Start(processStartInfo);
containsSecurityWindow = false;
while (!containsSecurityWindow) // wait for windows to bring up the "Windows Security" dialog
{
CheckSecurityWindow();
Thread.Sleep(25);
}
while (containsSecurityWindow) // while this process contains a "Windows Security" dialog, stay open
{
containsSecurityWindow = false;
CheckSecurityWindow();
Thread.Sleep(50);
}
return process;
}
private static void CheckSecurityWindow()
{
ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads;
for (int i = 0; i < ptc.Count; i++)
EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero);
}
private static bool CheckSecurityThread(IntPtr hwnd, IntPtr lParam)
{
if (GetWindowTitle(hwnd) == "Windows Security")
containsSecurityWindow = true;
return true;
}
private static string GetWindowTitle(IntPtr hwnd)
{
StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1);
GetWindowText(hwnd, sb, sb.Capacity);
return sb.ToString();
}
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "")
{
var info = new ProcessStartInfo
{
FileName = fileName,
WorkingDirectory = workingDirectory,
Arguments = arguments,
Verb = verb
};
return info;
}
}
}

View File

@ -78,6 +78,7 @@
<Compile Include="Result.cs" />
<Compile Include="ActionContext.cs" />
<Compile Include="SharedCommands\SearchWeb.cs" />
<Compile Include="SharedCommands\ShellCommand.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using Wox.Infrastructure;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
namespace Wox.Test
@ -11,7 +12,7 @@ namespace Wox.Test
[TestFixture]
public class FuzzyMatcherTest
{
public List<string> GetSearchStrings()
public List<string> GetSearchStrings()
=> new List<string>
{
"Chrome",
@ -48,14 +49,13 @@ namespace Wox.Test
"aac"
};
var results = new List<Result>();
foreach (var str in sources)
{
results.Add(new Result
{
Title = str,
Score = StringMatcher.FuzzySearch("inst", str).Score
Score = StringMatcher.FuzzySearch("inst", str).RawScore
});
}
@ -72,7 +72,7 @@ namespace Wox.Test
{
var compareString = "Can have rum only in my glass";
var scoreResult = StringMatcher.FuzzySearch(searchString, compareString).Score;
var scoreResult = StringMatcher.FuzzySearch(searchString, compareString).RawScore;
Assert.True(scoreResult == 0);
}
@ -129,13 +129,12 @@ namespace Wox.Test
.ToList();
var results = new List<Result>();
foreach (var str in searchStrings)
{
results.Add(new Result
{
Title = str,
Score = StringMatcher.FuzzySearch(searchTerm, str).Score
Score = StringMatcher.FuzzySearch(searchTerm, str).RawScore
});
}
@ -168,8 +167,11 @@ namespace Wox.Test
[TestCase("ccs", "Candy Crush Saga from King", (int)StringMatcher.SearchPrecisionScore.Low, true)]
[TestCase("cand", "Candy Crush Saga from King", (int)StringMatcher.SearchPrecisionScore.Regular, true)]
[TestCase("cand", "Help cure hope raise on mind entity Chrome", (int)StringMatcher.SearchPrecisionScore.Regular, false)]
public void WhenGivenDesiredPrecisionThenShouldReturnAllResultsGreaterOrEqual(string queryString, string compareString,
int expectedPrecisionScore, bool expectedPrecisionResult)
public void WhenGivenDesiredPrecisionThenShouldReturnAllResultsGreaterOrEqual(
string queryString,
string compareString,
int expectedPrecisionScore,
bool expectedPrecisionResult)
{
var expectedPrecisionString = (StringMatcher.SearchPrecisionScore)expectedPrecisionScore;
StringMatcher.UserSettingSearchPrecision = expectedPrecisionString.ToString();

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Timers;
@ -56,6 +56,7 @@ namespace Wox
Alphabet.Initialize(_settings);
StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
StringMatcher.ShouldUsePinyin = _settings.ShouldUsePinyin;
PluginManager.LoadPlugins(_settings.PluginSettings);
_mainVM = new MainViewModel(_settings);

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
namespace Wox.Converters
{
public class HighlightTextConverter : IMultiValueConverter
{
public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
{
var text = value[0] as string;
var highlightData = value[1] as List<int>;
var textBlock = new Span();
if (highlightData == null || !highlightData.Any())
{
// No highlight data, just return the text
return new Run(text);
}
for (var i = 0; i < text.Length; i++)
{
var currentCharacter = text.Substring(i, 1);
if (this.ShouldHighlight(highlightData, i))
{
textBlock.Inlines.Add(new Bold(new Run(currentCharacter)));
}
else
{
textBlock.Inlines.Add(new Run(currentCharacter));
}
}
return textBlock;
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
return new[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue };
}
private bool ShouldHighlight(List<int> highlightData, int index)
{
return highlightData.Contains(index);
}
}
}

View File

@ -36,6 +36,7 @@
<KeyBinding Key="P" Modifiers="Ctrl" Command="{Binding SelectPrevItemCommand}"></KeyBinding>
<KeyBinding Key="K" Modifiers="Ctrl" Command="{Binding SelectPrevItemCommand}"></KeyBinding>
<KeyBinding Key="U" Modifiers="Ctrl" Command="{Binding SelectPrevPageCommand}"></KeyBinding>
<KeyBinding Key="Home" Modifiers="Alt" Command="{Binding SelectFirstResultCommand}"></KeyBinding>
<KeyBinding Key="O" Modifiers="Ctrl" Command="{Binding LoadContextMenuCommand}"></KeyBinding>
<KeyBinding Key="H" Modifiers="Ctrl" Command="{Binding LoadHistoryCommand}"></KeyBinding>
<KeyBinding Key="Enter" Modifiers="Shift" Command="{Binding LoadContextMenuCommand}"></KeyBinding>

View File

@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:Wox.ViewModel"
xmlns:converter="clr-namespace:Wox.Converters"
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100"
d:DataContext="{d:DesignInstance vm:ResultsViewModel}"
MaxHeight="{Binding MaxHeight}"
@ -30,6 +31,9 @@
<Button.Content>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"
Cursor="Hand" UseLayoutRounding="False">
<Grid.Resources>
<converter:HighlightTextConverter x:Key="HighlightTextConverter"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32" />
<ColumnDefinition />
@ -44,9 +48,23 @@
</Grid.RowDefinitions>
<TextBlock Style="{DynamicResource ItemTitleStyle}" DockPanel.Dock="Left"
VerticalAlignment="Center" ToolTip="{Binding Result.Title}" x:Name="Title"
Text="{Binding Result.Title}" />
Text="{Binding Result.Title}">
<vm:ResultsViewModel.FormattedText>
<MultiBinding Converter="{StaticResource HighlightTextConverter}">
<Binding Path="Result.Title" />
<Binding Path="Result.TitleHighlightData" />
</MultiBinding>
</vm:ResultsViewModel.FormattedText>
</TextBlock>
<TextBlock Style="{DynamicResource ItemSubTitleStyle}" ToolTip="{Binding Result.SubTitle}"
Grid.Row="1" x:Name="SubTitle" Text="{Binding Result.SubTitle}" />
Grid.Row="1" x:Name="SubTitle" Text="{Binding Result.SubTitle}">
<vm:ResultsViewModel.FormattedText>
<MultiBinding Converter="{StaticResource HighlightTextConverter}">
<Binding Path="Result.SubTitle" />
<Binding Path="Result.SubTitleHighlightData" />
</MultiBinding>
</vm:ResultsViewModel.FormattedText>
</TextBlock>
</Grid>
</Grid>
</Button.Content>

View File

@ -1,47 +1,50 @@
using System.Collections.Generic;
using System.Linq;
using Wox.Infrastructure.Storage;
using Newtonsoft.Json;
using Wox.Plugin;
namespace Wox.Storage
{
// todo this class is not thread safe.... but used from multiple threads.
public class TopMostRecord
{
public Dictionary<string, Record> records = new Dictionary<string, Record>();
[JsonProperty]
private Dictionary<string, Record> records = new Dictionary<string, Record>();
internal bool IsTopMost(Result result)
{
if (records.Count == 0)
{
return false;
}
// since this dictionary should be very small (or empty) going over it should be pretty fast.
return records.Any(o => o.Value.Title == result.Title
&& o.Value.SubTitle == result.SubTitle
&& o.Value.PluginID == result.PluginID
&& o.Key == result.OriginQuery.RawQuery);
&& o.Value.SubTitle == result.SubTitle
&& o.Value.PluginID == result.PluginID
&& o.Key == result.OriginQuery.RawQuery);
}
internal void Remove(Result result)
{
if (records.ContainsKey(result.OriginQuery.RawQuery))
{
records.Remove(result.OriginQuery.RawQuery);
}
records.Remove(result.OriginQuery.RawQuery);
}
internal void AddOrUpdate(Result result)
{
if (records.ContainsKey(result.OriginQuery.RawQuery))
var record = new Record
{
records[result.OriginQuery.RawQuery].Title = result.Title;
records[result.OriginQuery.RawQuery].SubTitle = result.SubTitle;
records[result.OriginQuery.RawQuery].PluginID = result.PluginID;
}
else
{
records.Add(result.OriginQuery.RawQuery, new Record
{
PluginID = result.PluginID,
Title = result.Title,
SubTitle = result.SubTitle
});
}
PluginID = result.PluginID,
Title = result.Title,
SubTitle = result.SubTitle
};
records[result.OriginQuery.RawQuery] = record;
}
public void Load(Dictionary<string, Record> dictionary)
{
records = dictionary;
}
}

View File

@ -12,21 +12,23 @@ namespace Wox.Storage
public void Add(Result result)
{
if (records.ContainsKey(result.ToString()))
var key = result.ToString();
if (records.TryGetValue(key, out int value))
{
records[result.ToString()] += 1;
records[key] = value + 1;
}
else
{
records.Add(result.ToString(), 1);
records.Add(key, 1);
}
}
public int GetSelectedCount(Result result)
{
if (records.ContainsKey(result.ToString()))
if (records.TryGetValue(result.ToString(), out int value))
{
return records[result.ToString()];
return value;
}
return 0;
}

View File

@ -126,6 +126,8 @@ namespace Wox.ViewModel
SelectedResults.SelectPrevPage();
});
SelectFirstResultCommand = new RelayCommand(_ => SelectedResults.SelectFirstResult());
StartHelpCommand = new RelayCommand(_ =>
{
Process.Start("http://doc.wox.one/");
@ -268,6 +270,7 @@ namespace Wox.ViewModel
public ICommand SelectPrevItemCommand { get; set; }
public ICommand SelectNextPageCommand { get; set; }
public ICommand SelectPrevPageCommand { get; set; }
public ICommand SelectFirstResultCommand { get; set; }
public ICommand StartHelpCommand { get; set; }
public ICommand LoadContextMenuCommand { get; set; }
public ICommand LoadHistoryCommand { get; set; }
@ -415,11 +418,15 @@ namespace Wox.ViewModel
var config = _settings.PluginSettings.Plugins[plugin.Metadata.ID];
if (!config.Disabled)
{
var results = PluginManager.QueryForPlugin(plugin, query);
UpdateResultView(results, plugin.Metadata, query);
}
});
// this should happen once after all queries are done so progress bar should continue
// until the end of all querying
_queryHasReturn = true;
ProgressBarVisibility = Visibility.Hidden;
}, _updateToken);
}
}
@ -628,9 +635,6 @@ namespace Wox.ViewModel
/// </summary>
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery)
{
_queryHasReturn = true;
ProgressBarVisibility = Visibility.Hidden;
foreach (var result in list)
{
if (_topMostRecord.IsTopMost(result))

View File

@ -5,7 +5,6 @@ namespace Wox.ViewModel
{
public class RelayCommand : ICommand
{
private Action<object> _action;
public RelayCommand(Action<object> action)

View File

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
@ -107,6 +109,11 @@ namespace Wox.ViewModel
SelectedIndex = NewIndex(SelectedIndex - MaxResults);
}
public void SelectFirstResult()
{
SelectedIndex = NewIndex(0);
}
public void Clear()
{
Results.Clear();
@ -155,7 +162,6 @@ namespace Wox.ViewModel
// Find the same results in A (old results) and B (new newResults)
var sameResults = oldResults
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result)))
.Select(t1 => t1)
.ToList();
// remove result of relative complement of B in A
@ -193,8 +199,37 @@ namespace Wox.ViewModel
return results;
}
#endregion
#region FormattedText Dependency Property
public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
"FormattedText",
typeof(Inline),
typeof(ResultsViewModel),
new PropertyMetadata(null, FormattedTextPropertyChanged));
public static void SetFormattedText(DependencyObject textBlock, IList<int> value)
{
textBlock.SetValue(FormattedTextProperty, value);
}
public static Inline GetFormattedText(DependencyObject textBlock)
{
return (Inline)textBlock.GetValue(FormattedTextProperty);
}
private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBlock = d as TextBlock;
if (textBlock == null) return;
var inline = (Inline)e.NewValue;
textBlock.Inlines.Clear();
if (inline == null) return;
textBlock.Inlines.Add(inline);
}
#endregion
public class ResultCollection : ObservableCollection<ResultViewModel>

View File

@ -158,6 +158,7 @@
<Compile Include="..\SolutionAssemblyInfo.cs">
<Link>Properties\SolutionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Converters\HighlightTextConverter.cs" />
<Compile Include="Helper\SingletonWindowOpener.cs" />
<Compile Include="PublicAPIInstance.cs" />
<Compile Include="ReportWindow.xaml.cs" />