mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Make Explorer plugin completely async
This commit is contained in:
parent
4cb4aa88ef
commit
d7805d7a8c
6 changed files with 96 additions and 78 deletions
|
|
@ -151,7 +151,7 @@ namespace Flow.Launcher.Test.Plugins
|
|||
var searchManager = new SearchManager(new Settings(), new PluginInitContext());
|
||||
|
||||
// When
|
||||
var results = searchManager.TopLevelDirectorySearchBehaviour(
|
||||
var results = searchManager.TopLevelDirectorySearchBehaviourAsync(
|
||||
MethodWindowsIndexSearchReturnsZeroResults,
|
||||
MethodDirectoryInfoClassSearchReturnsTwoResults,
|
||||
false,
|
||||
|
|
@ -171,7 +171,7 @@ namespace Flow.Launcher.Test.Plugins
|
|||
var searchManager = new SearchManager(new Settings(), new PluginInitContext());
|
||||
|
||||
// When
|
||||
var results = searchManager.TopLevelDirectorySearchBehaviour(
|
||||
var results = searchManager.TopLevelDirectorySearchBehaviourAsync(
|
||||
MethodWindowsIndexSearchReturnsZeroResults,
|
||||
MethodDirectoryInfoClassSearchReturnsTwoResults,
|
||||
true,
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@ using Flow.Launcher.Plugin.Explorer.Search;
|
|||
using Flow.Launcher.Plugin.Explorer.ViewModels;
|
||||
using Flow.Launcher.Plugin.Explorer.Views;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Flow.Launcher.Plugin.Explorer
|
||||
{
|
||||
public class Main : ISettingProvider, IPlugin, ISavable, IContextMenu, IPluginI18n
|
||||
public class Main : ISettingProvider, IAsyncPlugin, ISavable, IContextMenu, IPluginI18n
|
||||
{
|
||||
internal PluginInitContext Context { get; set; }
|
||||
|
||||
|
|
@ -17,17 +19,21 @@ namespace Flow.Launcher.Plugin.Explorer
|
|||
|
||||
private IContextMenu contextMenu;
|
||||
|
||||
private SearchManager searchManager;
|
||||
|
||||
public Control CreateSettingPanel()
|
||||
{
|
||||
return new ExplorerSettings(viewModel);
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
public async Task InitAsync(PluginInitContext context)
|
||||
{
|
||||
Context = context;
|
||||
viewModel = new SettingsViewModel(context);
|
||||
await viewModel.LoadStorage();
|
||||
Settings = viewModel.Settings;
|
||||
contextMenu = new ContextMenu(Context, Settings);
|
||||
searchManager = new SearchManager(Settings, Context);
|
||||
}
|
||||
|
||||
public List<Result> LoadContextMenus(Result selectedResult)
|
||||
|
|
@ -35,9 +41,9 @@ namespace Flow.Launcher.Plugin.Explorer
|
|||
return contextMenu.LoadContextMenus(selectedResult);
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
|
||||
{
|
||||
return new SearchManager(Settings, Context).Search(query);
|
||||
return await searchManager.SearchAsync(query, token);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo
|
||||
{
|
||||
|
|
@ -22,7 +23,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo
|
|||
|
||||
if (search.LastIndexOf(Constants.AllFilesFolderSearchWildcard) > search.LastIndexOf(Constants.DirectorySeperator))
|
||||
return DirectorySearch(SearchOption.AllDirectories, query, search, criteria);
|
||||
|
||||
|
||||
return DirectorySearch(SearchOption.TopDirectoryOnly, query, search, criteria);
|
||||
}
|
||||
|
||||
|
|
@ -57,9 +58,8 @@ namespace Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo
|
|||
try
|
||||
{
|
||||
var directoryInfo = new System.IO.DirectoryInfo(path);
|
||||
var fileSystemInfos = directoryInfo.GetFileSystemInfos(searchCriteria, searchOption);
|
||||
|
||||
foreach (var fileSystemInfo in fileSystemInfos)
|
||||
foreach (var fileSystemInfo in directoryInfo.EnumerateFileSystemInfos(searchCriteria, searchOption))
|
||||
{
|
||||
if ((fileSystemInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ using Flow.Launcher.Plugin.SharedCommands;
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Flow.Launcher.Plugin.Explorer.Search
|
||||
{
|
||||
|
|
@ -28,20 +30,20 @@ namespace Flow.Launcher.Plugin.Explorer.Search
|
|||
this.settings = settings;
|
||||
}
|
||||
|
||||
internal List<Result> Search(Query query)
|
||||
internal async Task<List<Result>> SearchAsync(Query query, CancellationToken token)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
|
||||
var querySearch = query.Search;
|
||||
|
||||
if (IsFileContentSearch(query.ActionKeyword))
|
||||
return WindowsIndexFileContentSearch(query, querySearch);
|
||||
return await WindowsIndexFileContentSearchAsync(query, querySearch, token).ConfigureAwait(false);
|
||||
|
||||
// This allows the user to type the assigned action keyword and only see the list of quick folder links
|
||||
if (settings.QuickFolderAccessLinks.Count > 0
|
||||
&& query.ActionKeyword == settings.SearchActionKeyword
|
||||
&& string.IsNullOrEmpty(query.Search))
|
||||
return quickFolderAccess.FolderListAll(query, settings.QuickFolderAccessLinks, context);
|
||||
return quickFolderAccess.FolderListAll(query, settings.QuickFolderAccessLinks, context);
|
||||
|
||||
var quickFolderLinks = quickFolderAccess.FolderListMatched(query, settings.QuickFolderAccessLinks, context);
|
||||
|
||||
|
|
@ -54,11 +56,11 @@ namespace Flow.Launcher.Plugin.Explorer.Search
|
|||
return EnvironmentVariables.GetEnvironmentStringPathSuggestions(querySearch, query, context);
|
||||
|
||||
// Query is a location path with a full environment variable, eg. %appdata%\somefolder\
|
||||
var isEnvironmentVariablePath = querySearch.Substring(1).Contains("%\\");
|
||||
var isEnvironmentVariablePath = querySearch[1..].Contains("%\\");
|
||||
|
||||
if (!FilesFolders.IsLocationPathString(querySearch) && !isEnvironmentVariablePath)
|
||||
{
|
||||
results.AddRange(WindowsIndexFilesAndFoldersSearch(query, querySearch));
|
||||
results.AddRange(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
|
||||
|
||||
return results;
|
||||
}
|
||||
|
|
@ -72,29 +74,34 @@ namespace Flow.Launcher.Plugin.Explorer.Search
|
|||
return results;
|
||||
|
||||
var useIndexSearch = UseWindowsIndexForDirectorySearch(locationPath);
|
||||
|
||||
|
||||
results.Add(resultManager.CreateOpenCurrentFolderResult(locationPath, useIndexSearch));
|
||||
|
||||
results.AddRange(TopLevelDirectorySearchBehaviour(WindowsIndexTopLevelFolderSearch,
|
||||
if (token.IsCancellationRequested)
|
||||
return null;
|
||||
|
||||
results.AddRange(await TopLevelDirectorySearchBehaviourAsync(WindowsIndexTopLevelFolderSearchAsync,
|
||||
DirectoryInfoClassSearch,
|
||||
useIndexSearch,
|
||||
query,
|
||||
locationPath));
|
||||
locationPath,
|
||||
token).ConfigureAwait(false));
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<Result> WindowsIndexFileContentSearch(Query query, string querySearchString)
|
||||
private async Task<List<Result>> WindowsIndexFileContentSearchAsync(Query query, string querySearchString, CancellationToken token)
|
||||
{
|
||||
var queryConstructor = new QueryConstructor(settings);
|
||||
|
||||
if (string.IsNullOrEmpty(querySearchString))
|
||||
return new List<Result>();
|
||||
|
||||
return indexSearch.WindowsIndexSearch(querySearchString,
|
||||
return await indexSearch.WindowsIndexSearchAsync(querySearchString,
|
||||
queryConstructor.CreateQueryHelper().ConnectionString,
|
||||
queryConstructor.QueryForFileContentSearch,
|
||||
query);
|
||||
query,
|
||||
token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public bool IsFileContentSearch(string actionKeyword)
|
||||
|
|
@ -109,37 +116,40 @@ namespace Flow.Launcher.Plugin.Explorer.Search
|
|||
return directoryInfoSearch.TopLevelDirectorySearch(query, querySearch);
|
||||
}
|
||||
|
||||
public List<Result> TopLevelDirectorySearchBehaviour(
|
||||
Func<Query, string, List<Result>> windowsIndexSearch,
|
||||
public async Task<List<Result>> TopLevelDirectorySearchBehaviourAsync(
|
||||
Func<Query, string, CancellationToken, Task<List<Result>>> windowsIndexSearch,
|
||||
Func<Query, string, List<Result>> directoryInfoClassSearch,
|
||||
bool useIndexSearch,
|
||||
Query query,
|
||||
string querySearchString)
|
||||
string querySearchString,
|
||||
CancellationToken token)
|
||||
{
|
||||
if (!useIndexSearch)
|
||||
return directoryInfoClassSearch(query, querySearchString);
|
||||
|
||||
return windowsIndexSearch(query, querySearchString);
|
||||
return await windowsIndexSearch(query, querySearchString, token);
|
||||
}
|
||||
|
||||
private List<Result> WindowsIndexFilesAndFoldersSearch(Query query, string querySearchString)
|
||||
private async Task<List<Result>> WindowsIndexFilesAndFoldersSearchAsync(Query query, string querySearchString, CancellationToken token)
|
||||
{
|
||||
var queryConstructor = new QueryConstructor(settings);
|
||||
|
||||
return indexSearch.WindowsIndexSearch(querySearchString,
|
||||
return await indexSearch.WindowsIndexSearchAsync(querySearchString,
|
||||
queryConstructor.CreateQueryHelper().ConnectionString,
|
||||
queryConstructor.QueryForAllFilesAndFolders,
|
||||
query);
|
||||
query,
|
||||
token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private List<Result> WindowsIndexTopLevelFolderSearch(Query query, string path)
|
||||
|
||||
private async Task<List<Result>> WindowsIndexTopLevelFolderSearchAsync(Query query, string path, CancellationToken token)
|
||||
{
|
||||
var queryConstructor = new QueryConstructor(settings);
|
||||
|
||||
return indexSearch.WindowsIndexSearch(path,
|
||||
return await indexSearch.WindowsIndexSearchAsync(path,
|
||||
queryConstructor.CreateQueryHelper().ConnectionString,
|
||||
queryConstructor.QueryForTopLevelDirectorySearch,
|
||||
query);
|
||||
query,
|
||||
token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private bool UseWindowsIndexForDirectorySearch(string locationPath)
|
||||
|
|
|
|||
|
|
@ -5,19 +5,13 @@ using System.Collections.Generic;
|
|||
using System.Data.OleDb;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
|
||||
{
|
||||
internal class IndexSearch
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
|
||||
private OleDbConnection conn;
|
||||
|
||||
private OleDbCommand command;
|
||||
|
||||
private OleDbDataReader dataReaderResults;
|
||||
|
||||
private readonly ResultManager resultManager;
|
||||
|
||||
// Reserved keywords in oleDB
|
||||
|
|
@ -28,7 +22,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
|
|||
resultManager = new ResultManager(context);
|
||||
}
|
||||
|
||||
internal List<Result> ExecuteWindowsIndexSearch(string indexQueryString, string connectionString, Query query)
|
||||
internal async Task<List<Result>> ExecuteWindowsIndexSearchAsync(string indexQueryString, string connectionString, Query query, CancellationToken token)
|
||||
{
|
||||
var folderResults = new List<Result>();
|
||||
var fileResults = new List<Result>();
|
||||
|
|
@ -36,47 +30,49 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
|
|||
|
||||
try
|
||||
{
|
||||
using (conn = new OleDbConnection(connectionString))
|
||||
using var conn = new OleDbConnection(connectionString);
|
||||
await conn.OpenAsync(token);
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
using var command = new OleDbCommand(indexQueryString, conn);
|
||||
// Results return as an OleDbDataReader.
|
||||
using var dataReaderResults = await command.ExecuteReaderAsync(token) as OleDbDataReader;
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
if (dataReaderResults.HasRows)
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (command = new OleDbCommand(indexQueryString, conn))
|
||||
while (await dataReaderResults.ReadAsync(token))
|
||||
{
|
||||
// Results return as an OleDbDataReader.
|
||||
using (dataReaderResults = command.ExecuteReader())
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (dataReaderResults.GetValue(0) != DBNull.Value && dataReaderResults.GetValue(1) != DBNull.Value)
|
||||
{
|
||||
if (dataReaderResults.HasRows)
|
||||
{
|
||||
while (dataReaderResults.Read())
|
||||
{
|
||||
if (dataReaderResults.GetValue(0) != DBNull.Value && dataReaderResults.GetValue(1) != DBNull.Value)
|
||||
{
|
||||
// # is URI syntax for the fragment component, need to be encoded so LocalPath returns complete path
|
||||
var encodedFragmentPath = dataReaderResults
|
||||
.GetString(1)
|
||||
.Replace("#", "%23", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
var path = new Uri(encodedFragmentPath).LocalPath;
|
||||
// # is URI syntax for the fragment component, need to be encoded so LocalPath returns complete path
|
||||
var encodedFragmentPath = dataReaderResults
|
||||
.GetString(1)
|
||||
.Replace("#", "%23", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (dataReaderResults.GetString(2) == "Directory")
|
||||
{
|
||||
folderResults.Add(resultManager.CreateFolderResult(
|
||||
dataReaderResults.GetString(0),
|
||||
path,
|
||||
path,
|
||||
query, true, true));
|
||||
}
|
||||
else
|
||||
{
|
||||
fileResults.Add(resultManager.CreateFileResult(path, query, true, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
var path = new Uri(encodedFragmentPath).LocalPath;
|
||||
|
||||
if (dataReaderResults.GetString(2) == "Directory")
|
||||
{
|
||||
folderResults.Add(resultManager.CreateFolderResult(
|
||||
dataReaderResults.GetString(0),
|
||||
path,
|
||||
path,
|
||||
query, true, true));
|
||||
}
|
||||
else
|
||||
{
|
||||
fileResults.Add(resultManager.CreateFileResult(path, query, true, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return new List<Result>(); // The source code indicates that without adding members, it won't allocate an array
|
||||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
// Internal error from ExecuteReader(): Connection closed.
|
||||
|
|
@ -91,18 +87,18 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
|
|||
return results.Concat(folderResults.OrderBy(x => x.Title)).Concat(fileResults.OrderBy(x => x.Title)).ToList(); ;
|
||||
}
|
||||
|
||||
internal List<Result> WindowsIndexSearch(string searchString, string connectionString, Func<string, string> constructQuery, Query query)
|
||||
internal async Task<List<Result>> WindowsIndexSearchAsync(string searchString, string connectionString,
|
||||
Func<string, string> constructQuery, Query query,
|
||||
CancellationToken token)
|
||||
{
|
||||
var regexMatch = Regex.Match(searchString, reservedStringPattern);
|
||||
|
||||
if (regexMatch.Success)
|
||||
return new List<Result>();
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
var constructedQuery = constructQuery(searchString);
|
||||
return ExecuteWindowsIndexSearch(constructedQuery, connectionString, query);
|
||||
}
|
||||
var constructedQuery = constructQuery(searchString);
|
||||
return await ExecuteWindowsIndexSearchAsync(constructedQuery, connectionString, query, token);
|
||||
|
||||
}
|
||||
|
||||
internal bool PathIsIndexed(string path)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using Flow.Launcher.Infrastructure.Storage;
|
|||
using Flow.Launcher.Plugin.Explorer.Search;
|
||||
using Flow.Launcher.Plugin.Explorer.Search.FolderLinks;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Flow.Launcher.Plugin.Explorer.ViewModels
|
||||
{
|
||||
|
|
@ -21,6 +22,11 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
|
|||
Settings = storage.Load();
|
||||
}
|
||||
|
||||
public Task LoadStorage()
|
||||
{
|
||||
return Task.Run(() => Settings = storage.Load());
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
storage.Save();
|
||||
|
|
|
|||
Loading…
Reference in a new issue