From 5263bf50dde624cf5a9aba2551aa6bfda55718fb Mon Sep 17 00:00:00 2001 From: Jeremy Date: Tue, 25 Oct 2022 20:25:04 +1100 Subject: [PATCH] add filter to file dialog for selecting Python exe --- .../ExternalPlugins/PluginEnvironment.cs | 27 ++++++++++++- Flow.Launcher/Languages/en.xaml | 1 + Flow.Launcher/SettingWindow.xaml.cs | 40 +++++-------------- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/Flow.Launcher.Core/ExternalPlugins/PluginEnvironment.cs b/Flow.Launcher.Core/ExternalPlugins/PluginEnvironment.cs index da24ad87f..a9a4b7f19 100644 --- a/Flow.Launcher.Core/ExternalPlugins/PluginEnvironment.cs +++ b/Flow.Launcher.Core/ExternalPlugins/PluginEnvironment.cs @@ -15,7 +15,7 @@ using System.Windows.Forms; namespace Flow.Launcher.Core.ExternalPlugins { - internal class PluginEnvironment + public class PluginEnvironment { private const string PythonExecutable = "pythonw.exe"; @@ -211,6 +211,29 @@ namespace Flow.Launcher.Core.ExternalPlugins return pluginPairs; } - + + public static string GetFileFromDialog(string title, string filter="") + { + var dlg = new OpenFileDialog + { + InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), + Multiselect = false, + CheckFileExists = true, + CheckPathExists = true, + Title = title, + Filter = filter + }; + + var result = dlg.ShowDialog(); + if (result == DialogResult.OK) + { + return dlg.FileName; + } + else + { + return string.Empty; + } + } + } } diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 0b7b58e78..c28c43d81 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -59,6 +59,7 @@ Python Directory Node.js Path Please select the Node.js executable + Please select pythonw.exe Auto Update Select Hide Flow Launcher on startup diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index 0ea846304..91244aeed 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -72,45 +72,25 @@ namespace Flow.Launcher private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e) { - var dlg = new FolderBrowserDialog - { - SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) - }; + var selectedFile = PluginEnvironment.GetFileFromDialog( + InternationalizationManager.Instance.GetTranslation("selectPythonExecutable"), + "Python|pythonw.exe"); - var result = dlg.ShowDialog(); - if (result == System.Windows.Forms.DialogResult.OK) + if (!string.IsNullOrEmpty(selectedFile)) { - string pythonDirectory = dlg.SelectedPath; - if (!string.IsNullOrEmpty(pythonDirectory)) - { - var pythonPath = Path.Combine(pythonDirectory, PluginsLoader.PythonExecutable); - if (File.Exists(pythonPath)) - { - settings.PluginSettings.PythonDirectory = pythonDirectory; - } - else - { - MessageBox.Show("Can't find python in given directory"); - } - } + Constant.PythonPath = selectedFile; + settings.PluginSettings.PythonDirectory = Constant.PythonPath; } } private void OnSelectNodeFilePathClick(object sender, RoutedEventArgs e) { - var dlg = new OpenFileDialog - { - InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), - Multiselect = false, - CheckFileExists = true, - CheckPathExists = true, - Title = InternationalizationManager.Instance.GetTranslation("selectNodeExecutable") - }; + var selectedFile = PluginEnvironment.GetFileFromDialog( + InternationalizationManager.Instance.GetTranslation("selectNodeExecutable")); - var result = dlg.ShowDialog(); - if (result == System.Windows.Forms.DialogResult.OK) + if (!string.IsNullOrEmpty(selectedFile)) { - Constant.NodePath = dlg.FileName; + Constant.NodePath = selectedFile; settings.PluginSettings.NodeFilePath = Constant.NodePath; } }