Private/UI/New-EwWorkbenchLibraryRegistrationDialog.ps1
|
Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' function New-EwWorkbenchLibraryRegistrationDialog { [CmdletBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Constructs a registration dialog without saving Library changes.')] [OutputType([object])] param( [Parameter(Mandatory)][System.Windows.Window]$Owner, [Parameter(Mandatory)][psobject]$Plan, [Parameter()][string]$InitialName, [Parameter()][string]$ValidationError ) $module = $ExecutionContext.SessionState.Module $xamlPath = Join-Path -Path (Split-Path -Path $script:ewModulePath -Parent) -ChildPath 'Resources\LibraryRegistrationDialog.xaml' Write-Verbose -Message "EvergreenUI: Loading Library registration choices from $xamlPath" $reader = [Xml.XmlReader]::Create($xamlPath) try { $dialog = [Windows.Markup.XamlReader]::Load($reader) } finally { $reader.Dispose() } if ($dialog -isnot [Windows.Window]) { throw "Registration XAML did not create a WPF Window: $xamlPath" } $dialog.Owner = $Owner $dialog.ThemeMode = $Owner.ThemeMode $dialog.FontFamily = $Owner.FontFamily $dialog.FontSize = [Math]::Max(14, $Owner.FontSize) if ($Owner.ActualHeight -gt 0) { $dialog.MaxHeight = [Math]::Max($dialog.MinHeight, [Math]::Min(720, $Owner.ActualHeight - 64)) } if ($Owner.ActualWidth -gt 0) { $dialog.MaxWidth = [Math]::Max($dialog.MinWidth, [Math]::Min(680, $Owner.ActualWidth - 64)) } $dialog.FindName('RegistrationApplicationText').Text = "Evergreen Application: $($Plan.EvergreenApp)" $dialog.FindName('RegistrationLibraryText').Text = "Library: $($Plan.Path)" $dialog.FindName('RequestedRegistrationFilterText').Text = $Plan.Filter $existing = $dialog.FindName('ExistingRegistrationCombo') $existingFilter = $dialog.FindName('ExistingRegistrationFilterText') $replaceButton = $dialog.FindName('ReplaceLibraryRegistrationButton') $addButton = $dialog.FindName('AddNamedLibraryRegistrationButton') $cancelButton = $dialog.FindName('CancelLibraryRegistrationButton') $namePanel = $dialog.FindName('NewRegistrationNamePanel') $nameBox = $dialog.FindName('NewRegistrationNameBox') $folderText = $dialog.FindName('NewRegistrationFolderText') $validationText = $dialog.FindName('RegistrationValidationText') $occupied = [Collections.Generic.HashSet[string]]::new([string[]]$Plan.OccupiedNames, [StringComparer]::OrdinalIgnoreCase) $existing.ItemsSource = [object[]]@($Plan.Registrations) $existing.IsEnabled = @($Plan.Registrations).Count -gt 0 $existingFilter.Text = 'Select a registration to inspect or replace.' $existing.Add_SelectionChanged({ $selected = $existing.SelectedItem $replaceButton.IsEnabled = $null -ne $selected $existingFilter.Text = if ($null -ne $selected) { [string]$selected.Filter } else { 'Select a registration to inspect or replace.' } }.GetNewClosure()) if (@($Plan.Registrations).Count -eq 1) { $existing.SelectedIndex = 0 } $validateName = { $value = [string]$nameBox.Text $errorMessage = & $module { param($Value) Get-EwLibraryRegistrationNameError -Name $Value } $value if (-not $errorMessage -and $occupied.Contains($value)) { $errorMessage = "The name '$value' is already used by a Library registration, file or folder. Choose a different name." } $folderText.Text = "New Library folder: $($Plan.Path)\$value" $validationText.Text = $errorMessage if ($namePanel.Visibility -eq [Windows.Visibility]::Visible) { $addButton.IsEnabled = [string]::IsNullOrEmpty($errorMessage) } }.GetNewClosure() $nameBox.Add_TextChanged({ & $validateName }.GetNewClosure()) $nameBox.Text = if ($InitialName) { $InitialName } else { $Plan.SuggestedName } if ($InitialName) { $namePanel.Visibility = [Windows.Visibility]::Visible $addButton.Content = 'Save new application' & $validateName } if ($ValidationError) { $validationText.Text = $ValidationError } $addButton.Add_Click({ if ($namePanel.Visibility -ne [Windows.Visibility]::Visible) { $namePanel.Visibility = [Windows.Visibility]::Visible $addButton.Content = 'Save new application' & $validateName $null = $nameBox.Focus() $nameBox.SelectAll() return } & $validateName if (-not $addButton.IsEnabled) { return } $dialog.Tag = [PSCustomObject]@{ Action = 'Add'; RegistrationName = [string]$nameBox.Text } $dialog.DialogResult = $true }.GetNewClosure()) $replaceButton.Add_Click({ if ($null -eq $existing.SelectedItem) { return } $dialog.Tag = [PSCustomObject]@{ Action = 'Replace'; RegistrationName = [string]$existing.SelectedItem.Name } $dialog.DialogResult = $true }.GetNewClosure()) $dialog.Add_ContentRendered({ $null = $cancelButton.Focus() }.GetNewClosure()) Initialize-EwWorkbenchWindowAppearance -Window $dialog Initialize-EwWorkbenchDialogChrome -Window $dialog return $dialog } |