Some refactoring and removal of redundant code

This commit is contained in:
Niels Laute
2025-12-03 15:31:03 +01:00
parent 90e4f1ca41
commit a40be6f9be
11 changed files with 78 additions and 307 deletions

View File

@@ -69,33 +69,9 @@
</Button> </Button>
</StackPanel> </StackPanel>
</Grid> </Grid>
<ListView <controls:ModuleList
Grid.Row="1" Grid.Row="1"
Margin="0,16,0,0" IsItemClickable="False"
ItemsSource="{x:Bind ViewModel.FlyoutMenuItems}" ItemsSource="{x:Bind ViewModel.FlyoutMenuItems, Mode=OneWay}" />
SelectionMode="None">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Padding" Value="0" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Padding="0,0,0,16" Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewModels:FlyoutMenuItem">
<controls:ModuleSettingsCard
HorizontalAlignment="Stretch"
Icon="{x:Bind Icon, Mode=OneWay}"
IsLocked="{x:Bind IsLocked, Mode=OneWay}"
IsOn="{x:Bind IsEnabled, Mode=TwoWay}"
Label="{x:Bind Label, Mode=OneWay}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid> </Grid>
</Page> </Page>

View File

@@ -1,32 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.UI.Xaml.Data;
namespace Microsoft.PowerToys.Settings.UI.Controls.Converters
{
public partial class BoolNegationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is bool b)
{
return !b;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (value is bool b)
{
return !b;
}
return false;
}
}
}

View File

@@ -1,39 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
namespace Microsoft.PowerToys.Settings.UI.Controls.Converters
{
public partial class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
bool bVal = false;
if (value is bool b)
{
bVal = b;
}
else if (value is bool?)
{
bVal = (bool?)value ?? false;
}
if (parameter is string s && s == "True")
{
// Invert
bVal = !bVal;
}
return bVal ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@@ -16,10 +16,11 @@ namespace Microsoft.PowerToys.Settings.UI.Controls.Converters
return false; return false;
} }
// Get the enum value as string
var enumString = value.ToString(); var enumString = value.ToString();
var parameterString = parameter.ToString(); var parameterString = parameter.ToString();
return string.Equals(enumString, parameterString, StringComparison.OrdinalIgnoreCase); return enumString!.Equals(parameterString, StringComparison.OrdinalIgnoreCase);
} }
public object ConvertBack(object value, Type targetType, object parameter, string language) public object ConvertBack(object value, Type targetType, object parameter, string language)

View File

@@ -1,35 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media.Imaging;
namespace Microsoft.PowerToys.Settings.UI.Controls.Converters
{
public partial class StringToBitmapImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is string path && !string.IsNullOrEmpty(path))
{
try
{
return new BitmapImage(new Uri(path));
}
catch
{
// Fallback or null
}
}
return Microsoft.UI.Xaml.DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,34 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.UI.Xaml.Data;
namespace Microsoft.PowerToys.Settings.UI.Controls.Converters
{
public partial class StringToUriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is string path && !string.IsNullOrEmpty(path))
{
try
{
return new Uri(path);
}
catch
{
// Fallback or null
}
}
return null!;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}

View File

