examples/02-Form.ps1
|
# 02 - Form: ask for values, get them back as an object, close the window from the script. if (-not (Get-Command Show-NativeWebView -ErrorAction Ignore)) { Import-Module NativeWebView -ErrorAction Stop } $Html = @' <!doctype html> <html><head><style> body { font-family: Verdana, 'Segoe UI', Arial, sans-serif; font-size: 13px; color: #454545; padding: 24px; background: #F9F9F9; } h2 { color: #154363; font-size: 18px; } label { display: block; margin: 12px 0 4px; } input, select { width: 100%; padding: 6px; box-sizing: border-box; border: 1px solid #CCCCCC; font: inherit; } button { margin-top: 18px; padding: 8px 18px; background: #095495; color: white; border: 0; border-radius: 4px; font: inherit; } button:hover { background: #154363; } </style></head> <body> <h2>New user</h2> <label>Name <input id="name" value="Ada Lovelace"></label> <label>Role <select id="role"><option>Reader</option><option selected>Editor</option><option>Owner</option></select></label> <button id="ok">Create</button> <script> document.getElementById("ok").onclick = function () { kHost.send({ action: "submit", name: document.getElementById("name").value, role: document.getElementById("role").value }); }; </script> </body></html> '@ $Result = @{} Show-NativeWebView -Html $Html -Title "New user" -Width 420 -Height 360 -OnMessage { param($Message, $Window) $Data = $Message | ConvertFrom-Json if ($Data.action -eq "submit") { $Result.Name = $Data.name $Result.Role = $Data.role $Window.Close() } }.GetNewClosure() if ($Result.Count -eq 0) { Write-Host "Cancelled (the window was closed)." } else { Write-Host ("Create " + $Result.Name + " as " + $Result.Role) } |