Skip to content

Commit e6c5843

Browse files
authored
Feature: Add Run As Admin and Run As Another User RichCommands (#11581)
1 parent dde07f7 commit e6c5843

File tree

9 files changed

+100
-99
lines changed

9 files changed

+100
-99
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Commands;
4+
using Files.App.Contexts;
5+
using Files.App.Extensions;
6+
using Files.App.Shell;
7+
using Files.Backend.Helpers;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
14+
namespace Files.App.Actions
15+
{
16+
internal class RunAsAdminAction : ObservableObject, IAction
17+
{
18+
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
19+
public bool IsExecutable => context.SelectedItem is not null &&
20+
FileExtensionHelpers.IsExecutableFile(context.SelectedItem.FileExtension);
21+
public string Label => "RunAsAdministrator".GetLocalizedResource();
22+
public RichGlyph Glyph => new("\uE7EF");
23+
24+
public RunAsAdminAction()
25+
{
26+
context.PropertyChanged += Context_PropertyChanged;
27+
}
28+
29+
public async Task ExecuteAsync()
30+
{
31+
await ContextMenu.InvokeVerb("runas", context.SelectedItem!.ItemPath);
32+
}
33+
34+
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
35+
{
36+
switch (e.PropertyName)
37+
{
38+
case nameof(IContentPageContext.SelectedItems):
39+
case nameof(IContentPageContext.Folder):
40+
OnPropertyChanged(nameof(IsExecutable));
41+
break;
42+
}
43+
}
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Commands;
4+
using Files.App.Contexts;
5+
using Files.App.Extensions;
6+
using Files.App.Shell;
7+
using Files.Backend.Helpers;
8+
using System.Threading.Tasks;
9+
10+
namespace Files.App.Actions
11+
{
12+
internal class RunAsAnotherUserAction : ObservableObject, IAction
13+
{
14+
public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
15+
public bool IsExecutable => context.SelectedItem is not null &&
16+
FileExtensionHelpers.IsExecutableFile(context.SelectedItem.FileExtension);
17+
public string Label => "BaseLayoutContextFlyoutRunAsAnotherUser/Text".GetLocalizedResource();
18+
public RichGlyph Glyph => new("\uE7EE");
19+
20+
public RunAsAnotherUserAction()
21+
{
22+
context.PropertyChanged += Context_PropertyChanged;
23+
}
24+
25+
public async Task ExecuteAsync()
26+
{
27+
await ContextMenu.InvokeVerb("runasuser", context.SelectedItem!.ItemPath);
28+
}
29+
30+
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
31+
{
32+
switch (e.PropertyName)
33+
{
34+
case nameof(IContentPageContext.SelectedItems):
35+
case nameof(IContentPageContext.Folder):
36+
OnPropertyChanged(nameof(IsExecutable));
37+
break;
38+
}
39+
}
40+
}
41+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public enum CommandCodes
4242
SetAsSlideshowBackground,
4343
SetAsLockscreenBackground,
4444

45-
// Archives
45+
// Run
46+
RunAsAdmin,
47+
RunAsAnotherUser,
48+
49+
// Archives
4650
CompressIntoArchive,
4751
CompressIntoSevenZip,
4852
CompressIntoZip

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ internal class CommandManager : ICommandManager
5454
public IRichCommand CopyItem => commands[CommandCodes.CopyItem];
5555
public IRichCommand CutItem => commands[CommandCodes.CutItem];
5656
public IRichCommand DeleteItem => commands[CommandCodes.DeleteItem];
57+
public IRichCommand RunAsAdmin => commands[CommandCodes.RunAsAdmin];
58+
public IRichCommand RunAsAnotherUser => commands[CommandCodes.RunAsAnotherUser];
5759
public IRichCommand CompressIntoArchive => commands[CommandCodes.CompressIntoArchive];
5860
public IRichCommand CompressIntoSevenZip => commands[CommandCodes.CompressIntoSevenZip];
5961
public IRichCommand CompressIntoZip => commands[CommandCodes.CompressIntoZip];
@@ -100,6 +102,8 @@ public CommandManager()
100102
[CommandCodes.CopyItem] = new CopyItemAction(),
101103
[CommandCodes.CutItem] = new CutItemAction(),
102104
[CommandCodes.DeleteItem] = new DeleteItemAction(),
105+
[CommandCodes.RunAsAdmin] = new RunAsAdminAction(),
106+
[CommandCodes.RunAsAnotherUser] = new RunAsAnotherUserAction(),
103107
[CommandCodes.CompressIntoArchive] = new CompressIntoArchiveAction(),
104108
[CommandCodes.CompressIntoSevenZip] = new CompressIntoSevenZipAction(),
105109
[CommandCodes.CompressIntoZip] = new CompressIntoZipAction()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public interface ICommandManager : IEnumerable<IRichCommand>
4141
IRichCommand SetAsSlideshowBackground { get; }
4242
IRichCommand SetAsLockscreenBackground { get; }
4343

44+
IRichCommand RunAsAdmin { get; }
45+
IRichCommand RunAsAnotherUser { get; }
46+
4447
IRichCommand CompressIntoArchive { get; }
4548
IRichCommand CompressIntoSevenZip { get; }
4649
IRichCommand CompressIntoZip { get; }

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -714,22 +714,8 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
714714
ShowInSearchPage = true,
715715
ShowItem = selectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false
716716
},
717-
new ContextMenuFlyoutItemViewModel()
718-
{
719-
Text = "RunAsAdministrator".GetLocalizedResource(),
720-
Glyph = "\uE7EF",
721-
Command = commandsViewModel.RunAsAdminCommand,
722-
ShowInSearchPage = true,
723-
ShowItem = itemsSelected && isFirstFileExecutable
724-
},
725-
new ContextMenuFlyoutItemViewModel()
726-
{
727-
Text = "BaseLayoutContextFlyoutRunAsAnotherUser/Text".GetLocalizedResource(),
728-
Glyph = "\uE7EE",
729-
Command = commandsViewModel.RunAsAnotherUserCommand,
730-
ShowInSearchPage = true,
731-
ShowItem = itemsSelected && isFirstFileExecutable
732-
},
717+
new ContextMenuFlyoutItemViewModelBuilder(commands.RunAsAdmin).Build(),
718+
new ContextMenuFlyoutItemViewModelBuilder(commands.RunAsAnotherUser).Build(),
733719
new ContextMenuFlyoutItemViewModel()
734720
{
735721
ItemType = ItemType.Separator,

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -86,54 +86,11 @@ public virtual void RenameItem(RoutedEventArgs e)
8686
itemManipulationModel.StartRenameItem();
8787
}
8888

89-
public virtual async void RunAsAdmin(RoutedEventArgs e)
90-
{
91-
await ContextMenu.InvokeVerb("runas", SlimContentPage.SelectedItem.ItemPath);
92-
}
93-
94-
public virtual async void RunAsAnotherUser(RoutedEventArgs e)
95-
{
96-
await ContextMenu.InvokeVerb("runasuser", SlimContentPage.SelectedItem.ItemPath);
97-
}
98-
99-
public virtual void SidebarPinItem(RoutedEventArgs e)
100-
{
101-
_ = QuickAccessService.PinToSidebar(SlimContentPage.SelectedItems.Select(x => x.ItemPath).ToArray());
102-
}
103-
104-
public virtual void SidebarUnpinItem(RoutedEventArgs e)
105-
{
106-
_ = QuickAccessService.UnpinFromSidebar(SlimContentPage.SelectedItems.Select(x => x.ItemPath).ToArray());
107-
}
108-
10989
public virtual void OpenItem(RoutedEventArgs e)
11090
{
11191
_ = NavigationHelpers.OpenSelectedItems(associatedInstance, false);
11292
}
11393

114-
public virtual async void RestoreRecycleBin(RoutedEventArgs e)
115-
{
116-
await RecycleBinHelpers.RestoreRecycleBin(associatedInstance);
117-
}
118-
119-
public virtual async void RestoreSelectionRecycleBin(RoutedEventArgs e)
120-
{
121-
await RecycleBinHelpers.RestoreSelectionRecycleBin(associatedInstance);
122-
}
123-
124-
public virtual async void QuickLook(RoutedEventArgs e)
125-
{
126-
await QuickLookHelpers.ToggleQuickLook(associatedInstance);
127-
}
128-
129-
public virtual async void RestoreItem(RoutedEventArgs e)
130-
{
131-
await RecycleBinHelpers.RestoreItem(associatedInstance);
132-
}
133-
134-
public virtual void ShowFolderProperties(RoutedEventArgs e)
135-
=> ShowProperties(e);
136-
13794
public virtual void ShowProperties(RoutedEventArgs e)
13895
{
13996
if (SlimContentPage.ItemContextMenuFlyout.IsOpen)

src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ public BaseLayoutCommandsViewModel(IBaseLayoutCommandImplementationModel command
2828
private void InitializeCommands()
2929
{
3030
RenameItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RenameItem);
31-
RunAsAdminCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RunAsAdmin);
32-
RunAsAnotherUserCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RunAsAnotherUser);
3331
OpenItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenItem);
34-
RestoreRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreRecycleBin);
35-
RestoreSelectionRecycleBinCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreSelectionRecycleBin);
36-
QuickLookCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.QuickLook);
37-
RestoreItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RestoreItem);
38-
ShowFolderPropertiesCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.ShowFolderProperties);
3932
ShowPropertiesCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.ShowProperties);
4033
OpenFileLocationCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenFileLocation);
4134
OpenParentFolderCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenParentFolder);
@@ -73,22 +66,8 @@ private void InitializeCommands()
7366

