Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

feat: add menu functions and many things

itsfatduckb8800ec42c58
diff --git a/optimizerDuck.Test/Services/OptimizationServiceTests.cs b/optimizerDuck.Test/Services/OptimizationServiceTests.cs
index 681930f..619dace 100644
--- a/optimizerDuck.Test/Services/OptimizationServiceTests.cs
+++ b/optimizerDuck.Test/Services/OptimizationServiceTests.cs
@@ -193,7 +193,7 @@ public class OptimizationServiceTests
{
public Type? OwnerType { get; set; }
public string OwnerKey { get; } = "Test";
- public OptimizationRiskVisual RiskVisual { get; } = new();
+ public RiskVisual RiskVisual { get; } = new();
public IEnumerable<OptimizationTagDisplay> TagDisplays { get; } = [];
public string Prefix { get; } = "Test";
diff --git a/optimizerDuck/Core/Models/Bloatware/AppXPackage.cs b/optimizerDuck/Core/Models/Bloatware/AppXPackage.cs
new file mode 100644
index 0000000..364bbf2
--- /dev/null
+++ b/optimizerDuck/Core/Models/Bloatware/AppXPackage.cs
@@ -0,0 +1,43 @@
+using CommunityToolkit.Mvvm.ComponentModel;
+using optimizerDuck.Core.Models.UI;
+using optimizerDuck.Resources.Languages;
+using Wpf.Ui.Controls;
+
+namespace optimizerDuck.Core.Models.Bloatware;
+
+public enum AppRisk
+{
+ Safe,
+ Caution
+}
+
+public partial class AppXPackage : ObservableObject
+{
+ [ObservableProperty] private bool _isSelected;
+
+ public string Name { get; init; }
+ public string PackageFullName { get; init; }
+ public string Publisher { get; init; }
+ public string Version { get; init; }
+ public string InstallLocation { get; init; }
+ public AppRisk Risk { get; init; }
+
+ public RiskVisual RiskVisual => Risk switch
+ {
+ AppRisk.Safe => new RiskVisual
+ {
+ Display = Translations.Optimizer_UI_Risk_Safe,
+ Icon = SymbolRegular.ShieldCheckmark24
+ },
+ AppRisk.Caution => new RiskVisual
+ {
+ Display = Translations.Optimizer_UI_Risk_Moderate,
+ Icon = SymbolRegular.Warning24
+ },
+ _ => new RiskVisual
+ {
+ Display = Translations.Optimizer_UI_Risk_Safe,
+ Icon = SymbolRegular.ShieldCheckmark24
+ }
+ };
+};
\ No newline at end of file
diff --git a/optimizerDuck/Core/Models/Bloatware/AppxPackage.cs b/optimizerDuck/Core/Models/Bloatware/AppxPackage.cs
deleted file mode 100644
index 7bdaafe..0000000
--- a/optimizerDuck/Core/Models/Bloatware/AppxPackage.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using CommunityToolkit.Mvvm.ComponentModel;
-
-namespace optimizerDuck.Core.Models.Bloatware;
-
-public enum AppRisk
-{
- Safe,
- Caution
-}
-
-public partial class AppxPackage : ObservableObject
-{
- [ObservableProperty] private bool _isSelected;
-
- public string Name { get; init; }
- public string PackageFullName { get; init; }
- public string Publisher { get; init; }
- public string Version { get; init; }
- public string InstallLocation { get; init; }
- public AppRisk Risk { get; init; }
-};
\ No newline at end of file
diff --git a/optimizerDuck/Core/Models/UI/OptimizationRiskVisual.cs b/optimizerDuck/Core/Models/UI/RiskVisual.cs
similarity index 82%
rename from optimizerDuck/Core/Models/UI/OptimizationRiskVisual.cs
rename to optimizerDuck/Core/Models/UI/RiskVisual.cs
index f390aed..836b5c9 100644
--- a/optimizerDuck/Core/Models/UI/OptimizationRiskVisual.cs
+++ b/optimizerDuck/Core/Models/UI/RiskVisual.cs
@@ -2,7 +2,7 @@ using Wpf.Ui.Controls;
namespace optimizerDuck.Core.Models.UI;
-public class OptimizationRiskVisual
+public class RiskVisual
{
public string Display { get; init; } = string.Empty;
public SymbolRegular Icon { get; init; }
diff --git a/optimizerDuck/Core/Optimizers/BaseOptimization.cs b/optimizerDuck/Core/Optimizers/BaseOptimization.cs
index 545d53e..1161a51 100644
--- a/optimizerDuck/Core/Optimizers/BaseOptimization.cs
+++ b/optimizerDuck/Core/Optimizers/BaseOptimization.cs
@@ -26,24 +26,24 @@ public abstract class BaseOptimization : IOptimization
?? throw new InvalidOperationException(
$"{GetType().Name} has no owner assigned");
- public OptimizationRiskVisual RiskVisual => Risk switch
+ public RiskVisual RiskVisual => Risk switch
{
- OptimizationRisk.Safe => new OptimizationRiskVisual
+ OptimizationRisk.Safe => new RiskVisual
{
Display = Translations.Optimizer_UI_Risk_Safe,
Icon = SymbolRegular.ShieldCheckmark24
},
- OptimizationRisk.Moderate => new OptimizationRiskVisual
+ OptimizationRisk.Moderate => new RiskVisual
{
Display = Translations.Optimizer_UI_Risk_Moderate,
Icon = SymbolRegular.Warning24
},
- OptimizationRisk.Risky => new OptimizationRiskVisual
+ OptimizationRisk.Risky => new RiskVisual
{
Display = Translations.Optimizer_UI_Risk_Risky,
Icon = SymbolRegular.ShieldError24
},
- _ => new OptimizationRiskVisual
+ _ => new RiskVisual
{
Display = Translations.Optimizer_UI_Risk_Safe,
Icon = SymbolRegular.ShieldCheckmark24
diff --git a/optimizerDuck/Services/BloatwareService.cs b/optimizerDuck/Services/BloatwareService.cs
index fe43198..38b0334 100644
--- a/optimizerDuck/Services/BloatwareService.cs
+++ b/optimizerDuck/Services/BloatwareService.cs
@@ -11,7 +11,7 @@ namespace optimizerDuck.Services;
public class BloatwareService(ILogger<BloatwareService> logger)
{
- public async Task<List<AppxPackage>> GetAppXPackagesAsync()
+ public async Task<List<AppXPackage>> GetAppXPackagesAsync()
{
try
{
@@ -67,10 +67,10 @@ public class BloatwareService(ILogger<BloatwareService> logger)
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
- Converters = { new JsonStringEnumConverter()}
+ Converters = { new JsonStringEnumConverter() }
};
- var apps = JsonSerializer.Deserialize<List<AppxPackage>>(result.Stdout, options);
+ var apps = JsonSerializer.Deserialize<List<AppXPackage>>(result.Stdout, options)!.OrderBy(a => a.Risk).ToList();
logger.LogInformation("Found {AppCount} AppX packages", apps.Count);
@@ -78,8 +78,64 @@ public class BloatwareService(ILogger<BloatwareService> logger)
}
catch (Exception ex)
{
- logger.LogError(ex, "Failed to get bloatware choices");
+ logger.LogError(ex, "Failed to get AppX packages");
return [];
}
}
+
+ public async Task RemoveAppXPackage(AppXPackage appXPackage)
+ {
+ try
+ {
+ if (string.IsNullOrWhiteSpace(appXPackage.PackageFullName))
+ {
+ logger.LogWarning("Skip removing app because PackageFullName is empty: {Name}", appXPackage.Name);
+ return;
+ }
+
+ logger.LogInformation("Removing AppX package {Name} ({Package})",
+ appXPackage.Name, appXPackage.PackageFullName);
+
+ var script = $$"""
+ $pkg = "{{appXPackage.PackageFullName}}"
+
+ Write-Output "Removing installed package..."
+
+ # remove for current + all users
+ Get-AppxPackage -AllUsers | Where-Object { $_.PackageFullName -eq $pkg } | ForEach-Object {
+ try {
+ Remove-AppxPackage -Package $_.PackageFullName -AllUsers -ErrorAction Stop
+ Write-Output "Removed installed package: $($_.PackageFullName)"
+ } catch {
+ Write-Output "Failed removing installed package: $($_.PackageFullName)"
+ }
+ }
+
+ Write-Output "Removing provisioned package..."
+
+ # remove provisioned version so it doesn't reinstall
+ Get-AppxProvisionedPackage -Online |
+ Where-Object { $_.PackageName -like "*$pkg*" } |
+ ForEach-Object {
+ try {
+ Remove-AppxProvisionedPackage -Online -PackageName $_.PackageName -ErrorAction Stop | Out-Null
+ Write-Output "Removed provisioned package: $($_.PackageName)"
+ } catch {
+ Write-Output "Failed removing provisioned package: $($_.PackageName)"
+ }
+ }
+ """;
+
+ var result = await ShellService.PowerShellAsync(script);
+
+ if (!string.IsNullOrWhiteSpace(result.Stderr))
+ logger.LogWarning("Remove AppX stderr: {Error}", result.Stderr);
+
+ logger.LogInformation("Remove AppX finished for {Name}", appXPackage.Name);
+ }
+ catch (Exception ex)
+ {
+ logger.LogError(ex, "Failed removing AppX package {Name}", appXPackage.Name);
+ }
+ }
}
\ No newline at end of file
diff --git a/optimizerDuck/Services/OptimizationService.cs b/optimizerDuck/Services/OptimizationService.cs
index 5f0906f..4b079c8 100644
--- a/optimizerDuck/Services/OptimizationService.cs
+++ b/optimizerDuck/Services/OptimizationService.cs
@@ -31,8 +31,8 @@ public class OptimizationService(
public async Task<bool> CreateRestorePointAsync()
{
- var dialogViewModel = new ProcessingOptimizationViewModel();
- var dialogContent = new ProcessingOptimizationDialog { DataContext = dialogViewModel };
+ var dialogViewModel = new ProcessingViewModel();
+ var dialogContent = new ProcessingDialog { DataContext = dialogViewModel };
var dialog = new ContentDialog
{
diff --git a/optimizerDuck/UI/ViewModels/Dialogs/ProcessingOptimizationViewModel.cs b/optimizerDuck/UI/ViewModels/Dialogs/ProcessingViewModel.cs
similarity index 84%
rename from optimizerDuck/UI/ViewModels/Dialogs/ProcessingOptimizationViewModel.cs
rename to optimizerDuck/UI/ViewModels/Dialogs/ProcessingViewModel.cs
index a5a4699..35f3f0e 100644
--- a/optimizerDuck/UI/ViewModels/Dialogs/ProcessingOptimizationViewModel.cs
+++ b/optimizerDuck/UI/ViewModels/Dialogs/ProcessingViewModel.cs
@@ -3,14 +3,14 @@ using optimizerDuck.Core.Models.UI;
namespace optimizerDuck.UI.ViewModels.Dialogs;
-public partial class ProcessingOptimizationViewModel : ObservableObject
+public partial class ProcessingViewModel : ObservableObject
{
[ObservableProperty] private bool _isIndeterminate;
[ObservableProperty] private string? _message;
[ObservableProperty] private int _total;
[ObservableProperty] private int _value;
- public ProcessingOptimizationViewModel()
+ public ProcessingViewModel()
{
ProgressReporter = new Progress<ProcessingProgress>(p =>
{
diff --git a/optimizerDuck/UI/ViewModels/Optimizer/OptimizationCategoryViewModel.cs b/optimizerDuck/UI/ViewModels/Optimizer/OptimizationCategoryViewModel.cs
index 0b81d02..4c43afe 100644
--- a/optimizerDuck/UI/ViewModels/Optimizer/OptimizationCategoryViewModel.cs
+++ b/optimizerDuck/UI/ViewModels/Optimizer/OptimizationCategoryViewModel.cs
@@ -64,11 +64,11 @@ public partial class OptimizationCategoryViewModel : ViewModel
IOptimization optimization,
Func<IProgress<ProcessingProgress>, Task<T>> action)
{
- var viewModel = new ProcessingOptimizationViewModel();
+ var viewModel = new ProcessingViewModel();
var dialog = new ContentDialog
{
Title = BuildDialogTitle(optimization),
- Content = new ProcessingOptimizationDialog { DataContext = viewModel },
+ Content = new ProcessingDialog { DataContext = viewModel },
IsFooterVisible = false
};
diff --git a/optimizerDuck/UI/ViewModels/Pages/BloatwareViewModel.cs b/optimizerDuck/UI/ViewModels/Pages/BloatwareViewModel.cs
index b69c265..594dff4 100644
--- a/optimizerDuck/UI/ViewModels/Pages/BloatwareViewModel.cs
+++ b/optimizerDuck/UI/ViewModels/Pages/BloatwareViewModel.cs
@@ -4,8 +4,13 @@ using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using optimizerDuck.Core.Models.Bloatware;
+using optimizerDuck.Core.Models.UI;
using optimizerDuck.Services;
using optimizerDuck.UI.ViewModels;
+using optimizerDuck.UI.ViewModels.Dialogs;
+using optimizerDuck.UI.Views.Dialogs;
+using Wpf.Ui;
+using Wpf.Ui.Controls;
namespace optimizerDuck.UI.ViewModels.Pages;
@@ -14,25 +19,27 @@ public partial class BloatwareViewModel : ViewModel
private bool _isInitialized;
private readonly BloatwareService _bloatwareService;
+ private readonly IContentDialogService _contentDialogService;
- public ObservableCollection<AppxPackage> AppxPackages { get; set; } = [];
+ public ObservableCollection<AppXPackage> AppxPackages { get; private set; } = [];
public bool HasSelectedItems => AppxPackages.Any(x => x.IsSelected);
public int SelectedCount => AppxPackages.Count(x => x.IsSelected);
- public BloatwareViewModel(BloatwareService bloatwareService)
+ public BloatwareViewModel(BloatwareService bloatwareService, IContentDialogService contentDialogService)
{
_bloatwareService = bloatwareService;
+ _contentDialogService = contentDialogService;
AppxPackages.CollectionChanged += (_, e) =>
{
if (e.NewItems != null)
- foreach (AppxPackage p in e.NewItems)
+ foreach (AppXPackage p in e.NewItems)
p.PropertyChanged += Item_PropertyChanged;
if (e.OldItems != null)
- foreach (AppxPackage p in e.OldItems)
+ foreach (AppXPackage p in e.OldItems)
p.PropertyChanged -= Item_PropertyChanged;
UpdateProperties();
@@ -55,23 +62,61 @@ public partial class BloatwareViewModel : ViewModel
#region Commands
[RelayCommand(CanExecute = nameof(CanRemoveSelected))]
- private void RemoveSelected()
+ private async Task RemoveSelected()
{
- var toRemove = AppxPackages.Where(x => x.IsSelected).ToList();
+ var viewModel = new ProcessingViewModel();
+ var dialog = new ContentDialog
+ {
+ Title = "Removing bloatware!",
+ Content = new ProcessingDialog { DataContext = viewModel },
+ IsFooterVisible = false
+ };
- foreach (var item in toRemove)
- AppxPackages.Remove(item);
+ try
+ {
+ _ = _contentDialogService.ShowAsync(dialog, CancellationToken.None);
+
+ var toRemove = AppxPackages.Where(x => x.IsSelected).ToList();
+
+ for (var i = 0; i < toRemove.Count; i++)
+ {
+ var item = toRemove[i];
+ viewModel.ProgressReporter.Report(new ProcessingProgress
+ {
+ Message = $"{i + 1} / {toRemove.Count}"
+ });
+ //await _bloatwareService.RemoveAppXPackage(item);
+ await Task.Delay(1000);
+ }
+ }
+ finally
+ {
+ dialog.Hide();
+ await Refresh();
+ }
}
private bool CanRemoveSelected() => HasSelectedItems;
+ [RelayCommand]
+ private async Task Refresh()
+ {
+ AppxPackages.Clear();
+ UpdateProperties();
+
+ var appxPackages = await _bloatwareService.GetAppXPackagesAsync();
+ foreach (var package in appxPackages)
+ AppxPackages.Add(package);
+ }
+
+
#endregion
#region Property Changed
private void Item_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
- if (e.PropertyName == nameof(AppxPackage.IsSelected))
+ if (e.PropertyName == nameof(AppXPackage.IsSelected))
{
UpdateProperties();
RemoveSelectedCommand.NotifyCanExecuteChanged();
diff --git a/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs b/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs
index 4583465..334fdb5 100644
--- a/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs
+++ b/optimizerDuck/UI/ViewModels/Pages/DashboardViewModel.cs
@@ -31,7 +31,7 @@ public partial class DashboardViewModel : ViewModel
[ObservableProperty] private RamInfo _runtimeRam = RamInfo.Unknown;
[ObservableProperty] private SystemSnapshot _systemInfo = SystemSnapshot.Unknown;
- private bool _updateNotified; // phân biệt close do user
+ private bool _updateNotified;
public DashboardViewModel(SystemInfoService systemInfoService, ISnackbarService snackbarService,
ILogger<DashboardViewModel> logger, UpdaterService updaterService, IContentDialogService contentDialogService)
@@ -63,7 +63,6 @@ public partial class DashboardViewModel : ViewModel
_isUpdateInfoOpen = value;
OnPropertyChanged();
- // User vừa bấm ❌
if (!_isUpdateInfoOpen && _updateNotified) OpenLatestRelease();
}
}
diff --git a/optimizerDuck/UI/Views/Dialogs/ProcessingOptimizationDialog.xaml b/optimizerDuck/UI/Views/Dialogs/ProcessingDialog.xaml
similarity index 92%
rename from optimizerDuck/UI/Views/Dialogs/ProcessingOptimizationDialog.xaml
rename to optimizerDuck/UI/Views/Dialogs/ProcessingDialog.xaml
index c24baff..528088f 100644
--- a/optimizerDuck/UI/Views/Dialogs/ProcessingOptimizationDialog.xaml
+++ b/optimizerDuck/UI/Views/Dialogs/ProcessingDialog.xaml
@@ -2,7 +2,7 @@
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d"
- x:Class="optimizerDuck.UI.Views.Dialogs.ProcessingOptimizationDialog"
+ x:Class="optimizerDuck.UI.Views.Dialogs.ProcessingDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:extensions="clr-namespace:optimizerDuck.Common.Extensions"
diff --git a/optimizerDuck/UI/Views/Dialogs/ProcessingOptimizationDialog.xaml.cs b/optimizerDuck/UI/Views/Dialogs/ProcessingDialog.xaml.cs
similarity index 55%
rename from optimizerDuck/UI/Views/Dialogs/ProcessingOptimizationDialog.xaml.cs
rename to optimizerDuck/UI/Views/Dialogs/ProcessingDialog.xaml.cs
index 6ea7425..768c101 100644
--- a/optimizerDuck/UI/Views/Dialogs/ProcessingOptimizationDialog.xaml.cs
+++ b/optimizerDuck/UI/Views/Dialogs/ProcessingDialog.xaml.cs
@@ -6,12 +6,12 @@ namespace optimizerDuck.UI.Views.Dialogs;
/// <summary>
/// Interaction logic for ProcessingOptimizationDialog.xaml
/// </summary>
-public partial class ProcessingOptimizationDialog : UserControl
+public partial class ProcessingDialog : UserControl
{
- public ProcessingOptimizationDialog()
+ public ProcessingDialog()
{
InitializeComponent();
}
- public ProcessingOptimizationViewModel? ViewModel => DataContext as ProcessingOptimizationViewModel;
+ public ProcessingViewModel? ViewModel => DataContext as ProcessingViewModel;
}
\ No newline at end of file
diff --git a/optimizerDuck/UI/Views/Pages/BloatwarePage.xaml b/optimizerDuck/UI/Views/Pages/BloatwarePage.xaml
index cbfd741..800bdf6 100644
--- a/optimizerDuck/UI/Views/Pages/BloatwarePage.xaml
+++ b/optimizerDuck/UI/Views/Pages/BloatwarePage.xaml
@@ -57,7 +57,11 @@
Header="{Binding ViewModel.SelectedCount, StringFormat='Remove ({0})'}"
Icon="{ui:SymbolIcon Delete24,
FontSize=18}" />
- <ui:MenuItem Header="Refresh" Icon="{ui:SymbolIcon ArrowClockwise24, FontSize=18}" />
+ <ui:MenuItem
+ Command="{Binding ViewModel.RefreshCommand}"
+ Header="Refresh"
+ Icon="{ui:SymbolIcon ArrowClockwise24,
+ FontSize=18}" />
</Menu>
<ui:VirtualizingItemsControl Grid.Row="1" ItemsSource="{Binding ViewModel.AppxPackages}">
<ui:VirtualizingItemsControl.Template>
@@ -76,7 +80,7 @@
<!-- Item template -->
<ui:VirtualizingItemsControl.ItemTemplate>
- <DataTemplate DataType="{x:Type bloatware:AppxPackage}">
+ <DataTemplate DataType="{x:Type bloatware:AppXPackage}">
<Border
Background="{DynamicResource CardBackgroundFillColorDefaultBrush}"
CornerRadius="6"
@@ -106,13 +110,51 @@
<!-- App Name -->
<ui:TextBlock FontTypography="BodyStrong" Text="{Binding Name}" />
- <!-- Version -->
- <ui:TextBlock
- Appearance="Tertiary"
- FontTypography="Body"
+ <Border
+ Background="{DynamicResource CardBackgroundFillColorSecondaryBrush}"
+ CornerRadius="5"
+ HorizontalAlignment="Left"
Margin="5,0,0,0"
- Text="{Binding Version, StringFormat=' v{0}'}"
- VerticalAlignment="Center" />
+ Padding="4,1"
+ VerticalAlignment="Center">
+ <!-- Version -->
+ <ui:TextBlock
+ Appearance="Tertiary"
+ FontTypography="Body"
+ Text="{Binding Version, StringFormat='v{0}'}"
+ VerticalAlignment="Center" />
+ </Border>
+
+ <Border
+ CornerRadius="5"
+ HorizontalAlignment="Left"
+ Margin="5,0,0,0"
+ Padding="4,2">
+ <Border.Style>
+ <Style TargetType="Border">
+ <Setter Property="Background" Value="{DynamicResource SystemFillColorSuccessBrush}" />
+ <Style.Triggers>
+ <DataTrigger Binding="{Binding Risk}" Value="Caution">
+ <Setter Property="Background" Value="{DynamicResource SystemFillColorCautionBrush}" />
+ </DataTrigger>
+ </Style.Triggers>
+ </Style>
+ </Border.Style>
+ <StackPanel Orientation="Horizontal">
+ <ui:SymbolIcon
+ Foreground="{DynamicResource TextFillColorInverseBrush}"
+ Margin="0,0,5,0"
+ Symbol="{Binding RiskVisual.Icon}" />
+
+ <ui:TextBlock
+ FontTypography="Caption"
+ Foreground="{DynamicResource TextFillColorInverseBrush}"
+ HorizontalAlignment="Center"
+ Text="{Binding RiskVisual.Display}"
+ TextWrapping="Wrap"
+ VerticalAlignment="Center" />
+ </StackPanel>
+ </Border>
</DockPanel>