File Selector (#807)

This commit is contained in:
Ottermandias 2022-04-23 16:43:16 +02:00 committed by GitHub
parent 2a36122c2c
commit 8c6b599a07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 32 deletions

View file

@ -115,22 +115,31 @@ namespace Dalamud.Interface.ImGuiFileDialog
/// <summary> /// <summary>
/// Gets the result of the selection. /// Gets the result of the selection.
/// </summary> /// </summary>
/// <param name="separator">The separator to put between multiple selected entries.</param> /// <returns>The result of the selection (file or folder path). If multiple entries were selected, they are separated with commas.</returns>
/// <returns>The result of the selection (file or folder path). If multiple entries were selected, they are separated with the given separator, which is a comma by default.</returns> [Obsolete("Use GetResults() instead.", true)]
public string GetResult(char separator = ',') public string GetResult()
{
return string.Join(',', this.GetResults());
}
/// <summary>
/// Gets the result of the selection.
/// </summary>
/// <returns>The list of selected paths.</returns>
public List<string> GetResults()
{ {
if (!this.flags.HasFlag(ImGuiFileDialogFlags.SelectOnly)) if (!this.flags.HasFlag(ImGuiFileDialogFlags.SelectOnly))
{ {
return this.GetFilePathName(); return new List<string> { this.GetFilePathName() };
} }
if (this.IsDirectoryMode() && this.selectedFileNames.Count == 0) if (this.IsDirectoryMode() && this.selectedFileNames.Count == 0)
{ {
return this.GetFilePathName(); // current directory return new List<string> { this.GetFilePathName() }; // current directory
} }
var fullPaths = this.selectedFileNames.Where(x => !string.IsNullOrEmpty(x)).Select(x => Path.Combine(this.currentPath, x)); var fullPaths = this.selectedFileNames.Where(x => !string.IsNullOrEmpty(x)).Select(x => Path.Combine(this.currentPath, x));
return string.Join(separator, fullPaths.ToArray()); return fullPaths.ToList();
} }
/// <summary> /// <summary>

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace Dalamud.Interface.ImGuiFileDialog namespace Dalamud.Interface.ImGuiFileDialog
{ {
@ -7,21 +8,33 @@ namespace Dalamud.Interface.ImGuiFileDialog
/// </summary> /// </summary>
public class FileDialogManager public class FileDialogManager
{ {
private FileDialog dialog; private FileDialog? dialog;
private Action<bool, string>? callback;
private Action<bool, List<string>>? multiCallback;
private string savedPath = "."; private string savedPath = ".";
private Action<bool, string> callback;
private char selectionSeparator;
/// <summary> /// <summary>
/// Create a dialog which selects an already existing folder. /// Create a dialog which selects an already existing folder.
/// </summary> /// </summary>
/// <param name="title">The header title of the dialog.</param> /// <param name="title">The header title of the dialog.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param> /// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of.</param> public void OpenFolderDialog(string title, Action<bool, string> callback)
/// <param name="isModal">Whether the dialog should be a modal popup.</param>
public void OpenFolderDialog(string title, Action<bool, string> callback, string? startPath = null, bool isModal = false)
{ {
this.SetDialog("OpenFolderDialog", title, string.Empty, startPath ?? this.savedPath, ".", string.Empty, 1, isModal, ImGuiFileDialogFlags.SelectOnly, callback); this.SetCallback(callback);
this.SetDialog("OpenFolderDialog", title, string.Empty, this.savedPath, ".", string.Empty, 1, false, ImGuiFileDialogFlags.SelectOnly);
}
/// <summary>
/// Create a dialog which selects an already existing folder.
/// </summary>
/// <param name="title">The header title of the dialog.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of. The last path this manager was in is used if this is null.</param>
/// <param name="isModal">Whether the dialog should be a modal popup.</param>
public void OpenFolderDialog(string title, Action<bool, string> callback, string? startPath, bool isModal = false)
{
this.SetCallback(callback);
this.SetDialog("OpenFolderDialog", title, string.Empty, startPath ?? this.savedPath, ".", string.Empty, 1, isModal, ImGuiFileDialogFlags.SelectOnly);
} }
/// <summary> /// <summary>
@ -30,34 +43,57 @@ namespace Dalamud.Interface.ImGuiFileDialog
/// <param name="title">The header title of the dialog.</param> /// <param name="title">The header title of the dialog.</param>
/// <param name="defaultFolderName">The default name to use when creating a new folder.</param> /// <param name="defaultFolderName">The default name to use when creating a new folder.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param> /// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of.</param> public void SaveFolderDialog(string title, string defaultFolderName, Action<bool, string> callback)
/// <param name="isModal">Whether the dialog should be a modal popup.</param>
public void SaveFolderDialog(string title, string defaultFolderName, Action<bool, string> callback, string? startPath = null, bool isModal = false)
{ {
this.SetDialog("SaveFolderDialog", title, string.Empty, startPath ?? this.savedPath, defaultFolderName, string.Empty, 1, isModal, ImGuiFileDialogFlags.None, callback); this.SetCallback(callback);
this.SetDialog("SaveFolderDialog", title, string.Empty, this.savedPath, defaultFolderName, string.Empty, 1, false, ImGuiFileDialogFlags.None);
} }
/// <summary> /// <summary>
/// Create a dialog which selects an already existing file. /// Create a dialog which selects an already existing folder or new folder.
/// </summary>
/// <param name="title">The header title of the dialog.</param>
/// <param name="defaultFolderName">The default name to use when creating a new folder.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of. The last path this manager was in is used if this is null.</param>
/// <param name="isModal">Whether the dialog should be a modal popup.</param>
public void SaveFolderDialog(string title, string defaultFolderName, Action<bool, string> callback, string? startPath, bool isModal = false)
{
this.SetCallback(callback);
this.SetDialog("SaveFolderDialog", title, string.Empty, startPath ?? this.savedPath, defaultFolderName, string.Empty, 1, isModal, ImGuiFileDialogFlags.None);
}
/// <summary>
/// Create a dialog which selects a single, already existing file.
/// </summary> /// </summary>
/// <param name="title">The header title of the dialog.</param> /// <param name="title">The header title of the dialog.</param>
/// <param name="filters">Which files to show in the dialog.</param> /// <param name="filters">Which files to show in the dialog.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param> /// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of.</param> public void OpenFileDialog(string title, string filters, Action<bool, string> callback)
{
this.SetCallback(callback);
this.SetDialog("OpenFileDialog", title, filters, this.savedPath, ".", string.Empty, 1, false, ImGuiFileDialogFlags.SelectOnly);
}
/// <summary>
/// Create a dialog which selects already existing files.
/// </summary>
/// <param name="title">The header title of the dialog.</param>
/// <param name="filters">Which files to show in the dialog.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of. The last path this manager was in is used if this is null.</param>
/// <param name="selectionCountMax">The maximum amount of files or directories which can be selected. Set to 0 for an infinite number.</param> /// <param name="selectionCountMax">The maximum amount of files or directories which can be selected. Set to 0 for an infinite number.</param>
/// <param name="selectionSeparator">The separator to put between multiple selected entries.</param>
/// <param name="isModal">Whether the dialog should be a modal popup.</param> /// <param name="isModal">Whether the dialog should be a modal popup.</param>
public void OpenFileDialog( public void OpenFileDialog(
string title, string title,
string filters, string filters,
Action<bool, string> callback, Action<bool, List<string>> callback,
string? startPath = null, string? startPath = null,
int selectionCountMax = 1, int selectionCountMax = 1,
char selectionSeparator = '\0',
bool isModal = false) bool isModal = false)
{ {
this.selectionSeparator = selectionSeparator; this.SetCallback(callback);
this.SetDialog("OpenFileDialog", title, filters, startPath ?? this.savedPath, ".", string.Empty, selectionCountMax, isModal, ImGuiFileDialogFlags.SelectOnly, callback); this.SetDialog("OpenFileDialog", title, filters, startPath ?? this.savedPath, ".", string.Empty, selectionCountMax, isModal, ImGuiFileDialogFlags.SelectOnly);
} }
/// <summary> /// <summary>
@ -68,7 +104,26 @@ namespace Dalamud.Interface.ImGuiFileDialog
/// <param name="defaultFileName">The default name to use when creating a new file.</param> /// <param name="defaultFileName">The default name to use when creating a new file.</param>
/// <param name="defaultExtension">The extension to use when creating a new file.</param> /// <param name="defaultExtension">The extension to use when creating a new file.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param> /// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of.</param> public void SaveFileDialog(
string title,
string filters,
string defaultFileName,
string defaultExtension,
Action<bool, string> callback)
{
this.SetCallback(callback);
this.SetDialog("SaveFileDialog", title, filters, this.savedPath, defaultFileName, defaultExtension, 1, false, ImGuiFileDialogFlags.None);
}
/// <summary>
/// Create a dialog which selects an already existing folder or new file.
/// </summary>
/// <param name="title">The header title of the dialog.</param>
/// <param name="filters">Which files to show in the dialog.</param>
/// <param name="defaultFileName">The default name to use when creating a new file.</param>
/// <param name="defaultExtension">The extension to use when creating a new file.</param>
/// <param name="callback">The action to execute when the dialog is finished.</param>
/// <param name="startPath">The directory which the dialog should start inside of. The last path this manager was in is used if this is null.</param>
/// <param name="isModal">Whether the dialog should be a modal popup.</param> /// <param name="isModal">Whether the dialog should be a modal popup.</param>
public void SaveFileDialog( public void SaveFileDialog(
string title, string title,
@ -76,10 +131,11 @@ namespace Dalamud.Interface.ImGuiFileDialog
string defaultFileName, string defaultFileName,
string defaultExtension, string defaultExtension,
Action<bool, string> callback, Action<bool, string> callback,
string? startPath = null, string? startPath,
bool isModal = false) bool isModal = false)
{ {
this.SetDialog("SaveFileDialog", title, filters, startPath ?? this.savedPath, defaultFileName, defaultExtension, 1, isModal, ImGuiFileDialogFlags.None, callback); this.SetCallback(callback);
this.SetDialog("SaveFileDialog", title, filters, startPath ?? this.savedPath, defaultFileName, defaultExtension, 1, isModal, ImGuiFileDialogFlags.None);
} }
/// <summary> /// <summary>
@ -90,7 +146,10 @@ namespace Dalamud.Interface.ImGuiFileDialog
if (this.dialog == null) return; if (this.dialog == null) return;
if (this.dialog.Draw()) if (this.dialog.Draw())
{ {
this.callback(this.dialog.GetIsOk(), this.dialog.GetResult(this.selectionSeparator)); var isOk = this.dialog.GetIsOk();
var results = this.dialog.GetResults();
this.callback?.Invoke(isOk, results.Count > 0 ? results[0] : string.Empty);
this.multiCallback?.Invoke(isOk, results);
this.savedPath = this.dialog.GetCurrentPath(); this.savedPath = this.dialog.GetCurrentPath();
this.Reset(); this.Reset();
} }
@ -104,7 +163,19 @@ namespace Dalamud.Interface.ImGuiFileDialog
this.dialog?.Hide(); this.dialog?.Hide();
this.dialog = null; this.dialog = null;
this.callback = null; this.callback = null;
this.selectionSeparator = '\0'; this.multiCallback = null;
}
private void SetCallback(Action<bool, string> action)
{
this.callback = action;
this.multiCallback = null;
}
private void SetCallback(Action<bool, List<string>> action)
{
this.multiCallback = action;
this.callback = null;
} }
private void SetDialog( private void SetDialog(
@ -116,11 +187,9 @@ namespace Dalamud.Interface.ImGuiFileDialog
string defaultExtension, string defaultExtension,
int selectionCountMax, int selectionCountMax,
bool isModal, bool isModal,
ImGuiFileDialogFlags flags, ImGuiFileDialogFlags flags)
Action<bool, string> callback)
{ {
this.Reset(); this.Reset();
this.callback = callback;
this.dialog = new FileDialog(id, title, filters, path, defaultFileName, defaultExtension, selectionCountMax, isModal, flags); this.dialog = new FileDialog(id, title, filters, path, defaultFileName, defaultExtension, selectionCountMax, isModal, flags);
this.dialog.Show(); this.dialog.Show();
} }