Skip to content

Commit 755085a

Browse files
committed
Update
1 parent d17ab07 commit 755085a

File tree

11 files changed

+64
-61
lines changed

11 files changed

+64
-61
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,4 @@ RoGetAgileReference
227227
IQueryInfo
228228
QITIPF_FLAGS
229229
GetDiskFreeSpaceEx
230+
GetDriveType
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Windows.Win32;
5+
using Windows.Win32.UI.Shell;
6+
7+
namespace Files.App.Storage
8+
{
9+
public interface IWindowsFile : IWindowsStorable, IChildFile
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Windows.Win32;
5+
using Windows.Win32.UI.Shell;
6+
7+
namespace Files.App.Storage
8+
{
9+
public interface IWindowsFolder : IWindowsStorable, IChildFolder
10+
{
11+
}
12+
}

src/Files.App.Storage/Storables/WindowsStorage/IWindowsStorable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Files.App.Storage
88
{
9-
public interface IWindowsStorable : IDisposable
9+
public interface IWindowsStorable : IStorableChild, IEquatable<IWindowsStorable>, IDisposable
1010
{
1111
ComPtr<IShellItem> ThisPtr { get; }
1212
}

src/Files.App.Storage/Storables/WindowsStorage/WindowsFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Files.App.Storage
99
{
1010
[DebuggerDisplay("{" + nameof(ToString) + "()}")]
11-
public sealed class WindowsFile : WindowsStorable, IChildFile
11+
public sealed class WindowsFile : WindowsStorable, IWindowsFile
1212
{
1313
public WindowsFile(ComPtr<IShellItem> nativeObject)
1414
{

src/Files.App.Storage/Storables/WindowsStorage/WindowsFolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Files.App.Storage
1111
{
1212
[DebuggerDisplay("{" + nameof(ToString) + "()}")]
13-
public sealed class WindowsFolder : WindowsStorable, IChildFolder
13+
public sealed class WindowsFolder : WindowsStorable, IWindowsFolder
1414
{
1515
public WindowsFolder(ComPtr<IShellItem> nativeObject)
1616
{

src/Files.App.Storage/Storables/WindowsStorage/WindowsStorable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Files.App.Storage
1010
{
11-
public abstract class WindowsStorable : IWindowsStorable, IStorableChild, IEquatable<IWindowsStorable>
11+
public abstract class WindowsStorable : IWindowsStorable
1212
{
1313
public ComPtr<IShellItem> ThisPtr { get; protected set; }
1414

src/Files.App.Storage/Storables/WindowsStorage/WindowsStorableHelpers.Storage.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using OwlCore.Storage;
54
using Windows.Win32;
65
using Windows.Win32.Foundation;
6+
using Windows.Win32.NetworkManagement.WNet;
77
using Windows.Win32.Storage.FileSystem;
88
using Windows.Win32.UI.Shell;
99

@@ -105,5 +105,17 @@ public static bool TryGetDriveFreeSpace(this IWindowsStorable storable, out ulon
105105

106106
return res;
107107
}
108+
109+
public static bool TryGetDriveType(this IWindowsStorable storable, out uint driveType)
110+
{
111+
driveType = PInvoke.GetDriveType(storable.GetDisplayName());
112+
113+
return driveType is 0U; // DRIVE_UNKNOWN
114+
}
115+
116+
public static bool TryDisconnectNetworkDrive(this IWindowsStorable storable)
117+
{
118+
return PInvoke.WNetCancelConnection2W(storable.GetDisplayName().TrimEnd('\\'), NET_CONNECT_FLAGS.CONNECT_UPDATE_PROFILE, true) is WIN32_ERROR.NO_ERROR;
119+
}
108120
}
109121
}

src/Files.App/Data/Items/WidgetDriveCardItem.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Files.App.Data.Items
99
{
10-
public sealed partial class WidgetDriveCardItem : WidgetCardItem, IWidgetCardItem<IFolder>, IDisposable
10+
public sealed partial class WidgetDriveCardItem : WidgetCardItem, IWidgetCardItem<IWindowsFolder>, IDisposable
1111
{
1212
// Properties
1313

14-
public required IFolder Item { get; set; }
14+
public required IWindowsFolder Item { get; set; }
1515

1616
public required new string Path { get; set; }
1717

@@ -29,6 +29,8 @@ public sealed partial class WidgetDriveCardItem : WidgetCardItem, IWidgetCardIte
2929

3030
public string? UsageText => string.Format(Strings.DriveFreeSpaceAndCapacity.GetLocalizedResource(), FreeSize.ToSizeString(), TotalSize.ToSizeString());
3131

32+
public required SystemIO.DriveType DriveType { get; set; }
33+
3234
private BitmapImage? _Thumbnail;
3335
public BitmapImage? Thumbnail { get => _Thumbnail; set => SetProperty(ref _Thumbnail, value); }
3436

@@ -38,6 +40,8 @@ public WidgetDriveCardItem()
3840
{
3941
}
4042

43+
// Methods
44+
4145
public async Task LoadCardThumbnailAsync()
4246
{
4347
if (string.IsNullOrEmpty(Path) || Item is not IWindowsStorable windowsStorable)
@@ -50,9 +54,11 @@ public async Task LoadCardThumbnailAsync()
5054
Thumbnail = await rawThumbnailData.ToBitmapAsync();
5155
}
5256

57+
// Disposer
58+
5359
public void Dispose()
5460
{
55-
throw new NotImplementedException();
61+
Item.Dispose();
5662
}
5763
}
5864
}

src/Files.App/ViewModels/UserControls/Widgets/DrivesWidgetViewModel.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public sealed partial class DrivesWidgetViewModel : BaseWidgetViewModel, IWidget
3434
// Commands
3535

3636
private ICommand EjectDeviceCommand { get; } = null!;
37-
private ICommand DisconnectNetworkDriveCommand { get; } = null!;
3837

3938
// Constructor
4039

@@ -47,7 +46,6 @@ public DrivesWidgetViewModel()
4746
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
4847
EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand);
4948
OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand);
50-
DisconnectNetworkDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteDisconnectNetworkDriveCommand);
5149
}
5250

5351
// Methods
@@ -61,20 +59,22 @@ public Task RefreshWidgetAsync()
6159

6260
Items.Clear();
6361

64-
await foreach (IWindowsStorable folder in HomePageContext.HomeFolder.GetLogicalDrivesAsync(default))
62+
await foreach (IWindowsFolder folder in HomePageContext.HomeFolder.GetLogicalDrivesAsync(default))
6563
{
6664
folder.TryGetDriveTotalSpace(out var totalSize);
6765
folder.TryGetDriveFreeSpace(out var freeSize);
66+
folder.TryGetDriveType(out var driveType);
6867

6968
Items.Insert(
7069
Items.Count,
7170
new()
7271
{
73-
Item = (IFolder)folder,
72+
Item = folder,
7473
Text = folder.GetDisplayName(SIGDN.SIGDN_PARENTRELATIVEFORUI),
7574
Path = folder.GetDisplayName(SIGDN.SIGDN_FILESYSPATH),
7675
TotalSize = ByteSizeLib.ByteSize.FromBytes(totalSize),
7776
FreeSize = ByteSizeLib.ByteSize.FromBytes(freeSize),
77+
DriveType = (SystemIO.DriveType)driveType,
7878
});
7979
}
8080
});
@@ -102,13 +102,8 @@ public async Task NavigateToPath(string path)
102102

103103
public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCardItem item, bool isPinned, bool isFolder = false)
104104
{
105-
var drive =
106-
Items.Where(x =>
107-
string.Equals(
108-
PathNormalization.NormalizePath(x.Path!),
109-
PathNormalization.NormalizePath(item.Path!),
110-
StringComparison.OrdinalIgnoreCase))
111-
.FirstOrDefault();
105+
if (item is not WidgetDriveCardItem driveItem)
106+
return [];
112107

113108
return new List<ContextMenuFlyoutItemViewModel>()
114109
{
@@ -145,7 +140,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
145140
Text = Strings.Eject.GetLocalizedResource(),
146141
Command = EjectDeviceCommand,
147142
CommandParameter = item,
148-
ShowItem = false
143+
ShowItem = driveItem.DriveType is SystemIO.DriveType.Removable or SystemIO.DriveType.CDRom
149144
},
150145
new()
151146
{
@@ -220,14 +215,6 @@ private void ExecuteOpenPropertiesCommand(WidgetDriveCardItem? item)
220215
flyout!.Closed += flyoutClosed;
221216
}
222217

223-
private void ExecuteDisconnectNetworkDriveCommand(WidgetDriveCardItem? item)
224-
{
225-
if (item is null)
226-
return;
227-
228-
NetworkService.DisconnectNetworkDrive(item.Item);
229-
}
230-
231218
// Event methods
232219

233220
private async void Items_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)

0 commit comments

Comments
 (0)