Skip to content

Commit b28301b

Browse files
committed
Drive widget
1 parent 1a0d29b commit b28301b

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Actions
5+
{
6+
internal sealed class FormatDriveFromHomeAction : FormatDriveAction
7+
{
8+
private IHomePageContext HomePageContext { get; } = Ioc.Default.GetRequiredService<IHomePageContext>();
9+
10+
private readonly DrivesViewModel drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
11+
12+
public override bool IsExecutable =>
13+
HomePageContext.IsAnyItemRightClicked &&
14+
HomePageContext.RightClickedItem is not null &&
15+
HomePageContext.RightClickedItem.Path is not null &&
16+
(drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x =>
17+
string.Equals(x.Path, HomePageContext.RightClickedItem.Path))?.MenuOptions.ShowFormatDrive ?? false);
18+
19+
public override bool IsAccessibleGlobally
20+
=> false;
21+
22+
public override Task ExecuteAsync(object? parameter = null)
23+
{
24+
return Win32Helper.OpenFormatDriveDialog(HomePageContext?.RightClickedItem?.Path ?? string.Empty);
25+
}
26+
}
27+
}

src/Files.App/Data/Commands/Manager/CommandCodes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum CommandCodes
4747
CreateShortcutFromDialog,
4848
EmptyRecycleBin,
4949
FormatDrive,
50+
FormatDriveFromHome,
5051
FormatDriveFromSidebar,
5152
RestoreRecycleBin,
5253
RestoreAllRecycleBin,

src/Files.App/Data/Commands/Manager/CommandManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public IRichCommand this[HotKey hotKey]
179179
public IRichCommand NewWindow => commands[CommandCodes.NewWindow];
180180
public IRichCommand NewTab => commands[CommandCodes.NewTab];
181181
public IRichCommand FormatDrive => commands[CommandCodes.FormatDrive];
182+
public IRichCommand FormatDriveFromHome => commands[CommandCodes.FormatDriveFromHome];
182183
public IRichCommand FormatDriveFromSidebar => commands[CommandCodes.FormatDriveFromSidebar];
183184
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
184185
public IRichCommand NavigateForward => commands[CommandCodes.NavigateForward];
@@ -380,6 +381,7 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
380381
[CommandCodes.NewWindow] = new NewWindowAction(),
381382
[CommandCodes.NewTab] = new NewTabAction(),
382383
[CommandCodes.FormatDrive] = new FormatDriveAction(),
384+
[CommandCodes.FormatDriveFromHome] = new FormatDriveFromHomeAction(),
383385
[CommandCodes.FormatDriveFromSidebar] = new FormatDriveFromSidebarAction(),
384386
[CommandCodes.NavigateBack] = new NavigateBackAction(),
385387
[CommandCodes.NavigateForward] = new NavigateForwardAction(),

src/Files.App/Data/Commands/Manager/ICommandManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
5858
IRichCommand RestoreRecycleBin { get; }
5959
IRichCommand RestoreAllRecycleBin { get; }
6060
IRichCommand FormatDrive { get; }
61+
IRichCommand FormatDriveFromHome { get; }
6162
IRichCommand FormatDriveFromSidebar { get; }
6263
IRichCommand OpenItem { get; }
6364
IRichCommand OpenItemWithApplicationPicker { get; }

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ public sealed class DrivesWidgetViewModel : BaseWidgetViewModel, IWidgetViewMode
2020
public ObservableCollection<WidgetDriveCardItem> Items { get; } = [];
2121

2222
public string WidgetName => nameof(DrivesWidget);
23-
public string AutomationProperties => "Drives".GetLocalizedResource();
24-
public string WidgetHeader => "Drives".GetLocalizedResource();
23+
public string AutomationProperties => Strings.Drives.GetLocalizedResource();
24+
public string WidgetHeader => Strings.Drives.GetLocalizedResource();
2525
public bool IsWidgetSettingEnabled => UserSettingsService.GeneralSettingsService.ShowDrivesWidget;
2626
public bool ShowMenuFlyout => false;
2727
public MenuFlyoutItem? MenuFlyoutItem => null;
2828

2929
// Commands
3030

