| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Busy.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Windows.Input; |
| | | 10 | | using MyNet.Observable; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.Loading.Models; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents a busy indicator that supports cancellation. |
| | | 17 | | /// </summary> |
| | | 18 | | public class Busy : ObservableObject, IBusy |
| | | 19 | | { |
| | | 20 | | private CancellationTokenSource? _cts; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="Busy"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | public Busy() |
| | 114 | 26 | | : this(RelayCommandFactory.Default) |
| | | 27 | | { |
| | 114 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="Busy"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="commandFactory">Command factory used to create the cancellation command.</param> |
| | 114 | 34 | | public Busy(ICommandFactory commandFactory) |
| | | 35 | | { |
| | 114 | 36 | | ArgumentNullException.ThrowIfNull(commandFactory); |
| | | 37 | | CancelCommand = commandFactory.Create(Cancel, () => IsCancellable && CanCancel && !IsCancelling); |
| | 114 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the cancellation token associated with this busy operation. |
| | | 42 | | /// </summary> |
| | 0 | 43 | | public CancellationToken CancellationToken => _cts?.Token ?? CancellationToken.None; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets a value indicating whether cancellation is possible. |
| | | 47 | | /// </summary> |
| | 3 | 48 | | public bool IsCancellable => _cts is { IsCancellationRequested: false }; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the command used to trigger cancellation. |
| | | 52 | | /// </summary> |
| | | 53 | | public ICommand CancelCommand { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a value indicating whether cancellation is in progress. |
| | | 57 | | /// </summary> |
| | | 58 | | public bool IsCancelling { get; private set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets a value indicating whether cancellation can be triggered by the user. |
| | | 62 | | /// </summary> |
| | 0 | 63 | | public bool CanCancel { get; set => SetProperty(ref field, value); } = true; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Binds a cancellation source to this busy indicator. |
| | | 67 | | /// Called by <see cref="Loading.BusyService"/> when a scope starts. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="cancellationTokenSource">The source that drives <see cref="CancellationToken"/> and user-initiated |
| | | 70 | | public void BindCancellation(CancellationTokenSource cancellationTokenSource) |
| | | 71 | | { |
| | 108 | 72 | | ArgumentNullException.ThrowIfNull(cancellationTokenSource); |
| | 108 | 73 | | _cts = cancellationTokenSource; |
| | 108 | 74 | | IsCancelling = false; |
| | 108 | 75 | | NotifyPropertyChanged(nameof(IsCancellable)); |
| | 108 | 76 | | NotifyPropertyChanged(nameof(IsCancelling)); |
| | 108 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Requests cancellation of the busy operation. |
| | | 81 | | /// </summary> |
| | | 82 | | public void Cancel() |
| | | 83 | | { |
| | 3 | 84 | | if (!IsCancellable) |
| | 0 | 85 | | return; |
| | | 86 | | |
| | 3 | 87 | | IsCancelling = true; |
| | 3 | 88 | | NotifyPropertyChanged(nameof(IsCancelling)); |
| | | 89 | | |
| | 3 | 90 | | _cts?.Cancel(); |
| | 3 | 91 | | } |
| | | 92 | | } |
| | | 93 | | |