Skip to content

Commit 6f95b3f

Browse files
authored
Feature: Moved "format drive" to main context menu (#11364)
1 parent 344dd07 commit 6f95b3f

File tree

11 files changed

+62
-3
lines changed

11 files changed

+62
-3
lines changed

src/Files.App/DataModels/NavigationControlItems/DriveItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root
169169
IsLocationItem = true,
170170
ShowEjectDevice = item.IsRemovable,
171171
ShowShellItems = true,
172+
ShowFormatDrive = !(item.Type == DriveType.Network || string.Equals(root.Path, "C:\\", StringComparison.OrdinalIgnoreCase)),
172173
ShowProperties = true
173174
};
174175
item.Path = string.IsNullOrEmpty(root.Path) ? $"\\\\?\\{root.Name}\\" : root.Path;
176+
App.Logger.Warn(item.Path);
175177
item.DeviceID = deviceId;
176178
item.Root = root;
177179

src/Files.App/DataModels/NavigationControlItems/INavigationControlItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class ContextMenuOptions
5454

5555
public bool ShowEjectDevice { get; set; }
5656

57+
public bool ShowFormatDrive { get; set; }
58+
5759
public bool ShowShellItems { get; set; }
5860
}
5961
}

src/Files.App/Filesystem/ListedItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ public override string ToString()
420420
public bool IsAlternateStream => this is AlternateStreamItem;
421421
public virtual bool IsExecutable => FileExtensionHelpers.IsExecutableFile(ItemPath);
422422
public bool IsPinned => App.QuickAccessManager.Model.FavoriteItems.Contains(itemPath);
423+
public bool IsDriveRoot => ItemPath == PathNormalization.GetPathRoot(ItemPath);
423424

424425
private BaseStorageFile itemFile;
425426
public BaseStorageFile ItemFile

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
559559
ShowItem = !itemsSelected
560560
},
561561
new ContextMenuFlyoutItemViewModel()
562+
{
563+
Text = "FormatDriveText".GetLocalizedResource(),
564+
Command = commandsViewModel.FormatDriveCommand,
565+
CommandParameter = itemViewModel?.CurrentFolder,
566+
ShowItem = itemViewModel.CurrentFolder is not null && (App.DrivesManager.Drives.Where(x => string.Equals(x.Path, itemViewModel?.CurrentFolder.ItemPath)).FirstOrDefault()?.MenuOptions.ShowFormatDrive ?? false),
567+
},
568+
new ContextMenuFlyoutItemViewModel()
562569
{
563570
Text = "BaseLayoutContextFlyoutEmptyRecycleBin/Text".GetLocalizedResource(),
564571
Glyph = "\uEF88",

src/Files.App/Helpers/ShellContextMenuHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Func<string, bool> FilterMenuItems(bool showOpenMenu)
4949
"cut", "copy", "paste", "delete", "properties", "link",
5050
"Windows.ModernShare", "Windows.Share", "setdesktopwallpaper",
5151
"eject", "rename", "explore", "openinfiles", "extract",
52-
"copyaspath", "undelete", "empty", "rotate90", "rotate270",
52+
"copyaspath", "undelete", "empty", "format", "rotate90", "rotate270",
5353
Win32API.ExtractStringFromDLL("shell32.dll", 34593), // Add to collection
5454
Win32API.ExtractStringFromDLL("shell32.dll", 5384), // Pin to Start
5555
Win32API.ExtractStringFromDLL("shell32.dll", 5385), // Unpin from Start

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,11 @@ public async Task PlayAll()
895895
await NavigationHelpers.OpenSelectedItems(associatedInstance);
896896
}
897897

898+
public void FormatDrive(ListedItem? e)
899+
{
900+
Win32API.OpenFormatDriveDialog(e?.ItemPath ?? string.Empty);
901+
}
902+
898903
#endregion Command Implementation
899904
}
900905
}

