19
19
using System . IO ;
20
20
using System . Linq ;
21
21
using System . Collections . ObjectModel ;
22
- using Windows . Devices . Enumeration ;
23
- using Windows . Devices . Portable ;
24
- using Windows . ApplicationModel . Core ;
25
22
using Windows . UI . Core ;
26
23
using Windows . UI . Xaml . Controls . Primitives ;
27
- using Files . Enums ;
28
- using Windows . UI . Xaml . Media . Imaging ;
29
- using Windows . Management . Deployment ;
30
- using Windows . Storage . Streams ;
31
24
using Windows . System ;
32
- using Microsoft . UI . Xaml . Controls ;
33
25
using Files . View_Models ;
34
26
35
27
namespace Files
@@ -57,14 +49,13 @@ public static ProHome OccupiedInstance
57
49
public static Dialogs . PropertiesDialog propertiesDialog { get ; set ; }
58
50
public static Dialogs . LayoutDialog layoutDialog { get ; set ; }
59
51
public static Dialogs . AddItemDialog addItemDialog { get ; set ; }
60
- private DeviceWatcher watcher ;
61
52
public static ObservableCollection < SidebarItem > sideBarItems = new ObservableCollection < SidebarItem > ( ) ;
62
53
public static ObservableCollection < WSLDistroItem > linuxDistroItems = new ObservableCollection < WSLDistroItem > ( ) ;
63
- public static SettingsViewModel AppSettings = new SettingsViewModel ( ) ;
54
+ public static SettingsViewModel AppSettings { get ; set ; }
64
55
65
56
public App ( )
66
57
{
67
- this . InitializeComponent ( ) ;
58
+ this . InitializeComponent ( ) ;
68
59
this . Suspending += OnSuspending ;
69
60
consentDialog = new Dialogs . ConsentDialog ( ) ;
70
61
propertiesDialog = new Dialogs . PropertiesDialog ( ) ;
@@ -76,8 +67,8 @@ public App()
76
67
Clipboard_ContentChanged ( null , null ) ;
77
68
AppCenter . Start ( "682666d1-51d3-4e4a-93d0-d028d43baaa0" , typeof ( Analytics ) , typeof ( Crashes ) ) ;
78
69
70
+ AppSettings = new SettingsViewModel ( ) ;
79
71
SetPropertiesFromLocalSettings ( ) ;
80
-
81
72
PopulatePinnedSidebarItems ( ) ;
82
73
DetectWSLDistros ( ) ;
83
74
}
@@ -267,217 +258,6 @@ private void SetPropertiesFromLocalSettings()
267
258
268
259
269
260
270
- }
271
-
272
-
273
-
274
- public void PopulateDrivesListWithLocalDisks ( )
275
- {
276
- var driveLetters = DriveInfo . GetDrives ( ) . Select ( x => x . RootDirectory . Root ) . ToList ( ) . OrderBy ( x => x . Root . FullName ) . ToList ( ) ;
277
- driveLetters . ForEach ( async roots =>
278
- {
279
- try
280
- {
281
- var content = string . Empty ;
282
- string icon = null ;
283
- if ( ! ( await KnownFolders . RemovableDevices . GetFoldersAsync ( ) ) . Select ( x => x . Path ) . ToList ( ) . Contains ( roots . Name ) )
284
- {
285
- // TODO: Display Custom Names for Local Disks as well
286
- if ( InstanceTabsView . NormalizePath ( roots . Name ) != InstanceTabsView . NormalizePath ( "A:" )
287
- && InstanceTabsView . NormalizePath ( roots . Name ) != InstanceTabsView . NormalizePath ( "B:" ) )
288
- {
289
- content = $ "Local Disk ({ roots . Name . TrimEnd ( '\\ ' ) } )";
290
- icon = "\uEDA2 " ;
291
- }
292
- else
293
- {
294
- content = $ "Floppy Disk ({ roots . Name . TrimEnd ( '\\ ' ) } )";
295
- icon = "\uE74E " ;
296
- }
297
-
298
-
299
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Low ,
300
- async ( ) =>
301
- {
302
- Visibility capacityBarVis = Visibility . Visible ;
303
- ulong totalSpaceProg = 0 ;
304
- ulong freeSpaceProg = 0 ;
305
- string free_space_text = "Unknown" ;
306
- string total_space_text = "Unknown" ;
307
-
308
- try
309
- {
310
- StorageFolder drive = await StorageFolder . GetFolderFromPathAsync ( roots . Name ) ;
311
- var retrivedProperties = await drive . Properties . RetrievePropertiesAsync ( new string [ ] { "System.FreeSpace" , "System.Capacity" } ) ;
312
-
313
- var sizeAsGBString = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.FreeSpace" ] ) . GigaBytes ;
314
- freeSpaceProg = Convert . ToUInt64 ( sizeAsGBString ) ;
315
-
316
- sizeAsGBString = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.Capacity" ] ) . GigaBytes ;
317
- totalSpaceProg = Convert . ToUInt64 ( sizeAsGBString ) ;
318
-
319
- free_space_text = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.FreeSpace" ] ) . ToString ( ) ;
320
- total_space_text = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.Capacity" ] ) . ToString ( ) ;
321
- }
322
- catch ( UnauthorizedAccessException )
323
- {
324
- capacityBarVis = Visibility . Collapsed ;
325
- }
326
- catch ( NullReferenceException )
327
- {
328
- capacityBarVis = Visibility . Collapsed ;
329
- }
330
-
331
- App . foundDrives . Add ( new DriveItem ( )
332
- {
333
- driveText = content ,
334
- glyph = icon ,
335
- maxSpace = totalSpaceProg ,
336
- spaceUsed = totalSpaceProg - freeSpaceProg ,
337
- tag = roots . Name ,
338
- progressBarVisibility = capacityBarVis ,
339
- spaceText = free_space_text + " free of " + total_space_text ,
340
- } ) ;
341
- } ) ;
342
- }
343
-
344
- }
345
- catch ( UnauthorizedAccessException e )
346
- {
347
- Debug . WriteLine ( e . Message ) ;
348
- }
349
-
350
- } ) ;
351
- }
352
-
353
- private async void Watcher_EnumerationCompleted ( DeviceWatcher sender , object args )
354
- {
355
- try
356
- {
357
- PopulateDrivesListWithLocalDisks ( ) ;
358
- }
359
- catch ( UnauthorizedAccessException )
360
- {
361
- await consentDialog . ShowAsync ( ) ;
362
- }
363
- DeviceAdded ( sender , null ) ;
364
-
365
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Low ,
366
- ( ) =>
367
- {
368
- App . foundDrives . Add ( new DriveItem ( )
369
- {
370
- driveText = "OneDrive" ,
371
- tag = "OneDrive" ,
372
- cloudGlyphVisibility = Visibility . Visible ,
373
- driveGlyphVisibility = Visibility . Collapsed
374
- } ) ;
375
- } ) ;
376
- }
377
-
378
- private void DeviceUpdated ( DeviceWatcher sender , DeviceInformationUpdate args )
379
- {
380
- Debug . WriteLine ( "Devices updated" ) ;
381
- }
382
-
383
-
384
- private async void DeviceRemoved ( DeviceWatcher sender , DeviceInformationUpdate args )
385
- {
386
- var devices = DriveInfo . GetDrives ( ) . Select ( x => x . RootDirectory . Root ) . ToList ( ) . OrderBy ( x => x . Root . FullName ) . ToList ( ) ;
387
-
388
- foreach ( DriveItem driveItem in foundDrives )
389
- {
390
- if ( ! driveItem . tag . Equals ( "OneDrive" ) )
391
- {
392
- if ( ! devices . Any ( x => x . Name == driveItem . tag ) || devices . Equals ( null ) )
393
- {
394
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Low ,
395
- ( ) =>
396
- {
397
- foundDrives . Remove ( driveItem ) ;
398
- } ) ;
399
- return ;
400
-
401
- }
402
- }
403
-
404
- }
405
- }
406
-
407
- private async void DeviceAdded ( DeviceWatcher sender , DeviceInformation args )
408
- {
409
- try
410
- {
411
- var devices = ( await KnownFolders . RemovableDevices . GetFoldersAsync ( ) ) . OrderBy ( x => x . Path ) ;
412
- foreach ( StorageFolder device in devices )
413
- {
414
- var letter = device . Path ;
415
- if ( ! foundDrives . Any ( x => x . tag == letter ) )
416
- {
417
- var content = device . DisplayName ;
418
- string icon = null ;
419
- await CoreApplication . MainView . CoreWindow . Dispatcher . RunAsync ( CoreDispatcherPriority . Low ,
420
- async ( ) =>
421
- {
422
- if ( content . Contains ( "DVD" ) )
423
- {
424
- icon = "\uE958 " ;
425
- }
426
- else
427
- {
428
- icon = "\uE88E " ;
429
- }
430
-
431
- ulong totalSpaceProg = 0 ;
432
- ulong freeSpaceProg = 0 ;
433
- string free_space_text = "Unknown" ;
434
- string total_space_text = "Unknown" ;
435
- Visibility capacityBarVis = Visibility . Visible ;
436
- try
437
- {
438
- StorageFolder drive = await StorageFolder . GetFolderFromPathAsync ( letter ) ;
439
- var retrivedProperties = await drive . Properties . RetrievePropertiesAsync ( new string [ ] { "System.FreeSpace" , "System.Capacity" } ) ;
440
-
441
- var sizeAsGBString = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.FreeSpace" ] ) . GigaBytes ;
442
- freeSpaceProg = Convert . ToUInt64 ( sizeAsGBString ) ;
443
-
444
- sizeAsGBString = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.Capacity" ] ) . GigaBytes ;
445
- totalSpaceProg = Convert . ToUInt64 ( sizeAsGBString ) ;
446
-
447
- free_space_text = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.FreeSpace" ] ) . ToString ( ) ;
448
- total_space_text = ByteSizeLib . ByteSize . FromBytes ( ( ulong ) retrivedProperties [ "System.Capacity" ] ) . ToString ( ) ;
449
- }
450
- catch ( UnauthorizedAccessException )
451
- {
452
- capacityBarVis = Visibility . Collapsed ;
453
- }
454
- catch ( NullReferenceException )
455
- {
456
- capacityBarVis = Visibility . Collapsed ;
457
- }
458
-
459
- if ( ! foundDrives . Any ( x => x . tag == letter ) )
460
- {
461
- foundDrives . Add ( new DriveItem ( )
462
- {
463
- driveText = content ,
464
- glyph = icon ,
465
- maxSpace = totalSpaceProg ,
466
- spaceUsed = totalSpaceProg - freeSpaceProg ,
467
- tag = letter ,
468
- progressBarVisibility = capacityBarVis ,
469
- spaceText = free_space_text + " free of " + total_space_text ,
470
- } ) ;
471
- }
472
- } ) ;
473
-
474
- }
475
- }
476
- }
477
- catch ( UnauthorizedAccessException )
478
- {
479
- await consentDialog . ShowAsync ( ) ;
480
- }
481
261
}
482
262
483
263
public static List < string > LinesToRemoveFromFile = new List < string > ( ) ;
@@ -662,7 +442,6 @@ public static IReadOnlyList<ContentDialog> FindDisplayedContentDialogs<T>()
662
442
663
443
public static PasteState PS { get ; set ; } = new PasteState ( ) ;
664
444
public static List < string > pathsToDeleteAfterPaste = new List < string > ( ) ;
665
- public static ObservableCollection < DriveItem > foundDrives = new ObservableCollection < DriveItem > ( ) ;
666
445
667
446
/// <summary>
668
447
/// Invoked when the application is launched normally by the end user. Other entry points
@@ -710,12 +489,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
710
489
711
490
712
491
}
713
- watcher = DeviceInformation . CreateWatcher ( StorageDevice . GetDeviceSelector ( ) ) ;
714
- watcher . Added += DeviceAdded ;
715
- watcher . Removed += DeviceRemoved ;
716
- watcher . Updated += DeviceUpdated ;
717
- watcher . EnumerationCompleted += Watcher_EnumerationCompleted ;
718
- watcher . Start ( ) ;
492
+
719
493
// Ensure the current window is active
720
494
Window . Current . Activate ( ) ;
721
495
Window . Current . CoreWindow . KeyDown += CoreWindow_KeyDown ;
@@ -776,18 +550,12 @@ protected override void OnActivated(IActivatedEventArgs args)
776
550
rootFrame . Navigate ( typeof ( InstanceTabsView ) , @trimmedPath , new SuppressNavigationTransitionInfo ( ) ) ;
777
551
}
778
552
// Ensure the current window is active.
779
- watcher = DeviceInformation . CreateWatcher ( StorageDevice . GetDeviceSelector ( ) ) ;
780
- watcher . Added += DeviceAdded ;
781
- watcher . Removed += DeviceRemoved ;
782
- watcher . Updated += DeviceUpdated ;
783
- watcher . EnumerationCompleted += Watcher_EnumerationCompleted ;
784
- watcher . Start ( ) ;
785
553
Window . Current . Activate ( ) ;
786
554
Window . Current . CoreWindow . KeyDown += CoreWindow_KeyDown ;
787
555
Window . Current . CoreWindow . Dispatcher . AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated ;
788
556
return ;
789
557
}
790
-
558
+
791
559
rootFrame . Navigate ( typeof ( InstanceTabsView ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
792
560
793
561
// Ensure the current window is active.
@@ -819,10 +587,7 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
819
587
{
820
588
var deferral = e . SuspendingOperation . GetDeferral ( ) ;
821
589
//TODO: Save application state and stop any background activity
822
- if ( watcher . Status == DeviceWatcherStatus . Started || watcher . Status == DeviceWatcherStatus . EnumerationCompleted )
823
- {
824
- watcher . Stop ( ) ;
825
- }
590
+ AppSettings . Dispose ( ) ;
826
591
deferral . Complete ( ) ;
827
592
}
828
593
}
0 commit comments