From 13a8f82bfe5b8ef9d7bdac59606b1b886e8c90e7 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 15 Oct 2022 21:24:02 +0800 Subject: [PATCH] Add PATH environment variable to program source --- .../Programs/Win32.cs | 41 +++++++++++++++++++ .../Flow.Launcher.Plugin.Program/Settings.cs | 3 +- .../Views/ProgramSetting.xaml | 9 ++++ .../Views/ProgramSetting.xaml.cs | 14 ++++++- 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs b/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs index 6a8b232e9..cb467f740 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs @@ -17,6 +17,7 @@ using System.Diagnostics; using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch; using System.Diagnostics.CodeAnalysis; using System.Threading.Channels; +using System.Collections.ObjectModel; namespace Flow.Launcher.Plugin.Program.Programs { @@ -275,6 +276,14 @@ namespace Flow.Launcher.Plugin.Program.Programs program.Valid = false; return program; } + catch (FileNotFoundException e) + { + ProgramLogger.LogException($"|Win32|LnkProgram|{path}" + + "|An unexpected error occurred in the calling method LnkProgram", e); + + program.Valid = false; + return program; + } #if !DEBUG //Only do a catch all in production. This is so make developer aware of any unhandled exception and add the exception handling in. catch (Exception e) { @@ -370,6 +379,32 @@ namespace Flow.Launcher.Plugin.Program.Programs return programs; } + private static IEnumerable PATHPrograms(string[] suffixes) + { + var disabledProgramsList = Main._settings.DisabledProgramSources; + + string? pathEnv = Environment.GetEnvironmentVariable("Path"); + if (String.IsNullOrEmpty(pathEnv)) { + return Array.Empty(); + } + + var toFilter = new List(); + var paths = pathEnv.Split(";", StringSplitOptions.RemoveEmptyEntries).ToList().ConvertAll(x => x.ToLower()).Distinct(); + + foreach (var path in paths) + { + var p = ProgramPaths(path, suffixes); + toFilter.AddRange(p); + } + var programs = ExceptDisabledSource(toFilter.Distinct()) + .Select(x => Extension(x) switch + { + ShortcutExtension => LnkProgram(x), + _ => Win32Program(x) + }).Where(x => x.Valid); + return programs; + } + private static IEnumerable AppPathsPrograms(string[] suffixes) { // https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121 @@ -522,6 +557,12 @@ namespace Flow.Launcher.Plugin.Program.Programs autoIndexPrograms = autoIndexPrograms.Concat(startMenu); } + if (settings.EnablePATHSource) + { + var path = PATHPrograms(settings.ProgramSuffixes); + autoIndexPrograms = autoIndexPrograms.Concat(path); + } + autoIndexPrograms = ProgramsHasher(autoIndexPrograms); return programs.Concat(autoIndexPrograms).Distinct().ToArray(); diff --git a/Plugins/Flow.Launcher.Plugin.Program/Settings.cs b/Plugins/Flow.Launcher.Plugin.Program/Settings.cs index d97ddd993..09e135d9b 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Settings.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Settings.cs @@ -12,10 +12,11 @@ namespace Flow.Launcher.Plugin.Program public string[] ProgramSuffixes { get; set; } = {"appref-ms", "exe", "lnk"}; public bool EnableStartMenuSource { get; set; } = true; - public bool EnableDescription { get; set; } = false; public bool HideAppsPath { get; set; } = true; public bool EnableRegistrySource { get; set; } = true; + public bool EnablePATHSource { get; set; } = false; + public string CustomizedExplorer { get; set; } = Explorer; public string CustomizedArgs { get; set; } = ExplorerArgs; diff --git a/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml b/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml index f07879465..1095eaf45 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml +++ b/Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml @@ -54,10 +54,19 @@ IsChecked="{Binding EnableDescription}" ToolTip="{DynamicResource flowlauncher_plugin_program_enable_description_tooltip}" /> + + + + _settings.EnablePATHSource; + set + { + _settings.EnablePATHSource = value; + ReIndexing(); + } + } + public string CustomizedExplorerPath { get => _settings.CustomizedExplorer; @@ -360,4 +370,4 @@ namespace Flow.Launcher.Plugin.Program.Views } } } -} \ No newline at end of file +}