From d02f8b4a39ea05cab64019c34ff4c54d88807877 Mon Sep 17 00:00:00 2001 From: JT Date: Sat, 27 May 2023 20:08:26 -0700 Subject: [PATCH] Added progress to taskbar icon --- StabilityMatrix/Helper/EventManager.cs | 16 +++++++ StabilityMatrix/MainWindow.xaml | 6 +++ .../ViewModels/InstallerViewModel.cs | 5 ++- .../ViewModels/MainWindowViewModel.cs | 42 ++++++++++++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 StabilityMatrix/Helper/EventManager.cs diff --git a/StabilityMatrix/Helper/EventManager.cs b/StabilityMatrix/Helper/EventManager.cs new file mode 100644 index 00000000..455edc1b --- /dev/null +++ b/StabilityMatrix/Helper/EventManager.cs @@ -0,0 +1,16 @@ +using System; + +namespace StabilityMatrix.Helper; + +public class EventManager +{ + public static EventManager Instance { get; } = new(); + + private EventManager() + { + + } + + public event EventHandler? GlobalProgressChanged; + public void OnGlobalProgressChanged(int progress) => GlobalProgressChanged?.Invoke(this, progress); +} diff --git a/StabilityMatrix/MainWindow.xaml b/StabilityMatrix/MainWindow.xaml index 2b73df67..52b8d2b5 100644 --- a/StabilityMatrix/MainWindow.xaml +++ b/StabilityMatrix/MainWindow.xaml @@ -19,6 +19,12 @@ xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> + + + + + diff --git a/StabilityMatrix/ViewModels/InstallerViewModel.cs b/StabilityMatrix/ViewModels/InstallerViewModel.cs index 3a6d3ced..39baefaa 100644 --- a/StabilityMatrix/ViewModels/InstallerViewModel.cs +++ b/StabilityMatrix/ViewModels/InstallerViewModel.cs @@ -9,6 +9,7 @@ using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.Logging; +using EventManager = StabilityMatrix.Helper.EventManager; namespace StabilityMatrix.ViewModels; @@ -86,7 +87,7 @@ public partial class InstallerViewModel : ObservableObject InstalledText = "Done"; IsIndeterminate = false; - ProgressValue = 100; + SelectedPackageOnProgressChanged(this, 100); if (settingsManager.Settings.InstalledPackages.FirstOrDefault(x => x.PackageName == SelectedPackage.Name) == null) @@ -133,6 +134,8 @@ public partial class InstallerViewModel : ObservableObject IsIndeterminate = false; ProgressValue = progress; } + + EventManager.Instance.OnGlobalProgressChanged(progress); } private async Task InstallGitIfNecessary() diff --git a/StabilityMatrix/ViewModels/MainWindowViewModel.cs b/StabilityMatrix/ViewModels/MainWindowViewModel.cs index 0095a9e3..2a0b584a 100644 --- a/StabilityMatrix/ViewModels/MainWindowViewModel.cs +++ b/StabilityMatrix/ViewModels/MainWindowViewModel.cs @@ -1,8 +1,14 @@ -using System.Windows; +using System; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Shell; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using Python.Runtime; using StabilityMatrix.Helper; using Wpf.Ui.Appearance; +using Dispatcher = System.Windows.Threading.Dispatcher; +using EventManager = StabilityMatrix.Helper.EventManager; namespace StabilityMatrix.ViewModels; @@ -20,11 +26,45 @@ public partial class MainWindowViewModel : ObservableObject [ObservableProperty] private Visibility simpleModeVisibility; + + [ObservableProperty] + private float progressValue; + + [ObservableProperty] + private TaskbarItemProgressState progressState; public void OnLoaded() { SetTheme(); GoAdvancedMode(); + EventManager.Instance.GlobalProgressChanged += OnGlobalProgressChanged; + } + + private void OnGlobalProgressChanged(object? sender, int progress) + { + if (progress == -1) + { + ProgressState = TaskbarItemProgressState.Indeterminate; + ProgressValue = 0; + } + else + { + ProgressState = TaskbarItemProgressState.Normal; + ProgressValue = progress / 100f; + } + + if (Math.Abs(ProgressValue - 1) < 0.01f) + { + Task.Run(async () => + { + await Task.Delay(5000); + Dispatcher.CurrentDispatcher.Invoke(() => + { + ProgressState = TaskbarItemProgressState.None; + ProgressValue = 0; + }); + }); + } } [RelayCommand]