UIfiedMaterialWPFBase.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using namespace System.Collections.Generic
using namespace System.Windows
using namespace System.Windows.Controls
using namespace MaterialDesignThemes.Wpf

class MaterialWPFHost : WPFHost {
}

class MaterialWPFWindow : WPFWindow {

    [void] StyleApplication() {
        $uris =
        "/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml",
        "/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml",
        "/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml",
        "/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml"

        $uris | ForEach-Object {
            $resourceDictionary = [System.Windows.ResourceDictionary] [System.Windows.Application]::LoadComponent([Uri]::new($_, [System.UriKind]::Relative))
            $this.Application.Resources.MergedDictionaries.Add($resourceDictionary)
        }
    }

    [void] StyleComponents() {
        $this.windowNativeUI.FontFamily = "MaterialDesignFont"
        $this.windowNativeUI.SetResourceReference([Control]::BackgroundProperty, "MaterialDesignPaper")
    }
}

class MaterialWPFStackPanel : WPFStackPanel {
}

class MaterialWPFIcon : WPFElement {
    hidden [String]  $KindName
    hidden           $TextInfo

    MaterialWPFIcon() {
        $this.TextInfo = ([System.Globalization.CultureInfo]::new("en-US",$false)).TextInfo
        $this.NativeUI = [MaterialDesignThemes.Wpf.PackIcon]::new()
        Add-Member -InputObject $this -Name Kind -MemberType ScriptProperty -Value {
            $this.KindName
        } -SecondValue {
            $this.KindName = $args[0]
            $this.NativeUI.Kind = $this.TextInfo.ToTitleCase($this.KindName).Replace("_", [String]::Empty)
        }
    }
}

class MaterialWPFLabel : WPFLabel {}

class MaterialWPFButton : WPFElement {
    hidden  [stackpanel]         $StackPanel    = [StackPanel]::new()
    hidden  [MaterialWPFIcon]    $CurrentIcon   = $null
    hidden  [String]             $CaptionText   = ""
            [bool]               $RightIcon     = $false

    MaterialWPFButton() {
        $this.SetNativeUI([Button]::new())
        $this.NativeUI.SetResourceReference([Control]::StyleProperty, "MaterialDesignRaisedButton")
        $this.StackPanel.Orientation = [Orientation]::Horizontal
        $this.NativeUI.Content = $this.StackPanel
        Add-Member -InputObject $this -Name Caption -MemberType ScriptProperty -Value {
            $this.CaptionText
        } -SecondValue {
            $this.CaptionText = $args[0]
            $this.RefreshCaption()
        }
        Add-Member -InputObject $this -Name Icon -MemberType ScriptProperty -Value {
            $this.CurrentIcon
        } -SecondValue {
            $this.CurrentIcon = $args[0]
            $this.RefreshCaption()
        }
        $this.AddScriptBlockProperty("Action")
        $this.NativeUI.Add_Click({ $this.Control.OnAction() })
    }

    [void] RefreshCaption() {
        $this.StackPanel.Children.Clear()

        $label = [TextBlock]::new()
        $label.Text = $this.CaptionText

        if ($this.RightIcon) {
            $this.StackPanel.AddChild($label)
            if ($this.CurrentIcon -ne $null) {
                $this.StackPanel.AddChild($this.CurrentIcon.NativeUI)
            }
        } else {
            if ($this.CurrentIcon -ne $null) {
                $this.StackPanel.AddChild($this.CurrentIcon.NativeUI)
            }
            $this.StackPanel.AddChild($label)
        }
    }

    [void] OnAction() {
        $this.InvokeTrappableCommand($this._Action, $this)
    }

}

class MaterialWPFTextBox : WPFTextBox {
}

class MaterialWPFCheckBox : WPFCheckBox {

    MaterialWPFCheckBox() {
        $this.NativeUI.SetResourceReference([Control]::StyleProperty, "MaterialDesignCheckBox")
    }
}

class MaterialWPFRadioButton : WPFRadioButton {

    MaterialWPFCheckBox() {
        $this.NativeUI.SetResourceReference([Control]::StyleProperty, "MaterialDesignRadioButton")
    }
}

class MaterialWPFRadioGroup : WPFRadioGroup {
}

class MaterialWPFList : WPFList {
}

class MaterialWPFListColumn : WPFListColumn {
}

