UIfiedBase.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using namespace System.Collections.Generic
using namespace System.Management.Automation.Language

#region UI Type selection

enum UIType {
    WPF
    CF
    Ooui
    MaterialWPF
    MaterialOoui
    MaterialCF
}

class UIConfig {
    static [UIType] $UIType = [UIType]::MaterialWPF
}

function Set-UIType {
    [CmdletBinding(SupportsShouldProcess)]
    param (
        [UIType]   $Type
    )
    if ($PSCmdlet.ShouldProcess('TARGET')) {
        [UIConfig]::UIType = $Type
    }
}

function Set-UICF {
    [CmdletBinding(SupportsShouldProcess)]
    param (
    )
    Set-UIType -Type ([UIType]::CF)
}

function Set-UIWPF {
    [CmdletBinding(SupportsShouldProcess)]
    param (
    )
    Set-UIType -Type ([UIType]::WPF)
}

function Set-UIOoui {
    [CmdletBinding(SupportsShouldProcess)]
    param (
    )
    Set-UIType -Type ([UIType]::Ooui)
}

function Set-UIMaterialWPF {
    [CmdletBinding(SupportsShouldProcess)]
    param (
    )
    Set-UIType -Type ([UIType]::MaterialWPF)
}

function Set-UIMaterialOoui {
    [CmdletBinding(SupportsShouldProcess)]
    param (
    )
    Set-UIType -Type ([UIType]::MaterialOoui)
}

function Set-UIMaterialCF {
    [CmdletBinding(SupportsShouldProcess)]
    param (
    )
    Set-UIType -Type ([UIType]::MaterialCF)
}

#endregion

#region UI Constructs

class UIElement {
    hidden   [ScriptBlock]     $AddNativeUIChild    = { param ([UIElement] $element) }
    hidden   [ScriptBlock]     $RemoveNativeUIChild = { param ([UIElement] $element) }
    hidden   [ScriptBlock]     $ShowError           = { param ([Object]    $errorObject) }
    hidden   [int]             $MaxErrors           = 3
    hidden   [Object]          $NativeUI            = $null
             [List[UIElement]] $Children            = [List[UIElement]]::new()
             [WindowBase]      $Form                = $null
             [UIElement]       $Parent              = $null
             [String]          $Name                = ""

    UIElement() {
    }

    [void] AddChild([UIElement] $element) {
        $this.Children.Add($element)
        $element.SetForm($this.Form)
        $element.SetParent($this)
        Invoke-Command -ScriptBlock $this.AddNativeUIChild -ArgumentList $element
    }

    [void] RemoveChild([UIElement] $element) {
        Invoke-Command -ScriptBlock $this.RemoveNativeUIChild -ArgumentList $element
        $this.Children.Remove($element) | Out-Null
    }

    hidden [void] SetForm([WindowBase] $form) {
        if ($null -ne $form) {
            $this.Form = $form
            $this.Children | ForEach-Object {
                $_.SetForm($form)
                Add-Member -InputObject $_.NativeUI -Name Form -MemberType NoteProperty -Value $form
            }
            if ($this.Name -ne "") {
                $nameMember = Get-Member -InputObject $form -Name $this.Name -MemberType NoteProperty
                if ($null -eq $nameMember) {
                    Add-Member -InputObject $form -Name $this.Name -MemberType NoteProperty -Value $this
                }
            }
        }
    }

    hidden [void] SetParent([UIElement] $parent) {
        if ($null -ne $parent) {
            $this.Parent = $parent
        }
    }

    hidden [void] WrapProperty([String] $elementPropertyName, [String] $nativeUIProperty) {
        $this.WrapProperty($elementPropertyName, $nativeUIProperty, "NativeUI")
    }

    hidden [void] WrapProperty([String] $elementPropertyName, [String] $nativeUIProperty, [String] $nativeUIName) {
        if (-not $this.IsValidName($elementPropertyName) -or -not $this.IsValidName($nativeUIProperty) -or -not $this.IsValidName($nativeUIName)) {
            return
        }
        Add-Member -InputObject $this -Name $elementPropertyName -MemberType ScriptProperty                `
                    -Value          ([ScriptBlock]::Create("`$this.$nativeUIName.$nativeUIProperty"))           `
                    -SecondValue    ([ScriptBlock]::Create("`$this.$nativeUIName.$nativeUIProperty = `$args[0]"))
    }

    hidden [void] WrapNegatedProperty([String] $elementPropertyName, [String] $nativeUIProperty) {
        if (-not $this.IsValidName($elementPropertyName) -or -not $this.IsValidName($nativeUIProperty)) {
            return
        }
        Add-Member -InputObject $this -Name $elementPropertyName -MemberType ScriptProperty                `
                    -Value          ([ScriptBlock]::Create("-not `$this.NativeUI.$nativeUIProperty"))           `
                    -SecondValue    ([ScriptBlock]::Create("`$this.NativeUI.$nativeUIProperty = -not `$args[0]"))
    }

