Public/Select/Select-ISETheme.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
function Select-ISETheme {

[cmdletbinding()]
Param()
    #========================================================================
    # Code Generated By: SAPIEN Technologies, Inc., PowerShell Studio 2014 v4.1.46
    # Generated On: 3/18/2014 10:19 AM
    # Generated By: Jeff Pollock
    #========================================================================
        <#
     .SYNOPSIS
      Selects and applies an ISE theme from the registry.
 
  .DESCRIPTION
   Selects and applies an ISE theme from the registry.
 
  .EXAMPLE
   PS C:\> Select-ISETheme
 
            Selects an ISE theme from the registry
 
  .NOTES
            Part of the ISEColorThemeCmdlets.ps1 Script by Jeff Pollock
            http://Lifeinpowerhsell.blogspot.com
            http://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e
 #>

    #----------------------------------------------
    #region Application Functions
    #----------------------------------------------

    function OnApplicationLoad {
    return $true #return true for success or false for failure
    }

    function OnApplicationExit {
    $script:ExitCode = 0 #Set the exit code for the Packager
    }

    #endregion Application Functions

    #----------------------------------------------
    # Generated Form Function
    #----------------------------------------------
    function Start-ThemeSelector {

    #----------------------------------------------
    #region Import the Assemblies
    #----------------------------------------------
    [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    #endregion Import Assemblies

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $formISEThemeSelector = New-Object 'System.Windows.Forms.Form'
        $buttonExit = New-Object 'System.Windows.Forms.Button'
    $buttonSelectTheme = New-Object 'System.Windows.Forms.Button'
    $listboxThemes = New-Object 'System.Windows.Forms.ListBox'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
        $formISEThemeSelector.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path) 
    #endregion Generated Form Objects

    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------
    $buttonSelectTheme_Click={
            Set-ISETheme -ThemeName $listboxThemes.SelectedItem
            $formISEThemeSelector.Close()
        }

        $formISEThemeSelector_KeyDown={
            if ($_.KeyCode -eq 'Enter') {Write-Output 'Inside';$buttonSelectTheme_Click}
        }

        $listboxThemes_KeyDown={
            if ($_.KeyCode -eq 'Enter') {
                Set-ISETheme -ThemeName $listboxThemes.SelectedItem
                $formISEThemeSelector.Close()
            }
        }
        
        $buttonExit_Click={$formISEThemeSelector.Close()}

    $formISEThemeSelector_Load={
    #TODO: Initialize Form Controls here
            $Themes = Get-ImportedISETheme | Select-Object  ThemeName
            $Themes | ForEach-Object {
                Set-ListBox $listboxThemes $_.ThemeName -Append
            }
    }

    #region Control Helper Functions
    function Set-ListBox 
    {
    <#
      .SYNOPSIS
       This functions helps you load items into a ListBox or CheckedListBox.
  
      .DESCRIPTION
       Use this function to dynamically load items into the ListBox control.
  
      .PARAMETER ListBox
       The ListBox control you want to add items to.
  
      .PARAMETER Items
       The object or objects you wish to load into the ListBox's Items collection.
  
      .PARAMETER DisplayMember
       Indicates the property to display for the items in this control.
   
      .PARAMETER Append
       Adds the item(s) to the ListBox without clearing the Items collection.
   
      .EXAMPLE
       Set-ListBox $ListBox1 "Red", "White", "Blue"
   
      .EXAMPLE
       Set-ListBox $listBox1 "Red" -Append
       Set-ListBox $listBox1 "White" -Append
       Set-ListBox $listBox1 "Blue" -Append
   
      .EXAMPLE
       Set-ListBox $listBox1 (Get-Process) "ProcessName"
     #>

        [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions','')]
        param (
    [ValidateNotNull()]
    [Parameter(Mandatory=$true)]
    [System.Windows.Forms.ListBox]$ListBox,
    [ValidateNotNull()]
    [Parameter(Mandatory=$true)]
    $Items,
        [Parameter(Mandatory=$false)]
    [string]$DisplayMember,
    [switch]$Append
    )

    if(-not $Append)
    {
    $listBox.Items.Clear()
    }

    if($Items -is [System.Windows.Forms.ListBox+ObjectCollection])
    {
    $listBox.Items.AddRange($Items)
    }
    elseif ($Items -is [Array])
    {
    $listBox.BeginUpdate()
    foreach($obj in $Items)
    {
    $listBox.Items.Add($obj)
    }
    $listBox.EndUpdate()
    }
    else
    {
    $listBox.Items.Add($Items)
    }

    $listBox.DisplayMember = $DisplayMember
    }
    #endregion

    # --End User Generated Script--
    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------

    $Form_StateCorrection_Load=
    {
    #Correct the initial state of the form to prevent the .Net maximized form issue
    $formISEThemeSelector.WindowState = $InitialFormWindowState
    }

    $Form_Cleanup_FormClosed=
    {
    #Remove all event handlers from the controls
    try
    {
    $formISEThemeSelector.remove_Load($formISEThemeSelector_Load)
    $formISEThemeSelector.remove_Load($Form_StateCorrection_Load)
    $formISEThemeSelector.remove_FormClosed($Form_Cleanup_FormClosed)
    }
    catch [Exception]
    {$null }
    }
    #endregion Generated Events

    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $formISEThemeSelector.SuspendLayout()
    #
    # formISEThemeSelector
    #
    $formISEThemeSelector.Controls.Add($buttonExit)
        $formISEThemeSelector.Controls.Add($buttonSelectTheme)
    $formISEThemeSelector.Controls.Add($listboxThemes)
    $formISEThemeSelector.ClientSize = '228, 278'
        $formISEThemeSelector.FormBorderStyle = 'None'
    $formISEThemeSelector.Name = 'formISEThemeSelector'
    $formISEThemeSelector.Text = 'Theme Selector'
    $formISEThemeSelector.add_Load($formISEThemeSelector_Load)
        $formISEThemeSelector.StartPosition = 'CenterScreen'
        $formISEThemeSelector.MinimizeBox = $False
        $formISEThemeSelector.MaximizeBox = $False
        $formISEThemeSelector.BackColor =  $PopupColor
        $formISEThemeSelector.Add_KeyDown($formISEThemeSelector_KeyDown) 
        #
    # buttonExit
    #
    $buttonExit.Location = '117, 233'
    $buttonExit.Name = 'buttonExit'
    $buttonExit.Size = '96, 30'
    $buttonExit.TabIndex = 2
    $buttonExit.Text = 'E&xit'
    $buttonExit.UseVisualStyleBackColor = $True
        $buttonExit.add_Click($buttonExit_Click)
    #
    # buttonSelectTheme
    #
    $buttonSelectTheme.Location = '14, 233'
    $buttonSelectTheme.Name = 'buttonSelectTheme'
    $buttonSelectTheme.Size = '96, 30'
    $buttonSelectTheme.TabIndex = 1
    $buttonSelectTheme.Text = '&Select'
    $buttonSelectTheme.UseVisualStyleBackColor = $True
        $buttonSelectTheme.add_Click($buttonSelectTheme_Click)
    #
    # listboxThemes
    #
        $listboxThemes.Font = 'Microsoft Sans Serif, 12pt'     
        $listboxThemes.FormattingEnabled = $True
    $listboxThemes.ItemHeight = 17
    $listboxThemes.Location = '14, 15'
    $listboxThemes.Name = 'listboxThemes'
    $listboxThemes.Size = '199, 220'
    $listboxThemes.TabIndex = 0
        $listboxThemes.add_KeyDown($listboxThemes_KeyDown)


    $formISEThemeSelector.ResumeLayout($false)

    #endregion Generated Form Code

    #----------------------------------------------

    #Save the initial state of the form
    $InitialFormWindowState = $formISEThemeSelector.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $formISEThemeSelector.add_Load($Form_StateCorrection_Load)
    #Clean up the control events
    $formISEThemeSelector.add_FormClosed($Form_Cleanup_FormClosed)
    #Show the Form
    return $formISEThemeSelector.ShowDialog()

    } #End Function

    #Call OnApplicationLoad to initialize
    if((OnApplicationLoad) -eq $true)
    {
        $BackgroundColor = $psISE.Options.ScriptPaneBackgroundColor
        IF ($BackgroundColor.A -lt 235) {$BackgroundColor.A = $BackgroundColor.A + 20}
        IF ($BackgroundColor.R -lt 235) {$BackgroundColor.R = $BackgroundColor.R + 20}
        IF ($BackgroundColor.G -lt 235) {$BackgroundColor.G = $BackgroundColor.G + 20}
        IF ($BackgroundColor.B -lt 235) {$BackgroundColor.B = $BackgroundColor.B + 20}
        $PopupColor = Convert-ARGBToHex "$($BackgroundColor.A),$($BackgroundColor.R),$($BackgroundColor.G),$($BackgroundColor.B)"
    #Call the form
    Start-ThemeSelector | Out-Null
    #Perform cleanup
    OnApplicationExit
    }
    

}