mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Files added to search results after folders
I think it is time we release version 2
This commit is contained in:
@@ -8,223 +8,209 @@ using System.Windows.Forms;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox.Plugin.SystemPlugins.FileSystem
|
||||
{
|
||||
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider
|
||||
{
|
||||
private PluginInitContext context;
|
||||
private static List<string> driverNames = null;
|
||||
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
||||
namespace Wox.Plugin.SystemPlugins.FileSystem {
|
||||
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider {
|
||||
private PluginInitContext context;
|
||||
private static List<string> driverNames = null;
|
||||
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
||||
|
||||
protected override List<Result> QueryInternal(Query query)
|
||||
{
|
||||
//TODO: Consider always clearing the cache
|
||||
List<Result> results = new List<Result>();
|
||||
if (string.IsNullOrEmpty(query.RawQuery))
|
||||
{
|
||||
// clear the cache
|
||||
if (parentDirectories.Count > 0)
|
||||
parentDirectories.Clear();
|
||||
protected override List<Result> QueryInternal(Query query) {
|
||||
//TODO: Consider always clearing the cache
|
||||
List<Result> results = new List<Result>();
|
||||
if (string.IsNullOrEmpty(query.RawQuery)) {
|
||||
// clear the cache
|
||||
if (parentDirectories.Count > 0)
|
||||
parentDirectories.Clear();
|
||||
|
||||
return results;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
InitialDriverList();
|
||||
InitialDriverList();
|
||||
|
||||
var input = query.RawQuery.ToLower();
|
||||
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
||||
var input = query.RawQuery.ToLower();
|
||||
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
||||
|
||||
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x =>
|
||||
x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
||||
var currentPath = link != null ? link.Path : null;
|
||||
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x =>
|
||||
x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
||||
var currentPath = link != null ? link.Path : null;
|
||||
|
||||
foreach (var item in UserSettingStorage.Instance.FolderLinks)
|
||||
{
|
||||
var Name = item.Nickname;
|
||||
foreach (var item in UserSettingStorage.Instance.FolderLinks) {
|
||||
var Name = item.Nickname;
|
||||
|
||||
if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length)
|
||||
{
|
||||
Result result = new Result
|
||||
{
|
||||
Title = Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
context.ChangeQuery(item.Nickname);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length) {
|
||||
Result result = new Result {
|
||||
Title = Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) => {
|
||||
context.ChangeQuery(item.Nickname);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
if (currentPath == null)
|
||||
{
|
||||
if (!driverNames.Any(input.StartsWith))
|
||||
return results;
|
||||
if (currentPath == null) {
|
||||
if (!driverNames.Any(input.StartsWith))
|
||||
return results;
|
||||
|
||||
currentPath = input;
|
||||
}
|
||||
else
|
||||
currentPath += input.Remove(0, inputName.Length);
|
||||
currentPath = input;
|
||||
}
|
||||
else
|
||||
currentPath += input.Remove(0, inputName.Length);
|
||||
|
||||
if (Directory.Exists(currentPath))
|
||||
{
|
||||
// show all child directory
|
||||
if (input.EndsWith("\\") || input.EndsWith("/"))
|
||||
{
|
||||
var dirInfo = new DirectoryInfo(currentPath);
|
||||
var dirs = dirInfo.GetDirectories();
|
||||
if (Directory.Exists(currentPath)) {
|
||||
// show all child directory
|
||||
if (input.EndsWith("\\") || input.EndsWith("/")) {
|
||||
var dirInfo = new DirectoryInfo(currentPath);
|
||||
var dirs = dirInfo.GetDirectories();
|
||||
|
||||
var parentDirKey = input.TrimEnd('\\', '/');
|
||||
if (!parentDirectories.ContainsKey(parentDirKey))
|
||||
parentDirectories.Add(parentDirKey, dirs);
|
||||
var parentDirKey = input.TrimEnd('\\', '/');
|
||||
if (!parentDirectories.ContainsKey(parentDirKey))
|
||||
parentDirectories.Add(parentDirKey, dirs);
|
||||
|
||||
foreach (var dir in dirs)
|
||||
{
|
||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
||||
continue;
|
||||
foreach (var dir in dirs) {
|
||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
||||
continue;
|
||||
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result
|
||||
{
|
||||
Title = dir.Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
context.ChangeQuery(dirPath);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result {
|
||||
Title = dir.Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) => {
|
||||
context.ChangeQuery(dirPath);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
|
||||
if (results.Count == 0)
|
||||
{
|
||||
Result result = new Result
|
||||
{
|
||||
Title = "Open this directory",
|
||||
SubTitle = "No files in this directory",
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
Process.Start(currentPath);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
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);
|
||||
}
|
||||
if (results.Count == 0) {
|
||||
Result result = new Result {
|
||||
Title = "Open this directory",
|
||||
SubTitle = "No files in this directory",
|
||||
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
|
||||
string parentDir = null;
|
||||
try
|
||||
{
|
||||
parentDir = Path.GetDirectoryName(input);
|
||||
}
|
||||
catch {}
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result {
|
||||
Title = global::System.IO.Path.GetFileNameWithoutExtension(dirPath),
|
||||
IcoPath = dirPath,
|
||||
Action = (c) => {
|
||||
try {
|
||||
Process.Start(dirPath);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
MessageBox.Show(ex.Message, "Could not start " + dir.Name);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0)
|
||||
{
|
||||
parentDir = parentDir.TrimEnd('\\', '/');
|
||||
if (parentDirectories.ContainsKey(parentDir))
|
||||
{
|
||||
|
||||
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;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
var matchResult = fuzzy.Evaluate(dir.Name);
|
||||
if (!matchResult.Success)
|
||||
continue;
|
||||
// change to search in current directory
|
||||
string parentDir = null;
|
||||
try {
|
||||
parentDir = Path.GetDirectoryName(input);
|
||||
}
|
||||
catch { }
|
||||
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result
|
||||
{
|
||||
Title = dir.Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Score = matchResult.Score,
|
||||
Action = (c) =>
|
||||
{
|
||||
context.ChangeQuery(dirPath);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0) {
|
||||
parentDir = parentDir.TrimEnd('\\', '/');
|
||||
if (parentDirectories.ContainsKey(parentDir)) {
|
||||
|
||||
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()
|
||||
{
|
||||
if (driverNames == null)
|
||||
{
|
||||
driverNames = new List<string>();
|
||||
var allDrives = DriveInfo.GetDrives();
|
||||
foreach (var driver in allDrives)
|
||||
{
|
||||
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
|
||||
}
|
||||
}
|
||||
}
|
||||
var matchResult = fuzzy.Evaluate(dir.Name);
|
||||
if (!matchResult.Success)
|
||||
continue;
|
||||
|
||||
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"; }
|
||||
}
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result {
|
||||
Title = dir.Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Score = matchResult.Score,
|
||||
Action = (c) => {
|
||||
context.ChangeQuery(dirPath);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public System.Windows.Controls.Control CreateSettingPanel()
|
||||
{
|
||||
return new FileSystemSettings();
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public override string Description
|
||||
{
|
||||
get { return base.Description; }
|
||||
}
|
||||
}
|
||||
private void InitialDriverList() {
|
||||
if (driverNames == null) {
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user