@@ -6,10 +6,23 @@
xmlns:converters="using:Microsoft.PowerToys.Settings.UI.Controls.Converters" xmlns:converters="using:Microsoft.PowerToys.Settings.UI.Controls.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tk="using:CommunityToolkit.WinUI"
xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls"
xmlns:tkconverters="using:CommunityToolkit.WinUI.Converters"
mc:Ignorable="d"> mc:Ignorable="d">
<UserControl.Resources> <UserControl.Resources>
<converters:ModuleListSortOptionToBooleanConverter x:Key="ModuleListSortOptionToBooleanConverter" /> <converters:ModuleListSortOptionToBooleanConverter x:Key="ModuleListSortOptionToBooleanConverter" />
<x:Double x:Key="SettingsCardWrapThreshold">0</x:Double>
<tkconverters:BoolToVisibilityConverter
x:Key="BoolToVisibilityConverter"
FalseValue="Collapsed"
TrueValue="Visible" />
<tkconverters:BoolToVisibilityConverter
x:Key="ReverseBoolToVisibilityConverter"
FalseValue="Visible"
TrueValue="Collapsed" />
<tkconverters:BoolNegationConverter x:Key="BoolNegationConverter" />
</UserControl.Resources> </UserControl.Resources>
<controls:Card <controls:Card
@@ -55,14 +68,54 @@
</ItemsRepeater.Layout> </ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate> <ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="controls:ModuleListItem"> <DataTemplate x:DataType="controls:ModuleListItem">
<controls:ModuleSettingsCard <tkcontrols:SettingsCard
Click="ModuleSettingsCard_Click" MinHeight="0"
Icon="{x:Bind Icon}" Padding="12,4,12,4"
IsLocked="{x:Bind IsLocked}" tk:FrameworkElementExtensions.AncestorType="ItemsRepeater"
IsNew="{x:Bind IsNew}" Background="Transparent"
IsOn="{x:Bind IsEnabled, Mode=TwoWay}" BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
Label="{x:Bind Label}" BorderThickness="0,1,0,0"
Tag="{x:Bind Tag}" /> Click="OnSettingsCardClick"
CornerRadius="0"
IsClickEnabled="{Binding (tk:FrameworkElementExtensions.Ancestor).IsItemClickable, RelativeSource={RelativeSource Self}}">
<tkcontrols:SettingsCard.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Label, Mode=OneWay}" />
<!-- InfoBadge -->
<InfoBadge
x:Name="NewInfoBadge"
Margin="4,0,0,0"
Style="{StaticResource AttentionDotInfoBadgeStyle}"
Visibility="{x:Bind IsNew, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
<FontIcon
Width="20"
Margin="4,0,0,0"
FontSize="16"
Glyph="&#xE72E;"
Visibility="{x:Bind IsLocked, Converter={StaticResource ReverseBoolToVisibilityConverter}, ConverterParameter=True}">
<ToolTipService.ToolTip>
<TextBlock Text="This setting is managed by your organization" TextWrapping="WrapWholeWords" />
</ToolTipService.ToolTip>
</FontIcon>
</StackPanel>
</tkcontrols:SettingsCard.Header>
<tkcontrols:SettingsCard.HeaderIcon>
<ImageIcon>
<ImageIcon.Source>
<BitmapImage UriSource="{x:Bind Icon, Mode=OneWay}" />
</ImageIcon.Source>
</ImageIcon>
</tkcontrols:SettingsCard.HeaderIcon>
<ToggleSwitch
HorizontalAlignment="Right"
AutomationProperties.Name="{x:Bind Label, Mode=OneWay}"
IsEnabled="{x:Bind IsLocked, Converter={StaticResource BoolNegationConverter}, ConverterParameter=True, Mode=OneWay}"
IsOn="{x:Bind IsEnabled, Mode=TwoWay}"
OffContent=""
OnContent="" />
</tkcontrols:SettingsCard>
</DataTemplate> </DataTemplate>
</ItemsRepeater.ItemTemplate> </ItemsRepeater.ItemTemplate>
</ItemsRepeater> </ItemsRepeater>

View File

@@ -14,6 +14,14 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
this.InitializeComponent(); this.InitializeComponent();
} }
public bool IsItemClickable
{
get => (bool)GetValue(IsItemClickableProperty);
set => SetValue(IsItemClickableProperty, value);
}
public static readonly DependencyProperty IsItemClickableProperty = DependencyProperty.Register(nameof(IsItemClickable), typeof(bool), typeof(ModuleList), new PropertyMetadata(true));
public object ItemsSource public object ItemsSource
{ {
get => (object)GetValue(ItemsSourceProperty); get => (object)GetValue(ItemsSourceProperty);
@@ -48,12 +56,10 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
SortOption = ModuleListSortOption.ByStatus; SortOption = ModuleListSortOption.ByStatus;
} }
private void ModuleSettingsCard_Click(object sender, RoutedEventArgs e) private void OnSettingsCardClick(object sender, RoutedEventArgs e)
{ {
if (sender is ModuleSettingsCard card && card.DataContext is ModuleListItem item) // TO DO:
{ // ViewModel.DashboardListItemClick(sender);
item.ClickCommand?.Execute(item.Tag);
}
} }
} }
} }

View File

