Refactor: Centralize file and folder dialog logic

Introduces a new `Invoke-BrowseAction` helper function to consolidate the creation of file and folder selection dialogs.

This change replaces scattered and repetitive `OpenFileDialog`, `SaveFileDialog`, and `ModernFolderPicker` implementations throughout the event handlers with a single, unified function call. This refactoring simplifies the event handler code, reduces duplication, and improves maintainability.
This commit is contained in:
rbalsleyMSFT
2025-06-20 11:13:58 -07:00
parent ca0a51ec15
commit 86ec6de894
2 changed files with 96 additions and 61 deletions
@@ -740,6 +740,59 @@ function Show-ModernFolderPicker {
return [ModernFolderBrowser]::ShowDialog($Title, [IntPtr]::Zero)
}
function Invoke-BrowseAction {
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Folder', 'OpenFile', 'SaveFile')]
[string]$Type,
[string]$Title,
[string]$Filter,
[string]$InitialDirectory,
[string]$FileName,
[string]$DefaultExt,
[switch]$AllowNewFile
)
switch ($Type) {
'Folder' {
# Show-ModernFolderPicker does not currently support setting an initial directory.
return Show-ModernFolderPicker -Title $Title
}
'OpenFile' {
$dialog = New-Object Microsoft.Win32.OpenFileDialog
$dialog.Title = $Title
if (-not [string]::IsNullOrWhiteSpace($Filter)) { $dialog.Filter = $Filter }
if ($AllowNewFile) { $dialog.CheckFileExists = $false }
if (-not [string]::IsNullOrWhiteSpace($InitialDirectory)) {
$dialog.InitialDirectory = $InitialDirectory
}
if ($dialog.ShowDialog()) {
return $dialog.FileName
}
}
'SaveFile' {
$dialog = New-Object Microsoft.Win32.SaveFileDialog
$dialog.Title = $Title
if (-not [string]::IsNullOrWhiteSpace($Filter)) { $dialog.Filter = $Filter }
if ($AllowNewFile) { $dialog.CheckFileExists = $false } # This property is obsolete but used in existing code.
if (-not [string]::IsNullOrWhiteSpace($InitialDirectory)) {
$dialog.InitialDirectory = $InitialDirectory
}
if (-not [string]::IsNullOrWhiteSpace($FileName)) {
$dialog.FileName = $FileName
}
if (-not [string]::IsNullOrWhiteSpace($DefaultExt)) {
$dialog.DefaultExt = $DefaultExt
}
if ($dialog.ShowDialog()) {
return $dialog.FileName
}
}
}
return $null
}
function Clear-ListViewContent {
[CmdletBinding()]
param(