# encoding: ascii
# api: powershell
# title: New-ISEMenu
# description: This is a little tweak of Ravikanth’s Script to generate a dialog for adding menu items in PowerShell ISE.  All I did was just clean it up a little bit following a few guidelines:
# version: 0.1
# type: module
# author: Joel Bennett
# license: CC0
# function: New-ISEMenu
# x-poshcode-id: 2800
# x-derived-from-id: 2801
# x-archived: 2016-10-04T02:40:39
# x-published: 2011-07-18T13:25:00
#
# Use WPF’s layout abilities: 
# When you want data input layout with labels and fields, use a grid and specify column and row on the child controls.
# Learn to use stackpanels or dockpanels too ;)
# Use margins to add whitespace around controls, not to position them
# Nest panels as needed, nested panels are almost always more flexible than hard-coded top/left (or margins)
# Use -MinHeight and -MinWidth to specify a minimum starting size, without limiting growth
# When using grids:
# Use a star width on one column and one row (either * or ##* with any number) to specify where the growth occurs (try resizing this version of the New-ISEMenu script, vs. the original)
# Use “Auto” on other columns and rows unless you need to force a specific size
# You should never have to specify the -Content parameter name in ShowUI
# On buttons and menus (see note below about labels), a “_” will mark the next letter as the hotkey. E.g.: “_Save” is a great label for the save button, and makes Alt+S save.
# Most boolean properties are switch parameters (and you only need to supply a value if the property defaults true, and you’re trying to turn it off)
# Button has an -IsDefault and -IsCancel … use them both, when appropriate
# Use -On_Loaded to set focus where you want it :-)
# NOTE: You can also set hotkeys with an underscore on Label controls, but you have to be sure to set the -Target too.  This can be a bit tricky. The simplest thing is to create the target control first, and then the label — which is easy to do when you’re using a grid, because the order of the controls doesn’t matter when you specify the Column parameter. Also, you can use Tee-Object to set the variable and output the control instead of the parentheses trick.
#
Import-Module ShowUI
Function New-ISEMenu {
   New-Grid -AllowDrop:$true -Name "ISEAddonCreator" -columns Auto, * -rows Auto,Auto,Auto,*,Auto,Auto -Margin 5 {
      New-Label -Name Warning -Foreground Red -FontWeight Bold -Column 1
      ($target = New-TextBox -Name txtName -Column 1 -Row ($Row=1))
      New-Label "Addon Menu _Name" -Target $target  -Row $Row
      ($target = New-TextBox -Name txtShortcut -Column 1 -Row ($Row+=1))
      New-Label "Shortcut _Key" -Row $Row -Target $target
      ($target = New-TextBox -Name txtScriptBlock  -Column 1 -Row ($Row+=1) -MinHeight 141 -MinWidth 336 -AcceptsReturn:$true -HorizontalScrollBarVisibility Auto -VerticalScrollBarVisibility Auto)
      New-Label "Script _Block" -Row $Row -Target $target
      New-CheckBox "_Add to ISE Profile" -Name chkProfile -Row ($Row+=1)
      New-StackPanel -Orientation Horizontal  -Column 1 -Row ($Row+=1) -HorizontalAlignment Right {
         New-Button "_Save" -Name btnSave -Width 75 -Margin "0,0,5,0" -IsDefault -On_Click {
            if ($txtName.Text.Trim() -eq "" -or $txtShortcut.text.Trim() -eq "" -or $txtScriptBlock.text.Trim() -eq "") {
                $Warning.Content = "You must supply all parameters"
            } else {
               $menuItems = $psise.CurrentPowerShellTab.AddOnsMenu.Submenus | Select -ExpandProperty DisplayName
               if ($menuItems -Contains $txtName.Text) {
                  $Warning.Content = "Select another Name for the menu"
                  return
               }            
               
               try {
                   $ScriptBlock = [ScriptBlock]::Create($txtScriptBlock.Text)
                   $psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add($txtName.Text,$ScriptBlock,$txtShortcut.Text) | Out-Null
               }
               catch {
                  $Warning.Content = "Fatal Error Creating MenuItem:`n$_"
                  return
               }
               if ($chkProfile.IsChecked) {
                  $profileText = "`n`#Added by ISE Menu Creator Addon if (`$psISE) { `$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add(`"$($txtName.Text)`",`{$ScriptBlock`},`"$($txtShortcut.Text)`") | Out-Null } "
                  Add-Content -Path $profile -Value $profileText
               }
               $window.Close()
            }
         }
         New-Button "Cancel" -Name btnCancel -Width 75 -IsCancel
      }
   } -show -On_Load { $txtName.Focus() }
}
# this will add a the "New ISE menu" menu item and load it every time you run this script!
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("New ISE menu",{New-ISEMenu},"ALT+M") | Out-Null