7467
public ICommand RenameItemCommand { get; private set; }
7568

76-
public ICommand RunAsAdminCommand { get; private set; }
77-
78-
public ICommand RunAsAnotherUserCommand { get; private set; }
79-
8069
public ICommand OpenItemCommand { get; private set; }
8170

82-
public ICommand RestoreRecycleBinCommand { get; private set; }
83-
84-
public ICommand RestoreSelectionRecycleBinCommand { get; private set; }
85-
86-
public ICommand QuickLookCommand { get; private set; }
87-
88-
public ICommand RestoreItemCommand { get; private set; }
89-
90-
public ICommand ShowFolderPropertiesCommand { get; private set; }
91-
9271
public ICommand ShowPropertiesCommand { get; private set; }
9372

9473
public ICommand OpenFileLocationCommand { get; private set; }

src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,8 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
1111
{
1212
void RenameItem(RoutedEventArgs e);
1313

14-
void RunAsAdmin(RoutedEventArgs e);
15-
16-
void RunAsAnotherUser(RoutedEventArgs e);
17-
18-
void SidebarPinItem(RoutedEventArgs e);
19-
20-
void SidebarUnpinItem(RoutedEventArgs e);
21-
2214
void OpenItem(RoutedEventArgs e);
2315

24-
void RestoreRecycleBin(RoutedEventArgs e);
25-
26-
void RestoreSelectionRecycleBin(RoutedEventArgs e);
27-
28-
void QuickLook(RoutedEventArgs e);
29-
30-
void RestoreItem(RoutedEventArgs e);
31-
32-
void ShowFolderProperties(RoutedEventArgs e);
33-
3416
void ShowProperties(RoutedEventArgs e);
3517

3618
void OpenFileLocation(RoutedEventArgs e);

0 commit comments

Comments
 (0)