mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2026-08-10 18:11:16 +10:00
Turn the Install category filters into toggle chips (#4926)
* Turn the Install category filters into toggle chips - Replace the filter buttons with toggles that show which ones are active - Add ctrl click to select more than one category - Keep the search box and the category filter independent of each other - Expand matching categories while a filter is active * Fix the filter interactions the category chips missed - Pass the selected categories to the lazy rendering batches, the old call used a parameter that no longer exists and threw while typing - Keep the category filter when switching tabs, the chips stayed checked while the filter itself was dropped - Put a category back to collapsed once the filter that expanded it is gone * Document the Install category filters - Add a guide section for the chips, ctrl click and clearing the filter - Note that search and the category chips apply together * Correct the category filter guide wording - Clearing by clicking the chip only applies when it is the only one selected - Only categories with matches expand, and only auto expanded ones re-collapse
This commit is contained in:
@@ -1,21 +1,26 @@
|
||||
function Find-AppsByNameOrDescription {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Searches through the Apps on the Install Tab and hides all entries that do not match the string
|
||||
Filters the Install tab entries by search text and by category
|
||||
|
||||
.DESCRIPTION
|
||||
Filters application entries by name or description using literal string matching.
|
||||
Respects collapsed category state and handles null $sync gracefully.
|
||||
Search text and categories are independent filters that both have to pass. An entry is
|
||||
shown when its name or description matches the search text, and when its category is in
|
||||
the selected set. An empty search matches everything, and an empty category set matches
|
||||
every category.
|
||||
|
||||
While either filter is active the matching categories are expanded, since a collapsed
|
||||
category would otherwise hide the very results that were asked for. With no filter at
|
||||
all the collapsed state the user set is restored.
|
||||
|
||||
.PARAMETER SearchString
|
||||
The string to be searched for. Wildcards are treated as literal characters.
|
||||
The string to search for. Wildcards are treated as literal characters.
|
||||
|
||||
.PARAMETER Category
|
||||
When provided, only applications in this exact category are shown.
|
||||
.PARAMETER Categories
|
||||
The categories to show. An empty or missing array shows all of them.
|
||||
|
||||
.NOTES
|
||||
- Uses module-scope $sync (no parameter needed; inherits from caller's scope)
|
||||
- Performs literal matching (no wildcard expansion)
|
||||
- Safely handles missing hashtable keys and null UI elements
|
||||
- Protected by try/catch to prevent UI thread crashes
|
||||
#>
|
||||
@@ -24,7 +29,7 @@ function Find-AppsByNameOrDescription {
|
||||
[string]$SearchString = "",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Category = ""
|
||||
[string[]]$Categories = @()
|
||||
)
|
||||
|
||||
# Validate that $sync exists and has required structure
|
||||
@@ -43,21 +48,34 @@ function Find-AppsByNameOrDescription {
|
||||
return
|
||||
}
|
||||
|
||||
# Categories that filtering expanded on the user's behalf, so clearing the filter can undo it
|
||||
if ($null -eq $sync.AppCategoryAutoExpanded) {
|
||||
$sync.AppCategoryAutoExpanded = @{}
|
||||
}
|
||||
|
||||
try {
|
||||
# Reset the visibility if the search string is empty or the search is cleared
|
||||
if ([string]::IsNullOrWhiteSpace($SearchString) -and [string]::IsNullOrWhiteSpace($Category)) {
|
||||
$activeCategories = @($Categories | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
|
||||
$hasSearch = -not [string]::IsNullOrWhiteSpace($SearchString)
|
||||
$hasCategories = $activeCategories.Count -gt 0
|
||||
|
||||
# Nothing is filtered, so put every entry back and leave the collapsed categories collapsed
|
||||
if (-not $hasSearch -and -not $hasCategories) {
|
||||
$sync.ItemsControl.Items | ForEach-Object {
|
||||
# Each item is a StackPanel container
|
||||
$_.Visibility = [Windows.Visibility]::Visible
|
||||
|
||||
if ($_.Children.Count -ge 2) {
|
||||
$categoryLabel = $_.Children[0]
|
||||
$wrapPanel = $_.Children[1]
|
||||
|
||||
# Keep category label visible
|
||||
$categoryLabel.Visibility = [Windows.Visibility]::Visible
|
||||
|
||||
# Respect the collapsed state of categories (indicated by + prefix)
|
||||
# A category that filtering expanded goes back to how the user left it
|
||||
$categoryName = $categoryLabel.Content -replace '^[+-] ', ''
|
||||
if ($sync.AppCategoryAutoExpanded.ContainsKey($categoryName)) {
|
||||
$categoryLabel.Content = $categoryLabel.Content -replace "^- ", "+ "
|
||||
$sync.AppCategoryAutoExpanded.Remove($categoryName)
|
||||
}
|
||||
|
||||
if ($categoryLabel.Content -like "+*") {
|
||||
$wrapPanel.Visibility = [Windows.Visibility]::Collapsed
|
||||
}
|
||||
@@ -65,7 +83,6 @@ function Find-AppsByNameOrDescription {
|
||||
$wrapPanel.Visibility = [Windows.Visibility]::Visible
|
||||
}
|
||||
|
||||
# Show all apps within the category
|
||||
$wrapPanel.Children | ForEach-Object {
|
||||
$_.Visibility = [Windows.Visibility]::Visible
|
||||
}
|
||||
@@ -77,7 +94,6 @@ function Find-AppsByNameOrDescription {
|
||||
# Escape wildcard characters for literal matching
|
||||
$escapedSearchString = [System.Management.Automation.WildcardPattern]::Escape($SearchString)
|
||||
|
||||
# Perform search
|
||||
$sync.ItemsControl.Items | ForEach-Object {
|
||||
# Each item is a StackPanel container with Children[0] = label, Children[1] = WrapPanel
|
||||
if ($_.Children.Count -ge 2) {
|
||||
@@ -85,12 +101,9 @@ function Find-AppsByNameOrDescription {
|
||||
$wrapPanel = $_.Children[1]
|
||||
$categoryHasMatch = $false
|
||||
|
||||
# Keep category label visible
|
||||
$categoryLabel.Visibility = [Windows.Visibility]::Visible
|
||||
|
||||
# Search through apps in this category
|
||||
foreach ($appControl in $wrapPanel.Children) {
|
||||
# Safely retrieve app entry from hashtable
|
||||
$appTag = $appControl.Tag
|
||||
$appEntry = $null
|
||||
|
||||
@@ -98,14 +111,13 @@ function Find-AppsByNameOrDescription {
|
||||
$appEntry = $sync.configs.applicationsHashtable[$appTag]
|
||||
}
|
||||
|
||||
# Check if app matches search criteria
|
||||
if ($null -ne $appEntry) {
|
||||
$categoryMatch = -not [string]::IsNullOrWhiteSpace($Category) -and $appEntry.Category -eq $Category
|
||||
$contentMatch = [string]::IsNullOrWhiteSpace($Category) -and $appEntry.Content -like "*$escapedSearchString*"
|
||||
$descriptionMatch = [string]::IsNullOrWhiteSpace($Category) -and $appEntry.Description -like "*$escapedSearchString*"
|
||||
$categoryMatch = -not $hasCategories -or $activeCategories -contains $appEntry.Category
|
||||
$textMatch = -not $hasSearch -or
|
||||
$appEntry.Content -like "*$escapedSearchString*" -or
|
||||
$appEntry.Description -like "*$escapedSearchString*"
|
||||
|
||||
if ($categoryMatch -or $contentMatch -or $descriptionMatch) {
|
||||
# Show the App and mark that this category has a match
|
||||
if ($categoryMatch -and $textMatch) {
|
||||
$appControl.Visibility = [Windows.Visibility]::Visible
|
||||
$categoryHasMatch = $true
|
||||
}
|
||||
@@ -119,17 +131,17 @@ function Find-AppsByNameOrDescription {
|
||||
}
|
||||
}
|
||||
|
||||
# If category has matches, show the WrapPanel and update the category label to expanded state
|
||||
if ($categoryHasMatch) {
|
||||
$wrapPanel.Visibility = [Windows.Visibility]::Visible
|
||||
$_.Visibility = [Windows.Visibility]::Visible
|
||||
# Update category label to show expanded state (-)
|
||||
# Expand it, otherwise the matches stay hidden behind a collapsed header.
|
||||
# Remember that it was collapsed so clearing the filter can put it back.
|
||||
if ($categoryLabel.Content -like "+*") {
|
||||
$categoryLabel.Content = $categoryLabel.Content -replace "^\+ ", "- "
|
||||
$sync.AppCategoryAutoExpanded[($categoryLabel.Content -replace '^- ', '')] = $true
|
||||
}
|
||||
}
|
||||
else {
|
||||
# Hide the entire category container if no matches
|
||||
$_.Visibility = [Windows.Visibility]::Collapsed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,11 @@ function Initialize-InstallCategoryAppList {
|
||||
# The WrapPanel is the second child
|
||||
$wrapPanel = $categoryContainer.Children[1]
|
||||
|
||||
# An explicit click wins over anything filtering expanded automatically
|
||||
if ($sync.AppCategoryAutoExpanded) {
|
||||
$sync.AppCategoryAutoExpanded.Remove(($categoryToggle.Content -replace '^[+-] ', ''))
|
||||
}
|
||||
|
||||
# Toggle visibility
|
||||
if ($wrapPanel.Visibility -eq [Windows.Visibility]::Visible) {
|
||||
$wrapPanel.Visibility = [Windows.Visibility]::Collapsed
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
function Invoke-WinUtilAppCategoryChip {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Handles a click on an Install tab category chip
|
||||
|
||||
.DESCRIPTION
|
||||
The chip carries its category in Tag, so every chip shares this handler. Holding ctrl
|
||||
adds the category to the current selection instead of replacing it.
|
||||
|
||||
.PARAMETER Chip
|
||||
The chip that was clicked
|
||||
#>
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
$Chip
|
||||
)
|
||||
|
||||
$ctrlDown = [bool]([System.Windows.Input.Keyboard]::Modifiers -band [System.Windows.Input.ModifierKeys]::Control)
|
||||
Set-WinUtilAppCategoryFilter -Category $Chip.Tag -Additive:$ctrlDown
|
||||
}
|
||||
@@ -1,17 +1,50 @@
|
||||
function Set-WinUtilAppCategoryFilter {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Applies an exact application category filter from an Install tab search chip.
|
||||
Applies the Install tab category filter and syncs the chip states to it
|
||||
|
||||
.DESCRIPTION
|
||||
The selection lives in $sync.SelectedAppCategories. An empty selection means every
|
||||
category is shown, which is what the All chip represents. The category filter and the
|
||||
search box are independent: this only touches categories, and the current search text
|
||||
is reapplied on top.
|
||||
|
||||
.PARAMETER Category
|
||||
The application category to show. An empty value clears the filter.
|
||||
The category to act on. An empty value clears the filter back to All.
|
||||
|
||||
.PARAMETER Additive
|
||||
Toggles this category in or out of the current selection instead of replacing it.
|
||||
Bound to ctrl click.
|
||||
#>
|
||||
param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Category = ""
|
||||
[string]$Category = "",
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[switch]$Additive
|
||||
)
|
||||
|
||||
$sync.SearchBar.Tag = $Category
|
||||
$sync.SearchBar.Text = $Category
|
||||
Find-AppsByNameOrDescription -SearchString $Category -Category $Category
|
||||
if ($null -eq $sync.SelectedAppCategories) {
|
||||
$sync.SelectedAppCategories = [System.Collections.Generic.List[string]]::new()
|
||||
}
|
||||
$selected = $sync.SelectedAppCategories
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($Category)) {
|
||||
$selected.Clear()
|
||||
} elseif ($Additive) {
|
||||
if ($selected.Contains($Category)) {
|
||||
[void]$selected.Remove($Category)
|
||||
} else {
|
||||
$selected.Add($Category)
|
||||
}
|
||||
} elseif ($selected.Count -eq 1 -and $selected.Contains($Category)) {
|
||||
# Clicking the only active category again clears the filter
|
||||
$selected.Clear()
|
||||
} else {
|
||||
$selected.Clear()
|
||||
$selected.Add($Category)
|
||||
}
|
||||
|
||||
Update-WinUtilAppCategoryChip
|
||||
Find-AppsByNameOrDescription -SearchString $sync.SearchBar.Text -Categories $selected.ToArray()
|
||||
}
|
||||
|
||||
@@ -8,8 +8,14 @@ function Invoke-WinUtilInstallAppRenderBatch {
|
||||
$sync.$appKey = Initialize-InstallAppEntry -TargetElement $CategoryBatch.TargetElement -AppKey $appKey
|
||||
}
|
||||
|
||||
if ($sync.currentTab -eq "Install" -and $sync.SearchBar -and -not [string]::IsNullOrWhiteSpace($sync.SearchBar.Text)) {
|
||||
Find-AppsByNameOrDescription -SearchString $sync.SearchBar.Text -Category $sync.SearchBar.Tag
|
||||
# Entries render in batches, so a filter that is already active has to be applied to each new
|
||||
# batch. Categories count as an active filter just like search text does.
|
||||
if ($sync.currentTab -eq "Install" -and $sync.SearchBar) {
|
||||
$selectedCategories = if ($sync.SelectedAppCategories) { $sync.SelectedAppCategories.ToArray() } else { @() }
|
||||
|
||||
if (-not [string]::IsNullOrWhiteSpace($sync.SearchBar.Text) -or $selectedCategories.Count -gt 0) {
|
||||
Find-AppsByNameOrDescription -SearchString $sync.SearchBar.Text -Categories $selectedCategories
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
function Update-WinUtilAppCategoryChip {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Pushes the current category selection onto the Install tab filter chips
|
||||
|
||||
.DESCRIPTION
|
||||
The chips are toggle buttons, so their checked state has to follow the selection
|
||||
rather than whatever the last click did to them. The All chip is checked when no
|
||||
category is selected.
|
||||
#>
|
||||
$selected = $sync.SelectedAppCategories
|
||||
if ($null -eq $selected) { return }
|
||||
|
||||
foreach ($chip in $sync.AppCategoryChips) {
|
||||
$control = $sync[$chip.Name]
|
||||
if ($null -eq $control) { continue }
|
||||
$control.IsChecked = if ($chip.Category) { $selected.Contains($chip.Category) } else { $selected.Count -eq 0 }
|
||||
}
|
||||
}
|
||||
@@ -32,8 +32,9 @@ function Invoke-WPFTab {
|
||||
|
||||
# Always reset the filter for the current tab
|
||||
if ($sync.currentTab -eq "Install") {
|
||||
# Reset Install tab filter
|
||||
Find-AppsByNameOrDescription -SearchString ""
|
||||
# Reset the search text, but keep the categories the chips are still showing as selected
|
||||
$selectedCategories = if ($sync.SelectedAppCategories) { $sync.SelectedAppCategories.ToArray() } else { @() }
|
||||
Find-AppsByNameOrDescription -SearchString "" -Categories $selectedCategories
|
||||
} elseif ($sync.currentTab -eq "Tweaks") {
|
||||
# Reset Tweaks tab filter
|
||||
Find-TweaksByNameOrDescription -SearchString ""
|
||||
|
||||
Reference in New Issue
Block a user