31-
private ICommand FormatDriveCommand { get; } = null!;
3231
private ICommand EjectDeviceCommand { get; } = null!;
3332
private ICommand OpenInNewPaneCommand { get; } = null!;
3433
private ICommand DisconnectNetworkDriveCommand { get; } = null!;
@@ -45,7 +44,6 @@ public DrivesWidgetViewModel()
4544
OpenInNewWindowCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteOpenInNewWindowCommand);
4645
PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand);
4746
UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand);
48-
FormatDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteFormatDriveCommand);
4947
EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand);
5048
OpenInNewPaneCommand = new AsyncRelayCommand<WidgetDriveCardItem>(ExecuteOpenInNewPaneCommand);
5149
OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand);
@@ -99,43 +97,43 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
9997
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewPaneFromHomeAction).Build(),
10098
new()
10199
{
102-
Text = "PinFolderToSidebar".GetLocalizedResource(),
100+
Text = Strings.PinFolderToSidebar.GetLocalizedResource(),
103101
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePin" },
104102
Command = PinToSidebarCommand,
105103
CommandParameter = item,
106104
ShowItem = !isPinned
107105
},
108106
new()
109107
{
110-
Text = "UnpinFolderFromSidebar".GetLocalizedResource(),
108+
Text = Strings.UnpinFolderFromSidebar.GetLocalizedResource(),
111109
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePinRemove" },
112110
Command = UnpinFromSidebarCommand,
113111
CommandParameter = item,
114112
ShowItem = isPinned
115113
},
116114
new()
117115
{
118-
Text = "Eject".GetLocalizedResource(),
116+
Text = Strings.Eject.GetLocalizedResource(),
119117
Command = EjectDeviceCommand,
120118
CommandParameter = item,
121119
ShowItem = options?.ShowEjectDevice ?? false
122120
},
123121
new()
124122
{
125-
Text = "Properties".GetLocalizedResource(),
123+
Text = Strings.Properties.GetLocalizedResource(),
126124
ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.Properties" },
127125
Command = OpenPropertiesCommand,
128126
CommandParameter = item
129127
},
130128
new()
131129
{
132-
Text = "TurnOnBitLocker".GetLocalizedResource(),
130+
Text = Strings.TurnOnBitLocker.GetLocalizedResource(),
133131
Tag = "TurnOnBitLockerPlaceholder",
134132
IsEnabled = false
135133
},
136134
new()
137135
{
138-
Text = "ManageBitLocker".GetLocalizedResource(),
136+
Text = Strings.ManageBitLocker.GetLocalizedResource(),
139137
Tag = "ManageBitLockerPlaceholder",
140138
IsEnabled = false
141139
},
@@ -144,25 +142,19 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
144142
ItemType = ContextMenuFlyoutItemType.Separator,
145143
ShowItem = CommandManager.OpenTerminalFromHome.IsExecutable ||
146144
CommandManager.OpenStorageSenseFromHome.IsExecutable ||
147-
(options?.ShowFormatDrive ?? false)
145+
CommandManager.FormatDriveFromHome.IsExecutable
148146
},
149147
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenTerminalFromHome).Build(),
150148
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenStorageSenseFromHome).Build(),
151-
new()
152-
{
153-
Text = "FormatDriveText".GetLocalizedResource(),
154-
Command = FormatDriveCommand,
155-
CommandParameter = item,
156-
ShowItem = options?.ShowFormatDrive ?? false
157-
},
149+
new ContextMenuFlyoutItemViewModelBuilder(CommandManager.FormatDriveFromHome).Build(),
158150
new()
159151
{
160152
ItemType = ContextMenuFlyoutItemType.Separator,
161153
Tag = "OverflowSeparator",
162154
},
163155
new()
164156
{
165-
Text = "Loading".GetLocalizedResource(),
157+
Text = Strings.Loading.GetLocalizedResource(),
166158
Glyph = "\xE712",
167159
Items = [],
168160
ID = "ItemOverflow",
@@ -190,11 +182,6 @@ private async Task ExecuteOpenInNewPaneCommand(WidgetDriveCardItem? item)
190182
ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(item.Item.Path);
191183
}
192184

193-
private void ExecuteFormatDriveCommand(WidgetDriveCardItem? item)
194-
{
195-
Win32Helper.OpenFormatDriveDialog(item?.Path ?? string.Empty);
196-
}
197-
198185
private void ExecuteOpenPropertiesCommand(WidgetDriveCardItem? item)
199186
{
200187
if (!HomePageContext.IsAnyItemRightClicked || item is null)

0 commit comments

Comments
 (0)