Files added to search results after folders

I think it is time we release version 2
This commit is contained in:
Aaron Campf
2014-05-08 17:38:51 -07:00
parent 7c2e2a01c2
commit d751805254
3 changed files with 208 additions and 205 deletions

2
.gitignore vendored
View File

@@ -114,3 +114,5 @@ UpgradeLog*.XML
*.DotSettings *.DotSettings
/Python.Runtime.dll /Python.Runtime.dll
Wox/Images/websearch/Thumbs.db
Wox/Images/Thumbs.db

View File

@@ -8,223 +8,209 @@ using System.Windows.Forms;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins.FileSystem namespace Wox.Plugin.SystemPlugins.FileSystem {
{ public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider {
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider private PluginInitContext context;
{ private static List<string> driverNames = null;
private PluginInitContext context; private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
private static List<string> driverNames = null;
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
protected override List<Result> QueryInternal(Query query) protected override List<Result> QueryInternal(Query query) {
{ //TODO: Consider always clearing the cache
//TODO: Consider always clearing the cache List<Result> results = new List<Result>();
List<Result> results = new List<Result>(); if (string.IsNullOrEmpty(query.RawQuery)) {
if (string.IsNullOrEmpty(query.RawQuery)) // clear the cache
{ if (parentDirectories.Count > 0)
// clear the cache parentDirectories.Clear();
if (parentDirectories.Count > 0)
parentDirectories.Clear();
return results; return results;
} }
InitialDriverList(); InitialDriverList();
var input = query.RawQuery.ToLower(); var input = query.RawQuery.ToLower();
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower(); var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x => var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x =>
x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase)); x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
var currentPath = link != null ? link.Path : null; var currentPath = link != null ? link.Path : null;
foreach (var item in UserSettingStorage.Instance.FolderLinks) foreach (var item in UserSettingStorage.Instance.FolderLinks) {
{ var Name = item.Nickname;
var Name = item.Nickname;
if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length) if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length) {
{ Result result = new Result {
Result result = new Result Title = Name,
{ IcoPath = "Images/folder.png",
Title = Name, Action = (c) => {
IcoPath = "Images/folder.png", context.ChangeQuery(item.Nickname);
Action = (c) => return false;
{ }
context.ChangeQuery(item.Nickname); };
return false;
}
};
results.Add(result); results.Add(result);
} }
} }
if (currentPath == null) if (currentPath == null) {
{ if (!driverNames.Any(input.StartsWith))
if (!driverNames.Any(input.StartsWith)) return results;
return results;
currentPath = input; currentPath = input;
} }
else else
currentPath += input.Remove(0, inputName.Length); currentPath += input.Remove(0, inputName.Length);
if (Directory.Exists(currentPath)) if (Directory.Exists(currentPath)) {
{ // show all child directory
// show all child directory if (input.EndsWith("\\") || input.EndsWith("/")) {
if (input.EndsWith("\\") || input.EndsWith("/")) var dirInfo = new DirectoryInfo(currentPath);
{ var dirs = dirInfo.GetDirectories();
var dirInfo = new DirectoryInfo(currentPath);
var dirs = dirInfo.GetDirectories();
var parentDirKey = input.TrimEnd('\\', '/'); var parentDirKey = input.TrimEnd('\\', '/');
if (!parentDirectories.ContainsKey(parentDirKey)) if (!parentDirectories.ContainsKey(parentDirKey))
parentDirectories.Add(parentDirKey, dirs); parentDirectories.Add(parentDirKey, dirs);
foreach (var dir in dirs) foreach (var dir in dirs) {
{ if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
continue;
var dirPath = dir.FullName; var dirPath = dir.FullName;
Result result = new Result Result result = new Result {
{ Title = dir.Name,
Title = dir.Name, IcoPath = "Images/folder.png",
IcoPath = "Images/folder.png", Action = (c) => {
Action = (c) => context.ChangeQuery(dirPath);
{ return false;
context.ChangeQuery(dirPath); }
return false; };
} results.Add(result);
}; }
results.Add(result);
}
if (results.Count == 0) if (results.Count == 0) {
{ Result result = new Result {
Result result = new Result Title = "Open this directory",
{ SubTitle = "No files in this directory",
Title = "Open this directory", IcoPath = "Images/folder.png",
SubTitle = "No files in this directory", Action = (c) => {
IcoPath = "Images/folder.png", Process.Start(currentPath);
Action = (c) => return true;
{ }
Process.Start(currentPath); };
return true; results.Add(result);
} }
};
results.Add(result);
}
}
else
{
Result result = new Result
{
Title = "Open this directory",
SubTitle = string.Format("path: {0}", currentPath),
Score = 50,
IcoPath = "Images/folder.png",
Action = (c) =>
{
Process.Start(currentPath);
return true;
}
};
results.Add(result);
}
} foreach (var dir in dirInfo.GetFiles()) {
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
continue;
// change to search in current directory var dirPath = dir.FullName;
string parentDir = null; Result result = new Result {
try Title = global::System.IO.Path.GetFileNameWithoutExtension(dirPath),
{ IcoPath = dirPath,
parentDir = Path.GetDirectoryName(input); Action = (c) => {
} try {
catch {} Process.Start(dirPath);
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Could not start " + dir.Name);
}
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0) return true;
{ }
parentDir = parentDir.TrimEnd('\\', '/'); };
if (parentDirectories.ContainsKey(parentDir)) results.Add(result);
{ }
}
var dirs = parentDirectories[parentDir]; else {
var queryFileName = Path.GetFileName(currentPath).ToLower(); Result result = new Result {
var fuzzy = FuzzyMatcher.Create(queryFileName); Title = "Open this directory",
foreach (var dir in dirs) SubTitle = string.Format("path: {0}", currentPath),
{ Score = 50,
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) IcoPath = "Images/folder.png",
continue; Action = (c) => {
Process.Start(currentPath);
return true;
}
};
results.Add(result);
}
}
var matchResult = fuzzy.Evaluate(dir.Name); // change to search in current directory
if (!matchResult.Success) string parentDir = null;
continue; try {
parentDir = Path.GetDirectoryName(input);
}
catch { }
var dirPath = dir.FullName; if (!string.IsNullOrEmpty(parentDir) && results.Count == 0) {
Result result = new Result parentDir = parentDir.TrimEnd('\\', '/');
{ if (parentDirectories.ContainsKey(parentDir)) {
Title = dir.Name,
IcoPath = "Images/folder.png",
Score = matchResult.Score,
Action = (c) =>
{
context.ChangeQuery(dirPath);
return false;
}
};
results.Add(result);
}
}
}
return results; var dirs = parentDirectories[parentDir];
} var queryFileName = Path.GetFileName(currentPath).ToLower();
var fuzzy = FuzzyMatcher.Create(queryFileName);
foreach (var dir in dirs) {
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
continue;
private void InitialDriverList() var matchResult = fuzzy.Evaluate(dir.Name);
{ if (!matchResult.Success)
if (driverNames == null) continue;
{
driverNames = new List<string>();
var allDrives = DriveInfo.GetDrives();
foreach (var driver in allDrives)
{
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
}
}
}
protected override void InitInternal(PluginInitContext context) var dirPath = dir.FullName;
{ Result result = new Result {
this.context = context; Title = dir.Name,
IcoPath = "Images/folder.png",
if (UserSettingStorage.Instance.FolderLinks == null) Score = matchResult.Score,
{ Action = (c) => {
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>(); context.ChangeQuery(dirPath);
UserSettingStorage.Instance.Save(); return false;
} }
} };
results.Add(result);
public override string Name }
{ }
get { return "File System"; } }
}
public override string IcoPath
{
get { return @"Images\folder.png"; }
}
public System.Windows.Controls.Control CreateSettingPanel() return results;
{ }
return new FileSystemSettings();
}
public override string Description private void InitialDriverList() {
{ if (driverNames == null) {
get { return base.Description; } driverNames = new List<string>();
} var allDrives = DriveInfo.GetDrives();
} foreach (var driver in allDrives) {
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
}
}
}
protected override void InitInternal(PluginInitContext context) {
this.context = context;
if (UserSettingStorage.Instance.FolderLinks == null) {
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
UserSettingStorage.Instance.Save();
}
}
public override string Name {
get { return "File System"; }
}
public override string IcoPath {
get { return @"Images\folder.png"; }
}
public System.Windows.Controls.Control CreateSettingPanel() {
return new FileSystemSettings();
}
public override string Description {
get { return base.Description; }
}
}
} }