class MaterialWPFTabItem : MaterialWPFStackPanel {
            [MaterialWPFRadioButton]   $HeaderRadioButton   = ([MaterialWPFRadioButton]  @{ Caption     = ""     })
    hidden  [String]                   $CaptionText         = ""

    MaterialWPFTabItem() {
        Add-Member -InputObject $this.HeaderRadioButton -Name Tab -Value $this -MemberType NoteProperty

        #$this.WrapProperty("Caption", "Caption", "HeaderRadioButton")
        Add-Member -InputObject $this -Name Caption -MemberType ScriptProperty -Value {
            $this.CaptionText
        } -SecondValue {
            $this.CaptionText = $args[0]
            $this.HeaderRadioButton.Caption = $args[0].ToUpperInvariant()
        }

        $this.HeaderRadioButton.NativeUI.SetResourceReference([Control]::StyleProperty, "MaterialDesignTabRadioButton")
        $this.Visible = $false
        $this.HeaderRadioButton.Click = {
            $this.Control.Tab.Parent.HideTabs()
            $this.Control.Tab.Visible = $this.Control.IsChecked
        }
    }
}

class MaterialWPFTabControl : MaterialWPFStackPanel {
    hidden [MaterialWPFStackPanel]  $HeadersStackPanel  = ([MaterialWPFStackPanel] @{ Orientation = [Orientation]::Horizontal })
    hidden [MaterialWPFStackPanel]  $TabsStackPanel     = ([MaterialWPFStackPanel] @{ Orientation = [Orientation]::Horizontal })
    hidden                          $TabItems           = [List[MaterialWPFTabItem]]::new()

    MaterialWPFTabControl() {
        $this.Orientation = [Orientation]::Vertical
        $this.AddChild($this.HeadersStackPanel)
        $this.AddChild($this.TabsStackPanel)
        $this.AddNativeUIChild = {
            param (
                [MaterialWPFTabItem] $element
            )
            $this.TabItems.Add($element)
            $this.HeadersStackPanel.AddChild($element.HeaderRadioButton)    | Out-Null
            $this.TabsStackPanel.NativeUI.AddChild($element.NativeUI)       | Out-Null
        }
        $this.RemoveNativeUIChild = {
            param (
                [MaterialWPFTabItem] $element
            )
            $this.TabItems.Remove($element)
            $this.HeadersStackPanel.RemoveChild($element.HeaderRadioButton)    | Out-Null
            $this.TabsStackPanel.NativeUI.Children.Remove($element.NativeUI)   | Out-Null
        }
    }

    [void] HideTabs() {
        $this.TabItems | ForEach-Object {
            $_.Visible = $false
        }
    }

}

class MaterialWPFModal : WPFModal {
}

class MaterialWPFTimer : WPFTimer {
}

class MaterialWPFDatePicker : WPFElement {

    MaterialWPFDatePicker() {
        $datePicker = [DatePicker]::new()
        $datePicker.Width = 100
        $datePicker.SetResourceReference([Control]::StyleProperty, "MaterialDesignDatePicker")
        $this.SetNativeUI($datePicker)

        $this.AddScriptBlockProperty("Change")
        $this.NativeUI.Add_SelectedDateChanged({ $this.Control.OnChange() })
        $this.AddScriptBlockProperty("LostFocus")
        $this.NativeUI.Add_LostFocus({ $this.Control.OnLostFocus() })
        
        $this.WrapProperty("Value", "SelectedDate")
    }

    [void] OnChange() {
        $this.InvokeTrappableCommand($this._Change, $this)
    }
    
    [void] OnLostFocus() {
        $this.InvokeTrappableCommand($this._LostFocus, $this)
    }
}

class MaterialWPFTimePicker : WPFElement {

    MaterialWPFTimePicker() {
        $timePicker = [TimePicker]::new()
        $timePicker.Width = 100
        $timePicker.SetResourceReference([Control]::StyleProperty, "MaterialDesignTimePicker")
        $timePicker.Is24Hours = $true
        $this.SetNativeUI($timePicker)

        $this.AddScriptBlockProperty("Change")
        $this.NativeUI.Add_SelectedTimeChanged({ $this.Control.OnChange() })
        $this.AddScriptBlockProperty("LostFocus")
        $this.NativeUI.Add_LostFocus({ $this.Control.OnLostFocus() })
        
        $this.WrapProperty("Value", "SelectedTime")
    }