    hidden [void] SetNativeUI([Object] $nativeUI) {
        $this.NativeUI = $nativeUI
        Add-Member -InputObject $NativeUI -Name Control -MemberType NoteProperty -Value $this -Force
        Add-Member -InputObject $this     -Name Control -MemberType NoteProperty -Value $this -Force
    }

    hidden [bool] IsValidScript([ScriptBlock] $scriptBlock) {
        $isValid = $true
        $commands = $scriptBlock.Ast.FindAll({ param ($o) $o -is [CommandAst] }, $true)
        $commands | ForEach-Object {
            $commandName = $_.CommandElements[0].Value
            $command = Get-Command -Name $commandName -ErrorAction SilentlyContinue
            if ($null -eq $command) {
                $isValid = $false
            }
        }
        return $isValid
    }

    hidden [void] AddProperty([String]$propertyName) {
        if (-not $this.IsValidName($propertyName)) {
            return
        }
        $memberName = "_$propertyName"
        Add-Member -InputObject $this -Name $memberName   -MemberType NoteProperty   -Value {}
        Add-Member -InputObject $this -Name $propertyName -MemberType ScriptProperty -Value (
            [ScriptBlock]::Create("`$this.$memberName")
        ) -SecondValue (
            [ScriptBlock]::Create("`$this.$memberName = `$args[0]")
        )
    }

    hidden [void] AddScriptBlockProperty([String]$propertyName) {
        if (-not $this.IsValidName($propertyName)) {
            return
        }
        $memberName = "_$propertyName"
        Add-Member -InputObject $this -Name $memberName   -MemberType NoteProperty   -Value {}
        Add-Member -InputObject $this -Name $propertyName -MemberType ScriptProperty -Value (
            [ScriptBlock]::Create("`$this.$memberName")
        ) -SecondValue (
            [ScriptBlock]::Create("
                    if (`$this.IsValidScript(`$args[0])) {
                       `$this.$memberName = `$args[0]
                    } else {
                        `$this.$memberName = {}
                        `$this.Visible = `$false
                    }
            "
)
        )
    }

    hidden [bool] IsValidName([String]$name) {
        return ($name -match '(^[a-zA-Z_$][a-zA-Z_$0-9]*$)')
    }

    hidden [void] InvokeTrappableCommand([ScriptBlock] $ScriptBlock, [Object[]] $ArgumentList) {
        try {
            Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList
        } catch {
            $Global:SyncHash.Errors += $_
            Invoke-Command -ScriptBlock $this.ShowError -ArgumentList $_
            if ($Global:SyncHash.Errors.Count -gt $this.MaxErrors) {
                $this.Form.Close()
            }
        }
    }

    [void] AddNoteProperty([String]$propertyName, $value) {
        if (-not $this.IsValidName($propertyName)) {
            return
        }
        Add-Member -InputObject $this -Name $propertyName   -MemberType NoteProperty -Value $value
    }
}

class WindowBase : UIElement {

    WindowBase() {
        $this.Form = $this
    }

    [void] Close() {
        $this.NativeUI.Close()
    }
}

class UIHost {

    [void] ShowFrame([WindowBase]$window) {
    }

}

enum Orientation {
    Horizontal
    Vertical
}

class ListItem {
    [List[UIElement]] $Children            = [List[UIElement]]::new()

    ListItem() {
    }

    [void] AddChild([UIElement] $element) {
        $this.Children.Add($element)
    }

    [void] RemoveChild([UIElement] $element) {
        $this.Children.Remove($element)
    }
}

class AutoCompleteItem {
    [String] $Id
    [String] $Text
}

#endregion

#region UI Icons

class IconStrinfy {
    static [bool]  $ShowIcon = $true
    
    hidden static $Icons = @{
        menu                       = [string] [char] 02261 # 62542
        add                        = [string] [char] 62541
        edit                       = [string] [char] 61504
        delete                     = [string] [char] 62606
        clear                      = [string] [char] 62567
        calendar_today             = [string] [char] 62957
        query_builder              = [string] [char] 63055
        left_semi_circle           = [string] [char] 57526
        right_semi_circle          = [string] [char] 57524
        first_page                 = [string] [char] 64255
        chevron_left               = [string] [char] 61523
        chevron_right              = [string] [char] 61524
        chevron_up                 = [string] [char] 61559
        chevron_down               = [string] [char] 61560
        last_page                  = [string] [char] 64256
        radio_button_checked       = [string] [char] 64611
        radio_button_unchecked     = [string] [char] 64612
        check_box                  = [string] [char] 62510
        check_box_outlined_blank   = [string] [char] 64609
        keyboard_arrow_down        = [string] [char] 63035
        keyboard_arrow_up          = [string] [char] 63038
    }

    static [String] ToIconString([String] $kindName) {
        if ([IconStrinfy]::ShowIcon) {
            return [IconStrinfy]::Icons."$kindName"
        } else {
            return ""
        }
    }
}

enum IconPosition {
    Left
    Right
}

#endregion