View File

@@ -361,6 +361,26 @@ namespace Wox {
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e) { private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e) {
//when alt is pressed, the real key should be e.SystemKey //when alt is pressed, the real key should be e.SystemKey
Action Shift_GoBack = () => {
if (e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)) {
if (tbQuery.Text.EndsWith("\\")) {
tbQuery.Text = tbQuery.Text.Remove(tbQuery.Text.Length - 1);
}
if (tbQuery.Text.Contains("\\")) {
var index = tbQuery.Text.LastIndexOf("\\");
tbQuery.Text = tbQuery.Text.Remove(index) + "\\";
}
else {
tbQuery.Text = "";
return;
}
}
tbQuery.CaretIndex = int.MaxValue;
};
Key key = (e.Key == Key.System ? e.SystemKey : e.Key); Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
switch (key) { switch (key) {
case Key.Escape: case Key.Escape:
@@ -397,22 +417,17 @@ namespace Wox {
break; break;
case Key.Enter: case Key.Enter:
Shift_GoBack();
AcceptSelect(resultCtrl.AcceptSelect()); AcceptSelect(resultCtrl.AcceptSelect());
e.Handled = true; e.Handled = true;
break; break;
case Key.Tab: case Key.Tab:
var SelectedItem = resultCtrl.lbResults.SelectedItem as Wox.Plugin.Result; Shift_GoBack();
if (SelectedItem != null) { AcceptSelect(resultCtrl.AcceptSelect());
if (tbQuery.Text.Contains("\\")) { if (!tbQuery.Text.EndsWith("\\")) tbQuery.Text += "\\";
var index = tbQuery.Text.LastIndexOf("\\"); AcceptSelect(resultCtrl.AcceptSelect());
tbQuery.Text = tbQuery.Text.Remove(index) + "\\"; tbQuery.CaretIndex = int.MaxValue;
}
tbQuery.Text += SelectedItem.Title;
tbQuery.CaretIndex = int.MaxValue;
}
e.Handled = true; e.Handled = true;
break; break;
} }
@@ -427,7 +442,7 @@ namespace Wox {
if (hideWindow) { if (hideWindow) {
HideWox(); HideWox();
} }
UserSelectedRecordStorage.Instance.Add(result); //UserSelectedRecordStorage.Instance.Add(result);
} }
} }
} }