From ad8dffe4688909fb82290ae6c8c10eac9e58c987 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 12:09:11 +0200 Subject: [PATCH 01/28] Add direct call to Everything API from `ResultManager` Obviously this is stupid because we forcefully tied Explorer plugin to Everything. This needs more thinking, but is a good enough for now solution to test what works and doesnt work --- Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index ed4f39735..d0ceedc73 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; +using Flow.Launcher.Plugin.Explorer.Search.Everything; namespace Flow.Launcher.Plugin.Explorer.Search { @@ -249,6 +250,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, Verb = "runas", }); + EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); } catch (Exception e) { @@ -259,10 +261,12 @@ namespace Flow.Launcher.Plugin.Explorer.Search else if (c.SpecialKeyState.CtrlPressed) { Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath); + EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); } else { FilesFolders.OpenPath(filePath); + EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); } } catch (Exception ex) From 86869ff5f450ae46dfc54e946db39dfd0b505658 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 12:29:03 +0200 Subject: [PATCH 02/28] Extract RunExplorer nested ifs as functions for readability --- .../Search/ResultManager.cs | 59 +++++++++++-------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index d0ceedc73..be36ce61d 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -216,10 +216,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool windowsIndexed = false) { - Result.PreviewInfo preview = IsMedia(Path.GetExtension(filePath)) ? new Result.PreviewInfo - { - IsMedia = true, PreviewImagePath = filePath, - } : Result.PreviewInfo.Default; + Result.PreviewInfo preview = IsMedia(Path.GetExtension(filePath)) + ? new Result.PreviewInfo { IsMedia = true, PreviewImagePath = filePath, } + : Result.PreviewInfo.Default; var title = Path.GetFileName(filePath); @@ -237,31 +236,14 @@ namespace Flow.Launcher.Plugin.Explorer.Search { try { + // TODO Why do we check if file exists here, but not in the other if conditions? if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) { - _ = Task.Run(() => - { - try - { - Process.Start(new ProcessStartInfo - { - FileName = filePath, - UseShellExecute = true, - WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, - Verb = "runas", - }); - EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Could not start " + filePath); - } - }); + RunExplorerAsAdminAtPath(filePath); } else if (c.SpecialKeyState.CtrlPressed) { - Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath); - EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); + RunExplorerAtPath(filePath); } else { @@ -288,6 +270,35 @@ namespace Flow.Launcher.Plugin.Explorer.Search return result; } + private static void RunExplorerAsAdminAtPath(string filePath) + { + _ = Task.Run(() => + { + try + { + Process.Start(new ProcessStartInfo + { + FileName = filePath, + UseShellExecute = true, + WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, + Verb = "runas", + }); + EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); + } + catch (Exception e) + { + MessageBox.Show(e.Message, "Could not start " + filePath); + } + }); + } + + private static void RunExplorerAtPath(string filePath) + { + Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath); + EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); + } + + public static bool IsMedia(string extension) { if (string.IsNullOrEmpty(extension)) From 742d80922cc18b445ffe9194d781dd5c04a05a37 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 12:30:25 +0200 Subject: [PATCH 03/28] Indentation --- .../Search/ResultManager.cs | 41 ++++--------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index be36ce61d..6110c0433 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -58,10 +58,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search { return result.Type switch { - ResultType.Folder or ResultType.Volume => CreateFolderResult(Path.GetFileName(result.FullPath), - result.FullPath, result.FullPath, query, 0, result.WindowsIndexed), - ResultType.File => CreateFileResult( - result.FullPath, query, 0, result.WindowsIndexed), + ResultType.Folder or ResultType.Volume => + CreateFolderResult(Path.GetFileName(result.FullPath), result.FullPath, result.FullPath, query, 0, result.WindowsIndexed), + ResultType.File => + CreateFileResult(result.FullPath, query, 0, result.WindowsIndexed), _ => throw new ArgumentOutOfRangeException() }; } @@ -99,12 +99,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search Score = score, TitleToolTip = InternationalizationManager.Instance.GetTranslation("plugin_explorer_plugin_ToolTipOpenDirectory"), SubTitleToolTip = path, - ContextData = new SearchResult - { - Type = ResultType.Folder, - FullPath = path, - WindowsIndexed = windowsIndexed - } + ContextData = new SearchResult { Type = ResultType.Folder, FullPath = path, WindowsIndexed = windowsIndexed } }; } @@ -141,12 +136,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search }, TitleToolTip = path, SubTitleToolTip = path, - ContextData = new SearchResult - { - Type = ResultType.Volume, - FullPath = path, - WindowsIndexed = windowsIndexed - } + ContextData = new SearchResult { Type = ResultType.Volume, FullPath = path, WindowsIndexed = windowsIndexed } }; } @@ -205,12 +195,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search Context.API.OpenDirectory(folderPath); return true; }, - ContextData = new SearchResult - { - Type = ResultType.Folder, - FullPath = folderPath, - WindowsIndexed = windowsIndexed - } + ContextData = new SearchResult { Type = ResultType.Folder, FullPath = folderPath, WindowsIndexed = windowsIndexed } }; } @@ -260,12 +245,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search }, TitleToolTip = InternationalizationManager.Instance.GetTranslation("plugin_explorer_plugin_ToolTipOpenContainingFolder"), SubTitleToolTip = filePath, - ContextData = new SearchResult - { - Type = ResultType.File, - FullPath = filePath, - WindowsIndexed = windowsIndexed - } + ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath, WindowsIndexed = windowsIndexed } }; return result; } @@ -311,10 +291,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search } } - public static readonly string[] MediaExtensions = - { - ".jpg", ".png", ".avi", ".mkv", ".bmp", ".gif", ".wmv", ".mp3", ".flac", ".mp4" - }; + public static readonly string[] MediaExtensions = { ".jpg", ".png", ".avi", ".mkv", ".bmp", ".gif", ".wmv", ".mp3", ".flac", ".mp4" }; } public enum ResultType From 52729e34318bed9a5001dea1f9aa74220b1a1f52 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 14:12:44 +0200 Subject: [PATCH 04/28] cleanup --- .../SharedCommands/FilesFolders.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index bd8d32ff5..017dfa62f 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -14,8 +14,6 @@ namespace Flow.Launcher.Plugin.SharedCommands { private const string FileExplorerProgramName = "explorer"; - private const string FileExplorerProgramEXE = "explorer.exe"; - /// /// Copies the folder and all of its files and folders /// including subfolders to the target location @@ -145,13 +143,25 @@ namespace Flow.Launcher.Plugin.SharedCommands return File.Exists(filePath); } + /* + ❓ + todo does it make sense to remove Flow.Launcher.Plugin.IPublicAPI.OpenDirectory (used as Context.API.OpenDirectory(path)), + and replace with this function? + todo or the other way around: remove this function and use OpenDirectory + because i don't see the point of having 2 functions which do the same thing + */ /// /// Open a directory window (using the OS's default handler, usually explorer) /// /// public static void OpenPath(string fileOrFolderPath) { - var psi = new ProcessStartInfo { FileName = FileExplorerProgramName, UseShellExecute = true, Arguments = '"' + fileOrFolderPath + '"' }; + var psi = new ProcessStartInfo + { + FileName = FileExplorerProgramName, + UseShellExecute = true, + Arguments = '"' + fileOrFolderPath + '"' + }; try { if (LocationExists(fileOrFolderPath) || FileExists(fileOrFolderPath)) @@ -173,7 +183,7 @@ namespace Flow.Launcher.Plugin.SharedCommands /// public static void OpenContainingFolder(string path) { - Process.Start(FileExplorerProgramEXE, $" /select,\"{path}\""); + Process.Start(FileExplorerProgramName, $" /select,\"{path}\""); } /// From f96bf4c59210a4d43ef9ad8948fa8e128e237081 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 14:35:08 +0200 Subject: [PATCH 05/28] cleanup --- .../Search/Everything/EverythingAPI.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index 3efd09c4d..2a007cd73 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -17,7 +17,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything { private const int BufferSize = 4096; - private static SemaphoreSlim _semaphore = new(1, 1); + private static readonly SemaphoreSlim _semaphore = new(1, 1); // cached buffer to remove redundant allocations. private static readonly StringBuilder buffer = new(BufferSize); @@ -78,7 +78,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything if (option.MaxCount < 0) throw new ArgumentOutOfRangeException(nameof(option.MaxCount), option.MaxCount, "MaxCount must be greater than or equal to 0"); - + await _semaphore.WaitAsync(token); From 94b841d341462ada653305b3b5a32b59da8d6d4b Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 14:36:36 +0200 Subject: [PATCH 06/28] Should be working --- .../Search/Everything/EverythingAPI.cs | 16 +++++++++++ .../Search/ResultManager.cs | 27 ++++++++++--------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index 2a007cd73..ade6d1a66 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -172,5 +172,21 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything throw new ArgumentOutOfRangeException(); } } + + public static async Task IncrementRunCounterAsync(string fileOrFolder) + { + try + { + await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)); + + if (await IsEverythingRunningAsync()) + _ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder); + } + catch (Exception) + { + /*ignored*/ + } + finally { _semaphore.Release(); } + } } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 6110c0433..bee7cb1bc 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -78,10 +78,12 @@ namespace Flow.Launcher.Plugin.Explorer.Search CopyText = path, Action = c => { + // open folder if (c.SpecialKeyState.CtrlPressed || (!Settings.PathSearchKeywordEnabled && !Settings.SearchActionKeywordEnabled)) { try { + IncrementRunCounterIfNeeded(path); Context.API.OpenDirectory(path); return true; } @@ -91,7 +93,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search return false; } } - + // or make this folder the current query Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword)); return false; @@ -224,16 +226,19 @@ namespace Flow.Launcher.Plugin.Explorer.Search // TODO Why do we check if file exists here, but not in the other if conditions? if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) { - RunExplorerAsAdminAtPath(filePath); + IncrementRunCounterIfNeeded(filePath); + OpenFileAsAdmin(filePath); } else if (c.SpecialKeyState.CtrlPressed) { - RunExplorerAtPath(filePath); - } + IncrementRunCounterIfNeeded(filePath); + FilesFolders.OpenContainingFolder(filePath); + } + else { - FilesFolders.OpenPath(filePath); - EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); + IncrementRunCounterIfNeeded(filePath); + FilesFolders.OpenPath(filePath); } } catch (Exception ex) @@ -250,7 +255,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search return result; } - private static void RunExplorerAsAdminAtPath(string filePath) + private static void OpenFileAsAdmin(string filePath) { _ = Task.Run(() => { @@ -263,7 +268,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, Verb = "runas", }); - EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); } catch (Exception e) { @@ -272,13 +276,12 @@ namespace Flow.Launcher.Plugin.Explorer.Search }); } - private static void RunExplorerAtPath(string filePath) + private static void IncrementRunCounterIfNeeded(string fileOrFolder) { - Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath); - EverythingApiDllImport.Everything_IncRunCountFromFileName(filePath); + if (Settings.EverythingEnabled) + _ = Task.Run(() => EverythingApi.IncrementRunCounterAsync(fileOrFolder)); } - public static bool IsMedia(string extension) { if (string.IsNullOrEmpty(extension)) From ac3c95553979bcff540a6bcdf39509ad41cbb619 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Fri, 3 Mar 2023 14:56:20 +0200 Subject: [PATCH 07/28] Async is hard. Not sure exactly why the previous version was not working, but i _assume_ it's because I was in a deadlock on that semaphore. --- .../Search/Everything/EverythingAPI.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index ade6d1a66..a90fab064 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -178,9 +178,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything try { await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)); - - if (await IsEverythingRunningAsync()) - _ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder); + _ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder); } catch (Exception) { From 5ff916a17bc20078151a80fe98b3a0d367a2e4d4 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Sun, 5 Mar 2023 10:55:09 +0200 Subject: [PATCH 08/28] Use the `SearchResult.Score` instead of `0` when creating the plugin `Result` --- Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index bee7cb1bc..a463ac6fe 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -59,9 +59,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search return result.Type switch { ResultType.Folder or ResultType.Volume => - CreateFolderResult(Path.GetFileName(result.FullPath), result.FullPath, result.FullPath, query, 0, result.WindowsIndexed), + CreateFolderResult(Path.GetFileName(result.FullPath), result.FullPath, result.FullPath, query, result.Score, result.WindowsIndexed), ResultType.File => - CreateFileResult(result.FullPath, query, 0, result.WindowsIndexed), + CreateFileResult(result.FullPath, query, result.Score, result.WindowsIndexed), _ => throw new ArgumentOutOfRangeException() }; } From 84b63e5f54eccce75ce4a136bc53e8920d41491e Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Sun, 5 Mar 2023 12:24:42 +0200 Subject: [PATCH 09/28] Use Request Flags when retrieving results from Everything, to be able to retrieve RunCount as well --- .../Search/Everything/EverythingAPI.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index a90fab064..997840ecf 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -34,6 +34,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything InvalidCallError } + const uint EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004u; + const uint EVERYTHING_REQUEST_RUN_COUNT = 0x00000400u; + /// /// Checks whether the sort option is Fast Sort. /// @@ -113,6 +116,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything EverythingApiDllImport.Everything_SetSort(option.SortOption); EverythingApiDllImport.Everything_SetMatchPath(option.IsFullPathSearch); + EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT); // todo need flag for this? + + if (token.IsCancellationRequested) yield break; if (!EverythingApiDllImport.Everything_QueryW(true)) @@ -132,10 +138,13 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything var result = new SearchResult { + // todo the types are wrong. Everything expects uint everywhere, but we send int just above. pls fix FullPath = buffer.ToString(), Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder : EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File : - ResultType.Volume + ResultType.Volume, + // todo need flag for this? + Score = (int)EverythingApiDllImport.Everything_GetResultRunCount( (uint)idx) }; yield return result; From 05ba89db3d840272a1f6654bb0574c93886fecab Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Sun, 5 Mar 2023 12:25:12 +0200 Subject: [PATCH 10/28] `Result.ToString` contains `Score` as well --- Flow.Launcher.Plugin/Result.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Plugin/Result.cs b/Flow.Launcher.Plugin/Result.cs index dc24872f5..1c4467762 100644 --- a/Flow.Launcher.Plugin/Result.cs +++ b/Flow.Launcher.Plugin/Result.cs @@ -178,7 +178,7 @@ namespace Flow.Launcher.Plugin /// public override string ToString() { - return Title + SubTitle; + return Title + SubTitle + Score; } /// From 7d6d0258d00d8e9ff0e920e098be939ba1058cbf Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Sun, 5 Mar 2023 12:27:03 +0200 Subject: [PATCH 11/28] Add more debugging info to DEBUG profile --- .../Search/ResultManager.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index a463ac6fe..7c9213a70 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -70,7 +70,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { return new Result { - Title = title, + Title = title + addScoreInDebug(score), IcoPath = path, SubTitle = subtitle, AutoCompleteText = GetAutoCompleteText(title, query, path, ResultType.Folder), @@ -207,7 +207,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search ? new Result.PreviewInfo { IsMedia = true, PreviewImagePath = filePath, } : Result.PreviewInfo.Default; - var title = Path.GetFileName(filePath); + var title = Path.GetFileName(filePath) + addScoreInDebug(score); var result = new Result { @@ -294,6 +294,15 @@ namespace Flow.Launcher.Plugin.Explorer.Search } } + private static string addScoreInDebug(int score) + { + #if DEBUG + return " ➡️ " + score; + #else + return ""; + #endif + } + public static readonly string[] MediaExtensions = { ".jpg", ".png", ".avi", ".mkv", ".bmp", ".gif", ".wmv", ".mp3", ".flac", ".mp4" }; } From 9bc6a4e0726506aa106d325be6d30fcea27aedf6 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 12:10:43 +0200 Subject: [PATCH 12/28] Waiting on a semaphore must be outside of `try` --- .../Search/Everything/EverythingAPI.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index 997840ecf..579c5075f 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -184,9 +184,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything public static async Task IncrementRunCounterAsync(string fileOrFolder) { + await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)); try { - await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)); _ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder); } catch (Exception) From 3cecf5b3025a363468a5397e0d5a7912d9111ea5 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 12:13:08 +0200 Subject: [PATCH 13/28] Use String Interpolation --- Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 7c9213a70..25fe657c1 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -297,7 +297,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search private static string addScoreInDebug(int score) { #if DEBUG - return " ➡️ " + score; + return $" ➡️ {score}"; #else return ""; #endif From 436ec7dd39d92fe775f54e857a3bd00bbf10a062 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 12:45:43 +0200 Subject: [PATCH 14/28] cleanup --- .../SharedCommands/FilesFolders.cs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index 017dfa62f..6b7f0c2d3 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -143,13 +143,6 @@ namespace Flow.Launcher.Plugin.SharedCommands return File.Exists(filePath); } - /* - ❓ - todo does it make sense to remove Flow.Launcher.Plugin.IPublicAPI.OpenDirectory (used as Context.API.OpenDirectory(path)), - and replace with this function? - todo or the other way around: remove this function and use OpenDirectory - because i don't see the point of having 2 functions which do the same thing - */ /// /// Open a directory window (using the OS's default handler, usually explorer) /// @@ -177,15 +170,6 @@ namespace Flow.Launcher.Plugin.SharedCommands } } - /// - /// Open the folder that contains - /// - /// - public static void OpenContainingFolder(string path) - { - Process.Start(FileExplorerProgramName, $" /select,\"{path}\""); - } - /// /// This checks whether a given string is a directory path or network location string. /// It does not check if location actually exists. From a9192541abe5adb12eb73558cf9a27831fa6ed38 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 12:47:49 +0200 Subject: [PATCH 15/28] Make code more readable --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 4 ++-- Flow.Launcher/PublicAPIInstance.cs | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 79d106ef2..d9cf68469 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -233,8 +233,8 @@ namespace Flow.Launcher.Plugin /// Open directory in an explorer configured by user via Flow's Settings. The default is Windows Explorer /// /// Directory Path to open - /// Extra FileName Info - public void OpenDirectory(string DirectoryPath, string FileName = null); + /// Extra FileName Info + public void OpenDirectory(string DirectoryPath, string FileNameOrFilePath = null); /// /// Opens the URL with the given Uri object. diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index b2487693e..ec997f537 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -1,4 +1,4 @@ - using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -195,17 +195,20 @@ namespace Flow.Launcher ((PluginJsonStorage)_pluginJsonStorages[type]).Save(); } - public void OpenDirectory(string DirectoryPath, string FileName = null) + public void OpenDirectory(string DirectoryPath, string FileNameOrFilePath = null) { using var explorer = new Process(); var explorerInfo = _settingsVM.Settings.CustomExplorer; explorer.StartInfo = new ProcessStartInfo { FileName = explorerInfo.Path, - Arguments = FileName is null ? - explorerInfo.DirectoryArgument.Replace("%d", DirectoryPath) : - explorerInfo.FileArgument.Replace("%d", DirectoryPath).Replace("%f", - Path.IsPathRooted(FileName) ? FileName : Path.Combine(DirectoryPath, FileName)) + Arguments = FileNameOrFilePath is null + ? explorerInfo.DirectoryArgument.Replace("%d", DirectoryPath) + : explorerInfo.FileArgument + .Replace("%d", DirectoryPath) + .Replace("%f", + Path.IsPathRooted(FileNameOrFilePath) ? FileNameOrFilePath : Path.Combine(DirectoryPath, FileNameOrFilePath) + ) }; explorer.Start(); } From a1a4c720c0d28b75a700d50597c45a9fead17a32 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 12:47:57 +0200 Subject: [PATCH 16/28] More comments --- .../Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 25fe657c1..0eb16faa7 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -226,17 +226,19 @@ namespace Flow.Launcher.Plugin.Explorer.Search // TODO Why do we check if file exists here, but not in the other if conditions? if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) { + // run the file as admin IncrementRunCounterIfNeeded(filePath); OpenFileAsAdmin(filePath); } else if (c.SpecialKeyState.CtrlPressed) { + // open folder and select this file IncrementRunCounterIfNeeded(filePath); - FilesFolders.OpenContainingFolder(filePath); + Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath); } - else { + // run the file IncrementRunCounterIfNeeded(filePath); FilesFolders.OpenPath(filePath); } From fa971c6b81eb683dbce54c638b28ba6a011ff261 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 13:00:00 +0200 Subject: [PATCH 17/28] Ask Everything for RunCount only if sorting by it --- .../Search/Everything/EverythingAPI.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index 579c5075f..32fbaee61 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -115,8 +115,16 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything EverythingApiDllImport.Everything_SetSort(option.SortOption); EverythingApiDllImport.Everything_SetMatchPath(option.IsFullPathSearch); + + if (option.SortOption == SortOption.RUN_COUNT_DESCENDING) + { + EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT); + } + else + { + EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME); + } - EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT); // todo need flag for this? if (token.IsCancellationRequested) yield break; @@ -143,7 +151,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder : EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File : ResultType.Volume, - // todo need flag for this? Score = (int)EverythingApiDllImport.Everything_GetResultRunCount( (uint)idx) }; From 4289d10af6e730f8fbe31c1873bd680db59c7d46 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 13:00:28 +0200 Subject: [PATCH 18/28] Remove `SortOption.RunCountAsc` because it doesn't make sense --- .../Search/Everything/SortOption.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/SortOption.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/SortOption.cs index 434afd1b4..c57e3fe4a 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/SortOption.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/SortOption.cs @@ -27,7 +27,6 @@ namespace Flow.Launcher.Plugin.Everything.Everything ATTRIBUTES_DESCENDING = 16u, FILE_LIST_FILENAME_ASCENDING = 17u, FILE_LIST_FILENAME_DESCENDING = 18u, - RUN_COUNT_ASCENDING = 19u, RUN_COUNT_DESCENDING = 20u, DATE_RECENTLY_CHANGED_ASCENDING = 21u, DATE_RECENTLY_CHANGED_DESCENDING = 22u, From 2be108d7409479ad82707dc72237848fc0c909bc Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 13:31:51 +0200 Subject: [PATCH 19/28] formatting --- .../Search/Everything/EverythingSearchManager.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingSearchManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingSearchManager.cs index 344707892..1ea23777c 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingSearchManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingSearchManager.cs @@ -39,6 +39,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything Main.Context.API.GetTranslation("flowlauncher_plugin_everything_sdk_issue")); } } + private async ValueTask ClickToInstallEverythingAsync(ActionContext _) { var installedPath = await EverythingDownloadHelper.PromptDownloadIfNotInstallAsync(Settings.EverythingInstalledPath, Main.Context.API); @@ -68,8 +69,8 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything await foreach (var result in EverythingApi.SearchAsync(option, token)) yield return result; } - public async IAsyncEnumerable ContentSearchAsync(string plainSearch, - string contentSearch, + + public async IAsyncEnumerable ContentSearchAsync(string plainSearch, string contentSearch, [EnumeratorCancellation] CancellationToken token) { await ThrowIfEverythingNotAvailableAsync(token); @@ -102,6 +103,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything yield return result; } } + public async IAsyncEnumerable EnumerateAsync(string path, string search, bool recursive, [EnumeratorCancellation] CancellationToken token) { await ThrowIfEverythingNotAvailableAsync(token); From 9debba9e4ed66dc852e0522917ef676e408cd1af Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 13:35:00 +0200 Subject: [PATCH 20/28] centralize the 3 actions: open file, open folder, open file as admin, for simpler logic --- .../Search/ResultManager.cs | 66 ++++++++++--------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 0eb16faa7..5c778f1fd 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -70,7 +70,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { return new Result { - Title = title + addScoreInDebug(score), + Title = title + _addScoreInDebug(score), IcoPath = path, SubTitle = subtitle, AutoCompleteText = GetAutoCompleteText(title, query, path, ResultType.Folder), @@ -83,8 +83,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { try { - IncrementRunCounterIfNeeded(path); - Context.API.OpenDirectory(path); + _openFolder(path); return true; } catch (Exception ex) @@ -133,7 +132,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search ProgressBarColor = progressBarColor, Action = c => { - Context.API.OpenDirectory(path); + _openFolder(path); return true; }, TitleToolTip = path, @@ -194,7 +193,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search CopyText = folderPath, Action = _ => { - Context.API.OpenDirectory(folderPath); + _openFolder(folderPath); return true; }, ContextData = new SearchResult { Type = ResultType.Folder, FullPath = folderPath, WindowsIndexed = windowsIndexed } @@ -207,7 +206,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search ? new Result.PreviewInfo { IsMedia = true, PreviewImagePath = filePath, } : Result.PreviewInfo.Default; - var title = Path.GetFileName(filePath) + addScoreInDebug(score); + var title = Path.GetFileName(filePath) + _addScoreInDebug(score); var result = new Result { @@ -226,21 +225,15 @@ namespace Flow.Launcher.Plugin.Explorer.Search // TODO Why do we check if file exists here, but not in the other if conditions? if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) { - // run the file as admin - IncrementRunCounterIfNeeded(filePath); - OpenFileAsAdmin(filePath); + _openFileAsAdmin(filePath); } else if (c.SpecialKeyState.CtrlPressed) { - // open folder and select this file - IncrementRunCounterIfNeeded(filePath); - Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath); + _openFolder(filePath, filePath); } else { - // run the file - IncrementRunCounterIfNeeded(filePath); - FilesFolders.OpenPath(filePath); + _openFile(filePath); } } catch (Exception ex) @@ -257,12 +250,37 @@ namespace Flow.Launcher.Plugin.Explorer.Search return result; } - private static void OpenFileAsAdmin(string filePath) + public static bool IsMedia(string extension) + { + if (string.IsNullOrEmpty(extension)) + { + return false; + } + else + { + return MediaExtensions.Contains(extension.ToLowerInvariant()); + } + } + + private static void _openFile(string filePath) + { + _incrementEverythingRunCounterIfNeeded(filePath); + FilesFolders.OpenPath(filePath); + } + + private static void _openFolder(string folderPath, string fileNameOrFilePath=null) + { + _incrementEverythingRunCounterIfNeeded(folderPath); + Context.API.OpenDirectory(Path.GetDirectoryName(folderPath), fileNameOrFilePath); + } + + private static void _openFileAsAdmin(string filePath) { _ = Task.Run(() => { try { + _incrementEverythingRunCounterIfNeeded(filePath); Process.Start(new ProcessStartInfo { FileName = filePath, @@ -278,25 +296,13 @@ namespace Flow.Launcher.Plugin.Explorer.Search }); } - private static void IncrementRunCounterIfNeeded(string fileOrFolder) + private static void _incrementEverythingRunCounterIfNeeded(string fileOrFolder) { if (Settings.EverythingEnabled) _ = Task.Run(() => EverythingApi.IncrementRunCounterAsync(fileOrFolder)); } - public static bool IsMedia(string extension) - { - if (string.IsNullOrEmpty(extension)) - { - return false; - } - else - { - return MediaExtensions.Contains(extension.ToLowerInvariant()); - } - } - - private static string addScoreInDebug(int score) + private static string _addScoreInDebug(int score) { #if DEBUG return $" ➡️ {score}"; From 96afa29905a7dc263f03932e6cc7076c605bea08 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 8 Mar 2023 14:20:33 +0200 Subject: [PATCH 21/28] cleanup + warnings --- .../Search/ResultManager.cs | 48 ++++++++----------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 5c778f1fd..54ecc1ff9 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -38,13 +38,13 @@ namespace Flow.Launcher.Plugin.Explorer.Search var keyword = usePathSearchActionKeyword ? pathSearchActionKeyword : searchActionKeyword; - var formatted_path = path; + var formattedPath = path; if (type == ResultType.Folder) // the separator is needed so when navigating the folder structure contents of the folder are listed - formatted_path = path.EndsWith(Constants.DirectorySeparator) ? path : path + Constants.DirectorySeparator; + formattedPath = path.EndsWith(Constants.DirectorySeparator) ? path : path + Constants.DirectorySeparator; - return $"{keyword}{formatted_path}"; + return $"{keyword}{formattedPath}"; } public static string GetAutoCompleteText(string title, Query query, string path, ResultType resultType) @@ -70,7 +70,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { return new Result { - Title = title + _addScoreInDebug(score), + Title = title, IcoPath = path, SubTitle = subtitle, AutoCompleteText = GetAutoCompleteText(title, query, path, ResultType.Folder), @@ -109,7 +109,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search var progressBarColor = "#26a0da"; var title = string.Empty; // hide title when use progress bar, var driveLetter = path[..1].ToUpper(); - var driveName = driveLetter + ":\\"; DriveInfo drv = new DriveInfo(driveLetter); var freespace = ToReadableSize(drv.AvailableFreeSpace, 2); var totalspace = ToReadableSize(drv.TotalSize, 2); @@ -130,7 +129,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search Score = 500, ProgressBar = progressValue, ProgressBarColor = progressBarColor, - Action = c => + Action = _ => { _openFolder(path); return true; @@ -145,7 +144,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { int mok = 0; double drvSize = pDrvSize; - string Space = "Byte"; + string space = "Byte"; while (drvSize > 1024.0) { @@ -154,23 +153,23 @@ namespace Flow.Launcher.Plugin.Explorer.Search } if (mok == 1) - Space = "KB"; + space = "KB"; else if (mok == 2) - Space = " MB"; + space = " MB"; else if (mok == 3) - Space = " GB"; + space = " GB"; else if (mok == 4) - Space = " TB"; + space = " TB"; - var returnStr = $"{Convert.ToInt32(drvSize)}{Space}"; + var returnStr = $"{Convert.ToInt32(drvSize)}{space}"; if (mok != 0) { returnStr = pi switch { - 1 => $"{drvSize:F1}{Space}", - 2 => $"{drvSize:F2}{Space}", - 3 => $"{drvSize:F3}{Space}", - _ => $"{Convert.ToInt32(drvSize)}{Space}" + 1 => $"{drvSize:F1}{space}", + 2 => $"{drvSize:F2}{space}", + 3 => $"{drvSize:F3}{space}", + _ => $"{Convert.ToInt32(drvSize)}{space}" }; } @@ -202,11 +201,11 @@ namespace Flow.Launcher.Plugin.Explorer.Search internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool windowsIndexed = false) { - Result.PreviewInfo preview = IsMedia(Path.GetExtension(filePath)) + Result.PreviewInfo preview = _isMedia(Path.GetExtension(filePath)) ? new Result.PreviewInfo { IsMedia = true, PreviewImagePath = filePath, } : Result.PreviewInfo.Default; - var title = Path.GetFileName(filePath) + _addScoreInDebug(score); + var title = Path.GetFileName(filePath); var result = new Result { @@ -250,7 +249,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search return result; } - public static bool IsMedia(string extension) + private static bool _isMedia(string extension) { if (string.IsNullOrEmpty(extension)) { @@ -302,16 +301,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search _ = Task.Run(() => EverythingApi.IncrementRunCounterAsync(fileOrFolder)); } - private static string _addScoreInDebug(int score) - { - #if DEBUG - return $" ➡️ {score}"; - #else - return ""; - #endif - } - - public static readonly string[] MediaExtensions = { ".jpg", ".png", ".avi", ".mkv", ".bmp", ".gif", ".wmv", ".mp3", ".flac", ".mp4" }; + private static readonly string[] MediaExtensions = { ".jpg", ".png", ".avi", ".mkv", ".bmp", ".gif", ".wmv", ".mp3", ".flac", ".mp4" }; } public enum ResultType From 569046b45de081ba0122b47b06dcd2daa9d597f0 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Tue, 14 Mar 2023 09:21:03 +0200 Subject: [PATCH 22/28] Fix private function naming convention --- .../Search/ResultManager.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 54ecc1ff9..2b4ca9969 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -83,7 +83,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { try { - _openFolder(path); + OpenFolder(path); return true; } catch (Exception ex) @@ -131,7 +131,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search ProgressBarColor = progressBarColor, Action = _ => { - _openFolder(path); + OpenFolder(path); return true; }, TitleToolTip = path, @@ -192,7 +192,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search CopyText = folderPath, Action = _ => { - _openFolder(folderPath); + OpenFolder(folderPath); return true; }, ContextData = new SearchResult { Type = ResultType.Folder, FullPath = folderPath, WindowsIndexed = windowsIndexed } @@ -201,7 +201,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool windowsIndexed = false) { - Result.PreviewInfo preview = _isMedia(Path.GetExtension(filePath)) + Result.PreviewInfo preview = IsMedia(Path.GetExtension(filePath)) ? new Result.PreviewInfo { IsMedia = true, PreviewImagePath = filePath, } : Result.PreviewInfo.Default; @@ -224,15 +224,15 @@ namespace Flow.Launcher.Plugin.Explorer.Search // TODO Why do we check if file exists here, but not in the other if conditions? if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) { - _openFileAsAdmin(filePath); + OpenFileAsAdmin(filePath); } else if (c.SpecialKeyState.CtrlPressed) { - _openFolder(filePath, filePath); + OpenFolder(filePath, filePath); } else { - _openFile(filePath); + OpenFile(filePath); } } catch (Exception ex) @@ -249,7 +249,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search return result; } - private static bool _isMedia(string extension) + private static bool IsMedia(string extension) { if (string.IsNullOrEmpty(extension)) { @@ -261,25 +261,25 @@ namespace Flow.Launcher.Plugin.Explorer.Search } } - private static void _openFile(string filePath) + private static void OpenFile(string filePath) { - _incrementEverythingRunCounterIfNeeded(filePath); + IncrementEverythingRunCounterIfNeeded(filePath); FilesFolders.OpenPath(filePath); } - private static void _openFolder(string folderPath, string fileNameOrFilePath=null) + private static void OpenFolder(string folderPath, string fileNameOrFilePath = null) { - _incrementEverythingRunCounterIfNeeded(folderPath); + IncrementEverythingRunCounterIfNeeded(folderPath); Context.API.OpenDirectory(Path.GetDirectoryName(folderPath), fileNameOrFilePath); } - private static void _openFileAsAdmin(string filePath) + private static void OpenFileAsAdmin(string filePath) { _ = Task.Run(() => { try { - _incrementEverythingRunCounterIfNeeded(filePath); + IncrementEverythingRunCounterIfNeeded(filePath); Process.Start(new ProcessStartInfo { FileName = filePath, @@ -295,7 +295,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search }); } - private static void _incrementEverythingRunCounterIfNeeded(string fileOrFolder) + private static void IncrementEverythingRunCounterIfNeeded(string fileOrFolder) { if (Settings.EverythingEnabled) _ = Task.Run(() => EverythingApi.IncrementRunCounterAsync(fileOrFolder)); From 48ed1fec1585ef61ce424d2a6fbb32f7355f972d Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Tue, 14 Mar 2023 09:21:11 +0200 Subject: [PATCH 23/28] more todo --- .../Search/Everything/EverythingAPI.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index 32fbaee61..e618b5c36 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -146,7 +146,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything var result = new SearchResult { - // todo the types are wrong. Everything expects uint everywhere, but we send int just above. pls fix + // todo the types are wrong. Everything expects uint everywhere, but we send int just above/below. how to fix? Is EverythingApiDllImport autogenerated or handmade? FullPath = buffer.ToString(), Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder : EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File : From 78cb45ce762377d3e95e61b392c4909e183c9158 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Tue, 14 Mar 2023 09:32:47 +0200 Subject: [PATCH 24/28] Do not check if file/folder exists --- .../Search/ResultManager.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 2b4ca9969..1f2c09f03 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -92,6 +92,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search return false; } } + // or make this folder the current query Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword)); @@ -221,15 +222,14 @@ namespace Flow.Launcher.Plugin.Explorer.Search { try { - // TODO Why do we check if file exists here, but not in the other if conditions? - if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) + if (c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed) { OpenFileAsAdmin(filePath); } else if (c.SpecialKeyState.CtrlPressed) { OpenFolder(filePath, filePath); - } + } else { OpenFile(filePath); @@ -251,14 +251,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search private static bool IsMedia(string extension) { - if (string.IsNullOrEmpty(extension)) - { - return false; - } - else - { - return MediaExtensions.Contains(extension.ToLowerInvariant()); - } + if (string.IsNullOrEmpty(extension)) { return false; } + + return MediaExtensions.Contains(extension.ToLowerInvariant()); } private static void OpenFile(string filePath) From 1a8339ec43e35ef7dfa4f59ff19990638bb5ef97 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Tue, 14 Mar 2023 10:39:51 +0200 Subject: [PATCH 25/28] RunCount is an allowed word --- .github/actions/spelling/allow.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/spelling/allow.txt b/.github/actions/spelling/allow.txt index 494d4de93..00cc67ea0 100644 --- a/.github/actions/spelling/allow.txt +++ b/.github/actions/spelling/allow.txt @@ -2,3 +2,4 @@ github https ssh ubuntu +runcount From 1da8c017f70ac689e3242dc0a7ebb94dee1acd42 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Tue, 14 Mar 2023 10:50:12 +0200 Subject: [PATCH 26/28] use more suggestive name --- .../Search/ResultManager.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 1f2c09f03..7147c348e 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -145,7 +145,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { int mok = 0; double drvSize = pDrvSize; - string space = "Byte"; + string uom = "Byte"; // Unit Of Measurement while (drvSize > 1024.0) { @@ -154,23 +154,23 @@ namespace Flow.Launcher.Plugin.Explorer.Search } if (mok == 1) - space = "KB"; + uom = "KB"; else if (mok == 2) - space = " MB"; + uom = " MB"; else if (mok == 3) - space = " GB"; + uom = " GB"; else if (mok == 4) - space = " TB"; + uom = " TB"; - var returnStr = $"{Convert.ToInt32(drvSize)}{space}"; + var returnStr = $"{Convert.ToInt32(drvSize)}{uom}"; if (mok != 0) { returnStr = pi switch { - 1 => $"{drvSize:F1}{space}", - 2 => $"{drvSize:F2}{space}", - 3 => $"{drvSize:F3}{space}", - _ => $"{Convert.ToInt32(drvSize)}{space}" + 1 => $"{drvSize:F1}{uom}", + 2 => $"{drvSize:F2}{uom}", + 3 => $"{drvSize:F3}{uom}", + _ => $"{Convert.ToInt32(drvSize)}{uom}" }; } From 02b321c4a36f4946777345bbc75eb4f16fee5597 Mon Sep 17 00:00:00 2001 From: Hongtao Date: Tue, 14 Mar 2023 16:42:09 -0500 Subject: [PATCH 27/28] add scoring based on index and small optimization --- .../Search/Everything/EverythingAPI.cs | 14 +++++++++++--- .../Search/SearchManager.cs | 8 ++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index e618b5c36..1b5f315f6 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -59,6 +59,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything { EverythingApiDllImport.Everything_GetMajorVersion(); var result = EverythingApiDllImport.Everything_GetLastError() != StateCode.IPCError; + return result; } finally @@ -67,6 +68,8 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything } } + const int ScoreScaleFactor = 5; + /// /// Searches the specified key word and reset the everything API afterwards /// @@ -115,7 +118,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything EverythingApiDllImport.Everything_SetSort(option.SortOption); EverythingApiDllImport.Everything_SetMatchPath(option.IsFullPathSearch); - + if (option.SortOption == SortOption.RUN_COUNT_DESCENDING) { EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT); @@ -132,10 +135,13 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything if (!EverythingApiDllImport.Everything_QueryW(true)) { CheckAndThrowExceptionOnError(); + yield break; } - for (var idx = 0; idx < EverythingApiDllImport.Everything_GetNumResults(); ++idx) + var numResults = EverythingApiDllImport.Everything_GetNumResults(); + + for (var idx = 0; idx < numResults; ++idx) { if (token.IsCancellationRequested) { @@ -151,7 +157,8 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder : EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File : ResultType.Volume, - Score = (int)EverythingApiDllImport.Everything_GetResultRunCount( (uint)idx) + Score = (option.SortOption is SortOption.RUN_COUNT_DESCENDING ? (int)EverythingApiDllImport.Everything_GetResultRunCount((uint)idx) : 0) * ScoreScaleFactor, + WindowsIndexed = false }; yield return result; @@ -192,6 +199,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything public static async Task IncrementRunCounterAsync(string fileOrFolder) { await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)); + try { _ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder); diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs index 51c4c3d9d..375fd6ce5 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs @@ -29,12 +29,13 @@ namespace Flow.Launcher.Plugin.Explorer.Search public class PathEqualityComparator : IEqualityComparer { private static PathEqualityComparator instance; + public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator(); public bool Equals(Result x, Result y) { return x.Title.Equals(y.Title, StringComparison.OrdinalIgnoreCase) - && string.Equals(x.SubTitle, y.SubTitle, StringComparison.OrdinalIgnoreCase); + && string.Equals(x.SubTitle, y.SubTitle, StringComparison.OrdinalIgnoreCase); } public int GetHashCode(Result obj) @@ -106,7 +107,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search try { - await foreach (var search in searchResults.WithCancellation(token).ConfigureAwait(false)) + await foreach (var search in searchResults.Select((r, i) => r with + { + Score = -i + 50 + }).WithCancellation(token).ConfigureAwait(false)) results.Add(ResultManager.CreateResult(query, search)); } catch (OperationCanceledException) From 1ed1c9993166d1dc72fbc17fea6ee17444838c08 Mon Sep 17 00:00:00 2001 From: TheBestPessimist Date: Wed, 15 Mar 2023 20:53:14 +0200 Subject: [PATCH 28/28] Revert "add scoring based on index and small optimization" This reverts commit 02b321c4a36f4946777345bbc75eb4f16fee5597. --- .../Search/Everything/EverythingAPI.cs | 14 +++----------- .../Search/SearchManager.cs | 8 ++------ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs index 1b5f315f6..e618b5c36 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/Everything/EverythingAPI.cs @@ -59,7 +59,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything { EverythingApiDllImport.Everything_GetMajorVersion(); var result = EverythingApiDllImport.Everything_GetLastError() != StateCode.IPCError; - return result; } finally @@ -68,8 +67,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything } } - const int ScoreScaleFactor = 5; - /// /// Searches the specified key word and reset the everything API afterwards /// @@ -118,7 +115,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything EverythingApiDllImport.Everything_SetSort(option.SortOption); EverythingApiDllImport.Everything_SetMatchPath(option.IsFullPathSearch); - + if (option.SortOption == SortOption.RUN_COUNT_DESCENDING) { EverythingApiDllImport.Everything_SetRequestFlags(EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME | EVERYTHING_REQUEST_RUN_COUNT); @@ -135,13 +132,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything if (!EverythingApiDllImport.Everything_QueryW(true)) { CheckAndThrowExceptionOnError(); - yield break; } - var numResults = EverythingApiDllImport.Everything_GetNumResults(); - - for (var idx = 0; idx < numResults; ++idx) + for (var idx = 0; idx < EverythingApiDllImport.Everything_GetNumResults(); ++idx) { if (token.IsCancellationRequested) { @@ -157,8 +151,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything Type = EverythingApiDllImport.Everything_IsFolderResult(idx) ? ResultType.Folder : EverythingApiDllImport.Everything_IsFileResult(idx) ? ResultType.File : ResultType.Volume, - Score = (option.SortOption is SortOption.RUN_COUNT_DESCENDING ? (int)EverythingApiDllImport.Everything_GetResultRunCount((uint)idx) : 0) * ScoreScaleFactor, - WindowsIndexed = false + Score = (int)EverythingApiDllImport.Everything_GetResultRunCount( (uint)idx) }; yield return result; @@ -199,7 +192,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything public static async Task IncrementRunCounterAsync(string fileOrFolder) { await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)); - try { _ = EverythingApiDllImport.Everything_IncRunCountFromFileName(fileOrFolder); diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs index 375fd6ce5..51c4c3d9d 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs @@ -29,13 +29,12 @@ namespace Flow.Launcher.Plugin.Explorer.Search public class PathEqualityComparator : IEqualityComparer { private static PathEqualityComparator instance; - public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator(); public bool Equals(Result x, Result y) { return x.Title.Equals(y.Title, StringComparison.OrdinalIgnoreCase) - && string.Equals(x.SubTitle, y.SubTitle, StringComparison.OrdinalIgnoreCase); + && string.Equals(x.SubTitle, y.SubTitle, StringComparison.OrdinalIgnoreCase); } public int GetHashCode(Result obj) @@ -107,10 +106,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search try { - await foreach (var search in searchResults.Select((r, i) => r with - { - Score = -i + 50 - }).WithCancellation(token).ConfigureAwait(false)) + await foreach (var search in searchResults.WithCancellation(token).ConfigureAwait(false)) results.Add(ResultManager.CreateResult(query, search)); } catch (OperationCanceledException)