From 0ec64eaf744bd2347a0421351152114ce5be35fb Mon Sep 17 00:00:00 2001
From: Martin Wiethan <47688561+Marterich@users.noreply.github.com>
Date: Tue, 4 Jun 2024 06:05:28 +0200
Subject: [PATCH] Optimize Display Behaviour of Category Labels (#1979)
* Hide Category title if empty
* Changed labels to a hashtable for faster access
* Extract WPFNamecreation to function, fix hide all if none match
---
functions/private/Get-TabXaml.ps1 | 8 ++++++-
functions/private/Get-WPFObjectName.ps1 | 27 ++++++++++++++++++++++++
scripts/main.ps1 | 28 +++++++++++++++++++++++--
3 files changed, 60 insertions(+), 3 deletions(-)
create mode 100644 functions/private/Get-WPFObjectName.ps1
diff --git a/functions/private/Get-TabXaml.ps1 b/functions/private/Get-TabXaml.ps1
index 135039e1..9caf2e9b 100644
--- a/functions/private/Get-TabXaml.ps1
+++ b/functions/private/Get-TabXaml.ps1
@@ -77,7 +77,13 @@ function Get-TabXaml {
$panelcount++
}
}
- $blockXml += "`n"
+
+ # Dot-source the Get-WPFObjectName function
+ . .\functions\private\Get-WPFObjectName
+
+ $categorycontent = $($category -replace '^.__', '')
+ $categoryname = Get-WPFObjectName -type "Label" -name $categorycontent
+ $blockXml += "`n"
$sortedApps = $organizedData[$panel][$category].Keys | Sort-Object
foreach ($appName in $sortedApps) {
$count++
diff --git a/functions/private/Get-WPFObjectName.ps1 b/functions/private/Get-WPFObjectName.ps1
new file mode 100644
index 00000000..654c564b
--- /dev/null
+++ b/functions/private/Get-WPFObjectName.ps1
@@ -0,0 +1,27 @@
+function Get-WPFObjectName {
+ <#
+ .SYNOPSIS
+ This is a helper function that generates an objectname with the prefix WPF that can be used as a Powershell Variable after compilation.
+ To achieve this, all characters that are not a-z, A-Z or 0-9 are simply removed from the name.
+ .PARAMETER type
+ The type of object for which the name should be generated. (e.g. Label, Button, CheckBox...)
+ .PARAMETER name
+ The name or description to be used for the object. (invalid characters are removed)
+ .OUTPUTS
+ A string that can be used as a object/variable name in powershell.
+ For example: WPFLabelMicrosoftTools
+
+ .EXAMPLE
+ Get-WPFObjectName -type Label -name "Microsoft Tools"
+ #>
+
+ param( [Parameter(Mandatory=$true)]
+ $type,
+ $name
+)
+
+$Output = $("WPF"+$type+$name) -replace '[^a-zA-Z0-9]', ''
+
+return $Output
+
+}
\ No newline at end of file
diff --git a/scripts/main.ps1 b/scripts/main.ps1
index 5507d641..23978ab7 100644
--- a/scripts/main.ps1
+++ b/scripts/main.ps1
@@ -367,6 +367,16 @@ Add-Type @"
})
+# Load Checkboxes and Labels outside of the Filter fuction only once on startup for performance reasons
+$filter = Get-WinUtilVariables -Type CheckBox
+$CheckBoxes = $sync.GetEnumerator() | Where-Object { $psitem.Key -in $filter }
+
+$filter = Get-WinUtilVariables -Type Label
+$labels = @{}
+$sync.GetEnumerator() | Where-Object {$PSItem.Key -in $filter} | ForEach-Object {$labels[$_.Key] = $_.Value}
+
+$allCategories = $checkBoxes.Name | ForEach-Object {$sync.configs.applications.$_} | Select-Object -Unique -ExpandProperty category
+
$sync["CheckboxFilter"].Add_TextChanged({
if ($sync.CheckboxFilter.Text -ne "") {
@@ -376,8 +386,7 @@ $sync["CheckboxFilter"].Add_TextChanged({
$sync.CheckboxFilterClear.Visibility = "Collapsed"
}
- $filter = Get-WinUtilVariables -Type CheckBox
- $CheckBoxes = $sync.GetEnumerator() | Where-Object { $psitem.Key -in $filter }
+ $activeApplications = @()
foreach ($CheckBox in $CheckBoxes) {
# Check if the checkbox is null or if it doesn't have content
@@ -394,6 +403,7 @@ $sync["CheckboxFilter"].Add_TextChanged({
if ($CheckBox.Value.Content.ToLower().Contains($textToSearch)) {
$CheckBox.Value.Visibility = "Visible"
+ $activeApplications += $sync.configs.applications.$checkboxName
# Set the corresponding text block visibility
if ($textBlock -ne $null) {
$textBlock.Visibility = "Visible"
@@ -407,7 +417,21 @@ $sync["CheckboxFilter"].Add_TextChanged({
}
}
}
+ $activeCategories = $activeApplications | Select-Object -ExpandProperty category -Unique
+ foreach ($category in $activeCategories){
+ $label = $labels[$(Get-WPFObjectName -type "Label" -name $category)]
+ $label.Visibility = "Visible"
+ }
+ if ($activeCategories){
+ $inactiveCategories = Compare-Object -ReferenceObject $allCategories -DifferenceObject $activeCategories -PassThru
+ }
+ else{
+ $inactiveCategories = $allCategories
+ }
+ foreach ($category in $inactiveCategories){
+ $label = $labels[$(Get-WPFObjectName -type "Label" -name $category)]
+ $label.Visibility = "Collapsed"}
})
# Define event handler for button click