    [void] OnChange() {
        $this.InvokeTrappableCommand($this._Change, $this)
    }
    
    [void] OnLostFocus() {
        $this.InvokeTrappableCommand($this._LostFocus, $this)
    }
}

class MaterialWPFBrowser : WPFBrowser {

    [void] StyleComponents() {
        $this.StyleButton($this.FirstButton,     "FirstPage",     "MaterialDesignIconButton")
        $this.StyleButton($this.PreviousButton,  "ChevronLeft",   "MaterialDesignIconButton")
        $this.StyleButton($this.NextButton,      "ChevronRight",  "MaterialDesignIconButton")
        $this.StyleButton($this.LastButton,      "LastPage",      "MaterialDesignIconButton")

        $this.StyleButton($this.AddNewButton,    "Add",           "MaterialDesignFloatingActionAccentButton")

        $this.FirstButton.Parent.NativeUI.Margin  = "0 -10 0 10"
        $this.AddNewButton.NativeUI.Margin        = "100 10 10 10"
    }

    [void] StyleButton($button, $iconKind, $styleName) {
        $button.NativeUI.Content = New-Object PackIcon -Property @{ Kind = $iconKind }
        $button.NativeUI.Margin       = 0
        $button.NativeUI.SetResourceReference([Control]::StyleProperty, $styleName)
    }

    [void] StyleEditionButtons([WPFButton] $editButton, [WPFButton] $deleteButton, [int] $rowIndex) {
        $editButton.Parent.NativeUI.Background  = $this.GetRowBackground($rowIndex)

        $this.StyleButton($editButton,      "ModeEdit",        "MaterialDesignIconMiniButton")
        $this.StyleButton($deleteButton,    "WindowClose",     "MaterialDesignIconMiniButton")

        $editButton.NativeUI.Background      = [System.Windows.Media.Brushes]::Transparent
        $deleteButton.NativeUI.Background    = [System.Windows.Media.Brushes]::Transparent
        
        $editButton.NativeUI.Foreground      = [System.Windows.Media.Brushes]::Green
        $deleteButton.NativeUI.Foreground    = [System.Windows.Media.Brushes]::Red
        
        $editButton.NativeUI.BorderThickness     = 0
        $deleteButton.NativeUI.BorderThickness   = 0
    }

}

class MaterialWPFMenuItem : WPFMenuItem {
}

class MaterialWPFDropDownMenu : MaterialWPFButton {

    MaterialWPFDropDownMenu() {
        $this.RightIcon = $true
        $this.Icon = [MaterialWPFIcon] @{ Kind = "chevron_down" }

        $this.NativeUI.ContextMenu = [ContextMenu]::new()
        $this.AddNativeUIChild = {
            param (
                [WPFElement] $element
            )
            $this.NativeUI.ContextMenu.Items.Add($element.NativeUI)
        }
        
        $this.Action = {
            param($this)
            $this.NativeUI.ContextMenu.IsOpen = -not $this.NativeUI.ContextMenu.IsOpen
        }
    }
    
}

class MaterialWPFAutoComplete : WPFAutoComplete {
}

class MaterialWPFCard : WPFCard {
    hidden $CurrentIcon        = [MaterialWPFIcon]::new()

    [void] StyleComponents() {
        ([WPFCard] $this).StyleComponents()
        $this.NativeUI.SetResourceReference([Control]::StyleProperty, "MaterialDesignCardGroupBox")
        $this.HeaderTextBlock.Foreground = [System.Windows.Media.Brushes]::White
        $this.CurrentIcon.NativeUI.Foreground = [System.Windows.Media.Brushes]::White
        $this.HeaderTextBlock.SetResourceReference([Control]::StyleProperty, "MaterialDesignSubHeadingTextBlock")
    }
}

class MaterialWPFImage : WPFImage {
}

class MaterialWPFTextEditor : WPFTextEditor {
}

class MaterialWPFExpander : WPFExpander {
}

class MaterialWPFInteger : WPFInteger {
}

class MaterialWPFDouble : WPFDouble {
}

class MaterialWPFComboBoxItem : WPFComboBoxItem {
}

class MaterialWPFComboBox : WPFComboBox {
}