src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CommunityToolkit.Mvvm.Input;
2+
using Files.App.Filesystem;
23
using Files.Shared;
34
using Microsoft.UI.Xaml;
45
using Microsoft.UI.Xaml.Input;
@@ -82,6 +83,7 @@ private void InitializeCommands()
8283
RotateImageRightCommand = new AsyncRelayCommand(CommandsModel.RotateImageRight);
8384
InstallFontCommand = new AsyncRelayCommand(CommandsModel.InstallFont);
8485
PlayAllCommand = new AsyncRelayCommand(CommandsModel.PlayAll);
86+
FormatDriveCommand = new RelayCommand<ListedItem>(CommandsModel.FormatDrive);
8587
}
8688

8789
#endregion Command Initialization
@@ -199,6 +201,7 @@ private void InitializeCommands()
199201
public ICommand InstallFontCommand { get; private set; }
200202

201203
public ICommand PlayAllCommand { get; private set; }
204+
public ICommand FormatDriveCommand { get; private set; }
202205

203206
#endregion Commands
204207

src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Files.App.Filesystem;
12
using Files.Shared;
23
using Microsoft.UI.Xaml;
34
using Microsoft.UI.Xaml.Input;
@@ -119,5 +120,7 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
119120
Task InstallFont();
120121

121122
Task PlayAll();
123+
124+
void FormatDrive(ListedItem? obj);
122125
}
123126
}

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,6 +2955,9 @@
29552955
<data name="ShowHiddenItems" xml:space="preserve">
29562956
<value>Show hidden items</value>
29572957
</data>
2958+
<data name="FormatDriveText" xml:space="preserve">
2959+
<value>Format...</value>
2960+
</data>
29582961
<data name="Help" xml:space="preserve">
29592962
<value>Help</value>
29602963
</data>

src/Files.App/UserControls/SidebarControl.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Files.App.Helpers;
1010
using Files.App.Helpers.ContextFlyouts;
1111
using Files.App.ServicesImplementation;
12+
using Files.App.Shell;
1213
using Files.App.ViewModels;
1314
using Files.Backend.Services.Settings;
1415
using Files.Shared.Extensions;
@@ -29,6 +30,7 @@
2930
using Windows.ApplicationModel.DataTransfer.DragDrop;
3031
using Windows.System;
3132
using Windows.UI.Core;
33+
using static System.Net.Mime.MediaTypeNames;
3234
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
3335

3436
namespace Files.App.UserControls
@@ -121,6 +123,8 @@ public ICommand EmptyRecycleBinCommand
121123

122124
private ICommand EjectDeviceCommand { get; }
123125

126+
private ICommand FormatDriveCommand { get; }
127+
124128
private ICommand OpenPropertiesCommand { get; }
125129

126130
private bool IsInPointerPressed = false;
@@ -141,6 +145,7 @@ public SidebarControl()
141145
OpenInNewWindowCommand = new RelayCommand(OpenInNewWindow);
142146
OpenInNewPaneCommand = new RelayCommand(OpenInNewPane);
143147
EjectDeviceCommand = new RelayCommand(EjectDevice);
148+
FormatDriveCommand = new RelayCommand(FormatDrive);
144149
OpenPropertiesCommand = new RelayCommand<CommandBarFlyout>(OpenProperties);
145150
}
146151

@@ -271,6 +276,13 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
271276
ShowItem = options.ShowEjectDevice
272277
},
273278
new ContextMenuFlyoutItemViewModel()
279+
{
280+
Text = "FormatDriveText".GetLocalizedResource(),
281+
Command = FormatDriveCommand,
282+
CommandParameter = item,
283+
ShowItem = options.ShowFormatDrive
284+
},
285+
new ContextMenuFlyoutItemViewModel()
274286
{
275287
Text = "BaseLayoutContextFlyoutPropertiesFolder/Text".GetLocalizedResource(),
276288
Glyph = "\uE946",
@@ -377,6 +389,11 @@ private async void EjectDevice()
377389
await UIHelpers.ShowDeviceEjectResultAsync(result);
378390
}
379391

