/data/test/powershell/97c4c4fac3c0e0d63e70588bb61c6baf4124aff9Show-Window.ps1

https://github.com/aliostad/deep-learning-lang-detection · Powershell · 226 lines · 187 code · 15 blank · 24 comment · 32 complexity · e094957c7ec42f326dee99e629cdf43f MD5 · raw file

  1. function Show-Window {
  2. <#
  3. .Synopsis
  4. Show-Window shows a WPF control within a window,
  5. and is used by the -Show Parameter of all commands within WPK
  6. .Description
  7. Show-Window displays a control within a window and adds several resources to the window
  8. to make several scenarios (like timed events or reusable scripts) easier to accomplish
  9. within the WPF control.
  10. .Parameter Control
  11. The UI Element to display within the window
  12. .Parameter Xaml
  13. The xaml to display within the window
  14. .Parameter WindowProperty
  15. Any additional properties the window should have.
  16. Use the values of this dictionary as you would parameters to New-Window
  17. .Parameter OutputWindowFirst
  18. Outputs the window object just before it is displayed.
  19. This is useful when you need to interact with the window from outside
  20. of the thread displaying it.
  21. .Example
  22. New-Label "Hello World" | Show-Window
  23. #>
  24. [CmdletBinding(DefaultParameterSetName="Window")]
  25. param(
  26. [Parameter(Mandatory=$true,
  27. ValueFromPipeline=$true,
  28. ParameterSetName="Control",
  29. Position=0)]
  30. [Windows.Media.Visual]
  31. $Control,
  32. [Parameter(Mandatory=$true,ParameterSetName="Xaml",ValueFromPipeline=$true,Position=0)]
  33. [xml]
  34. $Xaml,
  35. [Parameter(ParameterSetName='Window',Mandatory=$true,ValueFromPipeline=$true,Position=0)]
  36. [Windows.Window]
  37. $Window,
  38. [Hashtable]
  39. $WindowProperty = @{},
  40. [Parameter(Mandatory=$true,ParameterSetName="ScriptBlock",ValueFromPipeline=$true,Position=0)]
  41. [ScriptBlock]
  42. $ScriptBlock,
  43. [Parameter(ParameterSetName="ScriptBlock")]
  44. [Hashtable]
  45. $ScriptParameter = @{},
  46. [Switch]
  47. $OutputWindowFirst,
  48. [Parameter(ParameterSetName="ScriptBlock")]
  49. [Parameter(ParameterSetName="Xaml")]
  50. [Alias('Async')]
  51. [switch]$AsJob
  52. )
  53. process {
  54. try {
  55. $windowProperty += @{
  56. SizeToContent="WidthAndHeight"
  57. }
  58. } catch {
  59. Write-Debug ($_ | Out-String)
  60. }
  61. switch ($psCmdlet.ParameterSetName) {
  62. Control {
  63. $window = New-Window
  64. Set-WpfProperty -inputObject $window -property $WindowProperty
  65. $window.Content = $Control
  66. $instanceName = $control.Name
  67. $specificWindowTitle = $Control.GetValue([Windows.Window]::TitleProperty)
  68. if ($specificWindowTitle) {
  69. $Window.Title = $specificWindowTitle
  70. } elseif ($instanceName) {
  71. $Window.Title = $instanceName
  72. } else {
  73. $controlName = $Control.GetValue([ShowUI.ShowUISetting]::ControlNameProperty)
  74. if ($controlName) {
  75. $Window.Title = $controlName
  76. }
  77. }
  78. }
  79. Xaml {
  80. if ($AsJob) {
  81. Start-WPFJob -Parameter @{
  82. Xaml = $xaml
  83. WindowProperty = $windowProperty
  84. } -ScriptBlock {
  85. param($Xaml, $windowProperty)
  86. $window = New-Window
  87. Set-WpfProperty -inputObject $window -property $WindowProperty
  88. $strWrite = New-Object IO.StringWriter
  89. $xaml.Save($strWrite)
  90. $Control = [windows.Markup.XamlReader]::Parse("$strWrite")
  91. $window.Content = $Control
  92. Show-Window -Window $window
  93. }
  94. return
  95. } else {
  96. $window = New-Window
  97. Set-WpfProperty -inputObject $window -property $WindowProperty
  98. $strWrite = New-Object IO.StringWriter
  99. $xaml.Save($strWrite)
  100. $Control = [windows.Markup.XamlReader]::Parse("$strWrite")
  101. $window.Content = $Control
  102. }
  103. }
  104. ScriptBlock {
  105. if ($AsJob) {
  106. Start-WPFJob -ScriptBlock {
  107. param($ScriptBlock, $scriptParameter = @{}, $windowProperty)
  108. $window = New-Window
  109. $exception = $null
  110. $results = . $ScriptBlock @scriptParameter 2>&1
  111. $errors = $results | Where-Object { $_ -is [Management.Automation.ErrorRecord] }
  112. if ($errors) {
  113. $window.Content = $errors | Out-String
  114. try {
  115. $windowProperty += @{
  116. FontFamily="Consolas"
  117. Foreground='Red'
  118. }
  119. } catch {
  120. Write-Debug ($_ | Out-String)
  121. }
  122. } else {
  123. if ($results -is [Windows.Media.Visual]) {
  124. $window.Content = $results
  125. } else {
  126. $window.Content = $results | Out-String
  127. try {
  128. $windowProperty += @{
  129. FontFamily="Consolas"
  130. }
  131. } catch {
  132. Write-Debug ($_ | Out-String)
  133. }
  134. }
  135. }
  136. Set-WpfProperty -inputObject $window -property $WindowProperty
  137. Show-Window -Window $window
  138. } -Parameter @{
  139. ScriptBlock = $ScriptBlock
  140. ScriptBlockParameter = $ScriptBlockParameter
  141. WindowProperty = $windowProperty
  142. }
  143. return
  144. } else {
  145. $window = New-Window
  146. $results = & $ScriptBlock @scriptParameter
  147. if ($results -is [Windows.Media.Visual]) {
  148. $window.Content = $results
  149. } else {
  150. $window.Content = $results | Out-String
  151. }
  152. try {
  153. $windowProperty += @{
  154. FontFamily="Consolas"
  155. }
  156. } catch {
  157. Write-Debug ($_ | Out-String)
  158. }
  159. Set-WpfProperty -inputObject $window -property $WindowProperty
  160. }
  161. }
  162. }
  163. $Window.Resources.Timers =
  164. New-Object Collections.Generic.Dictionary["string,Windows.Threading.DispatcherTimer"]
  165. $Window.Resources.TemporaryControls = @{}
  166. $Window.Resources.Scripts =
  167. New-Object Collections.Generic.Dictionary["string,ScriptBlock"]
  168. $Window.add_Closing({
  169. foreach ($timer in $this.Resources.Timers.Values) {
  170. if (-not $timer) { continue }
  171. $null = $timer.Stop()
  172. }
  173. $this |
  174. Get-ChildControl -PeekIntoNestedControl |
  175. Where-Object {
  176. $_.Resources.EventHandlers
  177. } |
  178. ForEach-Object {
  179. $object = $_
  180. $handlerNames = @($_.Resources.EventHandlers.Keys)
  181. foreach ($handler in $handlerNames){
  182. $object."remove_$($handler.Substring(3))".Invoke($object.Resources.EventHandlers[$handler])
  183. $null = $object.Resources.EventHandlers.Remove($handler)
  184. }
  185. $object.Resources.Remove("EventHandlers")
  186. }
  187. })
  188. if ($outputWindowFirst) {
  189. $Window
  190. }
  191. $null = $Window.ShowDialog()
  192. if ($Control.Tag -ne $null) {
  193. $Control.Tag
  194. } elseif ($Window.Tag -ne $null) {
  195. $Window.Tag
  196. } else {
  197. if ($Control.SelectedItems) {
  198. $Control.SelectedItems
  199. }
  200. if ($Control.Text) {
  201. $Control.Text
  202. }
  203. if ($Control.IsChecked) {
  204. $Control.IsChecked
  205. }
  206. }
  207. return
  208. }
  209. }
  210. # Set-Alias Show-BootsWindow Show-Window
  211. # Set-Alias Show-UI Show-Window