Powershell GUI fronted (WPF) to run categorized console scripts

⌈⌋ ⎇ branch:  ClickyColoury


W

The W function is quite essential to the GUI building (a simple alias to New-Object System.Windows.Controls.Widget). Hencewhy it departs from the common Verb-Noun scheme in powershell. It's about conciseness.

The W shortcut is for WPF, whereas WF summons WinForms widgets. While WD is an alias to System.Windows.Document.xyz elements.

creating WPF widgets

Creating a WPF button is quite trivial and eased by the parameter definition and method invocation using a single hashtable:

$btn1 = W Button @{Content="Hello"; Add_Click={Write-Host "World"}}
#       ↑    ↑       ↑                 ↑
#      WPF  widget  properties   +   methods
#                   (both in one hash param)

Adding widgets to a parent container:

$w_grd = W Grid @{Add=$w1, $w2, $w3}

Or augment existing form objects:

W $w_btn @{Background="Black"; Foreground="White"}

Perhaps even nesting if you can stand the parentheses/curly braces:

$form = W Window @{Width=800; Height=600; Add=@(
    (W StackPanel @{Orientation="Vertical"; Add=@(
        (W Label @{Content="title"; Foreground="Blue"}),
        (W Button @{Content="Click me"; Background="Red"})
    )})
)}

The syntax gets confusing with too much nesting of course. But widgets could still be created, modified or bound individually obviously.

WinForms widget

The type wrapper WF does the same for WinForms widgets. It's practically even simpler to define complex forms:

$W.FORM = WF Form @{Text="$($meta.title) [$($meta.version)]"; Size=270,220; StartPosition="Manual"; Location=1160,5} -Add (@(
    (WF Button @{Text="From NEW thingy..."; Location=20,20; Size=210,20; ToolTip="..."} -click {Create-ThingFromNew}),
    (WF Label @{Text="Thingy display thingy.."; Location=20,50; Size=140,30}),
    ($W.INPUT = WF Textbox @{Width=25; Location=20,80; Size=140,25} -Click {$W.INPUT.Text = [Windows.Forms.Clipboard]::getText()}),
    (WF Button @{Text="Create for thing"; Location=170,50; Size=60,50; Linebreak=$True} -click {Create-ThingForUser $W.INPUT.Text}),
    (WF Label @{Text="Shortcuts"; Location=20,115; Size=160,15}),
    (WF Button @{Text="Me"; Location=20,130; Size=45,20} -Click {Generate-ThingFromHash $null }),
    (WF Button @{Text="You"; Location=65,130; Size=45,20} -Click {Generate-ThingFromHash $null -Add you}),
    (WF Button @{Text="Her"; Location=110,130; Size=55,20} -Click {Generate-ThingFromHash $null -Add else}),
    (WF Button @{Text="It"; Location=165,130; Size=70,20} -Click {Generate-ThingFromHash $null -Add and}),
    ($W.STATUS = WF StatusBar @{Text=" -> ready."; BorderSize="Raised"; Location=0,150; Size=230,20}),
    (WF Label @{Text="-- More fields --"; FontWeight="Bold"; Location=20,160; Size=140,14})
) + (Add-FormFields))

Of course, http://poshgui.com/ is very very neat for getting started. This same code however might have taken 200 lines with manual instantations (the unshortened version had notebook tabs).