Skip to content

Commit

Permalink
RichCommand: Run With Powershell (#11914)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa authored Apr 3, 2023
1 parent b0e7ee9 commit d1cfd1e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
47 changes: 47 additions & 0 deletions src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using Files.App.Shell;
using Files.Backend.Helpers;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class RunWithPowershellAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public bool IsExecutable => context.SelectedItem is not null &&
FileExtensionHelpers.IsPowerShellFile(context.SelectedItem.FileExtension);

public string Label => "RunWithPowerShell".GetLocalizedResource();

public string Description => "TODO: Need to be described.";

public RichGlyph Glyph => new("\uE756");

public RunWithPowershellAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public async Task ExecuteAsync()
{
Win32API.RunPowershellCommand($"{context.ShellPage?.SlimContentPage?.SelectedItem.ItemPath}", false);
}

private void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.SelectedItems):
case nameof(IContentPageContext.Folder):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public enum CommandCodes
// Run
RunAsAdmin,
RunAsAnotherUser,
RunWithPowershell,

// QuickLook
LaunchQuickLook,
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ internal class CommandManager : ICommandManager
public IRichCommand InstallInfDriver => commands[CommandCodes.InstallInfDriver];
public IRichCommand RunAsAdmin => commands[CommandCodes.RunAsAdmin];
public IRichCommand RunAsAnotherUser => commands[CommandCodes.RunAsAnotherUser];
public IRichCommand RunWithPowershell => commands[CommandCodes.RunWithPowershell];
public IRichCommand LaunchQuickLook => commands[CommandCodes.LaunchQuickLook];
public IRichCommand CompressIntoArchive => commands[CommandCodes.CompressIntoArchive];
public IRichCommand CompressIntoSevenZip => commands[CommandCodes.CompressIntoSevenZip];
Expand Down Expand Up @@ -200,6 +201,7 @@ public CommandManager()
[CommandCodes.InstallInfDriver] = new InstallInfDriverAction(),
[CommandCodes.RunAsAdmin] = new RunAsAdminAction(),
[CommandCodes.RunAsAnotherUser] = new RunAsAnotherUserAction(),
[CommandCodes.RunWithPowershell] = new RunWithPowershellAction(),
[CommandCodes.LaunchQuickLook] = new LaunchQuickLookAction(),
[CommandCodes.CompressIntoArchive] = new CompressIntoArchiveAction(),
[CommandCodes.CompressIntoSevenZip] = new CompressIntoSevenZipAction(),
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>

IRichCommand RunAsAdmin { get; }
IRichCommand RunAsAnotherUser { get; }
IRichCommand RunWithPowershell { get; }

IRichCommand LaunchQuickLook { get; }

Expand Down
10 changes: 5 additions & 5 deletions src/Files.App/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@
x:Name="RunWithPowerShellButton"
Width="Auto"
MinWidth="40"
x:Load="{x:Bind ViewModel.IsPowerShellScript, Mode=OneWay, FallbackValue=False}"
x:Load="{x:Bind Commands.RunWithPowershell.IsExecutable, Mode=OneWay}"
AutomationProperties.Name="RunWithPowerShell"
Command="{x:Bind ViewModel.RunWithPowerShellCommand, Mode=OneWay}"
Label="{helpers:ResourceString Name=RunScript}"
Command="{x:Bind Commands.RunWithPowershell}"
Label="{x:Bind Commands.RunWithPowershell.Label}"
LabelPosition="Default"
ToolTipService.ToolTip="{helpers:ResourceString Name=RunWithPowerShell}">
ToolTipService.ToolTip="{x:Bind Commands.RunWithPowershell.LabelWithHotKey}">
<AppBarButton.Icon>
<FontIcon Glyph="&#xE756;" />
<FontIcon Glyph="{x:Bind Commands.RunWithPowershell.Glyph.BaseGlyph}" />
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton
Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,6 @@ private void SearchRegion_Escaped(object? sender, ISearchBox searchBox)

public ICommand PropertiesCommand { get; set; }

public ICommand? RunWithPowerShellCommand { get; set; }

public ICommand? UpdateCommand { get; set; }

public ICommand? PlayAllCommand { get; set; }
Expand Down
1 change: 0 additions & 1 deletion src/Files.App/Views/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ protected void InitToolbarCommands()
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));
ToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItem.ItemPath)));
ToolbarViewModel.PropertiesCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShowPropertiesCommand.Execute(null));
ToolbarViewModel.UpdateCommand = new AsyncRelayCommand(async () => await updateSettingsService.DownloadUpdates());
ToolbarViewModel.PlayAllCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.PlayAllCommand.Execute(null));
Expand Down

0 comments on commit d1cfd1e

Please sign in to comment.