Skip to content

Commit d1cfd1e

Browse files
authored
RichCommand: Run With Powershell (#11914)
1 parent b0e7ee9 commit d1cfd1e

File tree

7 files changed

+56
-8
lines changed

7 files changed

+56
-8
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.Helpers;
7+
using Files.App.Shell;
8+
using Files.Backend.Helpers;
9+
using System.Threading.Tasks;
10+
11+
namespace Files.App.Actions
12+
{
13+
internal class RunWithPowershellAction : ObservableObject, IAction
14+
{
15+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
16+
17+
public bool IsExecutable => context.SelectedItem is not null &&
18+
FileExtensionHelpers.IsPowerShellFile(context.SelectedItem.FileExtension);
19+
20+
public string Label => "RunWithPowerShell".GetLocalizedResource();
21+
22+
public string Description => "TODO: Need to be described.";
23+
24+
public RichGlyph Glyph => new("\uE756");
25+
26+
public RunWithPowershellAction()
27+
{
28+
context.PropertyChanged += Context_PropertyChanged;
29+
}
30+
31+
public async Task ExecuteAsync()
32+
{
33+
Win32API.RunPowershellCommand($"{context.ShellPage?.SlimContentPage?.SelectedItem.ItemPath}", false);
34+
}
35+
36+
private void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
37+
{
38+
switch (e.PropertyName)
39+
{
40+
case nameof(IContentPageContext.SelectedItems):
41+
case nameof(IContentPageContext.Folder):
42+
OnPropertyChanged(nameof(IsExecutable));
43+
break;
44+
}
45+
}
46+
}
47+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public enum CommandCodes
6767
// Run
6868
RunAsAdmin,
6969
RunAsAnotherUser,
70+
RunWithPowershell,
7071

7172
// QuickLook
7273
LaunchQuickLook,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ internal class CommandManager : ICommandManager
6363
public IRichCommand InstallInfDriver => commands[CommandCodes.InstallInfDriver];
6464
public IRichCommand RunAsAdmin => commands[CommandCodes.RunAsAdmin];
6565
public IRichCommand RunAsAnotherUser => commands[CommandCodes.RunAsAnotherUser];
66+
public IRichCommand RunWithPowershell => commands[CommandCodes.RunWithPowershell];
6667
public IRichCommand LaunchQuickLook => commands[CommandCodes.LaunchQuickLook];
6768
public IRichCommand CompressIntoArchive => commands[CommandCodes.CompressIntoArchive];
6869
public IRichCommand CompressIntoSevenZip => commands[CommandCodes.CompressIntoSevenZip];
@@ -200,6 +201,7 @@ public CommandManager()
200201
[CommandCodes.InstallInfDriver] = new InstallInfDriverAction(),
201202
[CommandCodes.RunAsAdmin] = new RunAsAdminAction(),
202203
[CommandCodes.RunAsAnotherUser] = new RunAsAnotherUserAction(),
204+
[CommandCodes.RunWithPowershell] = new RunWithPowershellAction(),
203205
[CommandCodes.LaunchQuickLook] = new LaunchQuickLookAction(),
204206
[CommandCodes.CompressIntoArchive] = new CompressIntoArchiveAction(),
205207
[CommandCodes.CompressIntoSevenZip] = new CompressIntoSevenZipAction(),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
5959

6060
IRichCommand RunAsAdmin { get; }
6161
IRichCommand RunAsAnotherUser { get; }
62+
IRichCommand RunWithPowershell { get; }
6263

6364
IRichCommand LaunchQuickLook { get; }
6465

src/Files.App/UserControls/InnerNavigationToolbar.xaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@
265265
x:Name="RunWithPowerShellButton"
266266
Width="Auto"
267267
MinWidth="40"
268-
x:Load="{x:Bind ViewModel.IsPowerShellScript, Mode=OneWay, FallbackValue=False}"
268+
x:Load="{x:Bind Commands.RunWithPowershell.IsExecutable, Mode=OneWay}"
269269
AutomationProperties.Name="RunWithPowerShell"
270-
Command="{x:Bind ViewModel.RunWithPowerShellCommand, Mode=OneWay}"
271-
Label="{helpers:ResourceString Name=RunScript}"
270+
Command="{x:Bind Commands.RunWithPowershell}"
271+
Label="{x:Bind Commands.RunWithPowershell.Label}"
272272
LabelPosition="Default"
273-
ToolTipService.ToolTip="{helpers:ResourceString Name=RunWithPowerShell}">
273+
ToolTipService.ToolTip="{x:Bind Commands.RunWithPowershell.LabelWithHotKey}">
274274
<AppBarButton.Icon>
275-
<FontIcon Glyph="&#xE756;" />
275+
<FontIcon Glyph="{x:Bind Commands.RunWithPowershell.Glyph.BaseGlyph}" />
276276
</AppBarButton.Icon>
277277
</AppBarButton>
278278
<AppBarButton

src/Files.App/ViewModels/ToolbarViewModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,6 @@ private void SearchRegion_Escaped(object? sender, ISearchBox searchBox)
577577

578578
public ICommand PropertiesCommand { get; set; }
579579

580-
public ICommand? RunWithPowerShellCommand { get; set; }
581-
582580
public ICommand? UpdateCommand { get; set; }
583581

584582
public ICommand? PlayAllCommand { get; set; }

src/Files.App/Views/BaseShellPage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,6 @@ protected void InitToolbarCommands()
620620
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
621621
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
622622
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));
623-
ToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItem.ItemPath)));
624623
ToolbarViewModel.PropertiesCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShowPropertiesCommand.Execute(null));
625624
ToolbarViewModel.UpdateCommand = new AsyncRelayCommand(async () => await updateSettingsService.DownloadUpdates());
626625
ToolbarViewModel.PlayAllCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.PlayAllCommand.Execute(null));

0 commit comments

Comments
 (0)