| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BusyTaskbarCoordinator.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.ComponentModel; |
| | | 9 | | using MyNet.UI.Loading; |
| | | 10 | | using MyNet.UI.Loading.Models; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.ViewModels.Shell.Taskbar; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Synchronizes application <see cref="IBusyService"/> state with <see cref="ITaskbarProgressSource"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class BusyTaskbarCoordinator : IDisposable |
| | | 18 | | { |
| | | 19 | | private readonly IBusyService _applicationBusy; |
| | | 20 | | private readonly ITaskbarProgressSource _taskbarProgress; |
| | | 21 | | private ProgressionBusy? _subscribedProgression; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="BusyTaskbarCoordinator"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | public BusyTaskbarCoordinator(IBusyService applicationBusy, ITaskbarProgressSource taskbarProgress) |
| | | 27 | | { |
| | 12 | 28 | | _applicationBusy = applicationBusy ?? throw new ArgumentNullException(nameof(applicationBusy)); |
| | 12 | 29 | | _taskbarProgress = taskbarProgress ?? throw new ArgumentNullException(nameof(taskbarProgress)); |
| | | 30 | | |
| | 12 | 31 | | _applicationBusy.PropertyChanged += OnApplicationBusyPropertyChanged; |
| | 12 | 32 | | SyncFromApplicationBusy(); |
| | 12 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public void Dispose() |
| | | 37 | | { |
| | 12 | 38 | | _applicationBusy.PropertyChanged -= OnApplicationBusyPropertyChanged; |
| | 12 | 39 | | UnsubscribeProgression(); |
| | 12 | 40 | | } |
| | | 41 | | |
| | | 42 | | private void OnApplicationBusyPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 43 | | { |
| | 36 | 44 | | if (e.PropertyName is not (nameof(IBusyService.IsBusy) or nameof(IBusyService.CurrentBusy))) |
| | 0 | 45 | | return; |
| | | 46 | | |
| | 36 | 47 | | SyncFromApplicationBusy(); |
| | 36 | 48 | | } |
| | | 49 | | |
| | | 50 | | private void SyncFromApplicationBusy() |
| | | 51 | | { |
| | 48 | 52 | | var progressionBusy = _applicationBusy.GetCurrent<ProgressionBusy>(); |
| | 48 | 53 | | UpdateProgressionSubscription(progressionBusy); |
| | | 54 | | |
| | 48 | 55 | | var progressState = _applicationBusy.IsBusy |
| | 48 | 56 | | ? TaskbarProgressState.Indeterminate |
| | 48 | 57 | | : _taskbarProgress.ProgressState == TaskbarProgressState.Error |
| | 48 | 58 | | ? TaskbarProgressState.Error |
| | 48 | 59 | | : TaskbarProgressState.None; |
| | | 60 | | |
| | 48 | 61 | | _taskbarProgress.SetProgress(progressState); |
| | 48 | 62 | | } |
| | | 63 | | |
| | | 64 | | private void UpdateProgressionSubscription(ProgressionBusy? progressionBusy) |
| | | 65 | | { |
| | 48 | 66 | | if (ReferenceEquals(_subscribedProgression, progressionBusy)) |
| | 36 | 67 | | return; |
| | | 68 | | |
| | 12 | 69 | | UnsubscribeProgression(); |
| | | 70 | | |
| | 12 | 71 | | _subscribedProgression = progressionBusy; |
| | | 72 | | |
| | 12 | 73 | | if (_subscribedProgression is not null && _applicationBusy.IsBusy) |
| | 6 | 74 | | _subscribedProgression.PropertyChanged += OnProgressionBusyPropertyChanged; |
| | 12 | 75 | | } |
| | | 76 | | |
| | | 77 | | private void UnsubscribeProgression() |
| | | 78 | | { |
| | 24 | 79 | | if (_subscribedProgression is null) |
| | 18 | 80 | | return; |
| | | 81 | | |
| | 6 | 82 | | _subscribedProgression.PropertyChanged -= OnProgressionBusyPropertyChanged; |
| | 6 | 83 | | _subscribedProgression = null; |
| | 6 | 84 | | } |
| | | 85 | | |
| | | 86 | | private void OnProgressionBusyPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 87 | | { |
| | 9 | 88 | | if (sender is not ProgressionBusy progressionBusy) |
| | 0 | 89 | | return; |
| | | 90 | | |
| | 9 | 91 | | switch (e.PropertyName) |
| | | 92 | | { |
| | | 93 | | case nameof(ProgressionBusy.Value): |
| | 6 | 94 | | _taskbarProgress.SetProgress(TaskbarProgressState.Normal, progressionBusy.Value); |
| | 6 | 95 | | break; |
| | | 96 | | |
| | | 97 | | case nameof(ProgressionBusy.IsCancelling): |
| | 3 | 98 | | _taskbarProgress.SetProgress(TaskbarProgressState.Paused, progressionBusy.Value); |
| | | 99 | | break; |
| | | 100 | | } |
| | 3 | 101 | | } |
| | | 102 | | } |
| | | 103 | | |