@@ -1,59 +0,0 @@
<UserControl
x:Class="Microsoft.PowerToys.Settings.UI.Controls.ModuleSettingsCard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:Microsoft.PowerToys.Settings.UI.Controls.Converters"
xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls">
<UserControl.Resources>
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
<converters:StringToBitmapImageConverter x:Key="StringToBitmapImageConverter" />
<converters:StringToUriConverter x:Key="StringToUriConverter" />
<x:Double x:Key="SettingsCardWrapThreshold">0</x:Double>
</UserControl.Resources>
<tkcontrols:SettingsCard
MinHeight="0"
Padding="12,4,12,4"
Background="Transparent"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="0,1,0,0"
Click="OnSettingsCardClick"
CornerRadius="0"
IsClickEnabled="True">
<tkcontrols:SettingsCard.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Label, Mode=OneWay}" />
<!-- InfoBadge -->
<InfoBadge
x:Name="NewInfoBadge"
Margin="4,0,0,0"
Style="{StaticResource AttentionDotInfoBadgeStyle}"
Visibility="{x:Bind IsNew, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
<FontIcon
Width="20"
Margin="4,0,0,0"
FontSize="16"
Glyph="&#xE72E;"
Visibility="{x:Bind IsLocked, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
<ToolTipService.ToolTip>
<TextBlock Text="This setting is managed by your organization" TextWrapping="WrapWholeWords" />
</ToolTipService.ToolTip>
</FontIcon>
</StackPanel>
</tkcontrols:SettingsCard.Header>
<tkcontrols:SettingsCard.HeaderIcon>
<BitmapIcon ShowAsMonochrome="False" UriSource="{x:Bind Icon, Mode=OneWay, Converter={StaticResource StringToUriConverter}}" />
</tkcontrols:SettingsCard.HeaderIcon>
<ToggleSwitch
HorizontalAlignment="Right"
AutomationProperties.Name="{x:Bind Label, Mode=OneWay}"
IsEnabled="{x:Bind IsLocked, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}"
IsOn="{x:Bind IsOn, Mode=TwoWay}"
OffContent=""
OnContent="" />
</tkcontrols:SettingsCard>
</UserControl>

View File

@@ -1,65 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Controls
{
public sealed partial class ModuleSettingsCard : UserControl
{
public ModuleSettingsCard()
{
this.InitializeComponent();
}
public string Label
{
get => (string)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(nameof(Label), typeof(string), typeof(ModuleSettingsCard), new PropertyMetadata(string.Empty));
public string Icon
{
get => (string)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(string), typeof(ModuleSettingsCard), new PropertyMetadata(null));
public bool IsNew
{
get => (bool)GetValue(IsNewProperty);
set => SetValue(IsNewProperty, value);
}
public static readonly DependencyProperty IsNewProperty = DependencyProperty.Register(nameof(IsNew), typeof(bool), typeof(ModuleSettingsCard), new PropertyMetadata(false));
public bool IsLocked
{
get => (bool)GetValue(IsLockedProperty);
set => SetValue(IsLockedProperty, value);
}
public static readonly DependencyProperty IsLockedProperty = DependencyProperty.Register(nameof(IsLocked), typeof(bool), typeof(ModuleSettingsCard), new PropertyMetadata(false));
public bool IsOn
{
get => (bool)GetValue(IsOnProperty);
set => SetValue(IsOnProperty, value);
}
public static readonly DependencyProperty IsOnProperty = DependencyProperty.Register(nameof(IsOn), typeof(bool), typeof(ModuleSettingsCard), new PropertyMetadata(false));
public event RoutedEventHandler? Click;
private void OnSettingsCardClick(object sender, RoutedEventArgs e)
{
Click?.Invoke(this, e);
}
}
}

View File

@@ -165,7 +165,6 @@
Margin="8,0,12,12" Margin="8,0,12,12"
ItemsSource="{x:Bind ViewModel.QuickAccessItems, Mode=OneWay}" ItemsSource="{x:Bind ViewModel.QuickAccessItems, Mode=OneWay}"
Visibility="{x:Bind ViewModel.QuickAccessItems.Count, Mode=OneWay, Converter={StaticResource DoubleToVisibilityConverter}}" /> Visibility="{x:Bind ViewModel.QuickAccessItems.Count, Mode=OneWay, Converter={StaticResource DoubleToVisibilityConverter}}" />
<TextBlock <TextBlock
x:Uid="NoActionsToShow" x:Uid="NoActionsToShow"
Margin="12" Margin="12"