Add file dialoge helpers

This commit is contained in:
Cj 2024-06-17 18:38:06 -04:00
parent a1e94b8c3d
commit a019faa13f
2 changed files with 66 additions and 42 deletions

View File

@ -164,17 +164,12 @@ public partial class ReCodeItForm : Form
private void LoadMappingFileButton_Click(object sender, EventArgs e) private void LoadMappingFileButton_Click(object sender, EventArgs e)
{ {
var fDialog = new OpenFileDialog var result = GUIHelpers.OpenFileDialog("Select a mapping file",
{ "JSON Files (*.json)|*.json|JSONC Files (*.jsonc)|*.jsonc|All Files (*.*)|*.*");
Title = "Select a mapping file",
Filter = "JSON Files (*.json)|*.json|JSONC Files (*.jsonc)|*.jsonc|All Files (*.*)|*.*",
Multiselect = false
};
if (fDialog.ShowDialog() == DialogResult.OK) if (result == string.Empty) { return; }
{
DataProvider.LoadMappingFile(fDialog.FileName); DataProvider.LoadMappingFile(result);
}
RemapTreeView.Nodes.Clear(); RemapTreeView.Nodes.Clear();
@ -186,31 +181,24 @@ public partial class ReCodeItForm : Form
private void PickAssemblyPathButton_Click_1(object sender, EventArgs e) private void PickAssemblyPathButton_Click_1(object sender, EventArgs e)
{ {
OpenFileDialog fDialog = new() var result = GUIHelpers.OpenFileDialog("Select a DLL file",
{ "DLL Files (*.dll)|*.dll|All Files (*.*)|*.*");
Title = "Select a DLL file",
Filter = "DLL Files (*.dll)|*.dll|All Files (*.*)|*.*",
Multiselect = false
};
if (fDialog.ShowDialog() == DialogResult.OK) if (result != string.Empty)
{ {
DataProvider.Settings.Remapper.AssemblyPath = fDialog.FileName; DataProvider.Settings.Remapper.AssemblyPath = result;
TargetAssemblyPath.Text = fDialog.FileName; TargetAssemblyPath.Text = result;
} }
} }
private void OutputDirectoryButton_Click_1(object sender, EventArgs e) private void OutputDirectoryButton_Click_1(object sender, EventArgs e)
{ {
using FolderBrowserDialog fDialog = new(); var result = GUIHelpers.OpenFolderDialog("Select an output directory");
fDialog.Description = "Select a directory"; if (result != string.Empty)
fDialog.ShowNewFolderButton = true;
if (fDialog.ShowDialog() == DialogResult.OK)
{ {
DataProvider.Settings.Remapper.OutputPath = fDialog.SelectedPath; DataProvider.Settings.Remapper.OutputPath = result;
RemapperOutputDirectoryPath.Text = fDialog.SelectedPath; RemapperOutputDirectoryPath.Text = result;
} }
} }
@ -517,32 +505,25 @@ public partial class ReCodeItForm : Form
private void AutoMapperChooseTargetPathButton_Click(object sender, EventArgs e) private void AutoMapperChooseTargetPathButton_Click(object sender, EventArgs e)
{ {
OpenFileDialog fDialog = new() var result = GUIHelpers.OpenFileDialog("Select a DLL file",
{ "DLL Files (*.dll)|*.dll|All Files (*.*)|*.*");
Title = "Select a DLL file",
Filter = "DLL Files (*.dll)|*.dll|All Files (*.*)|*.*",
Multiselect = false
};
if (fDialog.ShowDialog() == DialogResult.OK) if (result != string.Empty)
{ {
DataProvider.Settings.AutoMapper.AssemblyPath = fDialog.FileName; DataProvider.Settings.AutoMapper.AssemblyPath = result;
TargetAssemblyPath.Text = fDialog.FileName; TargetAssemblyPath.Text = result;
DataProvider.SaveAppSettings(); DataProvider.SaveAppSettings();
} }
} }
private void AutoMapperChooseOutpathButton_Click(object sender, EventArgs e) private void AutoMapperChooseOutpathButton_Click(object sender, EventArgs e)
{ {
using FolderBrowserDialog fDialog = new(); var result = GUIHelpers.OpenFolderDialog("Select an output directory");
fDialog.Description = "Select a directory"; if (result != string.Empty)
fDialog.ShowNewFolderButton = true;
if (fDialog.ShowDialog() == DialogResult.OK)
{ {
DataProvider.Settings.AutoMapper.OutputPath = fDialog.SelectedPath; DataProvider.Settings.AutoMapper.OutputPath = result;
RemapperOutputDirectoryPath.Text = fDialog.SelectedPath; RemapperOutputDirectoryPath.Text = result;
DataProvider.SaveAppSettings(); DataProvider.SaveAppSettings();
} }
} }

View File

@ -238,4 +238,47 @@ internal static class GUIHelpers
return tmp; return tmp;
} }
/// <summary>
/// Opens and returns a path from a file dialogue
/// </summary>
/// <param name="title"></param>
/// <param name="filter"></param>
/// <returns>Path if selected, or empty string</returns>
public static string OpenFileDialog(string title, string filter)
{
OpenFileDialog fDialog = new()
{
Title = title,
Filter = filter,
Multiselect = false
};
if (fDialog.ShowDialog() == DialogResult.OK)
{
return fDialog.FileName;
}
return string.Empty;
}
/// <summary>
/// Opens and returns a path from a folder dialogue
/// </summary>
/// <param name="description"></param>
/// <returns></returns>
public static string OpenFolderDialog(string description)
{
using FolderBrowserDialog fDialog = new();
fDialog.Description = description;
fDialog.ShowNewFolderButton = true;
if (fDialog.ShowDialog() == DialogResult.OK)
{
return fDialog.SelectedPath;
}
return string.Empty;
}
} }