2024-08-28 11:13:23 -05:00
|
|
|
function Invoke-WPFUIElements {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Adds UI elements to a specified Grid in the WinUtil GUI based on a JSON configuration.
|
|
|
|
.PARAMETER configVariable
|
|
|
|
The variable/link containing the JSON configuration.
|
|
|
|
.PARAMETER targetGridName
|
|
|
|
The name of the grid to which the UI elements should be added.
|
|
|
|
.PARAMETER columncount
|
|
|
|
The number of columns to be used in the Grid. If not provided, a default value is used based on the panel.
|
|
|
|
.EXAMPLE
|
|
|
|
Invoke-WPFUIElements -configVariable $sync.configs.applications -targetGridName "install" -columncount 5
|
|
|
|
.NOTES
|
2024-10-01 11:55:27 -05:00
|
|
|
Future me/contributor: If possible, please wrap this into a runspace to make it load all panels at the same time.
|
2024-08-28 11:13:23 -05:00
|
|
|
#>
|
|
|
|
|
|
|
|
param(
|
2024-10-01 11:55:27 -05:00
|
|
|
[Parameter(Mandatory, Position = 0)]
|
2024-08-28 11:13:23 -05:00
|
|
|
[PSCustomObject]$configVariable,
|
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
[Parameter(Mandatory, Position = 1)]
|
2024-08-28 11:13:23 -05:00
|
|
|
[string]$targetGridName,
|
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
[Parameter(Mandatory, Position = 2)]
|
2024-08-28 11:13:23 -05:00
|
|
|
[int]$columncount
|
|
|
|
)
|
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
$window = $sync.form
|
2024-08-28 11:13:23 -05:00
|
|
|
|
|
|
|
$borderstyle = $window.FindResource("BorderStyle")
|
|
|
|
$HoverTextBlockStyle = $window.FindResource("HoverTextBlockStyle")
|
|
|
|
$ColorfulToggleSwitchStyle = $window.FindResource("ColorfulToggleSwitchStyle")
|
2024-10-26 07:08:53 -05:00
|
|
|
$ToggleButtonStyle = $window.FindResource("ToggleButtonStyle")
|
2024-08-28 11:13:23 -05:00
|
|
|
|
|
|
|
if (!$borderstyle -or !$HoverTextBlockStyle -or !$ColorfulToggleSwitchStyle) {
|
|
|
|
throw "Failed to retrieve Styles using 'FindResource' from main window element."
|
|
|
|
}
|
|
|
|
|
|
|
|
$targetGrid = $window.FindName($targetGridName)
|
|
|
|
|
|
|
|
if (!$targetGrid) {
|
|
|
|
throw "Failed to retrieve Target Grid by name, provided name: $targetGrid"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Clear existing ColumnDefinitions and Children
|
|
|
|
$targetGrid.ColumnDefinitions.Clear() | Out-Null
|
|
|
|
$targetGrid.Children.Clear() | Out-Null
|
|
|
|
|
|
|
|
# Add ColumnDefinitions to the target Grid
|
|
|
|
for ($i = 0; $i -lt $columncount; $i++) {
|
|
|
|
$colDef = New-Object Windows.Controls.ColumnDefinition
|
|
|
|
$colDef.Width = New-Object Windows.GridLength(1, [Windows.GridUnitType]::Star)
|
|
|
|
$targetGrid.ColumnDefinitions.Add($colDef) | Out-Null
|
|
|
|
}
|
|
|
|
|
|
|
|
# Convert PSCustomObject to Hashtable
|
|
|
|
$configHashtable = @{}
|
|
|
|
$configVariable.PSObject.Properties.Name | ForEach-Object {
|
|
|
|
$configHashtable[$_] = $configVariable.$_
|
|
|
|
}
|
|
|
|
|
2024-10-02 01:17:47 -05:00
|
|
|
$radioButtonGroups = @{}
|
|
|
|
|
2024-08-28 11:13:23 -05:00
|
|
|
$organizedData = @{}
|
|
|
|
# Iterate through JSON data and organize by panel and category
|
|
|
|
foreach ($entry in $configHashtable.Keys) {
|
|
|
|
$entryInfo = $configHashtable[$entry]
|
|
|
|
|
|
|
|
# Create an object for the application
|
|
|
|
$entryObject = [PSCustomObject]@{
|
2024-10-01 11:55:27 -05:00
|
|
|
Name = $entry
|
|
|
|
Order = $entryInfo.order
|
|
|
|
Category = $entryInfo.Category
|
|
|
|
Content = $entryInfo.Content
|
|
|
|
Panel = if ($entryInfo.Panel) { $entryInfo.Panel } else { "0" }
|
|
|
|
Link = $entryInfo.link
|
2024-08-28 11:13:23 -05:00
|
|
|
Description = $entryInfo.description
|
2024-10-01 11:55:27 -05:00
|
|
|
Type = $entryInfo.type
|
|
|
|
ComboItems = $entryInfo.ComboItems
|
|
|
|
Checked = $entryInfo.Checked
|
2024-08-28 11:13:23 -05:00
|
|
|
ButtonWidth = $entryInfo.ButtonWidth
|
2024-10-01 11:55:27 -05:00
|
|
|
GroupName = $entryInfo.GroupName # Added for RadioButton groupings
|
2024-08-28 11:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (-not $organizedData.ContainsKey($entryObject.Panel)) {
|
|
|
|
$organizedData[$entryObject.Panel] = @{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not $organizedData[$entryObject.Panel].ContainsKey($entryObject.Category)) {
|
|
|
|
$organizedData[$entryObject.Panel][$entryObject.Category] = @()
|
|
|
|
}
|
|
|
|
|
|
|
|
# Store application data in an array under the category
|
|
|
|
$organizedData[$entryObject.Panel][$entryObject.Category] += $entryObject
|
|
|
|
|
|
|
|
# Only apply the logic for distributing entries across columns if the targetGridName is "appspanel"
|
|
|
|
if ($targetGridName -eq "appspanel") {
|
|
|
|
$panelcount = 0
|
|
|
|
$entrycount = $configHashtable.Keys.Count + $organizedData["0"].Keys.Count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
# Initialize panel count
|
|
|
|
$panelcount = 0
|
|
|
|
|
2024-08-28 11:13:23 -05:00
|
|
|
# Iterate through 'organizedData' by panel, category, and application
|
|
|
|
$count = 0
|
|
|
|
foreach ($panelKey in ($organizedData.Keys | Sort-Object)) {
|
|
|
|
# Create a Border for each column
|
|
|
|
$border = New-Object Windows.Controls.Border
|
|
|
|
$border.VerticalAlignment = "Stretch" # Ensure the border stretches vertically
|
|
|
|
[System.Windows.Controls.Grid]::SetColumn($border, $panelcount)
|
|
|
|
$border.style = $borderstyle
|
|
|
|
$targetGrid.Children.Add($border) | Out-Null
|
|
|
|
|
2024-09-29 02:32:10 -05:00
|
|
|
# Use a DockPanel to contain both the top buttons and the main content
|
|
|
|
$dockPanelContainer = New-Object Windows.Controls.DockPanel
|
|
|
|
$border.Child = $dockPanelContainer
|
|
|
|
|
|
|
|
# Create a ScrollViewer to contain the main content (excluding buttons)
|
|
|
|
$scrollViewer = New-Object Windows.Controls.ScrollViewer
|
|
|
|
$scrollViewer.VerticalScrollBarVisibility = 'Auto'
|
|
|
|
$scrollViewer.HorizontalScrollBarVisibility = 'Auto'
|
|
|
|
$scrollViewer.HorizontalAlignment = 'Stretch'
|
|
|
|
$scrollViewer.VerticalAlignment = 'Stretch'
|
2024-10-01 11:55:27 -05:00
|
|
|
$scrollViewer.CanContentScroll = $true # Enable virtualization
|
|
|
|
|
|
|
|
# Create an ItemsControl inside the ScrollViewer for application content
|
|
|
|
$itemsControl = New-Object Windows.Controls.ItemsControl
|
|
|
|
$itemsControl.HorizontalAlignment = 'Stretch'
|
|
|
|
$itemsControl.VerticalAlignment = 'Stretch'
|
|
|
|
|
|
|
|
# Set the ItemsPanel to a VirtualizingStackPanel
|
|
|
|
$itemsPanelTemplate = New-Object Windows.Controls.ItemsPanelTemplate
|
|
|
|
$factory = New-Object Windows.FrameworkElementFactory ([Windows.Controls.VirtualizingStackPanel])
|
|
|
|
$itemsPanelTemplate.VisualTree = $factory
|
|
|
|
$itemsControl.ItemsPanel = $itemsPanelTemplate
|
2024-09-29 02:32:10 -05:00
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
# Set virtualization properties
|
|
|
|
$itemsControl.SetValue([Windows.Controls.VirtualizingStackPanel]::IsVirtualizingProperty, $true)
|
|
|
|
$itemsControl.SetValue([Windows.Controls.VirtualizingStackPanel]::VirtualizationModeProperty, [Windows.Controls.VirtualizationMode]::Recycling)
|
2024-09-29 02:32:10 -05:00
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
# Add the ItemsControl to the ScrollViewer
|
|
|
|
$scrollViewer.Content = $itemsControl
|
2024-09-29 02:32:10 -05:00
|
|
|
|
|
|
|
# Add the ScrollViewer to the DockPanel (it will be below the top buttons StackPanel)
|
|
|
|
[Windows.Controls.DockPanel]::SetDock($scrollViewer, [Windows.Controls.Dock]::Bottom)
|
|
|
|
$dockPanelContainer.Children.Add($scrollViewer) | Out-Null
|
2024-08-28 11:13:23 -05:00
|
|
|
$panelcount++
|
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
# Now proceed with adding category labels and entries to $itemsControl
|
2024-08-28 11:13:23 -05:00
|
|
|
foreach ($category in ($organizedData[$panelKey].Keys | Sort-Object)) {
|
|
|
|
$count++
|
|
|
|
|
|
|
|
$label = New-Object Windows.Controls.Label
|
|
|
|
$label.Content = $category -replace ".*__", ""
|
2024-10-13 12:25:02 -05:00
|
|
|
$label.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "FontSizeHeading")
|
|
|
|
$label.SetResourceReference([Windows.Controls.Control]::FontFamilyProperty, "HeaderFontFamily")
|
2024-10-01 11:55:27 -05:00
|
|
|
$itemsControl.Items.Add($label) | Out-Null
|
2024-08-28 11:13:23 -05:00
|
|
|
|
|
|
|
$sync[$category] = $label
|
|
|
|
|
2024-10-01 11:55:27 -05:00
|
|
|
# Sort entries by Order and then by Name
|
2024-08-28 11:13:23 -05:00
|
|
|
$entries = $organizedData[$panelKey][$category] | Sort-Object Order, Name
|
|
|
|
foreach ($entryInfo in $entries) {
|
|
|
|
$count++
|
2024-10-13 13:55:25 -05:00
|
|
|
# Create the UI elements based on the entry type
|
|
|
|
switch ($entryInfo.Type) {
|
|
|
|
"Toggle" {
|
|
|
|
$dockPanel = New-Object Windows.Controls.DockPanel
|
|
|
|
$checkBox = New-Object Windows.Controls.CheckBox
|
|
|
|
$checkBox.Name = $entryInfo.Name
|
|
|
|
$checkBox.HorizontalAlignment = "Right"
|
|
|
|
$dockPanel.Children.Add($checkBox) | Out-Null
|
|
|
|
$checkBox.Style = $ColorfulToggleSwitchStyle
|
|
|
|
|
|
|
|
$label = New-Object Windows.Controls.Label
|
|
|
|
$label.Content = $entryInfo.Content
|
|
|
|
$label.ToolTip = $entryInfo.Description
|
|
|
|
$label.HorizontalAlignment = "Left"
|
|
|
|
$label.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "FontSize")
|
|
|
|
$label.SetResourceReference([Windows.Controls.Control]::ForegroundProperty, "MainForegroundColor")
|
|
|
|
$dockPanel.Children.Add($label) | Out-Null
|
|
|
|
$itemsControl.Items.Add($dockPanel) | Out-Null
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name] = $checkBox
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name].IsChecked = Get-WinUtilToggleStatus $sync[$entryInfo.Name].Name
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name].Add_Click({
|
2024-10-26 07:08:53 -05:00
|
|
|
[System.Object]$Sender = $args[0]
|
|
|
|
Invoke-WPFToggle $Sender.name
|
|
|
|
})
|
2024-10-13 13:55:25 -05:00
|
|
|
}
|
2024-08-28 11:13:23 -05:00
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
"ToggleButton" {
|
2024-10-26 07:08:53 -05:00
|
|
|
# Determine contentOn and contentOff based on the Content property
|
|
|
|
if ($entryInfo.Content -is [array]) {
|
|
|
|
# If Content is an array, use its elements
|
|
|
|
write-host "Content is an array"
|
|
|
|
write-host $entryInfo.Content
|
|
|
|
$contentOn = if ($entryInfo.Content.Count -ge 1) { $entryInfo.Content[0] } else { "" }
|
|
|
|
$contentOff = if ($entryInfo.Content.Count -ge 2) { $entryInfo.Content[1] } else { $contentOn }
|
|
|
|
} else {
|
|
|
|
# If Content is a single value, use it for both states
|
|
|
|
$contentOn = $entryInfo.Content
|
|
|
|
$contentOff = $entryInfo.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
$toggleButton = New-Object Windows.Controls.Primitives.ToggleButton
|
2024-10-13 13:55:25 -05:00
|
|
|
$toggleButton.Name = $entryInfo.Name
|
2024-10-26 07:08:53 -05:00
|
|
|
$toggleButton.Content = $contentOff
|
|
|
|
$toggleButton.ToolTip = $entryInfo.Description
|
2024-10-13 13:55:25 -05:00
|
|
|
$toggleButton.HorizontalAlignment = "Left"
|
2024-10-26 07:08:53 -05:00
|
|
|
$toggleButton.Style = $ToggleButtonStyle
|
2024-08-28 11:13:23 -05:00
|
|
|
|
2024-10-26 07:08:53 -05:00
|
|
|
$toggleButton.Tag = @{
|
|
|
|
contentOn = $contentOn
|
|
|
|
contentOff = $contentOff
|
|
|
|
}
|
2024-08-28 11:13:23 -05:00
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
$itemsControl.Items.Add($toggleButton) | Out-Null
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name] = $toggleButton
|
2024-10-26 07:08:53 -05:00
|
|
|
|
|
|
|
$sync[$entryInfo.Name].Add_Checked({
|
|
|
|
$this.Content = $this.Tag.contentOn
|
|
|
|
})
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name].Add_Unchecked({
|
|
|
|
$this.Content = $this.Tag.contentOff
|
|
|
|
})
|
2024-10-13 13:55:25 -05:00
|
|
|
}
|
2024-08-28 11:13:23 -05:00
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
"Combobox" {
|
|
|
|
$horizontalStackPanel = New-Object Windows.Controls.StackPanel
|
|
|
|
$horizontalStackPanel.Orientation = "Horizontal"
|
|
|
|
$horizontalStackPanel.Margin = "0,5,0,0"
|
|
|
|
|
|
|
|
$label = New-Object Windows.Controls.Label
|
|
|
|
$label.Content = $entryInfo.Content
|
|
|
|
$label.HorizontalAlignment = "Left"
|
|
|
|
$label.VerticalAlignment = "Center"
|
|
|
|
$label.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "ButtonFontSize")
|
|
|
|
$horizontalStackPanel.Children.Add($label) | Out-Null
|
|
|
|
|
|
|
|
$comboBox = New-Object Windows.Controls.ComboBox
|
|
|
|
$comboBox.Name = $entryInfo.Name
|
|
|
|
$comboBox.SetResourceReference([Windows.Controls.Control]::HeightProperty, "ButtonHeight")
|
|
|
|
$comboBox.SetResourceReference([Windows.Controls.Control]::WidthProperty, "ButtonWidth")
|
|
|
|
$comboBox.HorizontalAlignment = "Left"
|
|
|
|
$comboBox.VerticalAlignment = "Center"
|
|
|
|
$comboBox.SetResourceReference([Windows.Controls.Control]::MarginProperty, "ButtonMargin")
|
|
|
|
|
|
|
|
foreach ($comboitem in ($entryInfo.ComboItems -split " ")) {
|
|
|
|
$comboBoxItem = New-Object Windows.Controls.ComboBoxItem
|
|
|
|
$comboBoxItem.Content = $comboitem
|
|
|
|
$comboBoxItem.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "ButtonFontSize")
|
|
|
|
$comboBox.Items.Add($comboBoxItem) | Out-Null
|
2024-08-28 11:13:23 -05:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
$horizontalStackPanel.Children.Add($comboBox) | Out-Null
|
|
|
|
$itemsControl.Items.Add($horizontalStackPanel) | Out-Null
|
|
|
|
|
|
|
|
$comboBox.SelectedIndex = 0
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name] = $comboBox
|
|
|
|
}
|
|
|
|
|
|
|
|
"Button" {
|
|
|
|
$button = New-Object Windows.Controls.Button
|
|
|
|
$button.Name = $entryInfo.Name
|
|
|
|
$button.Content = $entryInfo.Content
|
|
|
|
$button.HorizontalAlignment = "Left"
|
|
|
|
$button.SetResourceReference([Windows.Controls.Control]::MarginProperty, "ButtonMargin")
|
|
|
|
$button.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "ButtonFontSize")
|
|
|
|
if ($entryInfo.ButtonWidth) {
|
|
|
|
$button.Width = $entryInfo.ButtonWidth
|
2024-08-28 11:13:23 -05:00
|
|
|
}
|
2024-10-13 13:55:25 -05:00
|
|
|
$itemsControl.Items.Add($button) | Out-Null
|
|
|
|
|
|
|
|
$sync[$entryInfo.Name] = $button
|
|
|
|
}
|
|
|
|
|
|
|
|
"RadioButton" {
|
|
|
|
# Check if a container for this GroupName already exists
|
|
|
|
if (-not $radioButtonGroups.ContainsKey($entryInfo.GroupName)) {
|
|
|
|
# Create a StackPanel for this group
|
|
|
|
$groupStackPanel = New-Object Windows.Controls.StackPanel
|
|
|
|
$groupStackPanel.Orientation = "Vertical"
|
2024-08-28 11:13:23 -05:00
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
# Add the group container to the ItemsControl
|
|
|
|
$itemsControl.Items.Add($groupStackPanel) | Out-Null
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# Retrieve the existing group container
|
|
|
|
$groupStackPanel = $radioButtonGroups[$entryInfo.GroupName]
|
2024-09-27 15:54:07 -05:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
# Create the RadioButton
|
|
|
|
$radioButton = New-Object Windows.Controls.RadioButton
|
|
|
|
$radioButton.Name = $entryInfo.Name
|
|
|
|
$radioButton.GroupName = $entryInfo.GroupName
|
|
|
|
$radioButton.Content = $entryInfo.Content
|
|
|
|
$radioButton.HorizontalAlignment = "Left"
|
|
|
|
$radioButton.SetResourceReference([Windows.Controls.Control]::MarginProperty, "CheckBoxMargin")
|
|
|
|
$radioButton.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "ButtonFontSize")
|
|
|
|
$radioButton.ToolTip = $entryInfo.Description
|
|
|
|
|
|
|
|
if ($entryInfo.Checked -eq $true) {
|
|
|
|
$radioButton.IsChecked = $true
|
2024-08-28 11:13:23 -05:00
|
|
|
}
|
|
|
|
|
2024-10-13 13:55:25 -05:00
|
|
|
# Add the RadioButton to the group container
|
|
|
|
$groupStackPanel.Children.Add($radioButton) | Out-Null
|
|
|
|
$sync[$entryInfo.Name] = $radioButton
|
|
|
|
}
|
|
|
|
|
|
|
|
default {
|
|
|
|
$horizontalStackPanel = New-Object Windows.Controls.StackPanel
|
|
|
|
$horizontalStackPanel.Orientation = "Horizontal"
|
|
|
|
|
|
|
|
$checkBox = New-Object Windows.Controls.CheckBox
|
|
|
|
$checkBox.Name = $entryInfo.Name
|
|
|
|
$checkBox.Content = $entryInfo.Content
|
|
|
|
$checkBox.SetResourceReference([Windows.Controls.Control]::FontSizeProperty, "FontSize")
|
|
|
|
$checkBox.ToolTip = $entryInfo.Description
|
|
|
|
$checkBox.SetResourceReference([Windows.Controls.Control]::MarginProperty, "CheckBoxMargin")
|
|
|
|
if ($entryInfo.Checked -eq $true) {
|
|
|
|
$checkBox.IsChecked = $entryInfo.Checked
|
2024-09-27 17:43:56 -05:00
|
|
|
}
|
2024-10-13 13:55:25 -05:00
|
|
|
$horizontalStackPanel.Children.Add($checkBox) | Out-Null
|
|
|
|
|
|
|
|
if ($entryInfo.Link) {
|
|
|
|
$textBlock = New-Object Windows.Controls.TextBlock
|
|
|
|
$textBlock.Name = $checkBox.Name + "Link"
|
|
|
|
$textBlock.Text = "(?)"
|
|
|
|
$textBlock.ToolTip = $entryInfo.Link
|
|
|
|
$textBlock.Style = $HoverTextBlockStyle
|
|
|
|
|
|
|
|
$horizontalStackPanel.Children.Add($textBlock) | Out-Null
|
|
|
|
|
|
|
|
$sync[$textBlock.Name] = $textBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemsControl.Items.Add($horizontalStackPanel) | Out-Null
|
|
|
|
$sync[$entryInfo.Name] = $checkBox
|
2024-08-28 11:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|