Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 15 additions & 54 deletions src/Files.App/Helpers/DriveHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,29 @@
using Files.App.Interacts;
using Files.App.Extensions;
using System.Diagnostics;
using Files.App.Interacts;
using System.Threading.Tasks;
using Windows.UI.Notifications;
using CommunityToolkit.WinUI.Notifications;
using Files.App.Shell;
using Microsoft.Management.Infrastructure;

namespace Files.App.Helpers
{
public static class DriveHelpers
{
// Eject using Shell Verb (fixes #6072, #6439)
public static Task EjectDeviceAsync(string path)
=> ContextMenu.InvokeVerb("eject", path);

// Eject using DeviceIoControl
/*public static async Task EjectDeviceAsync(string path)
public static async Task<bool> EjectDeviceAsync(string path)
{
var removableDevice = new RemovableDevice(path);
bool result = await removableDevice.EjectAsync();
if (result)
{
Debug.WriteLine("Device successfully ejected");
return await removableDevice.EjectAsync();
}

var toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "EjectNotificationHeader".GetLocalizedResource()
},
new AdaptiveText()
{
Text = "EjectNotificationBody".GetLocalizedResource()
}
},
Attribution = new ToastGenericAttributionText()
{
Text = "SettingsAboutAppName".GetLocalizedResource()
}
}
},
ActivationType = ToastActivationType.Protocol
};

// Create the toast notification
var toastNotif = new ToastNotification(toastContent.GetXml());
public static string GetVolumeId(string driveName)
{
string name = driveName.ToUpperInvariant();
string query = $"SELECT DeviceID FROM Win32_Volume WHERE DriveLetter = '{name}'";

// And send the notification
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
}
else
using var cimSession = CimSession.Create(null);
foreach (var item in cimSession.QueryInstances(@"root\cimv2", "WQL", query)) // max 1 result because DriveLetter is unique.
{
Debug.WriteLine("Can't eject device");

await DialogDisplayHelper.ShowDialogAsync(
"EjectNotificationErrorDialogHeader".GetLocalizedResource(),
"EjectNotificationErrorDialogBody".GetLocalizedResource());
return (string)item.CimInstanceProperties["DeviceID"]?.Value ?? string.Empty;
}
}*/

return string.Empty;
}
}
}
58 changes: 58 additions & 0 deletions src/Files.App/Helpers/UIHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,69 @@
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Files.App.Shell;
using System.Diagnostics;
using CommunityToolkit.WinUI.Notifications;
using Windows.UI.Notifications;
using Files.App.Extensions;

namespace Files.App.Helpers
{
public static class UIHelpers
{
/// <summary>
/// Displays a toast or dialog to indicate the result of
/// a device ejection operation.
/// </summary>
/// <param name="result">Only true implies a successful device ejection</param>
/// <returns></returns>
public static async Task ShowDeviceEjectResultAsync(bool result)
{
if (result)
{
Debug.WriteLine("Device successfully ejected");

var toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "EjectNotificationHeader".GetLocalizedResource()
},
new AdaptiveText()
{
Text = "EjectNotificationBody".GetLocalizedResource()
}
},
Attribution = new ToastGenericAttributionText()
{
Text = "SettingsAboutAppName".GetLocalizedResource()
}
}
},
ActivationType = ToastActivationType.Protocol
};

// Create the toast notification
var toastNotif = new ToastNotification(toastContent.GetXml());

// And send the notification
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
}
else
{
Debug.WriteLine("Can't eject device");

await DialogDisplayHelper.ShowDialogAsync(
"EjectNotificationErrorDialogHeader".GetLocalizedResource(),
"EjectNotificationErrorDialogBody".GetLocalizedResource());
}
}

