add filter to file dialog for selecting Python exe

This commit is contained in:
Jeremy 2022-10-25 20:25:04 +11:00
parent 65db4e4141
commit 5263bf50dd
3 changed files with 36 additions and 32 deletions

View file

@ -15,7 +15,7 @@ using System.Windows.Forms;
namespace Flow.Launcher.Core.ExternalPlugins namespace Flow.Launcher.Core.ExternalPlugins
{ {
internal class PluginEnvironment public class PluginEnvironment
{ {
private const string PythonExecutable = "pythonw.exe"; private const string PythonExecutable = "pythonw.exe";
@ -211,6 +211,29 @@ namespace Flow.Launcher.Core.ExternalPlugins
return pluginPairs; 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;
}
}
} }
} }

View file

@ -59,6 +59,7 @@
<system:String x:Key="pythonDirectory">Python Directory</system:String> <system:String x:Key="pythonDirectory">Python Directory</system:String>
<system:String x:Key="nodeFilePath">Node.js Path</system:String> <system:String x:Key="nodeFilePath">Node.js Path</system:String>
<system:String x:Key="selectNodeExecutable">Please select the Node.js executable</system:String> <system:String x:Key="selectNodeExecutable">Please select the Node.js executable</system:String>
<system:String x:Key="selectPythonExecutable">Please select pythonw.exe</system:String>
<system:String x:Key="autoUpdates">Auto Update</system:String> <system:String x:Key="autoUpdates">Auto Update</system:String>
<system:String x:Key="select">Select</system:String> <system:String x:Key="select">Select</system:String>
<system:String x:Key="hideOnStartup">Hide Flow Launcher on startup</system:String> <system:String x:Key="hideOnStartup">Hide Flow Launcher on startup</system:String>

View file

@ -72,45 +72,25 @@ namespace Flow.Launcher
private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e) private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e)
{ {
var dlg = new FolderBrowserDialog var selectedFile = PluginEnvironment.GetFileFromDialog(
{ InternationalizationManager.Instance.GetTranslation("selectPythonExecutable"),
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) "Python|pythonw.exe");
};
var result = dlg.ShowDialog(); if (!string.IsNullOrEmpty(selectedFile))
if (result == System.Windows.Forms.DialogResult.OK)
{ {
string pythonDirectory = dlg.SelectedPath; Constant.PythonPath = selectedFile;
if (!string.IsNullOrEmpty(pythonDirectory)) settings.PluginSettings.PythonDirectory = Constant.PythonPath;
{
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");
}
}
} }
} }
private void OnSelectNodeFilePathClick(object sender, RoutedEventArgs e) private void OnSelectNodeFilePathClick(object sender, RoutedEventArgs e)
{ {
var dlg = new OpenFileDialog var selectedFile = PluginEnvironment.GetFileFromDialog(
{ InternationalizationManager.Instance.GetTranslation("selectNodeExecutable"));
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true,
Title = InternationalizationManager.Instance.GetTranslation("selectNodeExecutable")
};
var result = dlg.ShowDialog(); if (!string.IsNullOrEmpty(selectedFile))
if (result == System.Windows.Forms.DialogResult.OK)
{ {
Constant.NodePath = dlg.FileName; Constant.NodePath = selectedFile;
settings.PluginSettings.NodeFilePath = Constant.NodePath; settings.PluginSettings.NodeFilePath = Constant.NodePath;
} }
} }