Merge pull request #2616 from Glorou/FileOnSelect

Add a file selection changed event to FileDialog and it's manager
This commit is contained in:
goat 2026-02-12 20:48:39 +01:00 committed by GitHub
commit c4faf84a2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View file

@ -657,6 +657,7 @@ public partial class FileDialog
this.fileNameBuffer = $"{this.selectedFileNames.Count} files Selected"; this.fileNameBuffer = $"{this.selectedFileNames.Count} files Selected";
} }
this.SelectionChanged(this, this.GetFilePathName());
if (setLastSelection) if (setLastSelection)
{ {
this.lastSelectedFileName = name; this.lastSelectedFileName = name;

View file

@ -97,6 +97,8 @@ public partial class FileDialog
this.SetupSideBar(); this.SetupSideBar();
} }
public event EventHandler<string>? SelectionChanged;
/// <summary> /// <summary>
/// Shows the dialog. /// Shows the dialog.
/// </summary> /// </summary>

View file

@ -30,6 +30,12 @@ public class FileDialogManager
private Action<bool, List<string>>? multiCallback; private Action<bool, List<string>>? multiCallback;
private string savedPath = "."; private string savedPath = ".";
/// <summary>
/// Event fires when a new file is selected by the user
/// </summary>
/// <returns>Returns the path of the file as a string</returns>
public event EventHandler<string>? SelectionChanged;
/// <summary> /// <summary>
/// Create a dialog which selects an already existing folder. /// Create a dialog which selects an already existing folder.
/// </summary> /// </summary>
@ -175,6 +181,8 @@ public class FileDialogManager
this.multiCallback = null; this.multiCallback = null;
} }
private void OnSelectionChange(object sender, string path) => this.SelectionChanged?.Invoke(sender, path);
private void SetDialog( private void SetDialog(
string id, string id,
string title, string title,
@ -200,6 +208,7 @@ public class FileDialogManager
if (this.dialog is not null) if (this.dialog is not null)
{ {
this.dialog.SortOrderChanged -= this.OnSortOrderChange; this.dialog.SortOrderChanged -= this.OnSortOrderChange;
this.dialog.SelectionChanged -= this.OnSelectionChange;
} }
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);
@ -217,6 +226,7 @@ public class FileDialogManager
} }
this.dialog.SortOrderChanged += this.OnSortOrderChange; this.dialog.SortOrderChanged += this.OnSortOrderChange;
this.dialog.SelectionChanged += this.OnSelectionChange;
this.dialog.WindowFlags |= this.AddedWindowFlags; this.dialog.WindowFlags |= this.AddedWindowFlags;
foreach (var (name, location, icon, position) in this.CustomSideBarItems) foreach (var (name, location, icon, position) in this.CustomSideBarItems)
this.dialog.SetQuickAccess(name, location, icon, position); this.dialog.SetQuickAccess(name, location, icon, position);