392+
private void FormatDrive()
393+
{
394+
Win32API.OpenFormatDriveDialog(rightClickedItem.Path);
395+
}
396+
380397
private async void Sidebar_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
381398
{
382399
if (IsInPointerPressed || args.InvokedItem is null || args.InvokedItemContainer is null)

src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Files.App.Extensions;
66
using Files.App.Filesystem;
77
using Files.App.Helpers;
8+
using Files.App.Shell;
89
using Files.App.ViewModels;
910
using Files.App.ViewModels.Widgets;
1011
using Files.Backend.Services.Settings;
@@ -18,6 +19,7 @@
1819
using System.Collections.ObjectModel;
1920
using System.Collections.Specialized;
2021
using System.ComponentModel;
22+
using System.IO;
2123
using System.Linq;
2224
using System.Runtime.CompilerServices;
2325
using System.Threading.Tasks;
@@ -81,6 +83,7 @@ public sealed partial class DrivesWidget : HomePageWidget, IWidgetItemModel, INo
8183

8284
private IShellPage associatedInstance;
8385

86+
public ICommand FormatDriveCommand;
8487
public ICommand EjectDeviceCommand;
8588
public ICommand DisconnectNetworkDriveCommand;
8689
public ICommand GoToStorageSenseCommand;
@@ -126,6 +129,7 @@ public DrivesWidget()
126129

127130
App.DrivesManager.DataChanged += Manager_DataChanged;
128131

132+
FormatDriveCommand = new RelayCommand<DriveCardItem>(FormatDrive);
129133
EjectDeviceCommand = new AsyncRelayCommand<DriveCardItem>(EjectDevice);
130134
OpenInNewTabCommand = new RelayCommand<WidgetCardItem>(OpenInNewTab);
131135
OpenInNewWindowCommand = new RelayCommand<WidgetCardItem>(OpenInNewWindow);
@@ -139,7 +143,8 @@ public DrivesWidget()
139143

140144
public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCardItem item, bool isPinned)
141145
{
142-
var options = (item.Item as DriveItem)?.MenuOptions;
146+
var drive = ItemsAdded.Where(x => string.Equals(PathNormalization.NormalizePath(x.Path), PathNormalization.NormalizePath(item.Path), StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
147+
var options = drive?.Item.MenuOptions;
143148

144149
return new List<ContextMenuFlyoutItemViewModel>()
145150
{
@@ -199,6 +204,13 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
199204
ShowItem = options?.ShowEjectDevice ?? false
200205
},
201206
new ContextMenuFlyoutItemViewModel()
207+
{
208+
Text = "FormatDriveText".GetLocalizedResource(),
209+
Command = FormatDriveCommand,
210+
CommandParameter = item,
211+
ShowItem = options?.ShowFormatDrive ?? false
212+
},
213+
new ContextMenuFlyoutItemViewModel()
202214
{
203215
Text = "BaseLayoutContextFlyoutPropertiesFolder/Text".GetLocalizedResource(),
204216
Glyph = "\uE946",
@@ -233,7 +245,7 @@ await DispatcherQueue.EnqueueAsync(async () =>
233245
{
234246
foreach (DriveItem drive in App.DrivesManager.Drives)
235247
{
236-
if (!ItemsAdded.Any(x => x.Item == drive) && drive.Type != DriveType.VirtualDrive)
248+
if (!ItemsAdded.Any(x => x.Item == drive) && drive.Type != DataModels.NavigationControlItems.DriveType.VirtualDrive)
237249
{
238250
var cardItem = new DriveCardItem(drive);
239251
ItemsAdded.AddSorted(cardItem);
@@ -260,6 +272,10 @@ private async Task EjectDevice(DriveCardItem item)
260272
await UIHelpers.ShowDeviceEjectResultAsync(result);
261273
}
262274

275+
private void FormatDrive(DriveCardItem? item)
276+
{
277+
Win32API.OpenFormatDriveDialog(item?.Path ?? string.Empty);
278+
}
263279

264280
private async Task OpenProperties(DriveCardItem item)
265281
{

0 commit comments

Comments
 (0)