public static async Task<ContentDialogResult> TryShowAsync(this ContentDialog dialog)
{
try
Expand Down
29 changes: 4 additions & 25 deletions src/Files.App/ServicesImplementation/VolumeInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
using Files.Backend.Models;
using Files.Backend.Services;
using Files.Shared.Extensions;
using Files.App.Helpers;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections;

namespace Files.App.ServicesImplementation
{
internal class VolumeInfoFactory : IVolumeInfoFactory
{
public async Task<VolumeInfo> BuildVolumeInfo(string driveName)
public VolumeInfo BuildVolumeInfo(string driveName)
{
string volumeId = await GetVolumeID(driveName);
string volumeId = GetVolumeID(driveName);
return new VolumeInfo(volumeId);
}

private async Task<string> GetVolumeID(string driveName)
private string GetVolumeID(string driveName)
{
var connection = await AppServiceConnectionHelper.Instance;
if (connection is null)
{
return string.Empty;
}

var parameter = new ValueSet
{
["Arguments"] = "VolumeID",
["DriveName"] = driveName,
};

var (status, response) = await connection.SendMessageForResponseAsync(parameter);
if (status is AppServiceResponseStatus.Success)
{
return response.Get("VolumeID", string.Empty);
}

return string.Empty;
return DriveHelpers.GetVolumeId(driveName);
}
}
}
8 changes: 5 additions & 3 deletions src/Files.App/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
using CommunityToolkit.WinUI.UI;
using Microsoft.UI.Input;
using UWPToWinAppSDKUpgradeHelpers;
using Windows.ApplicationModel.Search.Core;
using Vanara.PInvoke;

namespace Files.App.UserControls
{
Expand Down Expand Up @@ -503,7 +503,8 @@ private void OpenProperties(CommandBarFlyout menu)

private async void EjectDevice()
{
await DriveHelpers.EjectDeviceAsync(rightClickedItem.Path);
var result = await DriveHelpers.EjectDeviceAsync(rightClickedItem.Path);
await UIHelpers.ShowDeviceEjectResultAsync(result);
}

private async void Sidebar_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
Expand Down Expand Up @@ -1159,7 +1160,8 @@ private async Task<bool> CheckEmptyDrive(string drivePath)
bool ejectButton = await DialogDisplayHelper.ShowDialogAsync("InsertDiscDialog/Title".GetLocalizedResource(), string.Format("InsertDiscDialog/Text".GetLocalizedResource(), matchingDrive.Path), "InsertDiscDialog/OpenDriveButton".GetLocalizedResource(), "Close".GetLocalizedResource());
if (ejectButton)
{
await DriveHelpers.EjectDeviceAsync(matchingDrive.Path);
var result = await DriveHelpers.EjectDeviceAsync(matchingDrive.Path);
await UIHelpers.ShowDeviceEjectResultAsync(result);
}
return true;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;
using CommunityToolkit.WinUI.Notifications;
using System.Diagnostics;
using Windows.UI.Notifications;
using Files.App.Helpers.XamlHelpers;
using Microsoft.UI.Xaml.Controls.Primitives;

Expand Down Expand Up @@ -146,7 +149,8 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
private async void EjectDevice_Click(object sender, RoutedEventArgs e)
{
var item = ((MenuFlyoutItem)sender).DataContext as DriveItem;
await DriveHelpers.EjectDeviceAsync(item.Path);
var result = await DriveHelpers.EjectDeviceAsync(item.Path);
await UIHelpers.ShowDeviceEjectResultAsync(result);
}

private async void OpenInNewTab_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -322,7 +326,8 @@ private async Task<bool> CheckEmptyDrive(string drivePath)
bool ejectButton = await DialogDisplayHelper.ShowDialogAsync("InsertDiscDialog/Title".GetLocalizedResource(), string.Format("InsertDiscDialog/Text".GetLocalizedResource(), matchingDrive.Path), "InsertDiscDialog/OpenDriveButton".GetLocalizedResource(), "Close".GetLocalizedResource());
if (ejectButton)
{
await DriveHelpers.EjectDeviceAsync(matchingDrive.Path);
var result = await DriveHelpers.EjectDeviceAsync(matchingDrive.Path);
await UIHelpers.ShowDeviceEjectResultAsync(result);
}
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,8 @@ public async Task CheckPathInput(string currentInput, string currentSelectedPath
bool ejectButton = await DialogDisplayHelper.ShowDialogAsync("InsertDiscDialog/Title".GetLocalizedResource(), string.Format("InsertDiscDialog/Text".GetLocalizedResource(), matchingDrive.Path), "InsertDiscDialog/OpenDriveButton".GetLocalizedResource(), "Close".GetLocalizedResource());
if (ejectButton)
{
await DriveHelpers.EjectDeviceAsync(matchingDrive.Path);
var result = await DriveHelpers.EjectDeviceAsync(matchingDrive.Path);
await UIHelpers.ShowDeviceEjectResultAsync(result);
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.Backend/Services/IVolumeInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Files.Backend.Services
{
public interface IVolumeInfoFactory
{
Task<VolumeInfo> BuildVolumeInfo(string driveName);
VolumeInfo BuildVolumeInfo(string driveName);
}
}
23 changes: 0 additions & 23 deletions src/Files.FullTrust/Helpers/DriveHelpers.cs

This file was deleted.

31 changes: 0 additions & 31 deletions src/Files.FullTrust/MessageHandlers/DriveHandler.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Files.FullTrust/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private static async Task Main()
messageHandlers = new List<IMessageHandler>
{
new RecycleBinHandler(),
new DriveHandler(),
new LibrariesHandler(),
new FileTagsHandler(),
new NetworkDrivesHandler(),
Expand Down