mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge branch 'dev' into add_shell_plugin_runasadmin
This commit is contained in:
commit
1fd31d83bf
8 changed files with 188 additions and 157 deletions
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
|
@ -21,7 +21,6 @@ namespace Wox.Plugin.Folder
|
|||
{
|
||||
_storage = new PluginJsonStorage<Settings>();
|
||||
_settings = _storage.Load();
|
||||
InitialDriverList();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
|
|
@ -37,54 +36,20 @@ namespace Wox.Plugin.Folder
|
|||
public void Init(PluginInitContext context)
|
||||
{
|
||||
_context = 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))
|
||||
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 +57,53 @@ namespace Wox.Plugin.Folder
|
|||
|
||||
return results;
|
||||
}
|
||||
|
||||
private Result CreateFolderResult(string title, string path, string queryActionKeyword)
|
||||
{
|
||||
return new Result
|
||||
{
|
||||
Title = title,
|
||||
IcoPath = path,
|
||||
SubTitle = "Ctrl + Enter to open the directory",
|
||||
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(queryActionKeyword)? changeTo : queryActionKeyword + " " + changeTo);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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.ActionKeyword))
|
||||
.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,21 +111,25 @@ 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;
|
||||
|
|
@ -128,94 +138,100 @@ namespace Wox.Plugin.Folder
|
|||
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 directoryInfo = new DirectoryInfo(search);
|
||||
|
||||
var searchOption= SearchOption.TopDirectoryOnly;
|
||||
incompleteName += "*";
|
||||
|
||||
if (incompleteName.StartsWith(">")) // give the ability to search all folder when starting with >
|
||||
{
|
||||
searchOption = SearchOption.AllDirectories;
|
||||
incompleteName = incompleteName.Substring(1);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// search folder and add results
|
||||
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.ActionKeyword)
|
||||
: CreateFileResult(fileSystemInfo.FullName);
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
};
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -108,7 +108,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; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
@ -419,6 +422,11 @@ namespace Wox.ViewModel
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -627,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))
|
||||
|
|
|
|||
|
|
@ -107,6 +107,11 @@ namespace Wox.ViewModel
|
|||
SelectedIndex = NewIndex(SelectedIndex - MaxResults);
|
||||
}
|
||||
|
||||
public void SelectFirstResult()
|
||||
{
|
||||
SelectedIndex = NewIndex(0);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Results.Clear();
|
||||
|
|
@ -155,7 +160,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
|
||||
|
|
|
|||
Loading…
Reference in a new issue