AnyBoxPrompt.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
Add-Type -TypeDefinition @"
namespace AnyBox {
    public enum InputType {
        None, Text, FileOpen, FileSave, Checkbox, Password, Date, Link
    };
     
    public enum MessagePosition { Top, Left };
    public enum SetPresentation { ComboBox, Radio, Radio_Wide };
 
    public class Prompt
    {
        public string Name;
        public string Tab;
        public string Group;
        public InputType InputType = InputType.Text;
        public string Message;
        public MessagePosition MessagePosition = MessagePosition.Top;
        public string Alignment;
        public System.UInt16 FontSize;
        public string FontFamily;
        public string FontColor;
        public string DefaultValue;
        public System.UInt16 LineHeight = 1;
        public bool ReadOnly = false;
        public string[] ValidateSet;
        public SetPresentation ShowSetAs = SetPresentation.ComboBox;
        public string RadioGroup;
        public bool ValidateNotEmpty = false;
        public System.Management.Automation.ScriptBlock ValidateScript;
        public bool ShowSeparator = false;
        public bool Collapsible = false;
    }
 
    public class Button
    {
        public string Name;
        public string Text;
        public bool IsCancel = false;
        public bool IsDefault = false;
        public System.Management.Automation.ScriptBlock OnClick;
    }
}
"@


function New-AnyBoxPrompt
{
    [cmdletbinding()]
    param(
        [string]$Message,
        [string]$Name,
        [string]$Tab,
        [string]$Group,
        [ValidateNotNullOrEmpty()]
        [AnyBox.InputType]$InputType = [AnyBox.InputType]::Text,
        [ValidateNotNullOrEmpty()]
        [AnyBox.MessagePosition]$MessagePosition = [AnyBox.MessagePosition]::Top,
        [System.Windows.HorizontalAlignment]$Alignment,
        [UInt16]$FontSize,
        [string]$FontFamily,
        [string]$FontColor,
        [string]$DefaultValue,
        [ValidateScript({$_ -gt 0})]
        [UInt16]$LineHeight = 1,
        [switch]$ReadOnly,
        [switch]$ValidateNotEmpty,
        [string[]]$ValidateSet,
        [AnyBox.SetPresentation]$ShowSetAs = [AnyBox.SetPresentation]::ComboBox,
        [string]$RadioGroup,
        [System.Management.Automation.ScriptBlock]$ValidateScript,
        [switch]$ShowSeparator,
        [switch]$Collapsible
    )

    if ($Name -and $Name -notmatch '^[A-Za-z_]+[A-Za-z0-9_]*$') {
        Write-Warning "Name must start with a letter or the underscore character (_), and must contain only letters, digits, or underscores."
        $Name = $null
    }

    if ($InputType -ne [AnyBox.InputType]::Text)
    {
        if ($InputType -eq [AnyBox.InputType]::None) {
            return($null)
        }

        if ($LineHeight -gt 1) {
            Write-Warning "'-LineHeight' parameter is only valid with text input."
        }

        if ($InputType -eq [AnyBox.InputType]::Checkbox) {
            if (-not $Message) {
                Write-Error "Checkbox input requires a message."
                break
            }
        }
        elseif ($InputType -eq [AnyBox.InputType]::Link) {
            if (-not $Message) {
                Write-Error "Checkbox input requires a message."
                break
            }
            if (-not $FontColor) {
                $FontColor = 'Blue'
            }
        }
        elseif ($InputType -eq [AnyBox.InputType]::Password) {
            if ($DefaultValue) {
                Write-Warning 'Password input does not accept a default value.'
                $DefaultValue = $null
            }
        }
    }
    
    $p = New-Object AnyBox.Prompt

    $p.Name = $Name
    $p.Tab = $Tab
    $p.Group = $Group
    $p.InputType = $InputType
    $p.ReadOnly = $ReadOnly -as [bool]
    $p.Message = $Message
    $p.Alignment = $Alignment
    $p.FontColor = $FontColor
    $p.FontFamily = $FontFamily
    $p.FontSize = $FontSize
    $p.MessagePosition = $MessagePosition
    $p.DefaultValue = $DefaultValue
    $p.LineHeight = $LineHeight
    $p.ValidateNotEmpty = $ValidateNotEmpty -as [bool]
    $p.ValidateSet = $ValidateSet
    $p.ShowSetAs = $ShowSetAs
    $p.RadioGroup = $RadioGroup
    $p.ValidateScript = $ValidateScript
    $p.ShowSeparator = $ShowSeparator -as [bool]
    $p.Collapsible = $Collapsible -as [bool]

    return($p)
}

function New-AnyBoxButton
{
    [cmdletbinding()]
    param(
        [ValidateNotNull()]
        [string]$Text,
        [string]$Name,
        [switch]$IsCancel,
        [switch]$IsDefault,
        [System.Management.Automation.ScriptBlock]$OnClick
    )

    if ($Name -and $Name -notmatch '^[A-Za-z_]+[A-Za-z0-9_]*$') {
        Write-Warning "Name must start with a letter or the underscore character (_), and must contain only letters, digits, or underscores."
        $Name = $null
    }

    $b = New-Object AnyBox.Button

    $b.Name = $Name
    $b.Text = $Text
    $b.IsCancel = $IsCancel -as [bool]
    $b.IsDefault = $IsDefault -as [bool]
    $b.OnClick = $OnClick

    return($b)
}

Set-Alias -Name 'New-Prompt' -Value 'New-AnyBoxPrompt' -Description 'New-AnyBoxPrompt'
Set-Alias -Name 'New-Button' -Value 'New-AnyBoxButton' -Description 'New-AnyBoxButton'