-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathStatusCenterItem.cs
More file actions
413 lines (341 loc) · 12.9 KB
/
Copy pathStatusCenterItem.cs
File metadata and controls
413 lines (341 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) Files Community
// Licensed under the MIT License.
using Microsoft.UI.Xaml.Media;
using System.Numerics;
using System.Windows.Input;
namespace Files.App.Utils.StatusCenter
{
/// <summary>
/// Represents an item for Status Center operation tasks.
/// <br/>
/// Handles all operation's functionality and UI.
/// </summary>
public sealed partial class StatusCenterItem : ObservableObject
{
private readonly StatusCenterViewModel _viewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
private int _ProgressPercentage;
public int ProgressPercentage
{
get => _ProgressPercentage;
set
{
ProgressPercentageText = $"{value}%";
SetProperty(ref _ProgressPercentage, value);
}
}
private string? _Header;
public string? Header
{
get => _Header;
set => SetProperty(ref _Header, value);
}
// Currently, shown on the tooltip
private string? _SubHeader;
public string? SubHeader
{
get => _SubHeader;
set => SetProperty(ref _SubHeader, value);
}
private string? _Message;
public string? Message
{
get => _Message;
set => SetProperty(ref _Message, value);
}
private string? _SpeedText;
public string? SpeedText
{
get => _SpeedText;
set => SetProperty(ref _SpeedText, value);
}
private string? _ProgressPercentageText;
public string? ProgressPercentageText
{
get => _ProgressPercentageText;
set => SetProperty(ref _ProgressPercentageText, value);
}
// Gets or sets the value that represents the current processing item name.
private string? _CurrentProcessingItemName;
public string? CurrentProcessingItemName
{
get => _CurrentProcessingItemName;
set => SetProperty(ref _CurrentProcessingItemName, value);
}
// TODO: Remove and replace with Message
private string? _CurrentProcessedSizeText;
public string? CurrentProcessedSizeHumanized
{
get => _CurrentProcessedSizeText;
set => SetProperty(ref _CurrentProcessedSizeText, value);
}
// This property is basically handled by an UI element - ToggleButton
private bool _IsExpanded;
public bool IsExpanded
{
get => _IsExpanded;
set
{
AnimatedIconState = value ? "NormalOn" : "NormalOff";
SetProperty(ref _IsExpanded, value);
}
}
// This property is used for AnimatedIcon state
private string? _AnimatedIconState;
public string? AnimatedIconState
{
get => _AnimatedIconState;
set => SetProperty(ref _AnimatedIconState, value);
}
// If true, the chevron won't be shown.
// This property will be false basically if the proper progress report is not supported in the operation.
private bool _IsSpeedAndProgressAvailable;
public bool IsSpeedAndProgressAvailable
{
get => _IsSpeedAndProgressAvailable;
set => SetProperty(ref _IsSpeedAndProgressAvailable, value);
}
// This property will be true basically if the operation was canceled or the operation doesn't support proper progress update.
private bool _IsIndeterminateProgress;
public bool IsIndeterminateProgress
{
get => _IsIndeterminateProgress;
set => SetProperty(ref _IsIndeterminateProgress, value);
}
// This property will be true if the item card is for in-progress and the operation supports cancellation token also.
private bool _IsCancelable;
public bool IsCancelable
{
get => _IsCancelable;
set => SetProperty(ref _IsCancelable, value);
}
// This property is not updated for now. Should be removed.
private StatusCenterItemProgressModel _Progress = null!;
public StatusCenterItemProgressModel Progress
{
get => _Progress;
set => SetProperty(ref _Progress, value);
}
public ReturnResult FileSystemOperationReturnResult { get; private set; }
public FileOperationType Operation { get; private set; }
public StatusCenterItemKind ItemKind { get; private set; }
public StatusCenterItemIconKind ItemIconKind { get; private set; }
public long TotalSize { get; private set; }
public long TotalItemsCount { get; private set; }
public bool IsInProgress { get; private set; }
public bool IsDiscovering { get; private set; } = true;
public IEnumerable<string>? Source { get; private set; }
public IEnumerable<string>? Destination { get; private set; }
public string? HeaderStringResource { get; private set; }
public string? SubHeaderStringResource { get; private set; }
public double IconBackgroundCircleBorderOpacity { get; private set; }
public CancellationToken CancellationToken
=> _operationCancellationToken?.Token ?? default;
public string? HeaderTooltip
=> string.IsNullOrWhiteSpace(SubHeader) ? SubHeader : Header;
public readonly Progress<StatusCenterItemProgressModel> ProgressEventSource;
private readonly CancellationTokenSource? _operationCancellationToken;
public readonly ObservableCollection<Vector2> SpeedGraphValues;
public ICommand CancelCommand { get; }
public StatusCenterItem(
string headerResource,
string subHeaderResource,
ReturnResult status,
FileOperationType operation,
IEnumerable<string>? source,
IEnumerable<string>? destination,
bool canProvideProgress = false,
long itemsCount = 0,
long totalSize = 0,
CancellationTokenSource? operationCancellationToken = default)
{
_operationCancellationToken = operationCancellationToken;
Header = headerResource == string.Empty ? headerResource : headerResource.GetLocalizedResource();
HeaderStringResource = headerResource;
SubHeader = subHeaderResource == string.Empty ? subHeaderResource : subHeaderResource.GetLocalizedResource();
SubHeaderStringResource = subHeaderResource;
FileSystemOperationReturnResult = status;
Operation = operation;
ProgressEventSource = new Progress<StatusCenterItemProgressModel>(ReportProgress);
Progress = new(ProgressEventSource, status: FileSystemStatusCode.InProgress);
IsCancelable = _operationCancellationToken is not null;
TotalItemsCount = itemsCount;
TotalSize = totalSize;
IconBackgroundCircleBorderOpacity = 1;
AnimatedIconState = "NormalOff";
SpeedGraphValues = [];
CancelCommand = new RelayCommand(ExecuteCancelCommand);
Message = Strings.DiscoveringItems.GetLocalizedResource();
// Status text only uses the first path. Retaining every path keeps large completed
// operations and their source storage objects alive for the lifetime of the card.
Source = source?.Take(1).ToArray();
Destination = destination?.Take(1).ToArray();
// Get the graph color
if (App.Current.Resources["App.Theme.FillColorAttentionBrush"] is not SolidColorBrush accentBrush)
return;
// Set icon and initialize string resources
switch (FileSystemOperationReturnResult)
{
case ReturnResult.InProgress:
{
IsSpeedAndProgressAvailable = canProvideProgress;
IsInProgress = true;
IsIndeterminateProgress = !canProvideProgress;
IconBackgroundCircleBorderOpacity = 0.1d;
if (Operation is FileOperationType.Prepare)
Header = "StatusCenter_PrepareInProgress".GetLocalizedResource();
ItemKind = StatusCenterItemKind.InProgress;
ItemIconKind = Operation switch
{
FileOperationType.Extract => StatusCenterItemIconKind.Extract,
FileOperationType.Copy => StatusCenterItemIconKind.Copy,
FileOperationType.Move => StatusCenterItemIconKind.Move,
FileOperationType.Delete => StatusCenterItemIconKind.Delete,
FileOperationType.Recycle => StatusCenterItemIconKind.Recycle,
FileOperationType.Compressed => StatusCenterItemIconKind.Compress,
FileOperationType.GitClone => StatusCenterItemIconKind.GitClone,
FileOperationType.InstallFont => StatusCenterItemIconKind.InstallFont,
_ => StatusCenterItemIconKind.Delete,
};
break;
}
case ReturnResult.Success:
{
ItemKind = StatusCenterItemKind.Successful;
ItemIconKind = StatusCenterItemIconKind.Successful;
break;
}
case ReturnResult.Failed:
{
ItemKind = StatusCenterItemKind.Error;
ItemIconKind = StatusCenterItemIconKind.Error;
break;
}
case ReturnResult.Cancelled:
{
IconBackgroundCircleBorderOpacity = 0.1d;
ItemKind = StatusCenterItemKind.Canceled;
ItemIconKind = Operation switch
{
FileOperationType.Extract => StatusCenterItemIconKind.Extract,
FileOperationType.Copy => StatusCenterItemIconKind.Copy,
FileOperationType.Move => StatusCenterItemIconKind.Move,
FileOperationType.Delete => StatusCenterItemIconKind.Delete,
FileOperationType.Recycle => StatusCenterItemIconKind.Recycle,
FileOperationType.Compressed => StatusCenterItemIconKind.Compress,
FileOperationType.GitClone => StatusCenterItemIconKind.GitClone,
FileOperationType.InstallFont => StatusCenterItemIconKind.InstallFont,
_ => StatusCenterItemIconKind.Delete,
};
break;
}
}
StatusCenterHelper.UpdateCardStrings(this);
OnPropertyChanged(nameof(HeaderTooltip));
}
private void ReportProgress(StatusCenterItemProgressModel value)
{
// The operation has been canceled.
// Do update neither progress value nor text.
if (CancellationToken.IsCancellationRequested)
return;
// Update status code
if (value.Status is FileSystemStatusCode status)
FileSystemOperationReturnResult = status.ToStatus();
// Update the footer message, percentage, processing item name
if (value.Percentage is double p)
{
if (ProgressPercentage != value.Percentage)
{
ProgressPercentage = (int)p;
if (Operation == FileOperationType.Recycle ||
Operation == FileOperationType.Delete ||
Operation == FileOperationType.Compressed ||
Operation == FileOperationType.GitClone)
{
Message = string.Format(
Strings.StatusCenter_ProcessedItems_Header.GetLocalizedFormatResource(value.ProcessedItemsCount, value.ItemsCount),
value.ProcessedItemsCount,
value.ItemsCount);
}
else
{
Message = string.Format(
Strings.StatusCenter_ProcessedSize_Header.GetLocalizedResource(),
value.ProcessedSize.ToSizeString(),
value.TotalSize.ToSizeString());
}
}
if (CurrentProcessingItemName != value.FileName)
CurrentProcessingItemName = value.FileName;
}
// Set total count
if (TotalItemsCount < value.ItemsCount)
TotalItemsCount = value.ItemsCount;
// Set total size
if (TotalSize < value.TotalSize)
TotalSize = value.TotalSize;
if (value.EnumerationCompleted && IsDiscovering)
{
IsDiscovering = false;
Message = Strings.ProcessingItems.GetLocalizedResource();
}
// Update UI for strings
StatusCenterHelper.UpdateCardStrings(this, value);
OnPropertyChanged(nameof(HeaderTooltip));
// Graph item point
Vector2 point;
// Set speed text and percentage
switch (value.TotalSize, value.ItemsCount)
{
// In progress, displaying items count & processed size
case (not 0, not 0):
ProgressPercentage = Math.Clamp((int)(value.ProcessedSize * 100.0 / value.TotalSize), 0, 100);
SpeedText = $"{value.ProcessingSizeSpeed.ToSizeString()}/s";
point = new((float)(value.ProcessedSize * 100.0 / value.TotalSize), (float)value.ProcessingSizeSpeed);
break;
// In progress, displaying processed size
case (not 0, _):
ProgressPercentage = Math.Clamp((int)(value.ProcessedSize * 100.0 / value.TotalSize), 0, 100);
SpeedText = $"{value.ProcessingSizeSpeed.ToSizeString()}/s";
point = new((float)(value.ProcessedSize * 100.0 / value.TotalSize), (float)value.ProcessingSizeSpeed);
break;
// In progress, displaying items count
case (_, not 0):
ProgressPercentage = Math.Clamp((int)(value.ProcessedItemsCount * 100.0 / value.ItemsCount), 0, 100);
SpeedText = $"{value.ProcessingItemsCountSpeed:0} items/s";
point = new((float)(value.ProcessedItemsCount * 100.0 / value.ItemsCount), (float)value.ProcessingItemsCountSpeed);
break;
default:
point = new(ProgressPercentage, (float)value.ProcessingItemsCountSpeed);
SpeedText = (value.ProcessedSize, value.ProcessedItemsCount) switch
{
(not 0, not 0) => $"{value.ProcessingSizeSpeed.ToSizeString()}/s",
(not 0, _) => $"{value.ProcessingSizeSpeed.ToSizeString()}/s",
(_, not 0) => $"{value.ProcessingItemsCountSpeed:0} items/s",
_ => "N/A",
};
break;
}
// 'debounce' updates a bit so the graph isn't too noisy
if (SpeedGraphValues.Count == 0 || (point.X - SpeedGraphValues[^1].X) > 0.5)
SpeedGraphValues?.Add(point);
// Add percentage to the header
if (!IsIndeterminateProgress && value.EnumerationCompleted)
Header = $"{Header} ({ProgressPercentage}%)";
// Update UI of the address bar
_viewModel.NotifyChanges();
}
public void ExecuteCancelCommand()
{
if (IsCancelable)
{
_operationCancellationToken?.Cancel();
IsIndeterminateProgress = true;
IsCancelable = false;
IsExpanded = false;
IsSpeedAndProgressAvailable = false;
Header = $"{Strings.Canceling.GetLocalizedResource()} - {Header}";
}
}
}
}